Skip to content

Commit

Permalink
fix: npm ls parsing (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored May 10, 2021
1 parent 3460faf commit 8ea9d50
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
10 changes: 7 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4055,9 +4055,10 @@ const { exec } = __webpack_require__(986);
const npmArgs = __webpack_require__(510);

/**
* @param {import("@actions/exec").ExecOptions?} options
* @returns {Promise<Map<string, string>>}
*/
module.exports = async function listPackages({ silent } = { silent: false }) {
module.exports = async function listPackages(options = {}) {
let lines = "";
let stderr = "";
const returnCode = await exec("npm", npmArgs("ls", "--parseable", "--long", "--all"), {
Expand All @@ -4069,8 +4070,8 @@ module.exports = async function listPackages({ silent } = { silent: false }) {
stderr += data.toString();
},
},
silent,
ignoreReturnCode: true,
...options,
});

// NOTE: Ignore missing peer deps error.
Expand All @@ -4090,15 +4091,18 @@ module.exports = async function listPackages({ silent } = { silent: false }) {

const match = /^(?<name>@?\S+)@(?<version>\S+)$/u.exec(pkg);
if (match == null || match.groups == null) {
throw new Error(`Invalid line: "${line}"`);
return; // skip
}

/* eslint-disable dot-notation, prefer-destructuring -- Prevent TS4111 */
const name = match.groups["name"];
const version = match.groups["version"];
/* eslint-enable */

if (name == null || version == null) {
throw new Error(`Invalid name and version: "${line}"`);
}

packages.set(name.trim(), version.trim());
});
return packages;
Expand Down
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/empty_package.json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions lib/__tests__/fixtures/noversion_package.json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
18 changes: 18 additions & 0 deletions lib/__tests__/listPackages.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require("path");

const listPackages = require("../listPackages");

test("listPackages()", async () => {
Expand All @@ -6,3 +8,19 @@ test("listPackages()", async () => {
expect(packages.get("jest")).toMatch(/^\d+\.\d+\.\d+$/u);
expect(packages.get("@actions/core")).toMatch(/^\d+\.\d+\.\d+$/u);
});

test("listPackages() with an empty package.json", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(__dirname, "fixtures", "empty_package.json"),
});
expect(packages.size).toEqual(0);
});

test("listPackages() with a package.json having no version", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(__dirname, "fixtures", "noversion_package.json"),
});
expect(packages.size).toEqual(0);
});
10 changes: 7 additions & 3 deletions lib/listPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const { exec } = require("@actions/exec");
const npmArgs = require("./npmArgs");

/**
* @param {import("@actions/exec").ExecOptions?} options
* @returns {Promise<Map<string, string>>}
*/
module.exports = async function listPackages({ silent } = { silent: false }) {
module.exports = async function listPackages(options = {}) {
let lines = "";
let stderr = "";
const returnCode = await exec("npm", npmArgs("ls", "--parseable", "--long", "--all"), {
Expand All @@ -16,8 +17,8 @@ module.exports = async function listPackages({ silent } = { silent: false }) {
stderr += data.toString();
},
},
silent,
ignoreReturnCode: true,
...options,
});

// NOTE: Ignore missing peer deps error.
Expand All @@ -37,15 +38,18 @@ module.exports = async function listPackages({ silent } = { silent: false }) {

const match = /^(?<name>@?\S+)@(?<version>\S+)$/u.exec(pkg);
if (match == null || match.groups == null) {
throw new Error(`Invalid line: "${line}"`);
return; // skip
}

/* eslint-disable dot-notation, prefer-destructuring -- Prevent TS4111 */
const name = match.groups["name"];
const version = match.groups["version"];
/* eslint-enable */

if (name == null || version == null) {
throw new Error(`Invalid name and version: "${line}"`);
}

packages.set(name.trim(), version.trim());
});
return packages;
Expand Down

0 comments on commit 8ea9d50

Please sign in to comment.