Skip to content

Commit

Permalink
fix(libnpmexec): workspaces support
Browse files Browse the repository at this point in the history
Fixes the proper path location to use when targetting specific
workspaces.

Fixes: npm#3520
Relates to: npm/statusboard#403
  • Loading branch information
ruyadorno committed Apr 11, 2022
1 parent ff1367f commit 1175520
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ class Exec extends BaseCommand {
static ignoreImplicitWorkspace = false
static isShellout = true

async exec (_args, { locationMsg, path, runPath } = {}) {
if (!path) {
path = this.npm.localPrefix
}
async exec (_args, { locationMsg, runPath } = {}) {
const path = this.npm.localPrefix

if (!runPath) {
runPath = process.cwd()
Expand Down Expand Up @@ -95,7 +93,7 @@ class Exec extends BaseCommand {

for (const path of this.workspacePaths) {
const locationMsg = await getLocationMsg({ color, path })
await this.exec(args, { locationMsg, path, runPath: path })
await this.exec(args, { locationMsg, runPath: path })
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions workspaces/libnpmexec/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,82 @@ t.test('local pkg, must not fetch manifest for avail pkg', async t => {
t.equal(res, 'LOCAL PKG', 'should run local pkg bin script')
})

t.test('multiple local pkgs', async t => {
const foo = {
name: '@ruyadorno/create-foo',
version: '2.0.0',
bin: {
'create-foo': './index.js',
},
}
const bar = {
name: '@ruyadorno/create-bar',
version: '2.0.0',
bin: {
'create-bar': './index.js',
},
}
const path = t.testdir({
cache: {},
npxCache: {},
node_modules: {
'.bin': {},
'@ruyadorno': {
'create-foo': {
'package.json': JSON.stringify(foo),
'index.js': `#!/usr/bin/env node
require('fs').writeFileSync(process.argv.slice(2)[0], 'foo')`,
},
'create-bar': {
'package.json': JSON.stringify(bar),
'index.js': `#!/usr/bin/env node
require('fs').writeFileSync(process.argv.slice(2)[0], 'bar')`,
},
},
},
'package.json': JSON.stringify({
name: 'pkg',
dependencies: {
'@ruyadorno/create-foo': '^2.0.0',
'@ruyadorno/create-bar': '^2.0.0',
},
}),
})
const runPath = path
const cache = resolve(path, 'cache')
const npxCache = resolve(path, 'npxCache')

const setupBins = async (pkg) => {
const executable =
resolve(path, `node_modules/${pkg.name}/index.js`)
fs.chmodSync(executable, 0o775)

await binLinks({
path: resolve(path, `node_modules/${pkg.name}`),
pkg,
})
}

await Promise.all([foo, bar]
.map(setupBins))

await libexec({
...baseOpts,
localBin: resolve(path, 'node_modules/.bin'),
cache,
npxCache,
packages: ['@ruyadorno/create-foo', '@ruyadorno/create-bar'],
call: 'create-foo resfile && create-bar bar',
path,
runPath,
})

const resFoo = fs.readFileSync(resolve(path, 'resfile')).toString()
t.equal(resFoo, 'foo', 'should run local pkg bin script')
const resBar = fs.readFileSync(resolve(path, 'bar')).toString()
t.equal(resBar, 'bar', 'should run local pkg bin script')
})

t.test('local file system path', async t => {
const path = t.testdir({
cache: {},
Expand Down

0 comments on commit 1175520

Please sign in to comment.