From 623f31497cbee69315c88f91348e571644c8acf2 Mon Sep 17 00:00:00 2001 From: lvqq Date: Mon, 12 Dec 2022 16:39:34 +0800 Subject: [PATCH] fix: throw an error while missing script start or file server.js --- .changeset/tricky-coats-join.md | 5 +++++ exec/lifecycle/package.json | 1 + exec/lifecycle/src/runLifecycleHook.ts | 5 +++++ .../without-scriptstart-serverjs/package.json | 5 +++++ exec/lifecycle/test/index.ts | 16 ++++++++++++++++ exec/lifecycle/tsconfig.json | 3 +++ pnpm-lock.yaml | 3 +++ 7 files changed, 38 insertions(+) create mode 100644 .changeset/tricky-coats-join.md create mode 100644 exec/lifecycle/test/fixtures/without-scriptstart-serverjs/package.json diff --git a/.changeset/tricky-coats-join.md b/.changeset/tricky-coats-join.md new file mode 100644 index 00000000000..6c7721963f6 --- /dev/null +++ b/.changeset/tricky-coats-join.md @@ -0,0 +1,5 @@ +--- +"@pnpm/lifecycle": patch +--- + +Throw an error while missing script start or file server.js diff --git a/exec/lifecycle/package.json b/exec/lifecycle/package.json index f903a83a2ff..484d231bfc0 100644 --- a/exec/lifecycle/package.json +++ b/exec/lifecycle/package.json @@ -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:*", diff --git a/exec/lifecycle/src/runLifecycleHook.ts b/exec/lifecycle/src/runLifecycleHook.ts index cde86cee426..a00c8195bc4 100644 --- a/exec/lifecycle/src/runLifecycleHook.ts +++ b/exec/lifecycle/src/runLifecycleHook.ts @@ -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 @@ -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)) diff --git a/exec/lifecycle/test/fixtures/without-scriptstart-serverjs/package.json b/exec/lifecycle/test/fixtures/without-scriptstart-serverjs/package.json new file mode 100644 index 00000000000..3debaddf0bb --- /dev/null +++ b/exec/lifecycle/test/fixtures/without-scriptstart-serverjs/package.json @@ -0,0 +1,5 @@ +{ + "name": "without-scriptstart-serverjs", + "version": "1.0.0", + "scripts": {} +} diff --git a/exec/lifecycle/test/index.ts b/exec/lifecycle/test/index.ts index d407cfd0c45..1909731a7d6 100644 --- a/exec/lifecycle/test/index.ts +++ b/exec/lifecycle/test/index.ts @@ -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') @@ -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')) +}) diff --git a/exec/lifecycle/tsconfig.json b/exec/lifecycle/tsconfig.json index cc2eff73b45..ecfbcbd9ca1 100644 --- a/exec/lifecycle/tsconfig.json +++ b/exec/lifecycle/tsconfig.json @@ -15,6 +15,9 @@ { "path": "../../packages/core-loggers" }, + { + "path": "../../packages/error" + }, { "path": "../../packages/types" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 240cd23c891..ba7ecf464fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -903,6 +903,9 @@ importers: '@pnpm/directory-fetcher': specifier: workspace:* version: link:../../fetching/directory-fetcher + '@pnpm/error': + specifier: workspace:* + version: link:../../packages/error '@pnpm/logger': specifier: ^5.0.0 version: 5.0.0