/
index.js
128 lines (112 loc) · 3.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
const got = require('got')
const tar = require('tar')
const ora = require('ora')
const sade = require('sade')
const chalk = require('chalk')
const spawn = require('cross-spawn')
const { join } = require('path')
const { Stream } = require('stream')
const { execSync } = require('child_process')
const { promisify } = require('util')
const { mkdirSync, existsSync } = require('fs')
sade('parcreate <dir>', true)
.version('2.0.0')
.describe('Bootstrap an app bundled with Parcel.js')
.example('my-app')
.example('my-react-app --template react')
.example('. --template tailwind')
.option(
'-t, --template',
'Choose a template for your app (defaults to basic)'
)
.option('-n, --use-npm', 'Use npm instead of yarn')
.action((dir, opts) => {
downloadGitRepo({
template: opts.template || 'basic',
directory: dir,
useNpm: !!opts['use-npm'],
})
})
.parse(process.argv)
/* This code was adapted from the `create-next-app` project:
https://github.com/vercel/next.js/blob/canary/packages/create-next-app/
*/
async function downloadGitRepo({ template, directory, useNpm }) {
const templatesResponse = await got(
'https://api.github.com/repos/kartiknair/parcreate/contents/templates',
{ responseType: 'json' }
)
const templates = templatesResponse.body.map((temp) => temp.name)
const packageManager = useNpm ? 'npm' : yarnInstalled() ? 'yarn' : 'npm'
const pipeline = promisify(Stream.pipeline)
if (!templates.includes(template)) {
console.log(`\n${chalk.red(template)} is not an available template`)
process.exit(1)
}
if (directory !== '.' && existsSync(directory)) {
console.log(
`\n${chalk.red(
'The directory already exists.'
)}\n\nIf you would like to create a project inside that directory,\n${chalk.cyan(
'`cd`'
)} into it & use '.' as the directory argument`
)
process.exit(1)
}
if (!existsSync(directory)) mkdirSync(join(process.cwd(), directory))
const downloadSpinner = ora(
`Downloading template files for ${chalk.greenBright(template)}`
).start()
await pipeline(
got.stream(
`https://codeload.github.com/kartiknair/parcreate/tar.gz/master`
),
tar.extract({ cwd: join(process.cwd(), directory), strip: 3 }, [
`parcreate-master/templates/${template}`,
])
)
downloadSpinner.succeed(
`Downloaded files for template ${chalk.greenBright(template)}`
)
console.log(
`\nInstalling dependancies with \`${chalk.cyan(packageManager)}\``
)
const installation = spawn(packageManager, ['install'], {
cwd: join(process.cwd(), directory),
stdio: 'inherit',
})
installation.on('close', (code) => {
if (code === 0) {
console.log(
'\nProject was succesfully bootstrapped. Here are the commands you have available:'
)
console.log('\n Get started by changing directories:')
console.log(chalk.cyanBright(`\n cd ${directory}`))
console.log('\n\n Start the dev server:')
console.log(
chalk.cyanBright(
`\n ${
packageManager === 'npm' ? 'npm run' : 'yarn'
} dev`
)
)
console.log('\n\n Build production-ready files:')
console.log(
chalk.cyanBright(
`\n ${
packageManager === 'npm' ? 'npm run' : 'yarn'
} build\n`
)
)
}
})
}
function yarnInstalled() {
try {
execSync('yarnpkg --version', { stdio: 'ignore' })
return true
} catch (e) {
return false
}
}