-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (74 loc) · 2.21 KB
/
main.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
import chalk from 'chalk'
import fs from 'fs'
import ncp from 'ncp'
import path from 'path'
import { promisify } from 'util'
import execa from 'execa'
import Listr from 'listr'
import { projectInstall } from 'pkg-install'
const { URL } = require('url')
const access = promisify(fs.access)
const copy = promisify(ncp)
const initGit = async options => {
const result = await execa('git', ['init'], {
cwd: options.targetDirectory,
})
if (result.failed) {
return Promise.reject(new Error('Failed to initialize Git'))
}
return false
}
const copyTemplateFiles = async options =>
copy(options.templateDirectory, options.targetDirectory, {
clobber: false,
})
const createProject = async options => {
const projectOptions = {
...options,
targetDirectory: `${options.targetDirectory || process.cwd()}/${options.projectName}`,
}
const currentFileUrl = import.meta.url
const templateDir = path.resolve(new URL(currentFileUrl).pathname, '../../template/iron-api')
projectOptions.templateDirectory = templateDir
try {
await access(templateDir, fs.constants.R_OK)
try {
await access(projectOptions.targetDirectory, fs.constants.R_OK)
process.stdout.write('Folder already exists! You must choose another name\n')
process.exit(1)
} catch (err) {
process.stdout.write(
`${chalk.blueBright.bold('Project folder:')}${projectOptions.targetDirectory}\n`,
)
}
} catch (err) {
process.stdout.write(`${chalk.red.bold('ERROR')} Invalid template name\n`)
process.exit(1)
}
const tasks = new Listr([
{
title: 'Copy project files',
task: () => copyTemplateFiles(projectOptions),
},
{
title: 'Initialize git',
task: () => initGit(projectOptions),
enabled: () => projectOptions.git,
},
{
title: 'Install dependencies',
task: () =>
projectInstall({
cwd: projectOptions.targetDirectory,
}),
skip: () =>
!projectOptions.runInstall
? 'Pass --install to automatically install dependencies'
: undefined,
},
])
await tasks.run()
process.stdout.write(`${chalk.green.bold('DONE')} Project ready\n`)
return true
}
export default createProject