Skip to content

Commit

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

Throw an error while missing script start or file `server.js` [#5782](https://github.com/pnpm/pnpm/pull/5782).
2 changes: 2 additions & 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 All @@ -46,6 +47,7 @@
},
"devDependencies": {
"@pnpm/lifecycle": "workspace:*",
"@pnpm/test-fixtures": "workspace:*",
"@types/rimraf": "^3.0.2",
"@zkochan/rimraf": "^2.1.2",
"json-append": "1.1.1",
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 @@ -34,6 +36,9 @@ export async function runLifecycleHook (
m.scripts = { ...m.scripts }

if (stage === 'start' && !m.scripts.start) {
if (!existsSync('server.js')) {
throw new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js')
}
m.scripts.start = 'node server.js'
}
if (opts.args?.length && m.scripts?.[stage]) {
Expand Down
@@ -0,0 +1,5 @@
{
"name": "without-scriptstart-serverjs",
"version": "1.0.0",
"scripts": {}
}
25 changes: 21 additions & 4 deletions exec/lifecycle/test/index.ts
Expand Up @@ -3,12 +3,14 @@ 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'
import { fixtures } from '@pnpm/test-fixtures'

const fixtures = path.join(__dirname, 'fixtures')
const f = fixtures(path.join(__dirname, 'fixtures'))
const rootModulesDir = path.join(__dirname, '..', 'node_modules')

test('runLifecycleHook()', async () => {
const pkgRoot = path.join(fixtures, 'simple')
const pkgRoot = f.find('simple')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('postinstall', pkg, {
depPath: '/simple/1.0.0',
Expand All @@ -23,7 +25,7 @@ test('runLifecycleHook()', async () => {
})

test('runLifecycleHook() escapes the args passed to the script', async () => {
const pkgRoot = path.join(fixtures, 'escape-args')
const pkgRoot = f.find('escape-args')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('echo', pkg, {
depPath: '/escape-args/1.0.0',
Expand All @@ -38,7 +40,7 @@ test('runLifecycleHook() escapes the args passed to the script', async () => {
})

test('runPostinstallHooks()', async () => {
const pkgRoot = path.join(fixtures, 'with-many-scripts')
const pkgRoot = f.find('with-many-scripts')
await rimraf(path.join(pkgRoot, 'output.json'))
await runPostinstallHooks({
depPath: '/with-many-scripts/1.0.0',
Expand All @@ -51,3 +53,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 = f.find('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'))
})
6 changes: 6 additions & 0 deletions exec/lifecycle/tsconfig.json
Expand Up @@ -9,12 +9,18 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../__utils__/test-fixtures"
},
{
"path": "../../fetching/directory-fetcher"
},
{
"path": "../../packages/core-loggers"
},
{
"path": "../../packages/error"
},
{
"path": "../../packages/types"
},
Expand Down
6 changes: 6 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 f5c377a

Please sign in to comment.