forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.js
345 lines (281 loc) · 8.79 KB
/
package.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { resolve } from 'path'
import consola from 'consola'
import spawn from 'cross-spawn'
import { existsSync, readJSONSync, writeFile, copy, remove } from 'fs-extra'
import _ from 'lodash'
import { rollup, watch } from 'rollup'
import { glob as _glob } from 'glob'
import pify from 'pify'
import sortPackageJson from 'sort-package-json'
import rollupConfig from './rollup.config'
const DEFAULTS = {
rootDir: process.cwd(),
pkgPath: 'package.json',
configPath: 'package.js',
distDir: 'dist',
build: false,
suffix: process.env.PACKAGE_SUFFIX ? `-${process.env.PACKAGE_SUFFIX}` : '',
hooks: {}
}
const glob = pify(_glob)
const sortObjectKeys = obj => _(obj).toPairs().sortBy(0).fromPairs().value()
export default class Package {
constructor(options) {
// Assign options
this.options = Object.assign({}, DEFAULTS, options)
// Basic logger
this.logger = consola
// Init (sync)
this._init()
}
_init() {
// Try to read package.json
this.readPkg()
// Use tagged logger
this.logger = consola.withTag(this.pkg.name)
// Try to load config
this.loadConfig()
}
resolvePath(...args) {
return resolve(this.options.rootDir, ...args)
}
readPkg() {
this.pkg = readJSONSync(this.resolvePath(this.options.pkgPath))
}
loadConfig() {
const configPath = this.resolvePath(this.options.configPath)
if (existsSync(configPath)) {
let config = require(configPath)
config = config.default || config
Object.assign(this.options, config)
}
}
async callHook(name, ...args) {
let fns = this.options.hooks[name]
if (!fns) {
return
}
if (!Array.isArray(fns)) {
fns = [fns]
}
for (const fn of fns) {
await fn(this, ...args)
}
}
load(relativePath, opts) {
return new Package(Object.assign({
rootDir: this.resolvePath(relativePath)
}, opts))
}
async writePackage() {
if (this.options.sortDependencies) {
this.sortDependencies()
}
const pkgPath = this.resolvePath(this.options.pkgPath)
this.logger.debug('Writing', pkgPath)
await writeFile(pkgPath, JSON.stringify(this.pkg, null, 2) + '\n')
}
generateVersion() {
const date = Math.round(Date.now() / (1000 * 60))
const gitCommit = this.gitShortCommit()
const baseVersion = this.pkg.version.split('-')[0]
this.pkg.version = `${baseVersion}-${date}.${gitCommit}`
}
tryRequire(id) {
try {
return require(id)
} catch (e) {}
}
suffixAndVersion() {
this.logger.info(`Adding suffix ${this.options.suffix}`)
const oldPkgName = this.pkg.name
// Add suffix to the package name
if (!oldPkgName.includes(this.options.suffix)) {
this.pkg.name += this.options.suffix
}
// Apply suffix to all linkedDependencies
if (this.pkg.dependencies) {
for (const oldName of (this.options.linkedDependencies || [])) {
const name = oldName + this.options.suffix
const version = this.pkg.dependencies[oldName] || this.pkg.dependencies[name]
delete this.pkg.dependencies[oldName]
this.pkg.dependencies[name] = version
}
}
if (typeof this.pkg.bin === 'string') {
const bin = this.pkg.bin
this.pkg.bin = {
[oldPkgName]: bin,
[this.pkg.name]: bin
}
}
this.generateVersion()
}
syncLinkedDependencies() {
// Apply suffix to all linkedDependencies
for (const _name of (this.options.linkedDependencies || [])) {
const name = _name + (this.options.suffix || '')
// Try to read pkg
const pkg = this.tryRequire(`${name}/package.json`) ||
this.tryRequire(`${_name}/package.json`)
// Skip if pkg or dependency not found
if (!pkg || !this.pkg.dependencies || !this.pkg.dependencies[name]) {
continue
}
// Current version
const currentVersion = this.pkg.dependencies[name]
const caret = currentVersion[0] === '^'
// Sync version
this.pkg.dependencies[name] = caret ? `^${pkg.version}` : pkg.version
}
}
async getWorkspacePackages() {
const packages = []
for (const workspace of this.pkg.workspaces || []) {
const dirs = await glob(workspace)
for (const dir of dirs) {
const pkg = new Package({
rootDir: this.resolvePath(dir)
})
packages.push(pkg)
}
}
return packages
}
async build(_watch = false) {
// Prepare rollup config
const config = {
rootDir: this.options.rootDir,
alias: {},
replace: {}
}
// Replace linkedDependencies with their suffixed version
if (this.options.suffix && this.options.suffix.length) {
for (const _name of (this.options.linkedDependencies || [])) {
const name = _name + this.options.suffix
config.replace[`'${_name}'`] = `'${name}'`
config.alias[_name] = name
}
}
// Allow extending config
await this.callHook('build:extend', { config })
// Create rollup config
const _rollupConfig = rollupConfig(config, this.pkg)
// Allow extending rollup config
await this.callHook('build:extendRollup', {
rollupConfig: _rollupConfig
})
if (_watch) {
// Watch
const watcher = watch(_rollupConfig)
watcher.on('event', (event) => {
switch (event.code) {
// The watcher is (re)starting
case 'START': return this.logger.debug('Watching for changes')
// Building an individual bundle
case 'BUNDLE_START': return this.logger.debug('Building bundle')
// Finished building a bundle
case 'BUNDLE_END': return
// Finished building all bundles
case 'END':
this.emit('build:done')
return this.logger.success('Bundle built')
// Encountered an error while bundling
case 'ERROR': return this.logger.error(event.error)
// Eencountered an unrecoverable error
case 'FATAL': return this.logger.fatal(event.error)
// Unknown event
default: return this.logger.info(JSON.stringify(event))
}
})
} else {
// Build
this.logger.info('Building bundle')
try {
const bundle = await rollup(_rollupConfig)
await remove(_rollupConfig.output.dir)
await bundle.write(_rollupConfig.output)
this.logger.success('Bundle built')
await this.callHook('build:done', { bundle })
// Analyze bundle imports against pkg
// if (this.pkg.dependencies) {
// const dependencies = {}
// for (const dep in this.pkg.dependencies) {
// dependencies[dep] = this.pkg.dependencies[dep]
// }
// for (const imp of bundle.imports) {
// delete dependencies[imp]
// }
// for (const dep in dependencies) {
// this.logger.warn(`Unused dependency ${dep}@${dependencies[dep]}`)
// }
// }
} catch (error) {
this.logger.error(error)
throw new Error('Error while building bundle')
}
}
}
watch() {
return this.build(true)
}
async publish(tag = 'latest') {
this.logger.info(`publishing ${this.pkg.name}@${this.pkg.version} with tag ${tag}`)
await this.exec('npm', `publish --tag ${tag}`)
}
copyFieldsFrom(source, fields = []) {
for (const field of fields) {
this.pkg[field] = source.pkg[field]
}
}
async copyFilesFrom(source, files) {
for (const file of files || source.pkg.files || []) {
const src = resolve(source.options.rootDir, file)
const dst = resolve(this.options.rootDir, file)
await copy(src, dst)
}
}
autoFix() {
this.pkg = sortPackageJson(this.pkg)
this.sortDependencies()
}
sortDependencies() {
if (this.pkg.dependencies) {
this.pkg.dependencies = sortObjectKeys(this.pkg.dependencies)
}
if (this.pkg.devDependencies) {
this.pkg.devDependencies = sortObjectKeys(this.pkg.devDependencies)
}
}
exec(command, args, silent = false) {
const r = spawn.sync(command, args.split(' '), { cwd: this.options.rootDir }, { env: process.env })
if (!silent) {
const fullCommand = command + ' ' + args
if (r.error) {
this.logger.error(fullCommand, r.error)
} else {
this.logger.success(fullCommand, r.output)
}
}
return {
error: r.error,
pid: r.pid,
status: r.status,
signal: r.signal,
stdout: String(r.stdout).trim(),
stderr: String(r.stderr).trim(),
output: (r.output || [])
.map(l => String(l).trim())
.filter(l => l.length)
.join('\n')
}
}
gitShortCommit() {
const { stdout } = this.exec('git', 'rev-parse --short HEAD', true)
return stdout
}
gitBranch() {
const { stdout } = this.exec('git', 'rev-parse --abbrev-ref HEAD', true)
return stdout
}
}