Skip to content

Commit

Permalink
fix: hidden --node-version flag to bypass version discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Molisani <[email protected]>
  • Loading branch information
molisani committed Oct 29, 2024
1 parent 1a762b0 commit f237e04
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/create-app/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2024 Bloomberg Finance L.P.
// Distributed under the terms of the Apache 2.0 license.
import { buildApplication, buildCommand } from "@stricli/core";
import { buildApplication, buildCommand, numberParser } from "@stricli/core";
import packageJson from "../package.json";

const command = buildCommand({
Expand Down Expand Up @@ -67,6 +67,13 @@ const command = buildCommand({
parse: String,
default: "",
},
nodeVersion: {
kind: "parsed",
brief: "Node.js major version to use for engines.node minimum and @types/node, bypasses version discovery logic",
parse: numberParser,
optional: true,
hidden: true,
},
},
aliases: {
n: "name",
Expand Down
8 changes: 7 additions & 1 deletion packages/create-app/src/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface CreateProjectFlags extends PackageJsonTemplateValues {
readonly template: "single" | "multi";
readonly autoComplete: boolean;
readonly command?: string;
readonly nodeVersion?: number;
}

export default async function (this: LocalContext, flags: CreateProjectFlags, directoryPath: string): Promise<void> {
Expand All @@ -137,7 +138,12 @@ export default async function (this: LocalContext, flags: CreateProjectFlags, di
const packageName = flags.name ?? path.basename(directoryPath);
const commandName = flags.command ?? packageName;

const nodeVersions = await calculateAcceptableNodeVersions(this.process);
let nodeVersions: NodeVersions;
if (flags.nodeVersion) {
nodeVersions = { engine: `>=${flags.nodeVersion}`, types: `${flags.nodeVersion}.x` };
} else {
nodeVersions = await calculateAcceptableNodeVersions(this.process);
}

let packageJson = buildPackageJson(
{
Expand Down
6 changes: 6 additions & 0 deletions packages/create-app/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export async function calculateAcceptableNodeVersions(process: NodeJS.Process):
process.stderr.write(
`No version of @types/node found with major ${majorVersion}, falling back to ${typesVersion}\n`,
);
process.stderr.write(
`Rerun this command with the hidden flag --node-version to manually specify the Node.js major version`,
);
}
}
}
Expand All @@ -49,6 +52,9 @@ export async function calculateAcceptableNodeVersions(process: NodeJS.Process):
process.stderr.write(
`Unable to determine version of @types/node for ${process.versions.node}, assuming ${typesVersion}\n`,
);
process.stderr.write(
`Rerun this command with the hidden flag --node-version to manually specify the Node.js major version`,
);
}

return {
Expand Down
42 changes: 42 additions & 0 deletions packages/create-app/tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,48 @@ describe("creates new application", () => {
});

describe("node version logic", () => {
it("version discovery skipped when --node-version is provided", async function () {
const stdout = new FakeWritableStream();
const stderr = new FakeWritableStream();
const cwd = sinon.stub().returns("/home");
const vol = Volume.fromJSON({});
const memfs = createFsFromVolume(vol);

const execFileSync = sandbox.stub(child_process, "execFileSync");
execFileSync.returns("REGISTRY");

const fetch = sandbox.stub(globalThis, "fetch");
fetch.resolves(new Response(JSON.stringify({})));

const context: DeepPartial<LocalContext> = {
process: {
stdout,
stderr,
cwd,
versions: {
node: `${futureLTSNodeMajorVersion}.0.0`,
},
},
fs: memfs as any,
path: nodePath,
};

await run(
app,
["node-version-test", "--node-version", `${futureLTSNodeMajorVersion + 1}`],
context as LocalContext,
);

const result = {
stdout: stdout.text,
stderr: stderr.text,
files: vol.toJSON(),
};
compareToBaseline(this, ApplicationTestResultFormat, result);
expect(execFileSync.callCount).to.equal(0, "execFileSync called unexpectedly");
expect(fetch.callCount).to.equal(0, "fetch called unexpectedly");
});

it("exact version exists for types", async function () {
const stdout = new FakeWritableStream();
const stderr = new FakeWritableStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[STDERR]
No version of @types/node found with major 30, falling back to 20.x

Rerun this command with the hidden flag --node-version to manually specify the Node.js major version
[FILES]
::::/home/node-version-test/.gitignore
# Logs
Expand Down
Loading

0 comments on commit f237e04

Please sign in to comment.