Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update dependencies & Node 16 supports #18

Merged
merged 4 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
npx --no-install lint-staged
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10, 12, 14]
node-version: [ 12, 14, 16 ]
steps:
- name: checkout
uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"timeout": "5000"
}
2 changes: 1 addition & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ canNpmPublish(cli.input[0], {
.then(() => {
process.exit(0);
})
.catch(error => {
.catch((error) => {
if (cli.flags.verbose) {
console.error(error.message);
}
Expand Down
39 changes: 29 additions & 10 deletions lib/can-npm-publish.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
// MIT © 2018 azu
"use strict";
const path = require("path");
const spawn = require("cross-spawn");
const readPkg = require("read-pkg");
const validatePkgName = require("validate-npm-package-name");
/**
* @param {string} [filePathOrDirPath]
* @returns {Promise<readPkg.NormalizedPackageJson>}
*/
const readPkgWithPath = (filePathOrDirPath) => {
if (filePathOrDirPath) {
const isJSON = path.extname(filePathOrDirPath) === ".json";
if (isJSON) {
return Promise.resolve(require(filePathOrDirPath));
}
return readPkg({ cwd: filePathOrDirPath });
} else {
return readPkg();
}
};
/**
* Return rejected promise if the package name is invalid
* @param {string} packagePath
* @param {{verbose:boolean}} options
* @returns {Promise}
*/
const checkPkgName = (packagePath, options) => {
return readPkg(packagePath).then(pkg => {
return readPkgWithPath(packagePath).then((pkg) => {
const name = pkg["name"];
const result = validatePkgName(name);
// Treat Legacy Names as valid
Expand All @@ -34,8 +50,8 @@ const checkPkgName = (packagePath, options) => {
* @param {string} packagePath
* @returns {Promise}
*/
const checkPrivateField = packagePath => {
return readPkg(packagePath).then(pkg => {
const checkPrivateField = (packagePath) => {
return readPkgWithPath(packagePath).then((pkg) => {
if (pkg["private"] === true) {
return Promise.reject(new Error("This package is private."));
}
Expand All @@ -56,15 +72,18 @@ const viewPackage = (packageName, registry) => {
let result = "";
let errorResult = "";

view.stdout.on("data", data => {
view.stdout.on("data", (data) => {
result += data.toString();
});

view.stderr.on("data", err => {
view.stderr.on("data", (err) => {
errorResult += err.toString();
});

view.on("close", code => {
view.on("close", (code) => {
if (code !== 0) {
Copy link
Owner Author

@azu azu Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js 16 support.

When package is 404, errorResult is "" in Node.js 16.

return reject(new Error(errorResult));
}
const resultJSON = JSON.parse(result);
if (resultJSON && resultJSON.error) {
// the package is not in the npm registry => can publish
Expand All @@ -75,13 +94,13 @@ const viewPackage = (packageName, registry) => {
return reject(new Error(errorResult));
}
}
resolve(JSON.parse(result));
resolve(resultJSON);
});
});
};

const checkAlreadyPublish = packagePath => {
return readPkg(packagePath).then(pkg => {
const checkAlreadyPublish = (packagePath) => {
return readPkgWithPath(packagePath).then((pkg) => {
const name = pkg["name"];
const version = pkg["version"];
const publishConfig = pkg["publishConfig"];
Expand All @@ -92,7 +111,7 @@ const checkAlreadyPublish = packagePath => {
if (version === undefined) {
return Promise.reject(new Error("This package has no `version`."));
}
return viewPackage(name, registry).then(versions => {
return viewPackage(name, registry).then((versions) => {
if (versions.includes(version)) {
return Promise.reject(new Error(`${name}@${version} is already published`));
}
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@
},
"scripts": {
"test": "mocha test",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"precommit": "lint-staged",
"postcommit": "git reset"
"postcommit": "git reset",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"prepare": "git config --local core.hooksPath .githooks"
},
"dependencies": {
"cross-spawn": "^6.0.5",
"meow": "^4.0.0",
"read-pkg": "^3.0.0",
"cross-spawn": "^7.0.3",
"meow": "^9.0.0",
"read-pkg": "^5.0.0",
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"husky": "^0.14.3",
"lint-staged": "^6.0.1",
"mocha": "^5.0.0",
"prettier": "^1.10.2"
"lint-staged": "^11.0.0",
"mocha": "^9.0.0",
"prettier": "^2.3.1"
},
"prettier": {
"singleQuote": false,
"printWidth": 120,
"tabWidth": 4
"tabWidth": 4,
"trailingComma": "none"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,css}": [
"prettier --write",
"git add"
"prettier --write"
]
}
}
12 changes: 6 additions & 6 deletions test/can-npm-publish-bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ const shouldNotCalled = () => {
};

describe("can-npm-publish bin", () => {
it("should return 0, it can publish", done => {
it("should return 0, it can publish", (done) => {
const bin = spawn("node", [binPath, path.join(__dirname, "fixtures/not-published-yet.json")]);

// Finish the test when the executable finishes and returns 0
bin.on("close", exit_code => {
bin.on("close", (exit_code) => {
assert.ok(exit_code === 0);
done();
});
});
it("should return 1, it can't publish", done => {
it("should return 1, it can't publish", (done) => {
const bin = spawn("node", [binPath, path.join(__dirname, "fixtures/already-published.json")]);

// Finish the test when the executable finishes and returns 1
bin.on("close", exit_code => {
bin.on("close", (exit_code) => {
assert.ok(exit_code === 1);
done();
});
});
it("should send errors to stderr when verbose, it can't publish", done => {
it("should send errors to stderr when verbose, it can't publish", (done) => {
const bin = spawn("node", [binPath, path.join(__dirname, "fixtures/already-published.json"), "--verbose"]);

// Finish the test and stop the executable when it outputs to stderr
bin.stderr.on("data", data => {
bin.stderr.on("data", (data) => {
assert.ok(/almin@0.15.2 is already published/.test(data));
bin.kill();
done();
Expand Down
29 changes: 17 additions & 12 deletions test/can-npm-publish-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ const shouldNotCalled = () => {
};
describe("can-npm-publish", () => {
it("should be rejected, it is invalid name", () => {
return canNpmPublish(path.join(__dirname, "fixtures/invalid-name.json")).then(shouldNotCalled, error => {
assert.ok(/Invalid name/s.test(error.message));
return canNpmPublish(path.join(__dirname, "fixtures/invalid-name.json")).then(shouldNotCalled, (error) => {
assert.ok(/name can only contain URL-friendly characters/s.test(error.message));
});
});
it("should be rejected, it is private:true", () => {
return canNpmPublish(path.join(__dirname, "fixtures/private.json")).then(shouldNotCalled, error => {
return canNpmPublish(path.join(__dirname, "fixtures/private.json")).then(shouldNotCalled, (error) => {
assert.ok(/This package is private/.test(error.message));
});
});
it("should be rejected, it is already published", () => {
return canNpmPublish(path.join(__dirname, "fixtures/already-published.json")).then(shouldNotCalled, error => {
return canNpmPublish(path.join(__dirname, "fixtures/already-published.json")).then(shouldNotCalled, (error) => {
assert.ok(/is already published/.test(error.message));
});
});
it("should be rejected, it is already published to yarnpkg registry", () => {
return canNpmPublish(path.join(__dirname, "fixtures/already-published-registry.json")).then(
shouldNotCalled,
error => {
(error) => {
assert.ok(/is already published/.test(error.message));
}
);
});
it("should be rejected, it is already published scoped package", () => {
return canNpmPublish(path.join(__dirname, "fixtures/scoped-package.json")).then(shouldNotCalled, error => {
return canNpmPublish(path.join(__dirname, "fixtures/scoped-package.json")).then(shouldNotCalled, (error) => {
assert.ok(/is already published/.test(error.message));
});
});
Expand All @@ -51,11 +51,16 @@ describe("can-npm-publish", () => {
}
};

return canNpmPublish(path.join(__dirname, "fixtures/legacy-name.json"), { verbose: true }).then(() => {
// Restore stderr to normal
process.stderr.write = stderrWrite;

assert.ok(/name can no longer contain capital letters/.test(stderrOutput));
}, shouldNotCalled);
return canNpmPublish(path.join(__dirname, "fixtures/legacy-name.json"), { verbose: true }).then(
() => {
// Restore stderr to normal
process.stderr.write = stderrWrite;
assert.ok(/name can no longer contain capital letters/.test(stderrOutput));
},
(error) => {
console.log(error);
shouldNotCalled();
}
);
});
});
2 changes: 1 addition & 1 deletion test/fixtures/legacy-name.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"private": false,
"name": "lEgAcY-nAmE",
"name": "JSONStream",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use actual example

"version": "1.0.0"
}
1 change: 0 additions & 1 deletion test/mocha.opts

This file was deleted.

Loading