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A3H,CAAR,CAAzC;;AAOA,SAAA,kBAAA,CAA4B,IAA5B,EAAuC,OAAvC,EAAyE;AACvE,QAAM,OAAO,KAAK,YAAlB,CADuE,CAEvE;;AACA,QAAM,gBAAgB,QAAQ,IAAR,IAAgB,OAAO,IAAP,KAAgB,QAAhC,IAA4C,CAAC,OAAO,mBAAP,CAA2B,IAA3B,EAAiC,IAAjC,CAAsC,MAAM,GAAG,UAAH,CAAc,OAAd,CAA5C,CAAnE;;AACA,MAAI;AACF,QAAI,UAAU,KAAd;;AACA,SAAK,MAAM,IAAX,IAAmB,OAAO,mBAAP,CAA2B,IAA3B,CAAnB,EAAqD;AACnD;AACA,UAAI,KAAK,CAAL,MAAY,GAAZ,IACF,iCAAiC,GAAjC,CAAqC,IAArC,CADE,IAED,QAAQ,sBAAR,IAAkC,SAAS,SAF1C,IAGD,QAAQ,MAAR,IAAkB,SAAS,iBAH1B,IAID,CAAC,QAAQ,MAAT,IAAmB,SAAS,MAJ3B,IAKD,iBAAiB,SAAS,OAL7B,EAKuC;AACrC,eAAO,KAAK,IAAL,CAAP;AACA,kBAAU,IAAV;AACD;AACF;;AAED,QAAI,OAAJ,EAAa;AACX,aAAO,KAAK,SAAL,CAAe,IAAf,EAAqB,IAArB,EAA2B,CAA3B,CAAP;AACD;AACF,GAlBD,CAmBA,OAAO,CAAP,EAAU;AACR,8BAAM,CAAN;AACD;;AAED,SAAO,IAAP;AACD","sourcesContent":["import { debug, log, deepAssign } from \"builder-util\"\nimport { FileTransformer } from \"builder-util/out/fs\"\nimport { readFile } from \"fs-extra-p\"\nimport * as path from \"path\"\nimport { Configuration } from \"./configuration\"\nimport { Packager } from \"./packager\"\n\n/** @internal */\nexport const NODE_MODULES_PATTERN = `${path.sep}node_modules${path.sep}`\n\n/** @internal */\nexport function isElectronCompileUsed(info: Packager): boolean {\n if (info.config.electronCompile != null) {\n return info.config.electronCompile\n }\n\n // if in devDependencies - it means that babel is used for precompilation or for some reason user decided to not use electron-compile for production\n return hasDep(\"electron-compile\", info)\n}\n\n/** @internal */\nexport function hasDep(name: string, info: Packager) {\n const deps = info.metadata.dependencies\n return deps != null && name in deps\n}\n\n/** @internal */\nexport function createTransformer(srcDir: string, configuration: Configuration, extraMetadata: any, extraTransformer: FileTransformer | null): FileTransformer {\n const mainPackageJson = path.join(srcDir, \"package.json\")\n const isRemovePackageScripts = configuration.removePackageScripts !== false\n const packageJson = path.sep + \"package.json\"\n return file => {\n if (file === mainPackageJson) {\n return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts)\n }\n\n if (file.endsWith(packageJson) && file.includes(NODE_MODULES_PATTERN)) {\n return readFile(file, \"utf-8\")\n .then(it => cleanupPackageJson(JSON.parse(it), {\n isMain: false,\n isRemovePackageScripts,\n }))\n .catch(e => log.warn(e))\n }\n else if (extraTransformer != null) {\n return extraTransformer(file)\n }\n else {\n return null\n }\n }\n}\n\n/** @internal */\nexport interface CompilerHost {\n compile(file: string): any\n\n saveConfiguration(): Promise<any>\n}\n\n/** @internal */\nexport function createElectronCompilerHost(projectDir: string, cacheDir: string): Promise<CompilerHost> {\n const electronCompilePath = path.join(projectDir, \"node_modules\", \"electron-compile\", \"lib\")\n return require(path.join(electronCompilePath, \"config-parser\")).createCompilerHostFromProjectRoot(projectDir, cacheDir)\n}\n\nconst ignoredPackageMetadataProperties = new Set([\"dist\", \"gitHead\", \"keywords\", \"build\", \"jspm\", \"ava\", \"xo\", \"nyc\", \"eslintConfig\", \"contributors\", \"bundleDependencies\", \"tags\"])\n\ninterface CleanupPackageFileOptions {\n readonly isRemovePackageScripts: boolean\n readonly isMain: boolean\n}\n\nfunction cleanupPackageJson(data: any, options: CleanupPackageFileOptions): any {\n const deps = data.dependencies\n // https://github.com/electron-userland/electron-builder/issues/507#issuecomment-312772099\n const isRemoveBabel = deps != null && typeof deps === \"object\" && !Object.getOwnPropertyNames(deps).some(it => it.startsWith(\"babel\"))\n try {\n let changed = false\n for (const prop of Object.getOwnPropertyNames(data)) {\n // removing devDependencies from package.json breaks levelup in electron, so, remove it only from main package.json\n if (prop[0] === \"_\" ||\n ignoredPackageMetadataProperties.has(prop) ||\n (options.isRemovePackageScripts && prop === \"scripts\") ||\n (options.isMain && prop === \"devDependencies\") ||\n (!options.isMain && prop === \"bugs\") ||\n (isRemoveBabel && prop === \"babel\")) {\n delete data[prop]\n changed = true\n }\n }\n\n if (changed) {\n return JSON.stringify(data, null, 2)\n }\n }\n catch (e) {\n debug(e)\n }\n\n return null\n}\n\nasync function modifyMainPackageJson(file: string, extraMetadata: any, isRemovePackageScripts: boolean) {\n const mainPackageData = JSON.parse(await readFile(file, \"utf-8\"))\n if (extraMetadata != null) {\n deepAssign(mainPackageData, extraMetadata)\n }\n\n // https://github.com/electron-userland/electron-builder/issues/1212\n const serializedDataIfChanged = cleanupPackageJson(mainPackageData, {\n isMain: true,\n isRemovePackageScripts,\n })\n if (serializedDataIfChanged != null) {\n return serializedDataIfChanged\n }\n else if (extraMetadata != null) {\n return JSON.stringify(mainPackageData, null, 2)\n }\n return null\n}"],"sourceRoot":""}