clean-publish
Advanced tools
Comparing version 3.4.2 to 3.4.3
@@ -45,3 +45,3 @@ #!/usr/bin/env node | ||
async function handleOptions () { | ||
async function handleOptions() { | ||
let options = {} | ||
@@ -87,3 +87,3 @@ for (let i = 2; i < process.argv.length; i++) { | ||
i += 1 | ||
} else if (process.argv[i] === '--temp-dir') { | ||
} else if (process.argv[i] === '--temp-dir') { | ||
options.tempDir = process.argv[i + 1] | ||
@@ -103,3 +103,3 @@ i += 1 | ||
async function run () { | ||
async function run() { | ||
const options = await handleOptions() | ||
@@ -127,3 +127,7 @@ | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields, options.exports) | ||
const cleanPackageJSON = clearPackageJSON( | ||
packageJson, | ||
options.fields, | ||
options.exports | ||
) | ||
await writePackageJSON(tempDirectoryName, cleanPackageJSON) | ||
@@ -130,0 +134,0 @@ |
#!/usr/bin/env node | ||
import { readJson, readJsonFromStdin, writeJson, parseListArg } from './utils.js' | ||
import { | ||
readJson, | ||
readJsonFromStdin, | ||
writeJson, | ||
parseListArg | ||
} from './utils.js' | ||
import { clearPackageJSON } from './core.js' | ||
@@ -17,3 +22,3 @@ import { getConfig } from './get-config.js' | ||
async function handleOptions () { | ||
async function handleOptions() { | ||
const options = {} | ||
@@ -58,6 +63,10 @@ let input, output | ||
async function run () { | ||
async function run() { | ||
const [input, output, options] = await handleOptions() | ||
const packageJson = await (input ? readJson(input) : readJsonFromStdin()) | ||
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields, options.exports) | ||
const cleanPackageJSON = clearPackageJSON( | ||
packageJson, | ||
options.fields, | ||
options.exports | ||
) | ||
if (output) { | ||
@@ -64,0 +73,0 @@ await writeJson(output, cleanPackageJSON, { spaces: 2 }) |
85
core.js
@@ -1,2 +0,2 @@ | ||
import { promises as fs } from "fs" | ||
import { promises as fs } from 'fs' | ||
import { join } from 'path' | ||
@@ -22,7 +22,7 @@ import spawn from 'cross-spawn' | ||
export function readPackageJSON () { | ||
export function readPackageJSON() { | ||
return readJson('package.json') | ||
} | ||
export function writePackageJSON (directoryName, packageJSON) { | ||
export function writePackageJSON(directoryName, packageJSON) { | ||
return writeJson(join(directoryName, 'package.json'), packageJSON, { | ||
@@ -33,19 +33,36 @@ spaces: 2 | ||
export function clearPackageJSON (packageJson, inputIgnoreFields, ignoreExports) { | ||
export function clearPackageJSON( | ||
packageJson, | ||
inputIgnoreFields, | ||
ignoreExports | ||
) { | ||
const ignoreFields = inputIgnoreFields | ||
? IGNORE_FIELDS.concat(inputIgnoreFields) | ||
: IGNORE_FIELDS | ||
const cleanPackageJSON = filterObjectByKey(packageJson, key => !ignoreFields.includes(key) && key !== 'scripts') | ||
const cleanPackageJSON = filterObjectByKey( | ||
packageJson, | ||
key => !ignoreFields.includes(key) && key !== 'scripts' | ||
) | ||
if (packageJson.scripts && !ignoreFields.includes('scripts')) { | ||
cleanPackageJSON.scripts = filterObjectByKey(packageJson.scripts, script => NPM_SCRIPTS.includes(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) | ||
const exportsFilter = | ||
ignoreExports && (condition => !ignoreExports.includes(condition)) | ||
cleanPackageJSON.exports = filterObjectByKey( | ||
packageJson.exports, | ||
exportsFilter, | ||
true | ||
) | ||
} | ||
for (const i in cleanPackageJSON) { | ||
if (isObject(cleanPackageJSON[i]) && Object.keys(cleanPackageJSON[i]).length === 0) { | ||
if ( | ||
isObject(cleanPackageJSON[i]) && | ||
Object.keys(cleanPackageJSON[i]).length === 0 | ||
) { | ||
delete cleanPackageJSON[i] | ||
@@ -57,3 +74,3 @@ } | ||
export function clearFilesList (files, inputIgnoreFiles) { | ||
export function clearFilesList(files, inputIgnoreFiles) { | ||
const ignoreFiles = inputIgnoreFiles | ||
@@ -68,3 +85,3 @@ ? IGNORE_FILES.concat(inputIgnoreFiles) | ||
export function publish (cwd, { packageManager, access, tag, dryRun }) { | ||
export function publish(cwd, { packageManager, access, tag, dryRun }) { | ||
return new Promise((resolve, reject) => { | ||
@@ -89,7 +106,7 @@ const args = ['publish'] | ||
export function readSrcDirectory () { | ||
export function readSrcDirectory() { | ||
return readdir('./') | ||
} | ||
export async function createTempDirectory (name) { | ||
export async function createTempDirectory(name) { | ||
if (name) { | ||
@@ -110,7 +127,7 @@ try { | ||
export function removeTempDirectory (directoryName) { | ||
export function removeTempDirectory(directoryName) { | ||
return remove(directoryName) | ||
} | ||
export function copyFiles (files, drectoryName) { | ||
export function copyFiles(files, drectoryName) { | ||
return multiCp( | ||
@@ -124,3 +141,3 @@ files.map(file => ({ | ||
export function runScript (script, ...args) { | ||
export function runScript(script, ...args) { | ||
return new Promise((resolve, reject) => { | ||
@@ -135,6 +152,5 @@ spawn(script, args, { stdio: 'inherit' }) | ||
export function getReadmeUrlFromRepository (repository) { | ||
const repoUrl = typeof repository === 'string' | ||
? repository | ||
: repository && repository.url | ||
export function getReadmeUrlFromRepository(repository) { | ||
const repoUrl = | ||
typeof repository === 'string' ? repository : repository && repository.url | ||
if (repoUrl) return hostedGitInfo.fromUrl(repoUrl).docs() | ||
@@ -145,3 +161,3 @@ | ||
export async function cleanDocs (drectoryName, repository) { | ||
export async function cleanDocs(drectoryName, repository) { | ||
const readmePath = join(drectoryName, 'README.md') | ||
@@ -151,3 +167,4 @@ const readme = await fs.readFile(readmePath) | ||
if (readmeUrl) { | ||
const cleaned = readme.toString().split(/\n##\s*\w/m)[0] + | ||
const cleaned = | ||
readme.toString().split(/\n##\s*\w/m)[0] + | ||
'\n## Docs\n' + | ||
@@ -159,14 +176,16 @@ `Read **[full docs](${readmeUrl})** on GitHub.\n` | ||
export async function cleanComments (drectoryName) { | ||
export async function cleanComments(drectoryName) { | ||
const files = await glob(['**/*.js'], { cwd: drectoryName }) | ||
await Promise.all(files.map(async i => { | ||
const file = join(drectoryName, i) | ||
const content = await fs.readFile(file) | ||
const cleaned = content | ||
.toString() | ||
.replace(/\s*\/\/.*\n/gm, '\n') | ||
.replace(/\n+/gm, '\n') | ||
.replace(/^\n+/gm, '') | ||
await fs.writeFile(file, cleaned) | ||
})) | ||
await Promise.all( | ||
files.map(async i => { | ||
const file = join(drectoryName, i) | ||
const content = await fs.readFile(file) | ||
const cleaned = content | ||
.toString() | ||
.replace(/\s*\/\/.*\n/gm, '\n') | ||
.replace(/\n+/gm, '\n') | ||
.replace(/^\n+/gm, '') | ||
await fs.writeFile(file, cleaned) | ||
}) | ||
) | ||
} |
@@ -23,3 +23,4 @@ export default [ | ||
'resolutions', | ||
'typeCoverage' | ||
'typeCoverage', | ||
'c8' | ||
] |
@@ -60,3 +60,3 @@ /** | ||
function isStrings (value) { | ||
function isStrings(value) { | ||
if (!Array.isArray(value)) return false | ||
@@ -66,3 +66,3 @@ return value.every(isString) | ||
function isStringsOrRegExps (value) { | ||
function isStringsOrRegExps(value) { | ||
if (!Array.isArray(value)) return false | ||
@@ -72,19 +72,19 @@ return value.every(i => isString(i) || i instanceof RegExp) | ||
function isStringOrUndefined (value) { | ||
function isStringOrUndefined(value) { | ||
return typeof value === 'undefined' || isString(value) | ||
} | ||
function isStringsOrUndefined (value) { | ||
function isStringsOrUndefined(value) { | ||
return typeof value === 'undefined' || isStrings(value) | ||
} | ||
function isStringsOrRegExpsOrUndefined (value) { | ||
function isStringsOrRegExpsOrUndefined(value) { | ||
return typeof value === 'undefined' || isStringsOrRegExps(value) | ||
} | ||
function capitalize (str) { | ||
function capitalize(str) { | ||
return str[0].toUpperCase() + str.slice(1) | ||
} | ||
function configError (config) { | ||
function configError(config) { | ||
if (!isObject(config)) { | ||
@@ -111,3 +111,3 @@ return 'notObject' | ||
export function getConfig () { | ||
export function getConfig() { | ||
const explorer = lilconfig('clean-publish', { | ||
@@ -114,0 +114,0 @@ searchPlaces: ['package.json', '.clean-publish', '.clean-publish.js'] |
{ | ||
"name": "clean-publish", | ||
"version": "3.4.2", | ||
"version": "3.4.3", | ||
"description": "Clean your package before publish", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
10
utils.js
@@ -1,2 +0,2 @@ | ||
import { promises as fs } from "fs" | ||
import { promises as fs } from 'fs' | ||
import fse from 'fs-extra' | ||
@@ -7,3 +7,3 @@ | ||
export function regExpIndexOf (array, item) { | ||
export function regExpIndexOf(array, item) { | ||
for (const i in array) { | ||
@@ -25,7 +25,7 @@ if (typeof array[i] === 'string' && item === array[i]) { | ||
export function multiCp (files) { | ||
export function multiCp(files) { | ||
return Promise.all(files.map(({ from, to }) => fse.copy(from, to))) | ||
} | ||
export function readJsonFromStdin () { | ||
export function readJsonFromStdin() { | ||
process.stdin.setEncoding('utf8') | ||
@@ -61,3 +61,3 @@ return new Promise((resolve, reject) => { | ||
export function filterObjectByKey (object, filterByKey = () => true, deep) { | ||
export function filterObjectByKey(object, filterByKey = () => true, deep) { | ||
let result = {} | ||
@@ -64,0 +64,0 @@ let changed = false |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
21408
665