@cosmicdrift/kumiko-feature-ast 0.416.0 → 0.417.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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-feature-ast",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.417.0",
|
|
4
4
|
"description": "Feature-file AST for Kumiko: parse, patch and render feature definitions, the pattern library, and the codemods kumiko-upgrade runs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cosmicdrift/kumiko-framework": "0.
|
|
26
|
-
"@cosmicdrift/kumiko-types": "0.
|
|
25
|
+
"@cosmicdrift/kumiko-framework": "0.417.0",
|
|
26
|
+
"@cosmicdrift/kumiko-types": "0.417.0",
|
|
27
27
|
"ts-morph": "^28.0.0",
|
|
28
28
|
"zod": "^4.6.7"
|
|
29
29
|
},
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// scaffold-kms-mode — brings apps scaffolded before kumiko-framework 0.416.0 in line with the
|
|
3
|
+
// current scaffold, so production boots accept `*_CIPHERTEXT` Key Manager keys:
|
|
4
|
+
// bin/main.ts requireKmsWiring(process.env, opts) -> { kmsMode: "required" as const }
|
|
5
|
+
// bin/kumiko.ts kmsSlotsOf(envSchema.schema) -> union with kmsSlotsOf(kmsEnvSchema)
|
|
6
|
+
// Only the exact scaffold shape is rewritten; a hand-edited file is reported as manual.
|
|
7
|
+
//
|
|
8
|
+
// Usage:
|
|
9
|
+
// bun scripts/codemod/scaffold-kms-mode.ts <targetDir...> [--check | --dry-run | --write] [--no-format]
|
|
10
|
+
|
|
11
|
+
import { Node, Project, type SourceFile, SyntaxKind } from "ts-morph";
|
|
12
|
+
import {
|
|
13
|
+
applyEdits,
|
|
14
|
+
type CodemodResult,
|
|
15
|
+
type CodemodSite,
|
|
16
|
+
hasImportBinding,
|
|
17
|
+
removeNamedImport,
|
|
18
|
+
runCodemodCli,
|
|
19
|
+
type SiteLists,
|
|
20
|
+
type TextEdit,
|
|
21
|
+
} from "./codemod-support.ts";
|
|
22
|
+
|
|
23
|
+
const MAIN_FILE_SUFFIX = "/bin/main.ts";
|
|
24
|
+
const CLI_FILE_SUFFIX = "/bin/kumiko.ts";
|
|
25
|
+
const PRODUCTION_CONDITION = 'process.env["NODE_ENV"] === "production"';
|
|
26
|
+
const REQUIRED_KMS_MODE = '{ kmsMode: "required" as const }';
|
|
27
|
+
const OLD_SLOTS_INITIALIZER = "kmsSlotsOf(envSchema.schema)";
|
|
28
|
+
const NEW_SLOTS_INITIALIZER =
|
|
29
|
+
"[...new Set([...kmsSlotsOf(envSchema.schema), ...kmsSlotsOf(kmsEnvSchema)])]";
|
|
30
|
+
const SLOTS_COMMENT =
|
|
31
|
+
"// bin/main.ts boots with kmsMode, which resolves the slots of kmsEnvSchema; the migrate step needs the same keys.";
|
|
32
|
+
const KMS_ENV_SPECIFIER = "@cosmicdrift/kumiko-bundled-features/crypto-shredding/env";
|
|
33
|
+
|
|
34
|
+
const CHEAP_PREFILTER = /requireKmsWiring|kmsSlotsOf/;
|
|
35
|
+
|
|
36
|
+
function parse(source: string, filePath: string): SourceFile {
|
|
37
|
+
return new Project({
|
|
38
|
+
useInMemoryFileSystem: true,
|
|
39
|
+
skipAddingFilesFromTsConfig: true,
|
|
40
|
+
skipFileDependencyResolution: true,
|
|
41
|
+
}).createSourceFile(filePath, source);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function site(file: string, node: Node, description: string): CodemodSite {
|
|
45
|
+
return { file, line: node.getStartLineNumber(), description };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function transformMain(source: string, filePath: string, result: SiteLists): string {
|
|
49
|
+
const sourceFile = parse(source, filePath);
|
|
50
|
+
const productionBranches = sourceFile
|
|
51
|
+
.getDescendantsOfKind(SyntaxKind.ConditionalExpression)
|
|
52
|
+
.filter((conditional) => conditional.getCondition().getText() === PRODUCTION_CONDITION)
|
|
53
|
+
.map((conditional) => conditional.getWhenTrue())
|
|
54
|
+
.filter(
|
|
55
|
+
(branch) =>
|
|
56
|
+
Node.isCallExpression(branch) && branch.getExpression().getText() === "requireKmsWiring",
|
|
57
|
+
);
|
|
58
|
+
const [branch] = productionBranches;
|
|
59
|
+
const usageCount = sourceFile
|
|
60
|
+
.getDescendantsOfKind(SyntaxKind.Identifier)
|
|
61
|
+
.filter((identifier) => identifier.getText() === "requireKmsWiring").length;
|
|
62
|
+
const importDeclaration = sourceFile
|
|
63
|
+
.getImportDeclarations()
|
|
64
|
+
.find((declaration) =>
|
|
65
|
+
declaration.getNamedImports().some((n) => n.getName() === "requireKmsWiring"),
|
|
66
|
+
);
|
|
67
|
+
const importSpecifier = importDeclaration
|
|
68
|
+
?.getNamedImports()
|
|
69
|
+
.find((n) => n.getName() === "requireKmsWiring");
|
|
70
|
+
// import + the one production call: anything else is hand-written code this codemod must not guess at.
|
|
71
|
+
const isScaffoldShape =
|
|
72
|
+
productionBranches.length === 1 &&
|
|
73
|
+
branch !== undefined &&
|
|
74
|
+
Node.isCallExpression(branch) &&
|
|
75
|
+
branch.getArguments()[0]?.getText() === "process.env" &&
|
|
76
|
+
importSpecifier !== undefined &&
|
|
77
|
+
importSpecifier.getAliasNode() === undefined &&
|
|
78
|
+
usageCount === 2;
|
|
79
|
+
if (!isScaffoldShape || importDeclaration === undefined) {
|
|
80
|
+
const stray = sourceFile
|
|
81
|
+
.getDescendantsOfKind(SyntaxKind.Identifier)
|
|
82
|
+
.find((identifier) => identifier.getText() === "requireKmsWiring");
|
|
83
|
+
if (stray !== undefined) {
|
|
84
|
+
result.manual.push(
|
|
85
|
+
site(
|
|
86
|
+
filePath,
|
|
87
|
+
stray,
|
|
88
|
+
`requireKmsWiring does not match the scaffold: pass kmsMode: "required" to runProdApp in production`,
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return source;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
result.rewrites.push(site(filePath, branch, `requireKmsWiring(...) -> ${REQUIRED_KMS_MODE}`));
|
|
96
|
+
const replaced = parse(
|
|
97
|
+
applyEdits(source, [
|
|
98
|
+
{ start: branch.getStart(), end: branch.getEnd(), text: REQUIRED_KMS_MODE },
|
|
99
|
+
]),
|
|
100
|
+
filePath,
|
|
101
|
+
);
|
|
102
|
+
const declaration = replaced
|
|
103
|
+
.getImportDeclarations()
|
|
104
|
+
.find((d) => d.getNamedImports().some((n) => n.getName() === "requireKmsWiring"));
|
|
105
|
+
const named = declaration?.getNamedImports().find((n) => n.getName() === "requireKmsWiring");
|
|
106
|
+
if (declaration !== undefined && named !== undefined) {
|
|
107
|
+
removeNamedImport(replaced, declaration, named);
|
|
108
|
+
}
|
|
109
|
+
return replaced.getFullText();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function insertKmsEnvImport(sourceFile: SourceFile): TextEdit {
|
|
113
|
+
const declarations = sourceFile.getImportDeclarations();
|
|
114
|
+
const next = declarations.find((d) => d.getModuleSpecifierValue() > KMS_ENV_SPECIFIER);
|
|
115
|
+
const line = `import { kmsEnvSchema } from "${KMS_ENV_SPECIFIER}";`;
|
|
116
|
+
if (next !== undefined) {
|
|
117
|
+
return { start: next.getStart(), end: next.getStart(), text: `${line}\n` };
|
|
118
|
+
}
|
|
119
|
+
const last = declarations[declarations.length - 1];
|
|
120
|
+
const at = last === undefined ? 0 : last.getEnd();
|
|
121
|
+
return { start: at, end: at, text: last === undefined ? `${line}\n` : `\n${line}` };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function transformCli(source: string, filePath: string, result: SiteLists): string {
|
|
125
|
+
const sourceFile = parse(source, filePath);
|
|
126
|
+
const declarations = sourceFile
|
|
127
|
+
.getDescendantsOfKind(SyntaxKind.VariableDeclaration)
|
|
128
|
+
.filter((declaration) => declaration.getName() === "kmsSlots");
|
|
129
|
+
const [declaration] = declarations;
|
|
130
|
+
const statement = declaration?.getVariableStatement();
|
|
131
|
+
const slotsOfImported = sourceFile
|
|
132
|
+
.getImportDeclarations()
|
|
133
|
+
.some(
|
|
134
|
+
(d) =>
|
|
135
|
+
d.getModuleSpecifierValue() === "@cosmicdrift/kumiko-framework/env" &&
|
|
136
|
+
d.getNamedImports().some((n) => n.getName() === "kmsSlotsOf"),
|
|
137
|
+
);
|
|
138
|
+
const isScaffoldShape =
|
|
139
|
+
declarations.length === 1 &&
|
|
140
|
+
declaration !== undefined &&
|
|
141
|
+
statement !== undefined &&
|
|
142
|
+
declaration.getInitializer()?.getText() === OLD_SLOTS_INITIALIZER &&
|
|
143
|
+
slotsOfImported &&
|
|
144
|
+
!hasImportBinding(sourceFile, "kmsEnvSchema");
|
|
145
|
+
if (!isScaffoldShape) {
|
|
146
|
+
const unmigrated = sourceFile
|
|
147
|
+
.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
148
|
+
.find((call) => call.getText() === OLD_SLOTS_INITIALIZER);
|
|
149
|
+
if (unmigrated !== undefined && !hasImportBinding(sourceFile, "kmsEnvSchema")) {
|
|
150
|
+
result.manual.push(
|
|
151
|
+
site(
|
|
152
|
+
filePath,
|
|
153
|
+
unmigrated,
|
|
154
|
+
`kmsSlots does not match the scaffold: add kmsSlotsOf(kmsEnvSchema) from ${KMS_ENV_SPECIFIER}`,
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return source;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
result.rewrites.push(
|
|
162
|
+
site(
|
|
163
|
+
filePath,
|
|
164
|
+
statement,
|
|
165
|
+
`kmsSlotsOf(envSchema.schema) -> union with kmsSlotsOf(kmsEnvSchema)`,
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
const indent = source.slice(statement.getStartLinePos(), statement.getStart());
|
|
169
|
+
return applyEdits(source, [
|
|
170
|
+
{
|
|
171
|
+
start: declaration.getInitializerOrThrow().getStart(),
|
|
172
|
+
end: declaration.getInitializerOrThrow().getEnd(),
|
|
173
|
+
text: NEW_SLOTS_INITIALIZER,
|
|
174
|
+
},
|
|
175
|
+
{ start: statement.getStart(), end: statement.getStart(), text: `${SLOTS_COMMENT}\n${indent}` },
|
|
176
|
+
insertKmsEnvImport(sourceFile),
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const normalizedPath = (file: string): string => file.replaceAll("\\", "/");
|
|
181
|
+
|
|
182
|
+
export function transformScaffoldKmsModeSource(source: string, filePath: string): CodemodResult {
|
|
183
|
+
const result: SiteLists = { rewrites: [], manual: [] };
|
|
184
|
+
if (!CHEAP_PREFILTER.test(source)) return { output: source, ...result };
|
|
185
|
+
const normalized = normalizedPath(filePath);
|
|
186
|
+
if (normalized.endsWith(MAIN_FILE_SUFFIX)) {
|
|
187
|
+
return { output: transformMain(source, filePath, result), ...result };
|
|
188
|
+
}
|
|
189
|
+
if (normalized.endsWith(CLI_FILE_SUFFIX)) {
|
|
190
|
+
return { output: transformCli(source, filePath, result), ...result };
|
|
191
|
+
}
|
|
192
|
+
return { output: source, ...result };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isSkippedPath(file: string): boolean {
|
|
196
|
+
const normalized = normalizedPath(file);
|
|
197
|
+
return !(normalized.endsWith(MAIN_FILE_SUFFIX) || normalized.endsWith(CLI_FILE_SUFFIX));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (import.meta.main) {
|
|
201
|
+
await runCodemodCli(
|
|
202
|
+
{ name: "scaffold-kms-mode", transform: transformScaffoldKmsModeSource, isSkippedPath },
|
|
203
|
+
process.argv.slice(2),
|
|
204
|
+
);
|
|
205
|
+
}
|