clean-publish
Advanced tools
Comparing version 3.2.0 to 3.3.0
#!/usr/bin/env node | ||
import { parseListArg } from './utils.js' | ||
import { | ||
@@ -29,2 +30,3 @@ createTempDirectory, | ||
' --fields One or more exclude package.json fields\n' + | ||
' --exports One or more exclude exports conditions\n' + | ||
' --without-publish Clean package without npm publish\n' + | ||
@@ -64,3 +66,3 @@ ' --dry-run Reports the details of what would have been published\n' + | ||
} else if (process.argv[i] === '--files') { | ||
options.files = process.argv[i + 1].split(/,\s*/) | ||
options.files = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
@@ -74,7 +76,10 @@ } else if (process.argv[i] === '--clean-docs') { | ||
} else if (process.argv[i] === '--tag') { | ||
options.tag = process.argv[i + 1].split(/,\s*/) | ||
options.tag = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
} else if (process.argv[i] === '--fields') { | ||
options.fields = process.argv[i + 1].split(/,\s*/) | ||
options.fields = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
} else if (process.argv[i] === '--exports') { | ||
options.exports = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
} else { | ||
@@ -115,3 +120,3 @@ options._ = process.argv[i] | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields) | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields, options.exports) | ||
await writePackageJSON(tempDirectoryName, cleanPackageJSON) | ||
@@ -118,0 +123,0 @@ |
#!/usr/bin/env node | ||
import { readJson, readJsonFromStdin, writeJson } from './utils.js' | ||
import { readJson, readJsonFromStdin, writeJson, parseListArg } from './utils.js' | ||
import { clearPackageJSON } from './core.js' | ||
@@ -14,2 +14,3 @@ import { getConfig } from './get-config.js' | ||
' --fields One or more exclude package.json fields\n' + | ||
' --exports One or more exclude exports conditions\n' + | ||
' --output, -o Output file name' | ||
@@ -32,4 +33,7 @@ | ||
} else if (process.argv[i] === '--fields') { | ||
options.fields = process.argv[i + 1].split(/,\s*/) | ||
options.fields = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
} else if (process.argv[i] === '--exports') { | ||
options.exports = parseListArg(process.argv[i + 1]) | ||
i += 1 | ||
} else { | ||
@@ -47,5 +51,6 @@ input = process.argv[i] | ||
if (!options.fields) { | ||
if (!options.fields && !options.exports) { | ||
let config = await getConfig() | ||
options.fields = config.fields | ||
options.exports = config.exports | ||
} | ||
@@ -58,3 +63,3 @@ return [input, output, options] | ||
const packageJson = await (input ? readJson(input) : readJsonFromStdin()) | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields) | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields, options.exports) | ||
if (output) { | ||
@@ -61,0 +66,0 @@ await writeJson(output, cleanPackageJSON, { spaces: 2 }) |
36
core.js
@@ -13,3 +13,5 @@ import { promises as fs } from "fs" | ||
mkdtemp, | ||
remove | ||
remove, | ||
isObject, | ||
filterObjectByKey | ||
} from './utils.js' | ||
@@ -30,26 +32,20 @@ import IGNORE_FILES from './exception/ignore-files.js' | ||
export function clearPackageJSON (packageJson, inputIgnoreFields) { | ||
export function clearPackageJSON (packageJson, inputIgnoreFields, ignoreExports) { | ||
const ignoreFields = inputIgnoreFields | ||
? IGNORE_FIELDS.concat(inputIgnoreFields) | ||
: IGNORE_FIELDS | ||
const cleanPackageJSON = {} | ||
for (const key in packageJson) { | ||
if (!ignoreFields.includes(key) && key !== 'scripts') { | ||
cleanPackageJSON[key] = packageJson[key] | ||
} | ||
} | ||
const cleanPackageJSON = filterObjectByKey(packageJson, key => !ignoreFields.includes(key) && key !== 'scripts') | ||
if (packageJson.scripts && !ignoreFields.includes('scripts')) { | ||
cleanPackageJSON.scripts = {} | ||
for (const script in packageJson.scripts) { | ||
if (NPM_SCRIPTS.includes(script)) { | ||
cleanPackageJSON.scripts[script] = packageJson.scripts[script] | ||
} | ||
} | ||
cleanPackageJSON.scripts = filterObjectByKey(packageJson.scripts, script => NPM_SCRIPTS.includes(script)) | ||
} | ||
if (isObject(packageJson.exports) && !ignoreFields.includes('exports')) { | ||
const exportsFilter = ignoreExports && (condition => !ignoreExports.includes(condition)) | ||
cleanPackageJSON.exports = filterObjectByKey(packageJson.exports, exportsFilter, true) | ||
} | ||
for (const i in cleanPackageJSON) { | ||
if (typeof cleanPackageJSON[i] === 'object') { | ||
if (Object.keys(cleanPackageJSON[i]).length === 0) { | ||
delete cleanPackageJSON[i] | ||
} | ||
if (isObject(cleanPackageJSON[i]) && Object.keys(cleanPackageJSON[i]).length === 0) { | ||
delete cleanPackageJSON[i] | ||
} | ||
@@ -135,3 +131,3 @@ } | ||
const files = await glob(['**/*.js'], { cwd: drectoryName }) | ||
await files.map(async i => { | ||
await Promise.all(files.map(async i => { | ||
const file = join(drectoryName, i) | ||
@@ -145,3 +141,3 @@ const content = await fs.readFile(file) | ||
await fs.writeFile(file, cleaned) | ||
}) | ||
})) | ||
} |
@@ -9,2 +9,4 @@ /** | ||
import { isObject } from './utils.js' | ||
const PACKAGE_ERRORS = { | ||
@@ -20,2 +22,5 @@ notObject: | ||
'The `fields` in the `"clean-publish"` section ' + | ||
'of package.json must be `an array of strings`', | ||
exportsNotStrings: | ||
'The `exports` in the `"clean-publish"` section ' + | ||
'of package.json must be `an array of strings`' | ||
@@ -30,3 +35,5 @@ } | ||
fieldsNotStrings: | ||
'The `fields` in Clean Publish config ' + 'must be `an array of strings`' | ||
'The `fields` in Clean Publish config ' + 'must be `an array of strings`', | ||
exportsNotStrings: | ||
'The `exports` in Clean Publish config ' + 'must be `an array of strings`' | ||
} | ||
@@ -70,3 +77,3 @@ | ||
function configError (config) { | ||
if (!config || typeof config !== 'object') { | ||
if (!isObject(config)) { | ||
return 'notObject' | ||
@@ -83,2 +90,5 @@ } | ||
} | ||
if (!isStringsOrUndefined(config.exports)) { | ||
return 'exportsNotStrings' | ||
} | ||
return false | ||
@@ -85,0 +95,0 @@ } |
{ | ||
"name": "clean-publish", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Clean your package before publish", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
31
utils.js
@@ -50,1 +50,32 @@ import { promises as fs } from "fs" | ||
} | ||
export function parseListArg(arg) { | ||
return arg.trim().split(/\s*,\s*/) | ||
} | ||
export function isObject(object) { | ||
return Boolean(object) && typeof object === 'object' | ||
} | ||
export function filterObjectByKey (object, filterByKey = () => true, deep) { | ||
let result = {} | ||
let changed = false | ||
for (const key in object) { | ||
if (filterByKey(key)) { | ||
if (deep && isObject(object[key])) { | ||
result[key] = filterObjectByKey(object[key], filterByKey, deep) | ||
if (result[key] !== object[key]) { | ||
changed = true | ||
} | ||
} else { | ||
result[key] = object[key] | ||
} | ||
} else { | ||
changed = true | ||
} | ||
} | ||
return changed ? result : object | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20080
594