@canonical/summon-core 0.20.0 → 0.21.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.
- package/package.json +15 -8
- package/src/discovery/discoverGeneratorTree.ts +1 -1
- package/src/discovery/generatorCache.ts +1 -1
- package/src/index.ts +13 -14
- package/src/stamp/applyStamp.test.ts +199 -0
- package/src/stamp/applyStamp.ts +90 -0
- package/src/stamp/index.ts +3 -0
- package/src/stamp/runTaskWithStamp.ts +31 -0
- package/src/stamp/types.ts +7 -0
- package/src/template/TemplatingEngine.ts +17 -0
- package/src/template/ejsEngine.ts +21 -0
- package/src/template/generateStamp.ts +27 -0
- package/src/template/generatorComment.test.ts +81 -0
- package/src/template/generatorComment.ts +32 -0
- package/src/template/getCommentStyle.ts +59 -0
- package/src/template/index.ts +13 -0
- package/src/template/prependStamp.ts +16 -0
- package/src/template/renderFile.ts +13 -0
- package/src/template/renderString.test.ts +107 -0
- package/src/template/renderString.ts +13 -0
- package/src/template/renderStringAsync.ts +13 -0
- package/src/template/tasks/index.ts +3 -0
- package/src/template/tasks/template.test.ts +66 -0
- package/src/template/tasks/template.ts +22 -0
- package/src/template/tasks/templateDir.ts +60 -0
- package/src/template/tasks/types.ts +29 -0
- package/src/template/templateHelpers.test.ts +227 -0
- package/src/template/templateHelpers.ts +35 -0
- package/src/template/withHelpers.test.ts +41 -0
- package/src/template/withHelpers.ts +10 -0
- package/src/testing/integration/template-rendering.test.ts +69 -0
- package/src/types/AnyGenerator.ts +9 -0
- package/src/types/ForbidReserved.ts +23 -0
- package/src/types/GeneratorDefinition.ts +19 -0
- package/src/types/GeneratorMeta.ts +29 -0
- package/src/types/PromptDefinition.ts +33 -0
- package/src/types/ReservedOption.ts +23 -0
- package/src/types/StampConfig.ts +6 -0
- package/src/types/index.ts +7 -0
- package/src/cli-types.ts +0 -59
- package/src/stamp.ts +0 -165
- package/src/template.test.ts +0 -666
- package/src/template.ts +0 -543
- package/src/types.ts +0 -97
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common template helpers that can be passed to templates.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
capitalize,
|
|
7
|
+
indent,
|
|
8
|
+
join,
|
|
9
|
+
toCamelCase,
|
|
10
|
+
toConstantCase,
|
|
11
|
+
toKebabCase,
|
|
12
|
+
toPascalCase,
|
|
13
|
+
toSnakeCase,
|
|
14
|
+
} from "@canonical/utils";
|
|
15
|
+
|
|
16
|
+
const templateHelpers = {
|
|
17
|
+
camelCase: toCamelCase,
|
|
18
|
+
pascalCase: toPascalCase,
|
|
19
|
+
kebabCase: toKebabCase,
|
|
20
|
+
snakeCase: toSnakeCase,
|
|
21
|
+
constantCase: toConstantCase,
|
|
22
|
+
capitalize,
|
|
23
|
+
indent,
|
|
24
|
+
join,
|
|
25
|
+
|
|
26
|
+
isoDate: (): string => new Date().toISOString(),
|
|
27
|
+
|
|
28
|
+
year: (): number => new Date().getFullYear(),
|
|
29
|
+
|
|
30
|
+
pluralize: (word: string, count: number): string => {
|
|
31
|
+
return count === 1 ? word : `${word}s`;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default templateHelpers;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import renderString from "./renderString.js";
|
|
3
|
+
import templateHelpers from "./templateHelpers.js";
|
|
4
|
+
import withHelpers from "./withHelpers.js";
|
|
5
|
+
|
|
6
|
+
describe("withHelpers", () => {
|
|
7
|
+
it("includes all template helpers", () => {
|
|
8
|
+
const vars = withHelpers({ name: "test" });
|
|
9
|
+
|
|
10
|
+
expect(vars.camelCase).toBe(templateHelpers.camelCase);
|
|
11
|
+
expect(vars.pascalCase).toBe(templateHelpers.pascalCase);
|
|
12
|
+
expect(vars.kebabCase).toBe(templateHelpers.kebabCase);
|
|
13
|
+
expect(vars.snakeCase).toBe(templateHelpers.snakeCase);
|
|
14
|
+
expect(vars.constantCase).toBe(templateHelpers.constantCase);
|
|
15
|
+
expect(vars.capitalize).toBe(templateHelpers.capitalize);
|
|
16
|
+
expect(vars.indent).toBe(templateHelpers.indent);
|
|
17
|
+
expect(vars.join).toBe(templateHelpers.join);
|
|
18
|
+
expect(vars.pluralize).toBe(templateHelpers.pluralize);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("preserves user variables", () => {
|
|
22
|
+
const vars = withHelpers({ name: "MyComponent", version: "1.0.0" });
|
|
23
|
+
|
|
24
|
+
expect(vars.name).toBe("MyComponent");
|
|
25
|
+
expect(vars.version).toBe("1.0.0");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("user variables override helpers", () => {
|
|
29
|
+
const customCapitalize = (s: string) => `CUSTOM: ${s}`;
|
|
30
|
+
const vars = withHelpers({ capitalize: customCapitalize });
|
|
31
|
+
|
|
32
|
+
expect(vars.capitalize).toBe(customCapitalize);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("works with renderString", () => {
|
|
36
|
+
const vars = withHelpers({ name: "my-component" });
|
|
37
|
+
const result = renderString("<%= pascalCase(name) %>", vars);
|
|
38
|
+
|
|
39
|
+
expect(result).toBe("MyComponent");
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import templateHelpers from "./templateHelpers.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create a vars object with helpers included.
|
|
5
|
+
*/
|
|
6
|
+
export default function withHelpers(
|
|
7
|
+
vars: Record<string, unknown>,
|
|
8
|
+
): Record<string, unknown> {
|
|
9
|
+
return { ...templateHelpers, ...vars };
|
|
10
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import renderString from "../../template/renderString.js";
|
|
3
|
+
import withHelpers from "../../template/withHelpers.js";
|
|
4
|
+
|
|
5
|
+
describe("template rendering integration", () => {
|
|
6
|
+
it("can use helpers in templates", () => {
|
|
7
|
+
const vars = withHelpers({
|
|
8
|
+
componentName: "my-button",
|
|
9
|
+
properties: ["label", "onClick", "disabled"],
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const tpl = `
|
|
13
|
+
export const <%= pascalCase(componentName) %> = () => {
|
|
14
|
+
// Props: <%= join(properties) %>
|
|
15
|
+
};
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
const result = renderString(tpl, vars);
|
|
19
|
+
|
|
20
|
+
expect(result).toContain("export const MyButton = () => {");
|
|
21
|
+
expect(result).toContain("// Props: label, onClick, disabled");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("can generate TypeScript interfaces", () => {
|
|
25
|
+
const vars = withHelpers({
|
|
26
|
+
interfaceName: "button-props",
|
|
27
|
+
properties: [
|
|
28
|
+
{ name: "label", type: "string" },
|
|
29
|
+
{ name: "onClick", type: "() => void" },
|
|
30
|
+
{ name: "disabled", type: "boolean" },
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const tpl = `
|
|
35
|
+
interface <%= pascalCase(interfaceName) %> {
|
|
36
|
+
<% properties.forEach(prop => { %>
|
|
37
|
+
<%= prop.name %>: <%- prop.type %>;
|
|
38
|
+
<% }) %>
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
const result = renderString(tpl, vars);
|
|
43
|
+
|
|
44
|
+
expect(result).toContain("interface ButtonProps {");
|
|
45
|
+
expect(result).toContain("label: string;");
|
|
46
|
+
expect(result).toContain("onClick: () => void;");
|
|
47
|
+
expect(result).toContain("disabled: boolean;");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("can generate import statements", () => {
|
|
51
|
+
const vars = withHelpers({
|
|
52
|
+
imports: [
|
|
53
|
+
{ from: "react", names: ["useState", "useEffect"] },
|
|
54
|
+
{ from: "./types", names: ["Props"] },
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const tpl = `
|
|
59
|
+
<% imports.forEach(imp => { %>
|
|
60
|
+
import { <%= join(imp.names) %> } from '<%= imp.from %>';
|
|
61
|
+
<% }) %>
|
|
62
|
+
`;
|
|
63
|
+
|
|
64
|
+
const result = renderString(tpl, vars);
|
|
65
|
+
|
|
66
|
+
expect(result).toContain("import { useState, useEffect } from 'react';");
|
|
67
|
+
expect(result).toContain("import { Props } from './types';");
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type GeneratorDefinition from "./GeneratorDefinition.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A generator definition without type parameters, used in barrels/collections.
|
|
5
|
+
*/
|
|
6
|
+
// biome-ignore lint/suspicious/noExplicitAny: Required for contravariant generator collections
|
|
7
|
+
type AnyGenerator = GeneratorDefinition<any>;
|
|
8
|
+
|
|
9
|
+
export default AnyGenerator;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type ReservedOption from "./ReservedOption.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type helper that produces a compile-time error if T contains any reserved option names.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* interface BadAnswers {
|
|
9
|
+
* name: string;
|
|
10
|
+
* help: string; // Reserved!
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* // This will error: Type 'string' is not assignable to type '["Error: 'help' is a reserved option name"]'
|
|
14
|
+
* const generator: GeneratorDefinition<ForbidReserved<BadAnswers>> = { ... };
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
type ForbidReserved<T> = {
|
|
18
|
+
[K in keyof T]: K extends ReservedOption
|
|
19
|
+
? [`Error: '${K & string}' is a reserved option name`]
|
|
20
|
+
: T[K];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default ForbidReserved;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Task } from "@canonical/task";
|
|
2
|
+
import type GeneratorMeta from "./GeneratorMeta.js";
|
|
3
|
+
import type PromptDefinition from "./PromptDefinition.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The complete definition of a generator.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam TAnswers - Type of the answers object passed to generate
|
|
9
|
+
*/
|
|
10
|
+
export default interface GeneratorDefinition<
|
|
11
|
+
TAnswers = Record<string, unknown>,
|
|
12
|
+
> {
|
|
13
|
+
/** Generator metadata for CLI display */
|
|
14
|
+
meta: GeneratorMeta;
|
|
15
|
+
/** Prompts to collect answers from user */
|
|
16
|
+
prompts: PromptDefinition[];
|
|
17
|
+
/** Pure function that returns a Task describing the generation */
|
|
18
|
+
generate: (answers: TAnswers) => Task<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata for a generator, displayed in CLI help and discovery.
|
|
3
|
+
*/
|
|
4
|
+
export default interface GeneratorMeta {
|
|
5
|
+
/** Generator name, used in CLI path (e.g., "component/react") */
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* Name used in generated file stamp comments.
|
|
9
|
+
* Use this when the stamp should differ from the CLI command path
|
|
10
|
+
* (e.g., "@canonical/summon-component:react" vs "component/react").
|
|
11
|
+
*/
|
|
12
|
+
displayName: string;
|
|
13
|
+
/** One-line description shown in generator listings */
|
|
14
|
+
description: string;
|
|
15
|
+
/** Semantic version of the generator */
|
|
16
|
+
version: string;
|
|
17
|
+
/** Author name or email */
|
|
18
|
+
author?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Extended help text shown when calling `summon <topic>` (without subgenerator)
|
|
21
|
+
* and in --help. Use this for detailed explanation and examples.
|
|
22
|
+
* Supports markdown-like formatting.
|
|
23
|
+
*/
|
|
24
|
+
help?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Usage examples shown in help. Each example should show a common invocation.
|
|
27
|
+
*/
|
|
28
|
+
examples?: string[];
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Definition of a prompt in a generator.
|
|
3
|
+
*
|
|
4
|
+
* Each prompt becomes a CLI flag. The prompt name is converted to kebab-case
|
|
5
|
+
* for the flag (e.g., `componentPath` → `--component-path`).
|
|
6
|
+
*/
|
|
7
|
+
export default interface PromptDefinition {
|
|
8
|
+
/** Unique identifier, used as answer key and CLI flag name */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Question text displayed to the user */
|
|
11
|
+
message: string;
|
|
12
|
+
/** Type of input */
|
|
13
|
+
type: "text" | "confirm" | "select" | "multiselect";
|
|
14
|
+
/** Default value if user provides no input */
|
|
15
|
+
default?: unknown;
|
|
16
|
+
/** Choices for select/multiselect prompts */
|
|
17
|
+
choices?: Array<{ label: string; value: string }>;
|
|
18
|
+
/** Conditional function - prompt is skipped if this returns false */
|
|
19
|
+
when?: (answers: Record<string, unknown>) => boolean;
|
|
20
|
+
/** Validation function, returns true or error message */
|
|
21
|
+
validate?: (value: unknown) => boolean | string;
|
|
22
|
+
/**
|
|
23
|
+
* Group name for organizing options in --help output.
|
|
24
|
+
* Options without a group appear under "Options".
|
|
25
|
+
*/
|
|
26
|
+
group?: string;
|
|
27
|
+
/**
|
|
28
|
+
* If true, this prompt can be provided as a positional argument.
|
|
29
|
+
* Only one prompt per generator should be positional.
|
|
30
|
+
* Only text prompts can be positional.
|
|
31
|
+
*/
|
|
32
|
+
positional?: boolean;
|
|
33
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reserved option names for CLI global options.
|
|
3
|
+
* Generator prompts MUST NOT use these names.
|
|
4
|
+
*/
|
|
5
|
+
type ReservedOption =
|
|
6
|
+
| "help"
|
|
7
|
+
| "version"
|
|
8
|
+
| "dryRun"
|
|
9
|
+
| "dry-run"
|
|
10
|
+
| "yes"
|
|
11
|
+
| "output"
|
|
12
|
+
| "preview"
|
|
13
|
+
| "generators"
|
|
14
|
+
| "run"
|
|
15
|
+
| "init"
|
|
16
|
+
| "llm"
|
|
17
|
+
| "format"
|
|
18
|
+
| "showFiles"
|
|
19
|
+
| "show-files"
|
|
20
|
+
| "verbose"
|
|
21
|
+
| "generatedStamp";
|
|
22
|
+
|
|
23
|
+
export default ReservedOption;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { default as AnyGenerator } from "./AnyGenerator.js";
|
|
2
|
+
export type { default as ForbidReserved } from "./ForbidReserved.js";
|
|
3
|
+
export type { default as GeneratorDefinition } from "./GeneratorDefinition.js";
|
|
4
|
+
export type { default as GeneratorMeta } from "./GeneratorMeta.js";
|
|
5
|
+
export type { default as PromptDefinition } from "./PromptDefinition.js";
|
|
6
|
+
export type { default as ReservedOption } from "./ReservedOption.js";
|
|
7
|
+
export type { default as StampConfig } from "./StampConfig.js";
|
package/src/cli-types.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CLI Type Definitions
|
|
3
|
-
*
|
|
4
|
-
* Types for generator authors to validate their prompt names at compile-time.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Reserved option names for CLI global options.
|
|
9
|
-
* Generator prompts MUST NOT use these names.
|
|
10
|
-
*
|
|
11
|
-
* Use the `ForbidReserved` type to enforce at compile-time:
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* interface MyAnswers {
|
|
15
|
-
* name: string; // OK
|
|
16
|
-
* help: string; // Will cause type error
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* // This will produce a compile-time error if any key is reserved
|
|
20
|
-
* type ValidatedAnswers = ForbidReserved<MyAnswers>;
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export type ReservedOption =
|
|
24
|
-
| "help"
|
|
25
|
-
| "version"
|
|
26
|
-
| "dryRun"
|
|
27
|
-
| "dry-run"
|
|
28
|
-
| "yes"
|
|
29
|
-
| "output"
|
|
30
|
-
| "preview"
|
|
31
|
-
| "generators"
|
|
32
|
-
| "run"
|
|
33
|
-
| "init"
|
|
34
|
-
| "llm"
|
|
35
|
-
| "format"
|
|
36
|
-
| "showFiles"
|
|
37
|
-
| "show-files"
|
|
38
|
-
| "verbose"
|
|
39
|
-
| "generatedStamp";
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Type helper that produces a compile-time error if T contains any reserved option names.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```typescript
|
|
46
|
-
* interface BadAnswers {
|
|
47
|
-
* name: string;
|
|
48
|
-
* help: string; // Reserved!
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* // This will error: Type 'string' is not assignable to type '["Error: 'help' is a reserved option name"]'
|
|
52
|
-
* const generator: GeneratorDefinition<ForbidReserved<BadAnswers>> = { ... };
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export type ForbidReserved<T> = {
|
|
56
|
-
[K in keyof T]: K extends ReservedOption
|
|
57
|
-
? [`Error: '${K & string}' is a reserved option name`]
|
|
58
|
-
: T[K];
|
|
59
|
-
};
|
package/src/stamp.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stamp — Generated File Stamp Logic
|
|
3
|
-
*
|
|
4
|
-
* Stamps are a generation concern: a comment prepended to generated files
|
|
5
|
-
* identifying the generator that produced them. The base interpreter
|
|
6
|
-
* (@canonical/task) is domain-agnostic and does not know about stamps.
|
|
7
|
-
*
|
|
8
|
-
* Stamps are applied via composition: `runTaskWithStamp` wraps `runTask`
|
|
9
|
-
* from @canonical/task, intercepting WriteFile effects to prepend stamps.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import * as path from "node:path";
|
|
13
|
-
import {
|
|
14
|
-
type Effect,
|
|
15
|
-
type RunTaskOptions,
|
|
16
|
-
runTask,
|
|
17
|
-
type Task,
|
|
18
|
-
} from "@canonical/task";
|
|
19
|
-
|
|
20
|
-
// =============================================================================
|
|
21
|
-
// Stamp Config
|
|
22
|
-
// =============================================================================
|
|
23
|
-
|
|
24
|
-
export interface StampConfig {
|
|
25
|
-
/** Generator name (e.g., "@canonical/summon-package") */
|
|
26
|
-
generator: string;
|
|
27
|
-
/** Generator version (e.g., "0.1.0") */
|
|
28
|
-
version: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// =============================================================================
|
|
32
|
-
// Comment Styles
|
|
33
|
-
// =============================================================================
|
|
34
|
-
|
|
35
|
-
const COMMENT_STYLES: Record<
|
|
36
|
-
string,
|
|
37
|
-
{
|
|
38
|
-
single?: string;
|
|
39
|
-
blockStart?: string;
|
|
40
|
-
blockEnd?: string;
|
|
41
|
-
preferBlock?: boolean;
|
|
42
|
-
}
|
|
43
|
-
> = {
|
|
44
|
-
".ts": { single: "//" },
|
|
45
|
-
".tsx": { single: "//" },
|
|
46
|
-
".js": { single: "//" },
|
|
47
|
-
".jsx": { single: "//" },
|
|
48
|
-
".mjs": { single: "//" },
|
|
49
|
-
".cjs": { single: "//" },
|
|
50
|
-
".css": { blockStart: "/*", blockEnd: "*/", preferBlock: true },
|
|
51
|
-
".scss": { single: "//" },
|
|
52
|
-
".sass": { single: "//" },
|
|
53
|
-
".less": { single: "//" },
|
|
54
|
-
".html": { blockStart: "<!--", blockEnd: "-->" },
|
|
55
|
-
".htm": { blockStart: "<!--", blockEnd: "-->" },
|
|
56
|
-
".xml": { blockStart: "<!--", blockEnd: "-->" },
|
|
57
|
-
".svg": { blockStart: "<!--", blockEnd: "-->" },
|
|
58
|
-
".vue": { blockStart: "<!--", blockEnd: "-->" },
|
|
59
|
-
".svelte": { blockStart: "<!--", blockEnd: "-->" },
|
|
60
|
-
".yaml": { single: "#" },
|
|
61
|
-
".yml": { single: "#" },
|
|
62
|
-
".toml": { single: "#" },
|
|
63
|
-
".sh": { single: "#" },
|
|
64
|
-
".bash": { single: "#" },
|
|
65
|
-
".zsh": { single: "#" },
|
|
66
|
-
".py": { single: "#" },
|
|
67
|
-
".rb": { single: "#" },
|
|
68
|
-
".go": { single: "//" },
|
|
69
|
-
".rs": { single: "//" },
|
|
70
|
-
".java": { single: "//" },
|
|
71
|
-
".kt": { single: "//" },
|
|
72
|
-
".swift": { single: "//" },
|
|
73
|
-
".c": { single: "//" },
|
|
74
|
-
".cpp": { single: "//" },
|
|
75
|
-
".h": { single: "//" },
|
|
76
|
-
".php": { single: "//" },
|
|
77
|
-
".md": { blockStart: "<!--", blockEnd: "-->" },
|
|
78
|
-
".mdx": { blockStart: "{/*", blockEnd: "*/}" },
|
|
79
|
-
".sql": { single: "--" },
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// =============================================================================
|
|
83
|
-
// Stamp Application (Pure Function)
|
|
84
|
-
// =============================================================================
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Apply a stamp to file content based on file extension.
|
|
88
|
-
* Returns the original content if the file type doesn't support comments.
|
|
89
|
-
*/
|
|
90
|
-
export const applyStamp = (
|
|
91
|
-
filePath: string,
|
|
92
|
-
content: string,
|
|
93
|
-
stamp: StampConfig,
|
|
94
|
-
): string => {
|
|
95
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
96
|
-
const style = COMMENT_STYLES[ext];
|
|
97
|
-
|
|
98
|
-
if (!style) {
|
|
99
|
-
return content;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const stampText = `Generated by ${stamp.generator} v${stamp.version}`;
|
|
103
|
-
let stampLine: string;
|
|
104
|
-
|
|
105
|
-
if (style.preferBlock || (!style.single && style.blockStart)) {
|
|
106
|
-
if (!style.blockStart || !style.blockEnd) return content;
|
|
107
|
-
stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
108
|
-
} else if (style.single) {
|
|
109
|
-
stampLine = `${style.single} ${stampText}`;
|
|
110
|
-
} else {
|
|
111
|
-
return content;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Handle shebang - place stamp after it
|
|
115
|
-
if (content.startsWith("#!")) {
|
|
116
|
-
const firstNewline = content.indexOf("\n");
|
|
117
|
-
if (firstNewline !== -1) {
|
|
118
|
-
const shebang = content.slice(0, firstNewline + 1);
|
|
119
|
-
const rest = content.slice(firstNewline + 1);
|
|
120
|
-
return `${shebang}${stampLine}\n\n${rest}`;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return `${stampLine}\n\n${content}`;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// =============================================================================
|
|
128
|
-
// Stamped Task Runner
|
|
129
|
-
// =============================================================================
|
|
130
|
-
|
|
131
|
-
export interface RunTaskWithStampOptions extends RunTaskOptions {
|
|
132
|
-
/** Stamp configuration for generated files. If provided, all written files get a stamp comment. */
|
|
133
|
-
stamp?: StampConfig;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Run a task with stamp support. Wraps `runTask` from @canonical/task,
|
|
138
|
-
* intercepting WriteFile effects to prepend generated-file stamps.
|
|
139
|
-
*/
|
|
140
|
-
export const runTaskWithStamp = async <A>(
|
|
141
|
-
task: Task<A>,
|
|
142
|
-
options: RunTaskWithStampOptions = {},
|
|
143
|
-
): Promise<A> => {
|
|
144
|
-
const { stamp, onEffectStart: userOnEffectStart, ...restOptions } = options;
|
|
145
|
-
|
|
146
|
-
if (!stamp) {
|
|
147
|
-
return runTask(task, options);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// Wrap onEffectStart to mutate WriteFile content in-place before execution.
|
|
151
|
-
// This is the composition point: stamps are applied as a pre-processing step
|
|
152
|
-
// on the effect data, not baked into the interpreter.
|
|
153
|
-
const onEffectStart = (effect: Effect) => {
|
|
154
|
-
if (effect._tag === "WriteFile") {
|
|
155
|
-
(effect as { content: string }).content = applyStamp(
|
|
156
|
-
effect.path,
|
|
157
|
-
effect.content,
|
|
158
|
-
stamp,
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
userOnEffectStart?.(effect);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
return runTask(task, { ...restOptions, onEffectStart });
|
|
165
|
-
};
|