This repository was archived by the owner on Sep 8, 2022. It is now read-only.
forked from transitive-bullshit/create-react-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-library.js
150 lines (124 loc) · 2.97 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
'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')
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, {
dot: true
})
{
const promise = pEachSeries(files, async (file) => {
return module.exports.copyTemplateFile({
file,
source,
dest,
info
})
})
ora.promise(promise, `Copying ${template} template to ${dest}`)
await promise
}
{
const promise = module.exports.initPackageManager({ dest, info })
ora.promise(promise, `Running ${manager} install and ${manager} link`)
await promise
}
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)
const destFilePath = path.join(dest, fileRelativePath)
const destFileDir = path.parse(destFilePath).dir
const template = handlebars.compile(fs.readFileSync(file, 'utf8'))
const content = template({
...info,
yarn: (info.manager === 'yarn')
})
await mkdirp(destFileDir)
fs.writeFileSync(destFilePath, content, 'utf8')
return fileRelativePath
}
module.exports.initPackageManager = async (opts) => {
const {
dest,
info
} = opts
const example = path.join(dest, 'example')
const commands = [
{
cmd: `${info.manager} install`,
cwd: dest
},
{
cmd: `${info.manager} link`,
cwd: dest
},
{
cmd: `${info.manager} install`,
cwd: example
}
]
return pEachSeries(commands, async ({ cmd, cwd }) => {
return execa.shell(cmd, { 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 cmd = `git init && git add . && git commit -m "init ${pkg.name}@${pkg.version}"`
return execa.shell(cmd, { cwd: dest })
}