Skip to content

Commit

Permalink
feat: add warning pacakge support
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jul 9, 2018
1 parent dc7c4e2 commit ed88122
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ if (cli.flags.help) {
cli.showHelp();
}

canNpmPublish(cli.input[0])
canNpmPublish(cli.input[0], {
verbose: cli.flags.verbose
})
.then(() => {
process.exit(0);
})
Expand Down
33 changes: 25 additions & 8 deletions lib/can-npm-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@ const readPkg = require("read-pkg");
const validatePkgName = require("validate-npm-package-name");
/**
* Return rejected promise if the package name is invalid
* @param packagePath
* @param {string} packagePath
* @param {{verbose:boolean}} options
* @returns {Promise}
*/
const checkPkgName = packagePath => {
const checkPkgName = (packagePath, options) => {
return readPkg(packagePath).then(pkg => {
const name = pkg["name"];
const result = validatePkgName(name);
if (!result.validForNewPackages) {
// Treat Legacy Names as valid
// https://github.com/npm/validate-npm-package-name#legacy-names
// https://github.com/azu/can-npm-publish/issues/8
const isInvalidNamingInNewRule = !result.validForNewPackages;
if (isInvalidNamingInNewRule) {
if (Array.isArray(result.errors)) {
return Promise.reject(new Error(result.errors.join("\n")));
} else {
return Promise.reject(new Error(JSON.stringify(result)));
}
// warning is ignored by default
if (options.verbose && result.warnings) {
console.log(result.warnings.join("\n"));
}
}
});
};

/**
* Return rejected promise if the package is not `private:true`
* @param packagePath
* @param {string} packagePath
* @returns {Promise}
*/
const checkPrivateField = packagePath => {
Expand Down Expand Up @@ -88,7 +95,17 @@ const checkAlreadyPublish = packagePath => {
});
});
};
const canNpmPublish = packagePath => {
return Promise.all([checkPkgName(packagePath), checkAlreadyPublish(packagePath), checkPrivateField(packagePath)]);
/**
*
* @param {string} packagePath
* @param {{verbose : boolean}} options
* @returns {Promise<[any]>}
*/
const canNpmPublish = (packagePath, options = { verbose: false }) => {
return Promise.all([
checkPkgName(packagePath, options),
checkAlreadyPublish(packagePath),
checkPrivateField(packagePath)
]);
};
module.exports.canNpmPublish = canNpmPublish;

0 comments on commit ed88122

Please sign in to comment.