Skip to content

Commit

Permalink
refactor: move runScriptsIfPresent() out
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Mar 29, 2020
1 parent d54dbf7 commit 455b533
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/lifecycle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { fromDir as readPackageJsonFromDir } from '@pnpm/read-package-json'
import path = require('path')
import exists = require('path-exists')
import runLifecycleHook from './runLifecycleHook'
import runLifecycleHook, { RunLifecycleHookOptions } from './runLifecycleHook'
import runLifecycleHooksConcurrently from './runLifecycleHooksConcurrently'

export default runLifecycleHook
export { runLifecycleHooksConcurrently }
export { runLifecycleHooksConcurrently, RunLifecycleHookOptions }

export async function runPostinstallHooks (
opts: {
Expand Down
24 changes: 13 additions & 11 deletions packages/lifecycle/src/runLifecycleHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import lifecycle = require('@zkochan/npm-lifecycle')

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

export type RunLifecycleHookOptions = {
args?: string[],
depPath: string,
extraBinPaths?: string[],
optional?: boolean,
pkgRoot: string,
rawConfig: object,
rootNodeModulesDir: string,
stdio?: string,
unsafePerm: boolean,
}

export default async function runLifecycleHook (
stage: string,
manifest: ProjectManifest | DependencyManifest,
opts: {
args?: string[],
depPath: string,
extraBinPaths?: string[],
optional?: boolean,
pkgRoot: string,
rawConfig: object,
rootNodeModulesDir: string,
stdio?: string,
unsafePerm: boolean,
},
opts: RunLifecycleHookOptions,
) {
const optional = opts.optional === true

Expand Down
27 changes: 16 additions & 11 deletions packages/plugin-commands-publishing/src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { docsUrl, readProjectManifest } from '@pnpm/cli-utils'
import { Config, types as allTypes } from '@pnpm/config'
import PnpmError from '@pnpm/error'
import runLifecycleHooks from '@pnpm/lifecycle'
import runLifecycleHooks, { RunLifecycleHookOptions } from '@pnpm/lifecycle'
import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
import runNpm from '@pnpm/run-npm'
import { Dependencies, ProjectManifest } from '@pnpm/types'
Expand Down Expand Up @@ -109,21 +109,15 @@ export async function handler (
}
const dir = params.length && params[0] || process.cwd()

const lifecycleOpts = {
const _runScriptsIfPresent = runScriptsIfPresent.bind(null, {
depPath: dir,
extraBinPaths: opts.extraBinPaths,
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootNodeModulesDir: await realNodeModulesDir(dir),
stdio: 'inherit',
unsafePerm: true, // when running scripts explicitly, assume that they're trusted.
}
const runScriptsIfPresent = async (scriptNames: string[], manifest: ProjectManifest) => {
for (const scriptName of scriptNames) {
if (!manifest.scripts?.[scriptName]) continue
await runLifecycleHooks(scriptName, manifest, lifecycleOpts)
}
}
})
let _status!: number
await fakeRegularManifest(
{
Expand All @@ -133,14 +127,14 @@ export async function handler (
},
async (publishManifest) => {
// Unfortunately, we cannot support postpack at the moment
await runScriptsIfPresent([
await _runScriptsIfPresent([
'prepublish',
'prepare',
'prepublishOnly',
'prepack',
], publishManifest)
const { status } = runNpm(opts.npmPath, ['publish', '--ignore-scripts', ...opts.argv.original.slice(1)])
await runScriptsIfPresent([
await _runScriptsIfPresent([
'publish',
'postpublish',
], publishManifest)
Expand All @@ -152,6 +146,17 @@ export async function handler (
}
}

async function runScriptsIfPresent (
opts: RunLifecycleHookOptions,
scriptNames: string[],
manifest: ProjectManifest,
) {
for (const scriptName of scriptNames) {
if (!manifest.scripts?.[scriptName]) continue
await runLifecycleHooks(scriptName, manifest, opts)
}
}

const LICENSE_GLOB = 'LICEN{S,C}E{,.*}'
const findLicenses = fg.bind(fg, [LICENSE_GLOB]) as (opts: { cwd: string }) => Promise<string[]>

Expand Down

0 comments on commit 455b533

Please sign in to comment.