Skip to content

Commit

Permalink
fix: throw an error while missing script start or file server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lvqq committed Dec 12, 2022
1 parent 2587011 commit 623f314
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-coats-join.md
@@ -0,0 +1,5 @@
---
"@pnpm/lifecycle": patch
---

Throw an error while missing script start or file server.js
1 change: 1 addition & 0 deletions exec/lifecycle/package.json
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@pnpm/core-loggers": "workspace:*",
"@pnpm/directory-fetcher": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/npm-lifecycle": "^2.0.0",
"@pnpm/read-package-json": "workspace:*",
"@pnpm/store-controller-types": "workspace:*",
Expand Down
5 changes: 5 additions & 0 deletions exec/lifecycle/src/runLifecycleHook.ts
Expand Up @@ -2,6 +2,8 @@ import { lifecycleLogger } from '@pnpm/core-loggers'
import { globalWarn } from '@pnpm/logger'
import lifecycle from '@pnpm/npm-lifecycle'
import { DependencyManifest, ProjectManifest } from '@pnpm/types'
import { PnpmError } from '@pnpm/error'
import { existsSync } from 'fs'

function noop () {} // eslint-disable-line:no-empty

Expand Down Expand Up @@ -35,6 +37,9 @@ export async function runLifecycleHook (

if (stage === 'start' && !m.scripts.start) {
m.scripts.start = 'node server.js'
if (!existsSync('server.js')) {
throw new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js')
}
}
if (opts.args?.length && m.scripts?.[stage]) {
const escapedArgs = opts.args.map((arg) => JSON.stringify(arg))
Expand Down
@@ -0,0 +1,5 @@
{
"name": "without-scriptstart-serverjs",
"version": "1.0.0",
"scripts": {}
}
16 changes: 16 additions & 0 deletions exec/lifecycle/test/index.ts
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import { runLifecycleHook, runPostinstallHooks } from '@pnpm/lifecycle'
import loadJsonFile from 'load-json-file'
import rimraf from '@zkochan/rimraf'
import { PnpmError } from '@pnpm/error'

const fixtures = path.join(__dirname, 'fixtures')
const rootModulesDir = path.join(__dirname, '..', 'node_modules')
Expand Down Expand Up @@ -51,3 +52,18 @@ test('runPostinstallHooks()', async () => {

expect(loadJsonFile.sync(path.join(pkgRoot, 'output.json'))).toStrictEqual(['preinstall', 'install', 'postinstall'])
})

test('runLifecycleHook() should throw an error while missing script start or file server.js', async () => {
const pkgRoot = path.join(fixtures, 'without-scriptstart-serverjs')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await expect(
runLifecycleHook('start', pkg, {
depPath: '/without-scriptstart-serverjs/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
).rejects.toThrow(new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js'))
})
3 changes: 3 additions & 0 deletions exec/lifecycle/tsconfig.json
Expand Up @@ -15,6 +15,9 @@
{
"path": "../../packages/core-loggers"
},
{
"path": "../../packages/error"
},
{
"path": "../../packages/types"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 623f314

Please sign in to comment.