-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
87 lines (73 loc) · 1.81 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
const fs = require('fs-extra')
const path = require('path')
const os = require('os')
const exec = require('child_process').execSync
const spawn = require('cross-spawn')
const install = () => {
return new Promise((resolve, reject) => {
const child = spawn('npm', [ 'install' ], {
stdio: 'inherit'
})
child.on('close', code => {
if (code !== 0) {
reject()
return
}
resolve()
})
})
}
const gitInit = () => {
exec('git --version', { stdio: 'inherit' })
exec('git init', { stdio: 'inherit' })
exec('git add .', { stdio: 'inherit' })
exec('git commit -am "Init"', { stdio: 'inherit' })
return true
}
const getTar = ({
user,
repo,
path = '',
name
}) => {
const url = `https://codeload.github.com/${user}/${repo}/tar.gz/master`
const cmd = `curl ${url} | tar -xz -C ${name} --strip=3 ${repo}-master/${path}`
exec(cmd, { stdio: 'inherit' })
}
const create = async (opts = {}) => {
if (!opts.name) {
throw new Error('name argument required')
return
}
if (!opts.template) {
throw new Error('template argument required')
return
}
const dirname = path.resolve(opts.name)
const name = path.basename(dirname)
const [ user, repo, ...paths ] = opts.template.split('/')
fs.ensureDirSync(name)
getTar(Object.assign({}, opts, {
name,
user,
repo,
path: paths.join('/')
}))
const templatePkg = require(
path.join(dirname, 'package.json')
)
const pkg = Object.assign({}, templatePkg, {
name,
version: '1.0.0',
})
fs.writeFileSync(
path.join(dirname, 'package.json'),
JSON.stringify(pkg, null, 2) + os.EOL
)
process.chdir(dirname)
const installed = await install()
const initialized = gitInit()
exec('npm test', { stdio: 'inherit' })
return { name, dirname }
}
module.exports = create