From d6455d137066e7b6e9c651aa889265258384172b Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 31 Jan 2018 18:33:10 -0800 Subject: [PATCH] feat: option `removePackageScripts` Close #2461 --- .../electron-builder-lib/src/configuration.ts | 8 +++++ .../src/fileTransformer.ts | 32 ++++++++++++++----- .../src/platformPackager.ts | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/electron-builder-lib/src/configuration.ts b/packages/electron-builder-lib/src/configuration.ts index 00027226d1a..c161a2ed1d3 100644 --- a/packages/electron-builder-lib/src/configuration.ts +++ b/packages/electron-builder-lib/src/configuration.ts @@ -185,8 +185,16 @@ export interface Configuration extends PlatformSpecificBuildOptions { /** * Whether to include PDB files. + * @default false */ readonly includePdb?: boolean + + /** + * Whether to remove `scripts` field from `package.json` files. + * + * @default true + */ + readonly removePackageScripts?: boolean } export interface AfterPackContext { diff --git a/packages/electron-builder-lib/src/fileTransformer.ts b/packages/electron-builder-lib/src/fileTransformer.ts index 57d88b1b0cc..6ff22317e42 100644 --- a/packages/electron-builder-lib/src/fileTransformer.ts +++ b/packages/electron-builder-lib/src/fileTransformer.ts @@ -3,6 +3,7 @@ import { FileTransformer } from "builder-util/out/fs" import { readFile } from "fs-extra-p" import * as path from "path" import { deepAssign } from "read-config-file/out/deepAssign" +import { Configuration } from "./configuration" import { Packager } from "./packager" /** @internal */ @@ -22,15 +23,19 @@ export function hasDep(name: string, info: Packager) { } /** @internal */ -export function createTransformer(srcDir: string, extraMetadata: any): FileTransformer { +export function createTransformer(srcDir: string, configuration: Configuration, extraMetadata: any): FileTransformer { const mainPackageJson = path.join(srcDir, "package.json") + const isRemovePackageScripts = configuration.removePackageScripts !== false return file => { if (file === mainPackageJson) { - return modifyMainPackageJson(file, extraMetadata) + return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts) } else if (file.endsWith("/package.json") && file.includes("/node_modules/")) { return readFile(file, "utf-8") - .then(it => cleanupPackageJson(JSON.parse(it), false)) + .then(it => cleanupPackageJson(JSON.parse(it), { + isMain: false, + isRemovePackageScripts, + })) .catch(e => log.warn(e)) } else { @@ -52,9 +57,14 @@ export function createElectronCompilerHost(projectDir: string, cacheDir: string) return require(path.join(electronCompilePath, "config-parser")).createCompilerHostFromProjectRoot(projectDir, cacheDir) } -const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "keywords", "build", "scripts", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "bugs", "tags"]) +const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "keywords", "build", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "bugs", "tags"]) -function cleanupPackageJson(data: any, isMain: boolean): any { +interface CleanupPackageFileOptions { + readonly isRemovePackageScripts: boolean + readonly isMain: boolean +} + +function cleanupPackageJson(data: any, options: CleanupPackageFileOptions): any { const deps = data.dependencies // https://github.com/electron-userland/electron-builder/issues/507#issuecomment-312772099 const isRemoveBabel = deps != null && typeof deps === "object" && !Object.getOwnPropertyNames(deps).some(it => it.startsWith("babel")) @@ -62,7 +72,10 @@ function cleanupPackageJson(data: any, isMain: boolean): any { let changed = false for (const prop of Object.getOwnPropertyNames(data)) { // removing devDependencies from package.json breaks levelup in electron, so, remove it only from main package.json - if (prop[0] === "_" || ignoredPackageMetadataProperties.has(prop) || (isMain && prop === "devDependencies") || (isRemoveBabel && prop === "babel")) { + if (prop[0] === "_" || + ignoredPackageMetadataProperties.has(prop) || + (options.isRemovePackageScripts && prop === "scripts") || + (options.isMain && prop === "devDependencies") || (isRemoveBabel && prop === "babel")) { delete data[prop] changed = true } @@ -79,14 +92,17 @@ function cleanupPackageJson(data: any, isMain: boolean): any { return null } -async function modifyMainPackageJson(file: string, extraMetadata: any) { +async function modifyMainPackageJson(file: string, extraMetadata: any, isRemovePackageScripts: boolean) { const mainPackageData = JSON.parse(await readFile(file, "utf-8")) if (extraMetadata != null) { deepAssign(mainPackageData, extraMetadata) } // https://github.com/electron-userland/electron-builder/issues/1212 - const serializedDataIfChanged = cleanupPackageJson(mainPackageData, true) + const serializedDataIfChanged = cleanupPackageJson(mainPackageData, { + isMain: true, + isRemovePackageScripts, + }) if (serializedDataIfChanged != null) { return serializedDataIfChanged } diff --git a/packages/electron-builder-lib/src/platformPackager.ts b/packages/electron-builder-lib/src/platformPackager.ts index e61380d24c1..02ae2f7b73a 100644 --- a/packages/electron-builder-lib/src/platformPackager.ts +++ b/packages/electron-builder-lib/src/platformPackager.ts @@ -227,7 +227,7 @@ export abstract class PlatformPackager matcher.excludePatterns = excludePatterns } } - const transformer = createTransformer(appDir, isElectronCompile ? { + const transformer = createTransformer(appDir, config, isElectronCompile ? { originalMain: this.info.metadata.main, main: ELECTRON_COMPILE_SHIM_FILENAME, ...config.extraMetadata } : config.extraMetadata)