2018-10-17 21:28:25 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import consola from 'consola'
|
2018-11-28 18:44:11 +00:00
|
|
|
import spawn from 'cross-spawn'
|
2018-11-27 15:32:00 +00:00
|
|
|
import { existsSync, readJSONSync, writeFile, copy, remove } from 'fs-extra'
|
2018-10-17 21:28:25 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { rollup, watch } from 'rollup'
|
2018-11-27 15:32:00 +00:00
|
|
|
import { glob as _glob } from 'glob'
|
|
|
|
import pify from 'pify'
|
2018-10-17 21:28:25 +00:00
|
|
|
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,
|
2018-11-27 15:32:00 +00:00
|
|
|
suffix: process.env.PACKAGE_SUFFIX ? `-${process.env.PACKAGE_SUFFIX}` : '',
|
|
|
|
hooks: {}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:32:00 +00:00
|
|
|
const glob = pify(_glob)
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
const sortObjectKeys = obj => _(obj).toPairs().sortBy(0).fromPairs().value()
|
|
|
|
|
2018-11-27 15:32:00 +00:00
|
|
|
export default class Package {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (options) {
|
2018-10-17 21:28:25 +00:00
|
|
|
// Assign options
|
|
|
|
this.options = Object.assign({}, DEFAULTS, options)
|
|
|
|
|
2018-11-27 15:32:00 +00:00
|
|
|
// Basic logger
|
|
|
|
this.logger = consola
|
|
|
|
|
|
|
|
// Init (sync)
|
|
|
|
this._init()
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
_init () {
|
2018-10-17 21:28:25 +00:00
|
|
|
// Try to read package.json
|
|
|
|
this.readPkg()
|
|
|
|
|
2018-11-27 15:32:00 +00:00
|
|
|
// Use tagged logger
|
2018-11-01 03:50:07 +00:00
|
|
|
this.logger = consola.withTag(this.pkg.name)
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Try to load config
|
|
|
|
this.loadConfig()
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
resolvePath (...args) {
|
2018-10-17 21:28:25 +00:00
|
|
|
return resolve(this.options.rootDir, ...args)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
readPkg () {
|
2018-10-17 21:28:25 +00:00
|
|
|
this.pkg = readJSONSync(this.resolvePath(this.options.pkgPath))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
loadConfig () {
|
2018-10-17 21:28:25 +00:00
|
|
|
const configPath = this.resolvePath(this.options.configPath)
|
|
|
|
|
|
|
|
if (existsSync(configPath)) {
|
|
|
|
let config = require(configPath)
|
|
|
|
config = config.default || config
|
|
|
|
|
|
|
|
Object.assign(this.options, config)
|
2018-11-27 15:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async callHook (name, ...args) {
|
2018-11-27 15:32:00 +00:00
|
|
|
let fns = this.options.hooks[name]
|
|
|
|
|
|
|
|
if (!fns) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(fns)) {
|
|
|
|
fns = [fns]
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const fn of fns) {
|
|
|
|
await fn(this, ...args)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
load (relativePath, opts) {
|
2018-11-27 15:32:00 +00:00
|
|
|
return new Package(Object.assign({
|
|
|
|
rootDir: this.resolvePath(relativePath)
|
|
|
|
}, opts))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async writePackage () {
|
2018-10-17 21:28:25 +00:00
|
|
|
if (this.options.sortDependencies) {
|
2018-12-12 10:47:54 +00:00
|
|
|
this.sortDependencies()
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
const pkgPath = this.resolvePath(this.options.pkgPath)
|
|
|
|
this.logger.debug('Writing', pkgPath)
|
2018-11-27 15:32:00 +00:00
|
|
|
await writeFile(pkgPath, JSON.stringify(this.pkg, null, 2) + '\n')
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
generateVersion () {
|
2018-10-17 21:28:25 +00:00
|
|
|
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}`
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
tryRequire (id) {
|
2018-10-17 21:28:25 +00:00
|
|
|
try {
|
|
|
|
return require(id)
|
2019-01-17 21:18:29 +00:00
|
|
|
} catch (e) {
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
suffixAndVersion () {
|
2018-10-17 21:28:25 +00:00
|
|
|
this.logger.info(`Adding suffix ${this.options.suffix}`)
|
|
|
|
|
2018-11-16 13:47:15 +00:00
|
|
|
const oldPkgName = this.pkg.name
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
// Add suffix to the package name
|
2018-11-16 13:47:15 +00:00
|
|
|
if (!oldPkgName.includes(this.options.suffix)) {
|
2018-10-17 21:28:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:47:15 +00:00
|
|
|
if (typeof this.pkg.bin === 'string') {
|
2019-01-17 21:18:29 +00:00
|
|
|
const { bin } = this.pkg
|
2018-11-16 13:47:15 +00:00
|
|
|
this.pkg.bin = {
|
|
|
|
[oldPkgName]: bin,
|
|
|
|
[this.pkg.name]: bin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
this.generateVersion()
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
syncLinkedDependencies () {
|
2018-10-17 21:28:25 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async getWorkspacePackages () {
|
2018-10-17 21:28:25 +00:00
|
|
|
const packages = []
|
|
|
|
|
|
|
|
for (const workspace of this.pkg.workspaces || []) {
|
2018-11-27 15:32:00 +00:00
|
|
|
const dirs = await glob(workspace)
|
2018-10-17 21:28:25 +00:00
|
|
|
for (const dir of dirs) {
|
2018-12-23 07:58:35 +00:00
|
|
|
if (existsSync(this.resolvePath(dir, 'package.json'))) {
|
|
|
|
const pkg = new Package({ rootDir: this.resolvePath(dir) })
|
|
|
|
packages.push(pkg)
|
|
|
|
} else {
|
|
|
|
consola.warn('Invalid workspace package:', dir)
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return packages
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async build (_watch = false) {
|
2018-11-27 15:32:00 +00:00
|
|
|
// Prepare rollup config
|
|
|
|
const config = {
|
|
|
|
rootDir: this.options.rootDir,
|
|
|
|
alias: {},
|
2020-02-06 13:02:05 +00:00
|
|
|
replace: {},
|
|
|
|
...this.options.rollup
|
2018-11-27 15:32:00 +00:00
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// 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
|
2018-11-27 15:32:00 +00:00
|
|
|
config.replace[`'${_name}'`] = `'${name}'`
|
|
|
|
config.alias[_name] = name
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:32:00 +00:00
|
|
|
// 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
|
|
|
|
})
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
if (_watch) {
|
|
|
|
// Watch
|
2018-11-27 15:32:00 +00:00
|
|
|
const watcher = watch(_rollupConfig)
|
2018-10-17 21:28:25 +00:00
|
|
|
watcher.on('event', (event) => {
|
|
|
|
switch (event.code) {
|
|
|
|
// The watcher is (re)starting
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'START':
|
|
|
|
return this.logger.debug('Watching for changes')
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Building an individual bundle
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'BUNDLE_START':
|
|
|
|
return this.logger.debug('Building bundle')
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Finished building a bundle
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'BUNDLE_END':
|
|
|
|
return
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Finished building all bundles
|
|
|
|
case 'END':
|
|
|
|
this.emit('build:done')
|
|
|
|
return this.logger.success('Bundle built')
|
|
|
|
|
|
|
|
// Encountered an error while bundling
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'ERROR':
|
|
|
|
return this.logger.error(event.error)
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2020-04-14 09:26:40 +00:00
|
|
|
// Encountered an unrecoverable error
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'FATAL':
|
|
|
|
return this.logger.fatal(event.error)
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Unknown event
|
2019-01-17 21:18:29 +00:00
|
|
|
default:
|
|
|
|
return this.logger.info(JSON.stringify(event))
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Build
|
|
|
|
this.logger.info('Building bundle')
|
|
|
|
try {
|
2018-11-27 15:32:00 +00:00
|
|
|
const bundle = await rollup(_rollupConfig)
|
|
|
|
await remove(_rollupConfig.output.dir)
|
|
|
|
await bundle.write(_rollupConfig.output)
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
this.logger.success('Bundle built')
|
2018-11-27 15:32:00 +00:00
|
|
|
await this.callHook('build:done', { bundle })
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// 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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
watch () {
|
2018-11-27 15:32:00 +00:00
|
|
|
return this.build(true)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
publish (tag = 'latest') {
|
2018-10-17 21:28:25 +00:00
|
|
|
this.logger.info(`publishing ${this.pkg.name}@${this.pkg.version} with tag ${tag}`)
|
2019-04-29 15:06:55 +00:00
|
|
|
this.exec('npm', `publish --tag ${tag}`)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
copyFieldsFrom (source, fields = []) {
|
2018-10-17 21:28:25 +00:00
|
|
|
for (const field of fields) {
|
|
|
|
this.pkg[field] = source.pkg[field]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async copyFilesFrom (source, files) {
|
2018-10-17 21:28:25 +00:00
|
|
|
for (const file of files || source.pkg.files || []) {
|
|
|
|
const src = resolve(source.options.rootDir, file)
|
|
|
|
const dst = resolve(this.options.rootDir, file)
|
2018-11-27 15:32:00 +00:00
|
|
|
await copy(src, dst)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
autoFix () {
|
2018-10-17 21:28:25 +00:00
|
|
|
this.pkg = sortPackageJson(this.pkg)
|
|
|
|
this.sortDependencies()
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
sortDependencies () {
|
2018-10-17 21:28:25 +00:00
|
|
|
if (this.pkg.dependencies) {
|
|
|
|
this.pkg.dependencies = sortObjectKeys(this.pkg.dependencies)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pkg.devDependencies) {
|
|
|
|
this.pkg.devDependencies = sortObjectKeys(this.pkg.devDependencies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
exec (command, args, silent = false) {
|
2018-11-28 18:44:11 +00:00
|
|
|
const r = spawn.sync(command, args.split(' '), { cwd: this.options.rootDir }, { env: process.env })
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
gitShortCommit () {
|
2018-11-28 18:44:11 +00:00
|
|
|
const { stdout } = this.exec('git', 'rev-parse --short HEAD', true)
|
2018-11-27 15:32:00 +00:00
|
|
|
return stdout
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
gitBranch () {
|
2018-11-28 18:44:11 +00:00
|
|
|
const { stdout } = this.exec('git', 'rev-parse --abbrev-ref HEAD', true)
|
2018-11-27 15:32:00 +00:00
|
|
|
return stdout
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|