-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (46 loc) · 1.68 KB
/
index.js
File metadata and controls
54 lines (46 loc) · 1.68 KB
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
#!/usr/bin/env node
const path = require('path')
const cp = require('child_process')
const fs = require('fs-extra')
const { Command } = require('commander')
const updateNotifier = require('update-notifier')
const pkg = require('./package.json')
updateNotifier({ pkg, updateCheckInterval: 3600 }).notify()
const templates = fs.readdirSync(path.join(__dirname, 'templates'))
const program = new Command()
program
.version(pkg.version)
program
.name('npm init @linke/project')
.command('ls', { isDefault: true })
.alias('list')
.description('list available project templates')
.action(() => {
console.log('Available project templates:')
templates.forEach((e) => console.log(` ${e}`))
})
templates.forEach((template) => {
program
.command(`${template} <name>`, { executableFile: path.join(__dirname, 'templates', 'eslint', 'index.js') })
.description(`Create a project with ${template} config`)
.option('-i, --install', 'run npm install after creation', false)
.action(async (name, options) => {
try {
const distDir = path.join(process.cwd(), name)
console.log('creating eslint project to', distDir)
await fs.ensureDir(distDir)
await fs.copy(path.join(__dirname, 'templates', template), distDir)
const packageJsonFilePath = path.join(distDir, 'package.json')
const packageJson = JSON.parse(await fs.readFile(packageJsonFilePath))
packageJson.name = name
const packageJsonContent = `${JSON.stringify(packageJson, null, 4)}\n`
await fs.writeFile(packageJsonFilePath, packageJsonContent)
if (options.install) {
cp.execSync('npm i', { cwd: distDir, stdio: 'inherit' })
}
} catch (e) {
console.error(e)
}
})
})
program.parse(process.argv)