Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clean-publish

Package Overview
Dependencies
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clean-publish - npm Package Compare versions

Comparing version 1.1.9 to 2.0.0

100

clean-publish.js
#!/usr/bin/env node
const chalk = require('chalk')
const yargs = require('yargs')
const {

@@ -19,30 +17,17 @@ createTempDirectory,

const { argv } = yargs
.usage('$0')
.option('files', {
type: 'array',
desc: 'One or more exclude files'
})
.option('fields', {
type: 'array',
desc: 'One or more exclude package.json fields'
})
.option('without-publish', {
type: 'boolean',
desc: 'Clean package without npm publish'
})
.option('package-manager', {
types: 'string',
default: 'npm',
desc: 'Package manager to use'
})
.option('access', {
types: 'string',
desc: 'Whether the npm registry publishes ' +
'this package as a public package, or restricted'
})
.option('before-script', {
types: 'string',
desc: 'Run script on the to-release dir before npm publish'
})
const HELP =
'npx clean-publish\n' +
'\n' +
'Options:\n' +
' --help Show help\n' +
' --version Show version number\n' +
' --files One or more exclude files\n' +
' --fields One or more exclude package.json fields\n' +
' --without-publish Clean package without npm publish\n' +
' --package-manager Package manager to use\n' +
' --access Whether the npm registry publishes this package\n' +
' as a public package, or restricted\n' +
' --tag Registers the package with the given tag\n' +
' --before-script Run script on the to-release dir before npm\n' +
' publish'

@@ -53,7 +38,35 @@ const options = {}

function handleOptions () {
Object.assign(options, argv, {
withoutPublish: argv['without-publish'],
packageManager: argv['package-manager']
})
if (options['_'].length === 0) {
options.packageManager = 'npm'
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === '--help') {
process.stdout.write(HELP + '\n')
process.exit(0)
} else if (process.argv[i] === '--version') {
process.stdout.write(require('./package.json').version + '\n')
process.exit(0)
} else if (process.argv[i] === '--without-publish') {
options.withoutPublish = true
} else if (process.argv[i] === '--package-manager') {
options.packageManager = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--before-script') {
options.beforeScript = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--access') {
options.access = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--files') {
options.files = process.argv[i + 1].split(/,\s*/)
i += 1
} else if (process.argv[i] === '--tag') {
options.tag = process.argv[i + 1].split(/,\s*/)
i += 1
} else if (process.argv[i] === '--fields') {
options.fields = process.argv[i + 1].split(/,\s*/)
i += 1
} else {
options._ = process.argv[i]
}
}
if (!options._) {
return getConfig().then(config => {

@@ -67,5 +80,3 @@ Object.assign(options, config)

handleOptions()
.then(() => (
createTempDirectory()
))
.then(() => createTempDirectory())
.then(directoryName => {

@@ -82,5 +93,3 @@ tempDirectoryName = directoryName

})
.then(() => (
readPackageJSON()
))
.then(() => readPackageJSON())
.then(packageJson => {

@@ -99,3 +108,8 @@ const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)

if (!options.withoutPublish && isPrepublishSuccess) {
return publish(tempDirectoryName, options.packageManager, options.access)
return publish(
tempDirectoryName,
options.packageManager,
options.access,
options.tag
)
}

@@ -109,4 +123,4 @@ })

.catch(error => {
console.error(chalk.red(error))
process.stderr.write(error.stack + '\n')
process.exit(1)
})
#!/usr/bin/env node
const chalk = require('chalk')
const yargs = require('yargs')
const {
clearPackageJSON
} = require('./core')
const {
readJson,
readJsonFromStdin,
writeJson
} = require('./utils')
const { clearPackageJSON } = require('./core')
const { readJson, readJsonFromStdin, writeJson } = require('./utils')
const getConfig = require('./get-config')
const { isTTY } = process.stdin
const { argv } = yargs
.usage('$0')
.command(
isTTY
? '$0 <input> [options]'
: '$0 [options]',
'Clear package.json file',
yargsCommand => {
if (isTTY) {
yargsCommand
.positional('input', {
type: 'string',
desc: 'Input package.json file'
})
.require('input')
}
}
)
.option('fields', {
type: 'array',
desc: 'One or more exclude package.json fields'
})
.option('output', {
alias: 'o',
type: 'string',
desc: 'Output file name'
})
const HELP =
'npx clear-package-json <input> [options]\n' +
'\n' +
'Options:\n' +
' --help Show help\n' +
' --version Show version number\n' +
' --fields One or more exclude package.json fields\n' +
' --output, -o Output file name'
const options = {
fields: argv.fields
}
const options = {}
let input, output
function handleOptions () {
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === '--help') {
process.stdout.write(HELP + '\n')
process.exit(0)
} else if (process.argv[i] === '--version') {
process.stdout.write(require('./package.json').version + '\n')
process.exit(0)
} else if (process.argv[i] === '-o' || process.argv[i] === '--output') {
output = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--fields') {
options.fields = process.argv[i + 1].split(/,\s*/)
i += 1
} else {
input = process.argv[i]
}
}
if (!input) {
process.stderr.write(
HELP + '\n\nNot enough non-option arguments: got 0, need at least 1'
)
process.exit(1)
}
if (!options.fields) {

@@ -58,21 +52,13 @@ return getConfig().then(config => {

handleOptions()
.then(() => (
isTTY
? readJson(argv.input)
: readJsonFromStdin()
))
.then(() => (input ? readJson(input) : readJsonFromStdin()))
.then(packageJson => {
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
if (argv.output) {
return writeJson(
argv.output,
cleanPackageJSON,
{ spaces: 2 }
)
if (output) {
return writeJson(output, cleanPackageJSON, { spaces: 2 })
}
process.stdout.write(`${ JSON.stringify(cleanPackageJSON, null, ' ') }\n`)
process.stdout.write(`${JSON.stringify(cleanPackageJSON, null, ' ')}\n`)
})
.catch(error => {
console.error(chalk.red(error))
process.stderr.write(error.stack + '\n')
process.exit()
})
const path = require('path')
const spawn = require('cross-spawn')
const {
omit,
pick
} = require('ramda')
const {
regExpIndexOf,

@@ -16,3 +13,2 @@ multiCp,

} = require('./utils')
const IGNORE_FILES = require('./exception/ignore-files')

@@ -27,7 +23,5 @@ const IGNORE_FIELDS = require('./exception/ignore-fields')

function writePackageJSON (directoryName, packageJSON) {
return writeJson(
path.join(directoryName, 'package.json'),
packageJSON,
{ spaces: 2 }
)
return writeJson(path.join(directoryName, 'package.json'), packageJSON, {
spaces: 2
})
}

@@ -39,13 +33,16 @@

: IGNORE_FIELDS
let clearedScripts = { }
if (packageJson.scripts) {
clearedScripts = {
scripts: pick(NPM_SCRIPTS, packageJson.scripts)
const cleanPackageJSON = {}
for (const key in packageJson) {
if (!ignoreFields.includes(key) && key !== 'scripts') {
cleanPackageJSON[key] = packageJson[key]
}
}
const cleanPackageJSON = omit(ignoreFields, Object.assign(
{},
packageJson,
clearedScripts
))
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]
}
}
}

@@ -66,22 +63,24 @@ for (const i in cleanPackageJSON) {

: IGNORE_FILES
const filteredFiles = files.filter(file => (
regExpIndexOf(ignoreFiles, file) === false
))
const filteredFiles = files.filter(
file => regExpIndexOf(ignoreFiles, file) === false
)
return filteredFiles
}
function publish (cwd, packageManager, access) {
function publish (cwd, packageManager, access, tag) {
return new Promise((resolve, reject) => {
const args = access
? ['publish', '--access', access]
: ['publish']
const args = ['publish']
if (access) args.push('--access', access)
if (tag) args.push('--tag', tag)
spawn(packageManager, args, {
stdio: 'inherit',
cwd
}).on('close', (code, signal) => {
resolve({
code,
signal
})
.on('close', (code, signal) => {
resolve({
code,
signal
})
})
}).on('error', reject)
.on('error', reject)
})

@@ -103,6 +102,8 @@ }

function copyFiles (files, drectoryName) {
return multiCp(files.map(file => ({
from: path.join('./', file),
to: path.join(drectoryName, file)
})))
return multiCp(
files.map(file => ({
from: path.join('./', file),
to: path.join(drectoryName, file)
}))
)
}

@@ -109,0 +110,0 @@

@@ -0,0 +0,0 @@ module.exports = [

@@ -29,3 +29,5 @@ module.exports = [

'karma.conf.js',
'.flowconfig'
'.flowconfig',
/^changelog/i,
'.simple-pre-commit.json'
]

@@ -0,0 +0,0 @@ module.exports = [

@@ -7,13 +7,15 @@ /**

const path = require('path')
const cosmiconfig = require('cosmiconfig')
const { lilconfig } = require('lilconfig')
const PACKAGE_ERRORS = {
notObject: 'The `"clean-publish"` section of package.json ' +
'must be `an object`',
notObject:
'The `"clean-publish"` section of package.json ' + 'must be `an object`',
empty: 'The `"clean-publish"` section of package.json must `not be empty`',
filesNotStringsOrRegExps: 'The `files` in the `"clean-publish"` section ' +
'of package.json must be ' +
'`an array of strings or RegExps`',
fieldsNotStrings: 'The `fields` in the `"clean-publish"` section ' +
'of package.json must be `an array of strings`'
filesNotStringsOrRegExps:
'The `files` in the `"clean-publish"` section ' +
'of package.json must be ' +
'`an array of strings or RegExps`',
fieldsNotStrings:
'The `fields` in the `"clean-publish"` section ' +
'of package.json must be `an array of strings`'
}

@@ -23,18 +25,21 @@ const FILE_ERRORS = {

empty: 'Clean Publish config must `not be empty`',
filesNotStringsOrRegExps: 'The `files` in the Clean Publish config ' +
'must be `an array of strings or RegExps`',
fieldsNotStrings: 'The `fields` in Clean Publish config ' +
'must be `an array of strings`'
filesNotStringsOrRegExps:
'The `files` in the Clean Publish config ' +
'must be `an array of strings or RegExps`',
fieldsNotStrings:
'The `fields` in Clean Publish config ' + 'must be `an array of strings`'
}
const PACKAGE_EXAMPLE = '\n' +
' "clean-publish": {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'
const FILE_EXAMPLE = '\n' +
' {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'
const PACKAGE_EXAMPLE =
'\n' +
' "clean-publish": {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'
const FILE_EXAMPLE =
'\n' +
' {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'

@@ -80,8 +85,4 @@ function isStrings (value) {

function getConfig () {
const explorer = cosmiconfig('clean-publish', {
searchPlaces: [
'package.json',
'.clean-publish',
'.clean-publish.js'
]
const explorer = lilconfig('clean-publish', {
searchPlaces: ['package.json', '.clean-publish', '.clean-publish.js']
})

@@ -99,5 +100,7 @@ return explorer

'Can not parse `package.json`. ' +
message + '. ' +
'Change config according to Clean Publish docs.\n' +
PACKAGE_EXAMPLE + '\n'
message +
'. ' +
'Change config according to Clean Publish docs.\n' +
PACKAGE_EXAMPLE +
'\n'
)

@@ -108,6 +111,12 @@ } else if (err.reason && err.mark && err.mark.name) {

throw new Error(
'Can not parse `' + file + '` at ' + position + '. ' +
capitalize(err.reason) + '. ' +
'Change config according to Clean Publish docs.\n' +
FILE_EXAMPLE + '\n'
'Can not parse `' +
file +
'` at ' +
position +
'. ' +
capitalize(err.reason) +
'. ' +
'Change config according to Clean Publish docs.\n' +
FILE_EXAMPLE +
'\n'
)

@@ -127,11 +136,13 @@ } else {

throw new Error(
PACKAGE_ERRORS[error] + '. ' +
'Fix it according to Clean Publish docs.' +
`\n${ PACKAGE_EXAMPLE }\n`
PACKAGE_ERRORS[error] +
'. ' +
'Fix it according to Clean Publish docs.' +
`\n${PACKAGE_EXAMPLE}\n`
)
} else {
throw new Error(
FILE_ERRORS[error] + '. ' +
'Fix it according to Clean Publish docs.' +
`\n${ FILE_EXAMPLE }\n`
FILE_ERRORS[error] +
'. ' +
'Fix it according to Clean Publish docs.' +
`\n${FILE_EXAMPLE}\n`
)

@@ -138,0 +149,0 @@ }

{
"name": "clean-publish",
"version": "1.1.9",
"version": "2.0.0",
"description": "Clean your package before publish",

@@ -19,38 +19,9 @@ "keywords": [

"engines": {
"node": ">=6.11.5"
"node": "^10.17.0 || ^12.0.0 || >=14.0.0"
},
"dependencies": {
"chalk": "^2.4.1",
"cosmiconfig": "^5.0.7",
"cross-spawn": "^6.0.5",
"fs-extra": "^6.0.1",
"ramda": "^0.25.0",
"util.promisify": "^1.0.0",
"yargs": "^12.0.1"
},
"devDependencies": {
"eslint": "^5.1.0",
"eslint-ci": "^0.1.1",
"eslint-config-logux": "^23.0.2",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jest": "^21.17.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^3.1.0",
"jest": "^23.4.0",
"lint-staged": "^7.2.0",
"pre-commit": "^1.2.2"
},
"scripts": {
"test-clean-publish": "cd test/package && node ../../clean-publish.js --without-publish",
"test-clear-package-json": "cd test/package && node ../../clear-package-json.js package.json -o package.min.json",
"lint-staged": "lint-staged",
"lint": "eslint-ci *.js",
"test": "jest --runInBand && yarn run lint"
},
"pre-commit": [
"lint-staged"
]
"cross-spawn": "^7.0.3",
"fs-extra": "^9.1.0",
"lilconfig": "^2.0.2"
}
}

@@ -0,0 +0,0 @@ # Clean Publish

@@ -1,12 +0,4 @@

const promisify = require('util.promisify/polyfill')()
const fs = require('fs')
const fse = require('fs-extra')
const { mkdtemp, readdir } = require('fs').promises
const { copy, remove, readJson, writeJson } = require('fs-extra')
const mkdtemp = promisify(fs.mkdtemp)
const readdir = promisify(fs.readdir)
const copy = fse.copy
const remove = fse.remove
const readJson = fse.readJson
const writeJson = fse.writeJson
function regExpIndexOf (array, item) {

@@ -25,5 +17,3 @@ for (const i in array) {

function multiCp (files) {
return Promise.all(
files.map(({ from, to }) => copy(from, to))
)
return Promise.all(files.map(({ from, to }) => copy(from, to)))
}

@@ -30,0 +20,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc