This repository was archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathcreate-library.js
192 lines (158 loc) · 4.07 KB
/
create-library.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'
const handlebars = require('handlebars')
const execa = require('execa')
const fs = require('fs')
const globby = require('globby')
const mkdirp = require('make-dir')
const ora = require('ora')
const path = require('path')
const pEachSeries = require('p-each-series')
const pkg = require('../package')
const templateBlacklist = new Set([
'example/public/favicon.ico',
'example/public/.gitignore',
'.git'
])
module.exports = async (info) => {
const { manager, template, name, templatePath, git } = info
// handle scoped package names
const parts = name.split('/')
info.shortName = parts[parts.length - 1]
const dest = path.join(process.cwd(), info.shortName)
info.dest = dest
await mkdirp(dest)
const source =
template === 'custom'
? path.join(process.cwd(), templatePath)
: path.join(__dirname, '..', 'template', template)
const files = await globby(source.replace(/\\/g, '/'), {
dot: true
})
{
const promise = pEachSeries(files, async (file) => {
return module.exports.copyTemplateFile({
file,
source,
dest,
info
})
})
ora.promise(promise, `Copying ${template} template to ${dest}`)
console.log()
await promise
}
{
console.log()
console.log('Initializing npm dependencies. This will take a minute.')
console.log()
const rootP = module.exports.initPackageManagerRoot({ dest, info })
ora.promise(rootP, `Running ${manager} install in root directory`)
await rootP
const exampleP = module.exports.initPackageManagerExample({ dest, info })
ora.promise(exampleP, `Running ${manager} install in example directory`)
await exampleP
}
if (git) {
const promise = module.exports.initGitRepo({ dest })
ora.promise(promise, 'Initializing git repo')
await promise
}
return dest
}
module.exports.copyTemplateFile = async (opts) => {
const { file, source, dest, info } = opts
const fileRelativePath = path.relative(source, file).replace(/\\/g, '/')
if (fileRelativePath.startsWith('.git')) {
return
}
const destFilePath = path.join(dest, fileRelativePath)
const destFileDir = path.parse(destFilePath).dir
console.log(fileRelativePath)
await mkdirp(destFileDir)
if (templateBlacklist.has(fileRelativePath)) {
const content = fs.readFileSync(file)
fs.writeFileSync(destFilePath, content)
} else {
const template = handlebars.compile(fs.readFileSync(file, 'utf8'))
const content = template({
...info,
yarn: info.manager === 'yarn'
})
fs.writeFileSync(destFilePath, content, 'utf8')
}
return fileRelativePath
}
module.exports.initPackageManagerRoot = async (opts) => {
const { dest, info } = opts
const commands = [
{
cmd: info.manager,
args: ['install'],
cwd: dest
}
]
return pEachSeries(commands, async ({ cmd, args, cwd }) => {
return execa(cmd, args, { cwd })
})
}
module.exports.initPackageManagerExample = async (opts) => {
const { dest, info } = opts
const example = path.join(dest, 'example')
const commands = [
{
cmd: info.manager,
args: ['install'],
cwd: example
}
]
return pEachSeries(commands, async ({ cmd, args, cwd }) => {
return execa(cmd, args, { cwd })
})
}
module.exports.initGitRepo = async (opts) => {
const { dest } = opts
const gitIgnorePath = path.join(dest, '.gitignore')
fs.writeFileSync(
gitIgnorePath,
`
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
node_modules
# builds
build
dist
.rpt2_cache
# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
`,
'utf8'
)
const commands = [
{
cmd: 'git',
args: ['init'],
cwd: dest
},
{
cmd: 'git',
args: ['add', '.'],
cwd: dest
},
{
cmd: 'git',
args: ['commit', '-m', `init ${pkg.name}@${pkg.version}`],
cwd: dest
}
]
return pEachSeries(commands, async ({ cmd, args, cwd }) => {
return execa(cmd, args, { cwd })
})
}