Skip to content

Commit

Permalink
read node version from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed May 24, 2024
1 parent fc962f7 commit 8e95bc5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/node-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,30 @@ class NodeVersion {
if (!Configuration.strict) {
return true;
}
let versionFile;
let version;
if (isValidFileSync(path.join(Configuration.rootPath, ".node-version"))) {
versionFile = ".node-version";
version = fs.readFileSync(
path.join(Configuration.rootPath, ".node-version"),
"utf-8",
);
} else if (isValidFileSync(path.join(Configuration.rootPath, ".nvmrc"))) {
versionFile = ".nvmrc";
version = fs.readFileSync(
path.join(Configuration.rootPath, ".nvmrc"),
"utf-8",
);
} else if (isValidFileSync(path.join(Configuration.rootPath, "package.json"))) {
const packageFileContent = fs.readFileSync(
path.join(Configuration.rootPath, "package.json"),
"utf-8",
);
const packageJson = JSON.parse(packageFileContent)
if (packageJson.engines && packageJson.engines.node) {
version = packageJson.engines.node.replace(/[^0-9.]/g, '');
}
}
if (!versionFile) {
if (!version) {
return true;
}
const version = fs.readFileSync(
path.join(Configuration.rootPath, versionFile),
"utf-8",
);
return compareVersions.compare(version, this.version, ">=");
}

Expand All @@ -48,19 +59,30 @@ class NodeVersion {
if (!Configuration.strict) {
return true;
}
let versionFile;
let version;
if (await isValidFile(path.join(Configuration.rootPath, ".node-version"))) {
versionFile = ".node-version";
version = await promisesFs.readFile(
path.join(Configuration.rootPath, ".node-version"),
"utf-8",
);
} else if (await isValidFile(path.join(Configuration.rootPath, ".nvmrc"))) {
versionFile = ".nvmrc";
version = await promisesFs.readFile(
path.join(Configuration.rootPath, ".nvmrc"),
"utf-8",
);
} else if (await isValidFile(path.join(Configuration.rootPath, "package.json"))) {
const packageFileContent = await promisesFs.readFile(
path.join(Configuration.rootPath, "package.json"),
"utf-8",
);
const packageJson = JSON.parse(packageFileContent)
if (packageJson.engines && packageJson.engines.node) {
version = packageJson.engines.node.replace(/[^0-9.]/g, '');
}
}
if (!versionFile) {
if (!version) {
return true;
}
const version = await promisesFs.readFile(
path.join(Configuration.rootPath, versionFile),
"utf-8",
);
return compareVersions.compare(version, this.version, ">=");
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/node-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ describe("matchSync", () => {
expect(nodeVersion.matchSync()).toBe(false);
});
});

describe("engines node in package.json", () => {
beforeEach(() => {
mock({
"package.json": JSON.stringify({ engines: { node: "^10.14.0" } }),
});
});

test("compares versions", () => {
const nodeVersion = new NodeVersion("10.0.0");
expect(nodeVersion.matchSync()).toBe(true);
});

test("compares versions", () => {
const nodeVersion = new NodeVersion("12.0.0");
expect(nodeVersion.matchSync()).toBe(false);
});
});
});

describe("match", () => {
Expand Down Expand Up @@ -99,4 +117,26 @@ describe("match", () => {
expect(await nodeVersion.match()).toBe(false);
});
});

describe("engines node in packages.json", () => {
beforeEach(() => {
mock({
"package.json": JSON.stringify({ engines: { node: "^10.14.0" } }),
});
});

afterEach(() => {
mock.restore();
});

test("matches versions", async () => {
const nodeVersion = new NodeVersion("10.0.0");
expect(await nodeVersion.match()).toBe(true);
});

test("does not match versions", async () => {
const nodeVersion = new NodeVersion("12.0.0");
expect(await nodeVersion.match()).toBe(false);
});
});
});

0 comments on commit 8e95bc5

Please sign in to comment.