Skip to content

Commit

Permalink
feat: add option removePackageKeywords (#5814)
Browse files Browse the repository at this point in the history
Co-authored-by: Keith Rocheck <749812+krocheck@users.noreply.github.com>
  • Loading branch information
beliaev-maksim and krocheck authored Apr 23, 2021
1 parent 79c9f6b commit dcf570f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/app-builder-lib/scheme.json
Original file line number Diff line number Diff line change
Expand Up @@ -6470,6 +6470,11 @@
"description": "Whether to remove `scripts` field from `package.json` files.",
"type": "boolean"
},
"removePackageKeywords": {
"default": true,
"description": "Whether to remove `keywords` field from `package.json` files.",
"type": "boolean"
},
"rpm": {
"anyOf": [
{
Expand Down
7 changes: 7 additions & 0 deletions packages/app-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ export interface Configuration extends PlatformSpecificBuildOptions {
* @default true
*/
readonly removePackageScripts?: boolean

/**
* Whether to remove `keywords` field from `package.json` files.
*
* @default true
*/
readonly removePackageKeywords?: boolean
}

export interface AfterPackContext {
Expand Down
11 changes: 8 additions & 3 deletions packages/app-builder-lib/src/fileTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export function hasDep(name: string, info: Packager) {
export function createTransformer(srcDir: string, configuration: Configuration, extraMetadata: any, extraTransformer: FileTransformer | null): FileTransformer {
const mainPackageJson = path.join(srcDir, "package.json")
const isRemovePackageScripts = configuration.removePackageScripts !== false
const isRemovePackageKeywords = configuration.removePackageKeywords !== false
const packageJson = path.sep + "package.json"
return file => {
if (file === mainPackageJson) {
return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts)
return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts, isRemovePackageKeywords)
}

if (file.endsWith(packageJson) && file.includes(NODE_MODULES_PATTERN)) {
Expand All @@ -40,6 +41,7 @@ export function createTransformer(srcDir: string, configuration: Configuration,
cleanupPackageJson(JSON.parse(it), {
isMain: false,
isRemovePackageScripts,
isRemovePackageKeywords
})
)
.catch(e => log.warn(e))
Expand All @@ -64,10 +66,11 @@ 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", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "tags"])
const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "build", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "tags"])

interface CleanupPackageFileOptions {
readonly isRemovePackageScripts: boolean
readonly isRemovePackageKeywords: boolean
readonly isMain: boolean
}

Expand All @@ -83,6 +86,7 @@ function cleanupPackageJson(data: any, options: CleanupPackageFileOptions): any
prop[0] === "_" ||
ignoredPackageMetadataProperties.has(prop) ||
(options.isRemovePackageScripts && prop === "scripts") ||
(options.isRemovePackageKeywords && prop === "keywords") ||
(options.isMain && prop === "devDependencies") ||
(!options.isMain && prop === "bugs") ||
(isRemoveBabel && prop === "babel")
Expand All @@ -102,7 +106,7 @@ function cleanupPackageJson(data: any, options: CleanupPackageFileOptions): any
return null
}

async function modifyMainPackageJson(file: string, extraMetadata: any, isRemovePackageScripts: boolean) {
async function modifyMainPackageJson(file: string, extraMetadata: any, isRemovePackageScripts: boolean, isRemovePackageKeywords: boolean) {
const mainPackageData = JSON.parse(await readFile(file, "utf-8"))
if (extraMetadata != null) {
deepAssign(mainPackageData, extraMetadata)
Expand All @@ -112,6 +116,7 @@ async function modifyMainPackageJson(file: string, extraMetadata: any, isRemoveP
const serializedDataIfChanged = cleanupPackageJson(mainPackageData, {
isMain: true,
isRemovePackageScripts,
isRemovePackageKeywords
})
if (serializedDataIfChanged != null) {
return serializedDataIfChanged
Expand Down

0 comments on commit dcf570f

Please sign in to comment.