Skip to content

Commit d6455d1

Browse files
committed
feat: option removePackageScripts
Close #2461
1 parent 6faf828 commit d6455d1

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

packages/electron-builder-lib/src/configuration.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,16 @@ export interface Configuration extends PlatformSpecificBuildOptions {
185185

186186
/**
187187
* Whether to include PDB files.
188+
* @default false
188189
*/
189190
readonly includePdb?: boolean
191+
192+
/**
193+
* Whether to remove `scripts` field from `package.json` files.
194+
*
195+
* @default true
196+
*/
197+
readonly removePackageScripts?: boolean
190198
}
191199

192200
export interface AfterPackContext {

packages/electron-builder-lib/src/fileTransformer.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FileTransformer } from "builder-util/out/fs"
33
import { readFile } from "fs-extra-p"
44
import * as path from "path"
55
import { deepAssign } from "read-config-file/out/deepAssign"
6+
import { Configuration } from "./configuration"
67
import { Packager } from "./packager"
78

89
/** @internal */
@@ -22,15 +23,19 @@ export function hasDep(name: string, info: Packager) {
2223
}
2324

2425
/** @internal */
25-
export function createTransformer(srcDir: string, extraMetadata: any): FileTransformer {
26+
export function createTransformer(srcDir: string, configuration: Configuration, extraMetadata: any): FileTransformer {
2627
const mainPackageJson = path.join(srcDir, "package.json")
28+
const isRemovePackageScripts = configuration.removePackageScripts !== false
2729
return file => {
2830
if (file === mainPackageJson) {
29-
return modifyMainPackageJson(file, extraMetadata)
31+
return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts)
3032
}
3133
else if (file.endsWith("/package.json") && file.includes("/node_modules/")) {
3234
return readFile(file, "utf-8")
33-
.then(it => cleanupPackageJson(JSON.parse(it), false))
35+
.then(it => cleanupPackageJson(JSON.parse(it), {
36+
isMain: false,
37+
isRemovePackageScripts,
38+
}))
3439
.catch(e => log.warn(e))
3540
}
3641
else {
@@ -52,17 +57,25 @@ export function createElectronCompilerHost(projectDir: string, cacheDir: string)
5257
return require(path.join(electronCompilePath, "config-parser")).createCompilerHostFromProjectRoot(projectDir, cacheDir)
5358
}
5459

55-
const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "keywords", "build", "scripts", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "bugs", "tags"])
60+
const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "keywords", "build", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "bugs", "tags"])
5661

57-
function cleanupPackageJson(data: any, isMain: boolean): any {
62+
interface CleanupPackageFileOptions {
63+
readonly isRemovePackageScripts: boolean
64+
readonly isMain: boolean
65+
}
66+
67+
function cleanupPackageJson(data: any, options: CleanupPackageFileOptions): any {
5868
const deps = data.dependencies
5969
// https://github.com/electron-userland/electron-builder/issues/507#issuecomment-312772099
6070
const isRemoveBabel = deps != null && typeof deps === "object" && !Object.getOwnPropertyNames(deps).some(it => it.startsWith("babel"))
6171
try {
6272
let changed = false
6373
for (const prop of Object.getOwnPropertyNames(data)) {
6474
// removing devDependencies from package.json breaks levelup in electron, so, remove it only from main package.json
65-
if (prop[0] === "_" || ignoredPackageMetadataProperties.has(prop) || (isMain && prop === "devDependencies") || (isRemoveBabel && prop === "babel")) {
75+
if (prop[0] === "_" ||
76+
ignoredPackageMetadataProperties.has(prop) ||
77+
(options.isRemovePackageScripts && prop === "scripts") ||
78+
(options.isMain && prop === "devDependencies") || (isRemoveBabel && prop === "babel")) {
6679
delete data[prop]
6780
changed = true
6881
}
@@ -79,14 +92,17 @@ function cleanupPackageJson(data: any, isMain: boolean): any {
7992
return null
8093
}
8194

82-
async function modifyMainPackageJson(file: string, extraMetadata: any) {
95+
async function modifyMainPackageJson(file: string, extraMetadata: any, isRemovePackageScripts: boolean) {
8396
const mainPackageData = JSON.parse(await readFile(file, "utf-8"))
8497
if (extraMetadata != null) {
8598
deepAssign(mainPackageData, extraMetadata)
8699
}
87100

88101
// https://github.com/electron-userland/electron-builder/issues/1212
89-
const serializedDataIfChanged = cleanupPackageJson(mainPackageData, true)
102+
const serializedDataIfChanged = cleanupPackageJson(mainPackageData, {
103+
isMain: true,
104+
isRemovePackageScripts,
105+
})
90106
if (serializedDataIfChanged != null) {
91107
return serializedDataIfChanged
92108
}

packages/electron-builder-lib/src/platformPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
227227
matcher.excludePatterns = excludePatterns
228228
}
229229
}
230-
const transformer = createTransformer(appDir, isElectronCompile ? {
230+
const transformer = createTransformer(appDir, config, isElectronCompile ? {
231231
originalMain: this.info.metadata.main,
232232
main: ELECTRON_COMPILE_SHIM_FILENAME, ...config.extraMetadata
233233
} : config.extraMetadata)

0 commit comments

Comments
 (0)