Skip to content

Commit

Permalink
feat: script runner commands support package.{json5,yaml}
Browse files Browse the repository at this point in the history
pr #1805
ref #1803
  • Loading branch information
zkochan committed May 3, 2019
1 parent a114025 commit bb3fd53
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 54 deletions.
28 changes: 24 additions & 4 deletions packages/lifecycle/src/runLifecycleHook.ts
@@ -1,13 +1,14 @@
import { lifecycleLogger } from '@pnpm/core-loggers'
import { PackageJson } from '@pnpm/types'
import { DependencyManifest, ImporterManifest } from '@pnpm/types'
import lifecycle = require('@zkochan/npm-lifecycle')

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

export default async function runLifecycleHook (
stage: string,
pkg: PackageJson,
manifest: ImporterManifest | DependencyManifest,
opts: {
args?: string[],
depPath: string,
optional?: boolean,
pkgRoot: string,
Expand All @@ -22,13 +23,22 @@ export default async function runLifecycleHook (
lifecycleLogger.debug({
depPath: opts.depPath,
optional,
script: pkg.scripts![stage],
script: manifest.scripts![stage],
stage,
wd: opts.pkgRoot,
})
}

return lifecycle(pkg, stage, opts.pkgRoot, {
const m = { _id: getId(manifest), ...manifest }
m.scripts = { ...m.scripts }

if (stage === 'start' && !m.scripts.start) {
m.scripts.start = 'node server.js'
}
if (opts.args && opts.args.length && m.scripts && m.scripts[stage]) {
m.scripts[stage] = `${m.scripts[stage]} ${opts.args.map((arg) => `"${arg}"`).join(' ')}`
}
return lifecycle(m, stage, opts.pkgRoot, {
config: opts.rawNpmConfig,
dir: opts.rootNodeModulesDir,
log: {
Expand Down Expand Up @@ -76,3 +86,13 @@ export default async function runLifecycleHook (
}
}
}

function getId (manifest: ImporterManifest | DependencyManifest) {
if (!manifest.name) {
return undefined
}
if (!manifest.version) {
return manifest.name
}
return `${manifest.name}@${manifest.version}`
}
14 changes: 0 additions & 14 deletions packages/pnpm/src/bin/pnpm.ts
Expand Up @@ -43,15 +43,12 @@ if (argv.includes('--help') || argv.includes('-h') || argv.includes('--h')) {
case 'profile':
case 'publish':
case 'repo':
case 'restart':
case 's':
case 'se':
case 'search':
case 'set':
case 'star':
case 'stars':
case 'start':
case 'stop':
case 'team':
case 'token':
case 'unpublish':
Expand All @@ -63,17 +60,6 @@ if (argv.includes('--help') || argv.includes('-h') || argv.includes('--h')) {
case 'xmas':
await passThruToNpm()
break
case 't':
case 'tst':
case 'test':
case 'run':
case 'run-script':
if (argv.includes('--filter')) {
await runPnpm()
} else {
await passThruToNpm()
}
break
default:
await runPnpm()
break
Expand Down
114 changes: 91 additions & 23 deletions packages/pnpm/src/cmd/help.ts
Expand Up @@ -37,7 +37,8 @@ function getHelpText (command: string) {
-E, --save-exact install exact version
-g, --global install as a global package
-r run installation recursively in every package found in subdirectories
or in every workspace package, when executed inside a workspace
or in every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
--store the location where all the packages are saved on the disk.
--offline trigger an error if any required dependencies are not available in local store
--prefer-offline skip staleness checks for cached data, but request missing data from the server
Expand Down Expand Up @@ -128,7 +129,8 @@ function getHelpText (command: string) {
Options:
-r uninstall from every package found in subdirectories
or from every workspace package, when executed inside a workspace
or from every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
Discouraged options:
--shamefully-flatten attempt to flatten the dependency tree, similar to what npm and Yarn do
Expand All @@ -154,7 +156,8 @@ function getHelpText (command: string) {
Options:
-r unlink in every package found in subdirectories
or in every workspace package, when executed inside a workspace
or in every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
`

case 'update':
Expand All @@ -166,7 +169,8 @@ function getHelpText (command: string) {
Options:
-r update in every package found in subdirectories
or every workspace package, when executed inside a workspace
or every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
-g, --global update globally installed packages
--depth how deep should levels of dependencies be inspected
0 is default, which means top-level dependencies
Expand All @@ -187,7 +191,8 @@ function getHelpText (command: string) {
Options:
-r perform command on every package in subdirectories
or on every workspace package, when executed inside a workspace
or on every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
--long show extended information
--parseable show parseable output instead of tree view
-g, --global list packages in the global install prefix instead of in the current project
Expand All @@ -212,8 +217,12 @@ function getHelpText (command: string) {

case 'install-test':
return stripIndent`
This command runs an \`npm install\` followed immediately by an \`npm test\`.
It takes exactly the same arguments as \`npm install\`.
pnpm install-test
Aliases: it
Runs a \`pnpm install\` followed immediately by a \`pnpm test\`.
It takes exactly the same arguments as \`pnpm install\`.
`

case 'store':
Expand Down Expand Up @@ -269,7 +278,8 @@ function getHelpText (command: string) {
Options:
-r check for outdated dependencies in every package found in subdirectories
or in every workspace package, when executed inside a workspace
or in every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
`

case 'rebuild':
Expand All @@ -283,10 +293,62 @@ function getHelpText (command: string) {
Options:
-r rebuild every package found in subdirectories
or every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
--pending rebuild packages that were not build during installation.
Packages are not build when installing with the --ignore-scripts flag
`

case 'run':
return stripIndent`
pnpm run <command> [-- <args>...]
Aliases: run-script
Runs a defined package script.
Options:
-r run the defined package script in every package found in subdirectories
or every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
`

case 'test':
return stripIndent`
pnpm test [-- <args>...]
Aliases: t, tst
Runs a package's "test" script, if one was provided.
Options:
-r run the tests in every package found in subdirectories
or every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
`

case 'start':
return stripIndent`
pnpm start [-- <args>...]
Runs an arbitrary command specified in the package's "start" property of its "scripts" object.
If no "start" property is specified on the "scripts" object, it will run node server.js.
`

case 'stop':
return stripIndent`
pnpm stop [-- <args>...]
Runs a package's "stop" script, if one was provided.
`

case 'restart':
return stripIndent`
pnpm restart [-- <args>...]
Restarts a package.
Runs a package's "stop", "restart", and "start" scripts, and associated pre- and post- scripts.
`

case 'server':
return stripIndent`
pnpm server start
Expand Down Expand Up @@ -405,36 +467,42 @@ function getHelpText (command: string) {
Commands:
- import
- install
- update
- uninstall
- install-test
- link
- unlink
- list
- outdated
- prune
- install-test
- store add
- store status
- store prune
- root
- rebuild
- import
- restart
- root
- run
- start
- stop
- test
- uninstall
- unlink
- update
- recursive unlink
- recursive exec
- recursive install
- recursive update
- recursive uninstall
- recursive list
- recursive outdated
- recursive rebuild
- recursive run
- recursive test
- recursive rebuild
- recursive exec
- recursive uninstall
- recursive unlink
- recursive update
- server start
- server stop
- server status
- server stop
- store add
- store prune
- store status
Other commands are passed through to npm
`
Expand Down
6 changes: 6 additions & 0 deletions packages/pnpm/src/cmd/index.ts
Expand Up @@ -9,6 +9,7 @@ import prune from './prune'
import rebuild from './rebuild'
import recursive from './recursive'
import root from './root'
import run, { restart, start, stop, test } from './run'
import server from './server'
import store from './store'
import uninstall from './uninstall'
Expand All @@ -26,9 +27,14 @@ export default {
prune,
rebuild,
recursive,
restart,
root,
run,
server,
start,
stop,
store,
test,
uninstall,
unlink,
update,
Expand Down
4 changes: 2 additions & 2 deletions packages/pnpm/src/cmd/installTest.ts
@@ -1,8 +1,8 @@
import { PnpmOptions } from '../types'
import install from './install'
import runNpm from './runNpm'
import { test } from './run'

export default async function (input: string[], opts: PnpmOptions) {
await install(input, opts)
runNpm(['test'])
await test(input, opts)
}

0 comments on commit bb3fd53

Please sign in to comment.