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'
|
2023-10-17 06:08:30 +00:00
|
|
|
// eslint-disable-next-line import/named
|
2018-10-17 21:28:25 +00:00
|
|
|
import { rollup, watch } from 'rollup'
|
2020-06-04 08:25:53 +00:00
|
|
|
import _glob from 'glob'
|
2018-11-27 15:32:00 +00:00
|
|
|
import pify from 'pify'
|
2023-10-17 06:08:30 +00:00
|
|
|
import { sortPackageJson } from 'sort-package-json'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
import rollupConfig from './rollup.config'
|
2020-12-22 17:07:50 +00:00
|
|
|
import { builtins } from './builtins'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
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 || [])) {
|
2020-12-22 17:46:36 +00:00
|
|
|
if (!this.pkg.dependencies[oldName]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
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) {
|
2020-12-22 17:07:50 +00:00
|
|
|
// Externals
|
|
|
|
const externals = [
|
|
|
|
// Dependencies that will be installed alongise with the nuxt package
|
|
|
|
...Object.keys(this.pkg.dependencies || {}),
|
|
|
|
// Builtin node modules
|
|
|
|
...builtins,
|
|
|
|
// Custom externals
|
|
|
|
...(this.options.externals || [])
|
|
|
|
]
|
|
|
|
|
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: {},
|
2020-12-22 17:07:50 +00:00
|
|
|
externals,
|
2020-02-06 13:02:05 +00:00
|
|
|
...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':
|
|
|
|
return this.logger.success('Bundle built')
|
|
|
|
|
|
|
|
// Encountered an error while bundling
|
2019-01-17 21:18:29 +00:00
|
|
|
case 'ERROR':
|
2020-05-16 13:07:56 +00:00
|
|
|
this.formatError(event.error)
|
2019-01-17 21:18:29 +00:00
|
|
|
return this.logger.error(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)
|
2020-12-22 17:07:50 +00:00
|
|
|
const result = await bundle.write(_rollupConfig.output)
|
2018-11-27 15:32:00 +00:00
|
|
|
|
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
|
2020-12-22 17:07:50 +00:00
|
|
|
const dependencies = Object.keys(this.pkg.dependencies || {})
|
|
|
|
const imports = [].concat(...result.output.map(o => o.imports))
|
|
|
|
|
|
|
|
const missingDependencies = imports
|
|
|
|
.filter(i => !i.endsWith('.js')) // dynamic imports
|
|
|
|
.filter(i => !dependencies.includes(i) && !externals.find(e => i.startsWith(e)))
|
|
|
|
if (missingDependencies.length) {
|
|
|
|
throw new Error(`Missing dependencies in ${this.pkg.name}: ` + missingDependencies.join(', '))
|
|
|
|
}
|
|
|
|
const ignoreUnused = this.options.ignoreUnused || []
|
2020-12-22 17:46:36 +00:00
|
|
|
const stripEdge = s => s.replace(/-edge$/, '')
|
2020-12-22 17:07:50 +00:00
|
|
|
const unusedDependencies = dependencies.filter(d =>
|
2020-12-22 17:46:36 +00:00
|
|
|
!imports.find(i => i.startsWith(d)) && !ignoreUnused.includes(stripEdge(d))
|
2020-12-22 17:07:50 +00:00
|
|
|
)
|
|
|
|
if (unusedDependencies.length) {
|
|
|
|
throw new Error(`Unused dependencies in ${this.pkg.name}: ` + unusedDependencies.join(', '))
|
|
|
|
}
|
2020-05-16 13:07:56 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.formatError(err)
|
|
|
|
throw err
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 13:07:56 +00:00
|
|
|
formatError (error) {
|
|
|
|
let loc = this.options.rootDir
|
|
|
|
if (error.loc) {
|
|
|
|
const { file, column, line } = error.loc
|
|
|
|
loc = `${file}:${line}:${column}`
|
|
|
|
}
|
2020-12-22 17:07:50 +00:00
|
|
|
error.message = `[${error.code || ''}] ${error.message}\nat ${loc}`
|
2020-05-16 13:07:56 +00:00
|
|
|
return error
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-04 08:25:53 +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)
|
2022-09-03 13:20:35 +00:00
|
|
|
return stdout.trim().substring(0, 7) // 7 to be consistent with github and nuxt3
|
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
|
|
|
}
|
|
|
|
}
|