ember.nvp 1.8.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/.release-plan.json +15 -35
  2. package/CHANGELOG.md +33 -0
  3. package/README.md +73 -4
  4. package/package.json +2 -1
  5. package/packages/rolldown/package.json +6 -2
  6. package/packages/rolldown/src/dts.test.ts +105 -1
  7. package/packages/rolldown/src/isolated-declarations.ts +67 -61
  8. package/packages/rolldown/src/transform.test.ts +212 -15
  9. package/packages/rolldown/src/transform.ts +106 -26
  10. package/src/cli/args.js +170 -48
  11. package/src/cli/generator.js +2 -1
  12. package/src/cli/help.js +82 -0
  13. package/src/cli/index.js +15 -0
  14. package/src/cli/questions/layer-options.js +130 -0
  15. package/src/cli/questions/validate-option.js +106 -0
  16. package/src/layers/eslint-bundled/app.test.ts +182 -0
  17. package/src/layers/eslint-bundled/index.js +172 -0
  18. package/src/layers/expect-type/files/type-tests/index.test.ts +4 -0
  19. package/src/layers/expect-type/files/type-tests/tsconfig.json +8 -0
  20. package/src/layers/expect-type/index.js +114 -0
  21. package/src/layers/github-actions/app.test.ts +1 -1
  22. package/src/layers/typescript/app.test.ts +6 -0
  23. package/src/layers/typescript/index.js +48 -148
  24. package/src/layers/typescript/typescript-6.js +102 -0
  25. package/src/layers/typescript/typescript-7.js +177 -0
  26. package/src/utils/manifest.js +13 -0
  27. package/src/utils/npm.js +27 -0
  28. package/src/utils/project.js +23 -0
  29. package/src/utils/types.ts +52 -1
  30. package/src/utils/typescript.js +147 -0
  31. package/test/ember.nvp/base-library.test.ts +11 -0
  32. package/test/ember.nvp/cli.test.ts +142 -1
  33. package/test/ember.nvp/layer-expect-type.test.ts +113 -0
  34. package/test/ember.nvp/layer-options.test.ts +248 -0
  35. package/test/ember.nvp/layer-readme.test.ts +4 -2
  36. package/test/ember.nvp/layer-typescript.test.ts +302 -0
  37. package/test/ember.nvp/layers.test.ts +2 -2
  38. package/test/ember.nvp/permutations/shared.ts +6 -6
  39. package/test/ember.nvp/validate-option.test.ts +152 -0
  40. package/test/test-helpers/permutate.test.ts +2 -7
  41. package/test/test-helpers.ts +12 -2
  42. package/vitest.config.js +7 -1
  43. package/src/layers/eslint-bundled-ember/index.js +0 -91
  44. package/src/layers/eslint-bundled-nvp/app.test.ts +0 -91
  45. package/src/layers/eslint-bundled-nvp/index.js +0 -91
  46. /package/src/layers/{eslint-bundled-ember/files → eslint-bundled/files/ember}/eslint.config.js +0 -0
  47. /package/src/layers/{eslint-bundled-nvp/files → eslint-bundled/files/nvp}/eslint.config.js +0 -0
package/src/utils/npm.js CHANGED
@@ -35,6 +35,7 @@ export async function getLatest(deps) {
35
35
  }
36
36
 
37
37
  let version;
38
+ let alias = parseAlias(range);
38
39
 
39
40
  /**
40
41
  * HACK FOR CI.
@@ -54,6 +55,8 @@ export async function getLatest(deps) {
54
55
  if (needsLocalLink && LOCAL_PACKAGES[dep]) {
55
56
  version =
56
57
  "link:" + resolve(join(import.meta.dirname, "../../packages", LOCAL_PACKAGES[dep]));
58
+ } else if (alias) {
59
+ version = `npm:${alias.name}@${await latestVersion(alias.name, { version: alias.range })}`;
57
60
  } else {
58
61
  if (range == "workspace:*") {
59
62
  range = "latest";
@@ -71,6 +74,30 @@ export async function getLatest(deps) {
71
74
  return Object.fromEntries(results);
72
75
  }
73
76
 
77
+ /**
78
+ * An npm alias installs a package under another name:
79
+ *
80
+ * "@typescript/native": "npm:typescript@^7.1.0-0"
81
+ *
82
+ * The range belongs to the aliased package.
83
+ *
84
+ * @param {string} range
85
+ * @returns {{ name: string; range: string } | undefined}
86
+ */
87
+ function parseAlias(range) {
88
+ if (!range.startsWith("npm:")) return;
89
+
90
+ let spec = range.slice("npm:".length);
91
+ // The last `@` starts the range. A scope's `@` is at index 0.
92
+ let at = spec.lastIndexOf("@");
93
+
94
+ if (at <= 0) {
95
+ return { name: spec, range: "latest" };
96
+ }
97
+
98
+ return { name: spec.slice(0, at), range: spec.slice(at + 1) };
99
+ }
100
+
74
101
  async function needsWorkspace() {
75
102
  if (process.env.GITHUB_REPOSITORY === "NullVoxPopuli/ember.nvp") return true;
76
103
 
@@ -80,6 +80,29 @@ export class Project {
80
80
  return this.#desires;
81
81
  }
82
82
 
83
+ /**
84
+ * Get resolved options for a specific layer, merging defaults with user-supplied options.
85
+ *
86
+ * @param {string} layerName
87
+ * @returns {Record<string, any>}
88
+ */
89
+ getLayerOptions(layerName) {
90
+ let layer = this.desires.layers?.find((l) => l.name === layerName);
91
+ /** @type {Record<string, any>} */
92
+ let defaults = {};
93
+
94
+ if (layer?.options) {
95
+ for (const [key, schema] of Object.entries(layer.options)) {
96
+ if (schema.default !== undefined) {
97
+ defaults[key] = schema.default;
98
+ }
99
+ }
100
+ }
101
+
102
+ let userOptions = this.desires.options?.[layerName] ?? {};
103
+ return { ...defaults, ...userOptions };
104
+ }
105
+
83
106
  /**
84
107
  * @param {string} name
85
108
  * @returns {Promise<boolean>}
@@ -1,13 +1,61 @@
1
+ import type { Validate } from "@clack/core";
1
2
  import { Project } from "./project.js";
2
3
  export type PackageManager = "pnpm" | "npm";
3
4
  export type ProjectType = "app" | "library" | "extension" | "custom-element";
4
5
 
6
+ export type LayerOptionType = "text" | "number" | "select" | "confirm" | "multiselect";
7
+
8
+ export interface LayerSelectChoice {
9
+ /**
10
+ * A string, because the same value is also typed as a CLI flag
11
+ */
12
+ value: string;
13
+ label: string;
14
+ hint?: string;
15
+ }
16
+
17
+ export interface LayerOptionSchema {
18
+ type: LayerOptionType;
19
+ /**
20
+ * The question to ask, also shown in `--help`
21
+ */
22
+ prompt: string;
23
+ default?: any;
24
+ /**
25
+ * The choices for "select" and "multiselect"
26
+ */
27
+ options?: LayerSelectChoice[];
28
+ /**
29
+ * The same as clack's `validate`:
30
+ * a function that returns a message (or an Error) to reject the value,
31
+ * or a Standard Schema.
32
+ *
33
+ * Receives the value after conversion to the option's type.
34
+ */
35
+ validate?: Validate<any>;
36
+ /**
37
+ * Reads the value that an existing project uses now.
38
+ *
39
+ * When updating a project, the prompt starts at this value instead of `default`,
40
+ * so that pressing enter keeps the project as it is.
41
+ */
42
+ detect?: (project: Project) => unknown;
43
+ }
44
+
45
+ export interface LayerOptionsSchema {
46
+ [optionKey: string]: LayerOptionSchema;
47
+ }
48
+
5
49
  export interface Layer {
6
50
  /**
7
51
  * The text to show during selection
8
52
  */
9
53
  label: string;
10
54
  hint?: string;
55
+ /**
56
+ * Optional schema of configurable options for this layer
57
+ */
58
+ options?: LayerOptionsSchema;
11
59
  /**
12
60
  * Whether the layer is pre-selected in the CLI.
13
61
  * The user can still deselect it.
@@ -24,8 +72,10 @@ export interface Layer {
24
72
  *
25
73
  * run _may_ be invoked multiple times,
26
74
  * so it's important to not require interaction here
75
+ *
76
+ * `options` are this layer's options, with defaults filled in.
27
77
  */
28
- run: (project: Project) => Promise<void>;
78
+ run: (project: Project, options: Record<string, any>) => Promise<void>;
29
79
  isSetup: <Explain extends boolean = false>(
30
80
  project: Project,
31
81
  explain?: Explain,
@@ -55,4 +105,5 @@ export interface Answers {
55
105
  name: string;
56
106
  layers: DiscoveredLayer[];
57
107
  packageManager: PackageManager;
108
+ options?: Record<string, Record<string, any>>;
58
109
  }
@@ -0,0 +1,147 @@
1
+ import { packageJson } from "ember-apply";
2
+ import { join } from "node:path";
3
+ import { cp } from "node:fs/promises";
4
+ import { existsSync } from "node:fs";
5
+ import { hasConfiguredTSBabel, prependPlugin } from "#utils/babel.js";
6
+ import { getLatest } from "#utils/npm.js";
7
+
8
+ const bases = join(import.meta.dirname, "../bases");
9
+ const appBase = join(bases, "minimal-app/files");
10
+ const extensionBase = join(bases, "minimal-extension/files");
11
+ const libraryBase = join(bases, "minimal-library/files");
12
+ const customElementBase = join(bases, "minimal-custom-element/files");
13
+
14
+ /**
15
+ * TypeScript 7 has no JavaScript API.
16
+ *
17
+ * typescript-eslint imports `typescript`, so it keeps using TypeScript 6.
18
+ *
19
+ * `@typescript/typescript6` is TypeScript 6 with a `tsc6` bin,
20
+ * so `tsc` stays TypeScript 7.
21
+ */
22
+ export const TYPESCRIPT_6 = "npm:@typescript/typescript6@^6.0.2";
23
+
24
+ /**
25
+ * Content mappers (for .gts and .gjs) need 7.1 or newer.
26
+ */
27
+ export const TYPESCRIPT_7 = "npm:typescript@^7.1.0-0";
28
+
29
+ /**
30
+ * @param {Record<string, any>} manifest
31
+ * @returns {boolean}
32
+ */
33
+ export function hasTypeScript6(manifest) {
34
+ let spec = manifest.devDependencies?.typescript;
35
+
36
+ return typeof spec === "string" && spec.startsWith("npm:@typescript/typescript6@");
37
+ }
38
+
39
+ /**
40
+ * @param {import('#utils/project.js').Project} project
41
+ * @returns {boolean}
42
+ */
43
+ export function wantsTypeScript7(project) {
44
+ return project.wantsLayer("typescript") && project.getLayerOptions("typescript").version === "7";
45
+ }
46
+
47
+ /**
48
+ * Reads the manifest instead of asking the typescript layer.
49
+ * That layer's isSetup asks about eslint, and the eslint layers ask about TypeScript 7.
50
+ *
51
+ * @param {import('#utils/project.js').Project} project
52
+ * @param {Record<string, any>} manifest
53
+ * @returns {boolean}
54
+ */
55
+ export function usesTypeScript7(project, manifest) {
56
+ return wantsTypeScript7(project) || Boolean(manifest.devDependencies?.["@typescript/native"]);
57
+ }
58
+
59
+ /**
60
+ * @param {import('#utils/project.js').Project} project
61
+ * @param {Record<string, any>} manifest
62
+ * @returns {boolean}
63
+ */
64
+ export function needsTypeScript6(project, manifest) {
65
+ return project.wantsLayer("eslint") || Boolean(manifest.devDependencies?.eslint);
66
+ }
67
+
68
+ /**
69
+ * In a TypeScript 7 project:
70
+ * - keeps `@typescript/typescript6`
71
+ * - adds it when eslint needs TypeScript 6
72
+ * - removes any other `typescript`, because it would be a second `tsc`
73
+ *
74
+ * @param {import('#utils/project.js').Project} project
75
+ */
76
+ export async function syncTypeScript6(project) {
77
+ let manifest = await packageJson.read(project.directory);
78
+
79
+ if (!usesTypeScript7(project, manifest)) return;
80
+ if (hasTypeScript6(manifest)) return;
81
+
82
+ if (needsTypeScript6(project, manifest)) {
83
+ await packageJson.addDevDependencies(
84
+ await getLatest({ typescript: TYPESCRIPT_6 }),
85
+ project.directory,
86
+ );
87
+ return;
88
+ }
89
+
90
+ await packageJson.removeDevDependencies(["typescript"], project.directory);
91
+ }
92
+
93
+ /**
94
+ * Copies the base's tsconfig, unless the project already has one.
95
+ *
96
+ * @param {import('#utils/project.js').Project} project
97
+ */
98
+ export async function addTSConfig(project) {
99
+ if (existsSync(project.path("tsconfig.json"))) {
100
+ return;
101
+ }
102
+
103
+ if (project.type === "app") {
104
+ await cp(join(appBase, "tsconfig.json"), project.path("tsconfig.json"));
105
+ return;
106
+ }
107
+
108
+ if (project.type === "extension") {
109
+ await cp(join(extensionBase, "tsconfig.json"), project.path("tsconfig.json"));
110
+ return;
111
+ }
112
+
113
+ if (project.type === "library") {
114
+ await cp(join(libraryBase, "tsconfig.json"), project.path("tsconfig.json"));
115
+ return;
116
+ }
117
+
118
+ if (project.type === "custom-element") {
119
+ await cp(join(customElementBase, "tsconfig.json"), project.path("tsconfig.json"));
120
+ }
121
+ }
122
+
123
+ /**
124
+ * @param {import('#utils/project.js').Project} project
125
+ */
126
+ export async function updateBabelConfig(project) {
127
+ if (!project.hasFile("babel.config.js")) {
128
+ // Nothing to patch (libraries): ember() strips types.
129
+ return;
130
+ }
131
+
132
+ if (await hasConfiguredTSBabel(project)) {
133
+ return;
134
+ }
135
+
136
+ await prependPlugin(
137
+ project,
138
+ `[
139
+ "@babel/plugin-transform-typescript",
140
+ {
141
+ allExtensions: true,
142
+ onlyRemoveTypeImports: true,
143
+ allowDeclareFields: true,
144
+ },
145
+ ]`,
146
+ );
147
+ }
@@ -204,6 +204,17 @@ describe("base: minimal-library", () => {
204
204
  //# sourceMappingURL=index.d.ts.map"
205
205
  `);
206
206
 
207
+ // A consumer's "go to definition" opens these, from the published src/
208
+ let declarationMap = JSON.parse((await project.read("dist/index.d.ts.map"))!);
209
+
210
+ expect(declarationMap.sources).toMatchInlineSnapshot(`
211
+ [
212
+ "../src/components/badge.gts",
213
+ "../src/components/greeting.gts",
214
+ "../src/utils/math.ts",
215
+ ]
216
+ `);
217
+
207
218
  expect(await listFiles(join(project.directory, "dist"))).toMatchInlineSnapshot(`
208
219
  [
209
220
  "index.d.ts",
@@ -1,5 +1,10 @@
1
1
  import { test, expect as hardExpect } from "vitest";
2
- import { cli } from "#test-helpers";
2
+ import { cli, mktemp } from "#test-helpers";
3
+ import { packageJson } from "ember-apply";
4
+ import { existsSync } from "node:fs";
5
+ import { rm } from "node:fs/promises";
6
+ import { join } from "node:path";
7
+ import { stripVTControlCharacters } from "node:util";
3
8
 
4
9
  const expect = hardExpect.soft;
5
10
 
@@ -10,3 +15,139 @@ test.skip("cli works", async () => {
10
15
 
11
16
  expect(execaPromise.exitCode).toBe(0);
12
17
  });
18
+
19
+ const BASIC_HELP_OUTPUT = `
20
+ " ember.nvp
21
+
22
+ Usage: npx ember.nvp [options]
23
+
24
+ Core Options:
25
+ -h, --help <boolean>
26
+ Show CLI help and option details
27
+
28
+ --name <string>
29
+ Name of the project
30
+
31
+ --path <string>
32
+ Target directory path for the project
33
+
34
+ --type <string>
35
+ Type of project to generate [choices: "app", "addon", "library", "extension", "custom-element"]
36
+
37
+ --confirm <string>
38
+ Bypass target confirmation step [choices: "yes", "no"]
39
+
40
+ --layers <string>
41
+ Layers to apply to the project (repeat for multiple layers)
42
+
43
+ --packageManager <string>
44
+ Package manager to configure for the project [choices: "npm", "pnpm"]
45
+
46
+ --replaceOrUpdate <string>
47
+ Strategy to use if target directory exists [choices: "replace", "update"]
48
+
49
+ --write <string>
50
+ Confirm writing changes to disk [choices: "yes", "no"]
51
+
52
+ Layer Options:
53
+ --eslint-bundled.preset <string>
54
+ Which ESLint config? [choices: "ember", "nvp"] [default: "ember"]
55
+
56
+ --typescript.version <string>
57
+ Which TypeScript version? [choices: "7", "6"] [default: "7"]
58
+ "
59
+ `;
60
+
61
+ test("cli --help outputs usage and core options", async () => {
62
+ let { execaPromise } = cli(["--help"]);
63
+
64
+ const res = await execaPromise;
65
+
66
+ expect(res.exitCode).toBe(0);
67
+ const outStr = stripVTControlCharacters(res.stdout);
68
+ expect(outStr).toMatchInlineSnapshot(BASIC_HELP_OUTPUT);
69
+ });
70
+
71
+ test("cli -h alias works", async () => {
72
+ let { execaPromise } = cli(["-h"]);
73
+
74
+ const res = await execaPromise;
75
+
76
+ expect(res.exitCode).toBe(0);
77
+ const outStr = stripVTControlCharacters(res.stdout);
78
+ expect(outStr).toMatchInlineSnapshot(BASIC_HELP_OUTPUT);
79
+ });
80
+
81
+ test("cli prints a message, not a stack trace, for an unknown flag", async () => {
82
+ let { execaPromise } = cli(["--bogus"]);
83
+
84
+ const res = await execaPromise.catch((error) => error);
85
+
86
+ expect(res.exitCode).toBe(1);
87
+ const outStr = stripVTControlCharacters(`${res.stdout}\n${res.stderr}`);
88
+ expect(outStr).toContain("Unknown option '--bogus'");
89
+ expect(outStr).not.toContain("TypeError");
90
+ });
91
+
92
+ /**
93
+ * Every question has a flag, so nothing prompts.
94
+ * The path does not exist yet, so there is no replace-or-update question either.
95
+ */
96
+ async function eslintProjectArgs(preset: string) {
97
+ let parent = await mktemp("cli-layer-options");
98
+ let path = join(parent, "my-app");
99
+
100
+ return {
101
+ parent,
102
+ path,
103
+ args: [
104
+ "--name",
105
+ "my-app",
106
+ "--path",
107
+ path,
108
+ "--type",
109
+ "app",
110
+ "--layers",
111
+ "eslint-bundled",
112
+ "--packageManager",
113
+ "pnpm",
114
+ "--confirm",
115
+ "yes",
116
+ "--eslint-bundled.preset",
117
+ preset,
118
+ ],
119
+ };
120
+ }
121
+
122
+ test("cli applies a layer option flag", async () => {
123
+ let { parent, path, args } = await eslintProjectArgs("nvp");
124
+
125
+ try {
126
+ let res = await cli(args).execaPromise;
127
+
128
+ expect(res.exitCode).toBe(0);
129
+
130
+ let manifest = await packageJson.read(path);
131
+
132
+ expect(manifest.devDependencies).toHaveProperty("@nullvoxpopuli/eslint-configs");
133
+ expect(manifest.devDependencies).not.toHaveProperty("ember-eslint");
134
+ } finally {
135
+ await rm(parent, { recursive: true, force: true });
136
+ }
137
+ });
138
+
139
+ test("cli exits on an invalid layer option flag, before writing anything", async () => {
140
+ let { parent, path, args } = await eslintProjectArgs("bogus");
141
+
142
+ try {
143
+ const res = await cli(args).execaPromise.catch((error) => error);
144
+
145
+ expect(res.exitCode).toBe(1);
146
+ expect(stripVTControlCharacters(res.stdout)).toContain(
147
+ "Invalid CLI argument '--eslint-bundled.preset': Invalid option 'bogus'. Must be one of: ember, nvp",
148
+ );
149
+ expect(existsSync(path)).toBe(false);
150
+ } finally {
151
+ await rm(parent, { recursive: true, force: true });
152
+ }
153
+ });
@@ -0,0 +1,113 @@
1
+ import { describe, it, beforeAll, afterAll, expect } from "vitest";
2
+ import { generate, expectIsSetup, layers } from "#test-helpers";
3
+ import { writeLibrarySource } from "./library-src-fixtures.ts";
4
+ import { execa } from "execa";
5
+ import { existsSync } from "node:fs";
6
+ import { rm, writeFile } from "node:fs/promises";
7
+ import { join } from "node:path";
8
+
9
+ import type { Project } from "ember.nvp";
10
+
11
+ const expectType = layers.find((layer) => layer.name === "expect-type")!;
12
+
13
+ async function typecheck(project: Project) {
14
+ return execa("pnpm lint:type-tests", {
15
+ cwd: project.directory,
16
+ shell: true,
17
+ all: true,
18
+ reject: false,
19
+ });
20
+ }
21
+
22
+ describe("layer: expect-type", () => {
23
+ const dirs: string[] = [];
24
+
25
+ afterAll(async () => {
26
+ if (process.env.CI) return;
27
+
28
+ for (const dir of dirs) {
29
+ await rm(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
30
+ }
31
+ });
32
+
33
+ describe("TypeScript library", () => {
34
+ let project: Project;
35
+
36
+ beforeAll(async () => {
37
+ project = await generate({
38
+ type: "library",
39
+ name: "my-lib",
40
+ layers: ["typescript", "expect-type"],
41
+ });
42
+ dirs.push(project.directory);
43
+
44
+ await writeLibrarySource(project, "typescript");
45
+
46
+ let install = await execa("pnpm install", { cwd: project.directory, shell: true });
47
+ expect(install.exitCode).toBe(0);
48
+ });
49
+
50
+ it("is setup", async () => {
51
+ await expectIsSetup(project, expectType);
52
+ });
53
+
54
+ it("the generated type tests pass", async () => {
55
+ let result = await typecheck(project);
56
+
57
+ expect(result.exitCode, result.all).toBe(0);
58
+ });
59
+
60
+ it("checks the library's real exports", async () => {
61
+ await writeFile(
62
+ join(project.directory, "type-tests/math.test.ts"),
63
+ `import { expectTypeOf } from "expect-type";
64
+ import { add } from "../src/index.ts";
65
+
66
+ expectTypeOf(add).parameters.toEqualTypeOf<[number, number]>();
67
+ expectTypeOf(add).returns.toEqualTypeOf<number>();
68
+ `,
69
+ );
70
+
71
+ let result = await typecheck(project);
72
+
73
+ expect(result.exitCode, result.all).toBe(0);
74
+ });
75
+
76
+ it("fails on a wrong type assertion", async () => {
77
+ await writeFile(
78
+ join(project.directory, "type-tests/wrong.test.ts"),
79
+ `import { expectTypeOf } from "expect-type";
80
+ import { add } from "../src/index.ts";
81
+
82
+ expectTypeOf(add).returns.toEqualTypeOf<string>();
83
+ `,
84
+ );
85
+
86
+ let result = await typecheck(project);
87
+
88
+ expect(result.exitCode).not.toBe(0);
89
+ expect(result.all).toContain("wrong.test.ts");
90
+ });
91
+
92
+ it("keeps type tests out of the build", async () => {
93
+ await rm(join(project.directory, "type-tests/wrong.test.ts"));
94
+
95
+ let build = await execa("pnpm build", { cwd: project.directory, shell: true });
96
+ expect(build.exitCode).toBe(0);
97
+
98
+ expect(existsSync(join(project.directory, "dist/type-tests"))).toBe(false);
99
+ });
100
+ });
101
+
102
+ describe("JavaScript library", () => {
103
+ it("adds nothing (no declarations to test)", async () => {
104
+ let project = await generate({ type: "library", name: "my-lib", layers: ["expect-type"] });
105
+ dirs.push(project.directory);
106
+
107
+ await expectIsSetup(project, expectType);
108
+
109
+ expect(existsSync(join(project.directory, "type-tests"))).toBe(false);
110
+ expect(await project.read("package.json")).not.toContain("expect-type");
111
+ });
112
+ });
113
+ });