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: parse Yarn JSON on any OS #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
68 changes: 39 additions & 29 deletions src/exec/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {EOL} from "os";

const STRING_PROPERTIES = ["name", "current", "wanted", "latest"];

function isNotEmptyString(value) {
Expand All @@ -21,7 +19,9 @@ function tryParse(parser) {
try {
return parser(stdout);
} catch (err) {
err.message = `Error when trying to parse stdout from command '${cmd}': ${err.message}`;
err.message = `Error when trying to parse stdout from command '${cmd}': ${
err.message
}`;
throw err;
}
};
Expand Down Expand Up @@ -60,8 +60,9 @@ function yarnParser(stdout, wantedTypeProperty) {
/* in some cases (e.g. when printing the outdated result), yarn prints for each line a separate JSON object */
/* in that case, we need to look for a { type: "table" } object which holds the interesting data to display */
}

const dataLine = stdout
.split(EOL)
.split("\n")
.map(line => line.trim())
.filter(line => line !== "")
.find(line => {
Expand All @@ -70,12 +71,16 @@ function yarnParser(stdout, wantedTypeProperty) {

return parsedLine.type === wantedTypeProperty;
} catch (error) {
console.error(error.stack);

return false;
}
});

if (dataLine === undefined) {
throw new Error(`Could not find object with type === ${wantedTypeProperty}`);
throw new Error(
`Could not find object with type === ${wantedTypeProperty}`
);
}

return JSON.parse(dataLine);
Expand Down Expand Up @@ -107,38 +112,36 @@ const parse = {
list: tryParse(stdout => {
const parsed = npmParser(stdout);

return (parsed.dependencies === undefined ?
return parsed.dependencies === undefined ?
[] :
Object.keys(parsed.dependencies)
.map(name => ({
name,
version: parsed.dependencies[name].version,
}))
.sort(sortByName));
.sort(sortByName);
}),
},
yarn: {
outdated: tryParse(
stdout => {
const parsed = yarnParser(stdout, "table");

if (parsed === null) {
return [];
}

return parsed.data.body
.map(row => arrToObj(row, parsed.data.head))
.map(dep =>
returnIfValid({
name: dep.Package,
current: dep.Current,
wanted: dep.Wanted,
latest: dep.Latest,
})
)
.sort(sortByName);
outdated: tryParse(stdout => {
const parsed = yarnParser(stdout, "table");

if (parsed === null) {
return [];
}
),

return parsed.data.body
.map(row => arrToObj(row, parsed.data.head))
.map(dep =>
returnIfValid({
name: dep.Package,
current: dep.Current,
wanted: dep.Wanted,
latest: dep.Latest,
})
)
.sort(sortByName);
}),
list: tryParse(stdout => {
const parsed = yarnParser(stdout, "tree");

Expand All @@ -150,8 +153,15 @@ const parse = {
.map(dependency => {
const [name, version] = dependency.name.split("@");

if (isNotEmptyString(name) === false || isNotEmptyString(version) === false) {
throw new Error(`Could not parse dependency name "${dependency.name}"`);
if (
isNotEmptyString(name) === false ||
isNotEmptyString(version) === false
) {
throw new Error(
`Could not parse dependency name "${
dependency.name
}"`
);
}

return {
Expand Down