Skip to content

Commit

Permalink
wip: including root in workspace commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzy committed Sep 21, 2021
1 parent 2c74190 commit 78393a7
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Diff extends BaseCommand {
'tag',
'workspace',
'workspaces',
'include-workspace-root',
]
}

Expand Down Expand Up @@ -90,7 +91,12 @@ class Diff extends BaseCommand {

async diffWorkspaces (args, filters) {
await this.setWorkspaces(filters)
for (const workspacePath of this.workspacePaths) {

const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.prefix)

for (const workspacePath of workspacePaths) {
this.top = workspacePath
this.prefix = workspacePath
await this.diff(args)
Expand Down
12 changes: 10 additions & 2 deletions lib/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DistTag extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['workspace', 'workspaces']
return ['workspace', 'workspaces', 'include-workspaces-root']
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -181,7 +181,15 @@ class DistTag extends BaseCommand {
async listWorkspaces (filters) {
await this.setWorkspaces(filters)

for (const name of this.workspaceNames) {
const workspaceNames = [...this.workspaceNames]
if (this.npm.config.get('include-workspace-root')) {
const pkg = await readPackageName(this.npm.prefix)
if (!pkg)
throw this.usageError()
workspaceNames.unshift(pkg)
}

for (const name of workspaceNames) {
try {
this.npm.output(`${name}:`)
await this.list(npa(name), this.npm.flatOptions)
Expand Down
9 changes: 7 additions & 2 deletions lib/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Docs extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['browser', 'registry', 'workspace', 'workspaces']
return ['browser', 'registry', 'workspace', 'workspaces', 'include-workspace-root']
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand All @@ -42,7 +42,12 @@ class Docs extends BaseCommand {

async docsWorkspaces (args, filters) {
await this.setWorkspaces(filters)
return this.docs(this.workspacePaths)

const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.prefix)

return this.docs(workspacePaths)
}

async getDocs (pkg) {
Expand Down
8 changes: 6 additions & 2 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Exec extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['package', 'call', 'workspace', 'workspaces']
return ['package', 'call', 'workspace', 'workspaces', 'include-workspace-root']
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -103,7 +103,11 @@ class Exec extends BaseCommand {
await this.setWorkspaces(filters)
const color = this.npm.color

for (const path of this.workspacePaths) {
const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.prefix)

for (const path of workspacePaths) {
const locationMsg = await getLocationMsg({ color, path })
await this._exec(args, {
locationMsg,
Expand Down
5 changes: 4 additions & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Init extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['yes', 'force', 'workspace', 'workspaces']
return ['yes', 'force', 'workspace', 'workspaces', 'include-workspace-root']
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -54,6 +54,9 @@ class Init extends BaseCommand {
}

async initWorkspaces (args, filters) {
if (this.npm.config.get('include-workspace-root'))
await this.init(args)

// reads package.json for the top-level folder first, by doing this we
// ensure the command throw if no package.json is found before trying
// to create a workspace package.json file or its folders
Expand Down
7 changes: 6 additions & 1 deletion lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Pack extends BaseCommand {
'pack-destination',
'workspace',
'workspaces',
'include-workspace-root',
]
}

Expand Down Expand Up @@ -105,7 +106,11 @@ class Pack extends BaseCommand {
}

await this.setWorkspaces(filters)
return this.pack([...this.workspacePaths, ...args.filter(a => a !== '.')])
const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.prefix)

return this.pack([...workspacePaths, ...args.filter(a => a !== '.')])
}
}
module.exports = Pack
9 changes: 9 additions & 0 deletions lib/pkg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const PackageJson = require('@npmcli/package-json')
const BaseCommand = require('./base-command.js')
const Queryable = require('./utils/queryable.js')
const readPackageName = require('./utils/read-package-name.js')

class Pkg extends BaseCommand {
static get description () {
Expand Down Expand Up @@ -28,6 +29,7 @@ class Pkg extends BaseCommand {
'json',
'workspace',
'workspaces',
'include-workspace-root',
]
}

Expand Down Expand Up @@ -64,6 +66,13 @@ class Pkg extends BaseCommand {
async pkgWorkspaces (args, filters) {
await this.setWorkspaces(filters)
const result = {}

if (this.npm.config.get('include-workspace-root')) {
this.prefix = this.npm.localPrefix;
const name = readPackageName(this.prefix)
result[name] = await this.pkg(args)
}

for (const [workspaceName, workspacePath] of this.workspaces.entries()) {
this.prefix = workspacePath
result[workspaceName] = await this.pkg(args)
Expand Down
18 changes: 16 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ class Publish extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['tag', 'access', 'dry-run', 'otp', 'workspace', 'workspaces']
return [
'tag',
'access',
'dry-run',
'otp',
'workspace',
'workspaces',
'include-workspace-root',
]
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -159,7 +167,13 @@ class Publish extends BaseCommand {
const color = this.npm.color ? chalk : { green: noop, bold: noop }
await this.setWorkspaces(filters)

for (const [name, workspace] of this.workspaces.entries()) {
const workspaceEntries = [...this.workspaces.entries()]
if (this.npm.config.get('include-workspace-root')) {
const pkg = await readJson(`${this.npm.localPrefix}/package.json`)
workspaceEntries.unshift([pkg.name, this.npm.localPrefix])
}

for (const [name, workspace] of workspaceEntries) {
let pkgContents
try {
pkgContents = await this.publish([workspace])
Expand Down
9 changes: 7 additions & 2 deletions lib/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Repo extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['browser', 'workspace', 'workspaces']
return ['browser', 'workspace', 'workspaces', 'include-workspace-root']
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand All @@ -44,7 +44,12 @@ class Repo extends BaseCommand {

async repoWorkspaces (args, filters) {
await this.setWorkspaces(filters)
return this.repo(this.workspacePaths)

const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.localPrefix)

return this.repo(workspacePaths)
}

async get (pkg) {
Expand Down
7 changes: 6 additions & 1 deletion lib/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class RunScript extends BaseCommand {
return [
'workspace',
'workspaces',
'include-workspace-root',
'if-present',
'ignore-scripts',
'script-shell',
Expand Down Expand Up @@ -196,7 +197,11 @@ class RunScript extends BaseCommand {
const res = []
await this.setWorkspaces(filters)

for (const workspacePath of this.workspacePaths) {
const workspacePaths = [...this.workspacePaths]
if (this.npm.config.get('include-workspace-root'))
workspacePaths.unshift(this.npm.prefix)

for (const workspacePath of workspacePaths) {
const pkg = await rpj(`${workspacePath}/package.json`)
const runResult = await this.run(args, {
path: workspacePath,
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,20 @@ define('workspaces', {
},
})

define('include-workspace-root', {
default: false,
type: Boolean,
short: 'iwr',
envExport: false,
description: `
Enable running a command in the context of the root package in addition to
other worskpaces specfied with --workspace X or --workspaces.
`,
flatten: (key, obj, flatOptions) => {
definitions['user-agent'].flatten('user-agent', obj, flatOptions)
},
})

define('yes', {
default: null,
type: [null, Boolean],
Expand Down

0 comments on commit 78393a7

Please sign in to comment.