Skip to content

Commit

Permalink
run: default the 'start' script when server.js present
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 6, 2020
1 parent a73dc67 commit 1d90b27
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/run-script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const run = require('@npmcli/run-script')
const { isServerPackage } = run
const npm = require('./npm.js')
const readJson = require('read-package-json-fast')
const { resolve } = require('path')
Expand Down Expand Up @@ -45,7 +46,7 @@ const runScript = async (args) => {

pkg.scripts = scripts

if (!scripts[event]) {
if (!scripts[event] && !(event === 'start' && await isServerPackage(path))) {
if (npm.config.get('if-present'))
return

Expand Down
29 changes: 25 additions & 4 deletions test/lib/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ const output = []

const npmlog = { level: 'warn' }
const getRS = windows => requireInject('../../lib/run-script.js', {
'@npmcli/run-script': async opts => {
'@npmcli/run-script': Object.assign(async opts => {
RUN_SCRIPTS.push(opts)
},
}, {
isServerPackage: require('@npmcli/run-script').isServerPackage,
}),
npmlog,
'../../lib/npm.js': npm,
'../../lib/utils/is-windows-shell.js': windows,
Expand Down Expand Up @@ -90,10 +92,29 @@ t.test('fail if no package.json', async t => {
await runScript(['test'], er => t.match(er, { code: 'ENOENT' }))
})

t.test('default env and restart scripts', async t => {
t.test('default env, start, and restart scripts', async t => {
npm.localPrefix = t.testdir({
'package.json': JSON.stringify({ name: 'x', version: '1.2.3' })
'package.json': JSON.stringify({ name: 'x', version: '1.2.3' }),
'server.js': 'console.log("hello, world")',
})

await runScript(['start'], er => {
if (er) {
throw er
}
t.match(RUN_SCRIPTS, [
{
path: npm.localPrefix,
args: [],
scriptShell: undefined,
stdio: 'inherit',
stdioString: true,
pkg: { name: 'x', version: '1.2.3', _id: 'x@1.2.3', scripts: {}},
event: 'start'
}
])
})
RUN_SCRIPTS.length = 0

await runScript(['env'], er => {
if (er) {
Expand Down

0 comments on commit 1d90b27

Please sign in to comment.