@@ -3,6 +3,7 @@ import { FileTransformer } from "builder-util/out/fs"
33import { readFile } from "fs-extra-p"
44import * as path from "path"
55import { deepAssign } from "read-config-file/out/deepAssign"
6+ import { Configuration } from "./configuration"
67import { 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 }
0 commit comments