This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (69 loc) · 2.49 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
'use strict'
const chalk = require('chalk')
const { execSync } = require('child_process')
const createPackageJson = require('create-package-json')
const fs = require('fs-extra')
const path = require('path')
const pkg = require('./package.json')
const config = require('./config.json')
const { commands, dependencies, devDependencies } = config
const logStep = (k, ...s) => console.log(`${chalk.bold(chalk.cyan(`${k.toUpperCase()}\t`))} ${config.steps[k]}${s}.`)
const runCommand = (command) => {
try {
execSync(`${command}`, { stdio: 'inherit' })
} catch (err) {
console.error(`Failed command "${command}"`, err)
return false
}
return true
}
const runCommands = (commandList) => runCommand(commandList.join(' && '))
const getTpl = (key) => path.join(__dirname, config.templates[key])
module.exports = async (args) => {
const { directory, silent, withFetch, withDocker, withCommitlint } = args
const cd = `cd ${directory}`
const packageOptions = {
...config.options,
prompt: !silent,
directory,
name: directory,
description: `Project created with ${pkg.name}`,
dependencies: [
...dependencies,
...(withFetch ? config.extraDeps.fetch : []),
...(withDocker ? config.extraDeps.docker : []),
],
devDependencies: [...devDependencies, ...(withCommitlint ? config.extraDeps.commitlint : [])],
scripts: withCommitlint
? config.options.scripts
: { ...config.options.scripts, ...config.extraOptions.commitlint.scripts },
}
logStep('start', chalk.cyan(directory))
try {
await fs.copy(getTpl('base'), directory)
logStep('base')
if (withFetch) {
await fs.copy(getTpl('fetch'), path.join(directory, 'src/utils/utils.mjs'))
logStep('fetch')
}
if (withDocker) {
await fs.copy(getTpl('docker'), directory)
logStep('docker')
}
if (withCommitlint) {
await fs.copy(getTpl('commitlint'), directory)
logStep('commitlint')
}
} catch (err) {
console.error(err)
return
}
logStep('npm-pre')
await createPackageJson(packageOptions)
logStep('npm')
if (runCommands([cd, `${commands.gitInit} ${silent ? '--quiet' : ''}`])) logStep('git')
if (runCommands([cd, commands.createLocalEnv])) logStep('env')
if (runCommands([cd, ...commands.huskyCommands])) logStep('husky')
if (withCommitlint && runCommands([cd, commands.huskyCommitlint])) logStep('commitlint_hook')
if (runCommands([cd, commands.gitAdd, `${commands.gitCommit} ${silent ? '--quiet' : ''}`])) logStep('commit')
}