Files
Jeff FisherandGitHub 3c84063ae8 Migrate dev-tool from CommonJS to ESM with native TypeScript support (#37255)
### Packages impacted by this PR

`@azure/dev-tool`

### Issues associated with this PR

https://github.com/Azure/azure-sdk-for-js/issues/36819

### Describe the problem that is addressed by this PR

Replace tsx runtime with Node's native TypeScript execution. This
simplifies the bootstrap chain and removes the tsx dependency.

Key changes:
- Add "type": "module" to package.json and set engines >= 22.6.0
- Convert launch.js, dtx.js to TypeScript with strip-types shebangs
- Delete register.js/register.d.ts (tsx loader no longer needed)
- Update tsconfig: module/moduleResolution to NodeNext, add
allowImportingTsExtensions and noEmit
- Replace all __dirname usage with import.meta.dirname
- Replace dynamic require() in resolveProject.ts with createRequire()
- Add .ts extensions to all relative imports (~82 files)
- Add JSON import assertions (with { type: "json" })
- Upgrade chalk from v4 to v5 (ESM-only)
- Remove tsx from dependencies
2026-05-05 15:33:34 -05:00

114 lines
3.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { describe, it, assert, beforeAll } from "vitest";
import { parseOptions } from "../src/framework/parseOptions.ts";
import { makeCommandInfo, subCommand, leafCommand } from "../src/framework/command.ts";
import { silenceLogger } from "./util.ts";
const simpleCommandInfo = makeCommandInfo("simple", "a simple command", {
simpleArg: {
kind: "string",
description: "a simple argument",
allowMultiple: false,
default: "foo",
},
});
interface SimpleExpectedOptionsType {
simpleArg: string;
help?: boolean;
args?: string[];
}
describe("Command Framework", () => {
beforeAll(silenceLogger);
describe("subCommand", () => {
it("simple dispatcher", async () => {
const dispatcher = subCommand(
{ name: "test", description: "a sub-command dispatcher" },
{
sub: () =>
new Promise((resolve) => {
resolve({
commandInfo: { name: "sub", description: "a leaf command" },
default: leafCommand(
{ name: "sub", description: "a leaf command" },
() =>
new Promise((resolve) => {
resolve(true);
}),
),
});
}),
fail: () =>
new Promise((resolve) => {
resolve({
commandInfo: { name: "fail", description: "a command that fails" },
default: leafCommand(
{ name: "fail", description: "a command that fails" },
() =>
new Promise((resolve) => {
resolve(false);
}),
),
});
}),
},
);
assert.isTrue(await dispatcher("sub"));
assert.isFalse(await dispatcher("fail"));
});
});
describe("leafCommand", () => {
it("simple leaf command with argument", async () => {
const command = leafCommand(simpleCommandInfo, (opts: SimpleExpectedOptionsType) => {
if (opts.simpleArg === "yes") {
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
});
assert.isTrue(await command("--simpleArg", "yes"));
assert.isFalse(await command("--simpleArg", "no"));
});
});
describe("parseOptions", () => {
it("simple", () => {
const opts: SimpleExpectedOptionsType = parseOptions(
["--simpleArg", "test"],
simpleCommandInfo.options,
);
assert.equal(opts.simpleArg, "test");
});
it("nested", async () => {
const nestedOptions = {
name: "nested",
description: "",
options: { internal: { kind: "boolean", description: "" } },
} as const;
const command = subCommand(
{ name: "top-level", description: "" },
{
nested: () =>
Promise.resolve({
default: leafCommand(nestedOptions, (options) => {
assert.deepStrictEqual(options["--"], ["test1", "test2"]);
return Promise.resolve(true);
}),
commandInfo: nestedOptions,
}),
},
);
assert.isTrue(await command("nested", "--internal", "--", "test1", "test2"));
});
});
});