Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw an error while missing script start or server.js #5782

Merged
merged 2 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.