Skip to content

Commit

Permalink
feat: option removePackageScripts
Browse files Browse the repository at this point in the history
Close #2461
  • Loading branch information
develar committed Feb 1, 2018
1 parent 6faf828 commit d6455d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/electron-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 24 additions & 8 deletions packages/electron-builder-lib/src/fileTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 {
Expand All @@ -52,17 +57,25 @@ 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"))
try {
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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder-lib/src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
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)
Expand Down

0 comments on commit d6455d1

Please sign in to comment.