diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2022-06-02 00:11:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-02 00:11:16 +0300 |
commit | aee6fc857458ac660bf90ecd0af94212c7269fd7 (patch) | |
tree | 464eec61f9a60ea2e97d76ffd04f6a886f6b1396 /lib | |
parent | 66981ecf08b888fde9188c162cc928fff7d6d9d6 (diff) |
feat(init): reify on init new workspace (#4892)
Adds a minimalistic reify step that updates the installed tree after
initializing a new workspace.
Moved the shared update logic from `lib/commands/version.js` to a
`lib/workspaces/update-workspaces.js` module that is reused between
both `npm version` and `npm init`.
Relates to: https://github.com/npm/rfcs/issues/556
Relates to: https://github.com/npm/cli/pull/4588
Diffstat (limited to 'lib')
-rw-r--r-- | lib/commands/init.js | 45 | ||||
-rw-r--r-- | lib/commands/version.js | 44 | ||||
-rw-r--r-- | lib/workspaces/update-workspaces.js | 40 |
3 files changed, 99 insertions, 30 deletions
diff --git a/lib/commands/init.js b/lib/commands/init.js index 2a6b6aadd..4c299e651 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -8,13 +8,22 @@ const libexec = require('libnpmexec') const mapWorkspaces = require('@npmcli/map-workspaces') const PackageJson = require('@npmcli/package-json') const log = require('../utils/log-shim.js') +const updateWorkspaces = require('../workspaces/update-workspaces.js') const getLocationMsg = require('../exec/get-workspace-location-msg.js') const BaseCommand = require('../base-command.js') class Init extends BaseCommand { static description = 'Create a package.json file' - static params = ['yes', 'force', 'workspace', 'workspaces', 'include-workspace-root'] + static params = [ + 'yes', + 'force', + 'workspace', + 'workspaces', + 'workspaces-update', + 'include-workspace-root', + ] + static name = 'init' static usage = [ '[--force|-f|--yes|-y|--scope]', @@ -46,11 +55,13 @@ class Init extends BaseCommand { const pkg = await rpj(resolve(this.npm.localPrefix, 'package.json')) const wPath = filterArg => resolve(this.npm.localPrefix, filterArg) + const workspacesPaths = [] // npm-exec style, runs in the context of each workspace filter if (args.length) { for (const filterArg of filters) { const path = wPath(filterArg) await mkdirp(path) + workspacesPaths.push(path) await this.execCreate({ args, path }) await this.setWorkspace({ pkg, workspacePath: path }) } @@ -61,9 +72,13 @@ class Init extends BaseCommand { for (const filterArg of filters) { const path = wPath(filterArg) await mkdirp(path) + workspacesPaths.push(path) await this.template(path) await this.setWorkspace({ pkg, workspacePath: path }) } + + // reify packages once all workspaces have been initialized + await this.update(workspacesPaths) } async execCreate ({ args, path }) { @@ -196,6 +211,34 @@ class Init extends BaseCommand { await pkgJson.save() } + + async update (workspacesPaths) { + // translate workspaces paths into an array containing workspaces names + const workspaces = [] + for (const path of workspacesPaths) { + const pkgPath = resolve(path, 'package.json') + const { name } = await rpj(pkgPath) + .catch(() => ({})) + + if (name) { + workspaces.push(name) + } + } + + const { + config, + flatOptions, + localPrefix, + } = this.npm + + await updateWorkspaces({ + config, + flatOptions, + localPrefix, + npm: this.npm, + workspaces, + }) + } } module.exports = Init diff --git a/lib/commands/version.js b/lib/commands/version.js index ed506f663..ab59fff5a 100644 --- a/lib/commands/version.js +++ b/lib/commands/version.js @@ -3,9 +3,7 @@ const { resolve } = require('path') const { promisify } = require('util') const readFile = promisify(require('fs').readFile) -const Arborist = require('@npmcli/arborist') -const reifyFinish = require('../utils/reify-finish.js') - +const updateWorkspaces = require('../workspaces/update-workspaces.js') const BaseCommand = require('../base-command.js') class Version extends BaseCommand { @@ -137,32 +135,20 @@ class Version extends BaseCommand { return this.list(results) } - async update (args) { - if (!this.npm.flatOptions.workspacesUpdate || !args.length) { - return - } - - // default behavior is to not save by default in order to avoid - // race condition problems when publishing multiple workspaces - // that have dependencies on one another, it might still be useful - // in some cases, which then need to set --save - const save = this.npm.config.isDefault('save') - ? false - : this.npm.config.get('save') - - // runs a minimalistic reify update, targetting only the workspaces - // that had version updates and skipping fund/audit/save - const opts = { - ...this.npm.flatOptions, - audit: false, - fund: false, - path: this.npm.localPrefix, - save, - } - const arb = new Arborist(opts) - - await arb.reify({ ...opts, update: args }) - await reifyFinish(this.npm, arb) + async update (workspaces) { + const { + config, + flatOptions, + localPrefix, + } = this.npm + + await updateWorkspaces({ + config, + flatOptions, + localPrefix, + npm: this.npm, + workspaces, + }) } } diff --git a/lib/workspaces/update-workspaces.js b/lib/workspaces/update-workspaces.js new file mode 100644 index 000000000..4cba1245a --- /dev/null +++ b/lib/workspaces/update-workspaces.js @@ -0,0 +1,40 @@ +'use strict' + +const Arborist = require('@npmcli/arborist') +const reifyFinish = require('../utils/reify-finish.js') + +async function updateWorkspaces ({ + config, + flatOptions, + localPrefix, + npm, + workspaces, +}) { + if (!flatOptions.workspacesUpdate || !workspaces.length) { + return + } + + // default behavior is to not save by default in order to avoid + // race condition problems when publishing multiple workspaces + // that have dependencies on one another, it might still be useful + // in some cases, which then need to set --save + const save = config.isDefault('save') + ? false + : config.get('save') + + // runs a minimalistic reify update, targetting only the workspaces + // that had version updates and skipping fund/audit/save + const opts = { + ...flatOptions, + audit: false, + fund: false, + path: localPrefix, + save, + } + const arb = new Arborist(opts) + + await arb.reify({ ...opts, update: workspaces }) + await reifyFinish(npm, arb) +} + +module.exports = updateWorkspaces |