@azure-net/cli 5.1.1 → 6.0.0-next.3
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/README.md +201 -0
- package/bin/azure-net.js +236 -221
- package/dist/bin/azure-net.d.ts +2 -0
- package/dist/src/checks/architecture.d.ts +20 -0
- package/dist/src/checks/checkDomain.d.ts +1 -0
- package/dist/src/checks/checkFoldersStructure.d.ts +1 -0
- package/dist/src/checks/checkHooks.d.ts +1 -0
- package/dist/src/checks/checkImports.d.ts +1 -0
- package/dist/src/checks/checkInternal.d.ts +1 -0
- package/dist/src/checks/checkLayerBoundaries.d.ts +1 -0
- package/dist/src/checks/checkLayerImports.d.ts +1 -0
- package/dist/src/checks/checkPresenterNames.d.ts +1 -0
- package/dist/src/checks/checkProject.d.ts +1 -0
- package/dist/src/checks/checkProviderGraph.d.ts +1 -0
- package/dist/src/checks/checkProviderNames.d.ts +1 -0
- package/dist/src/generate/layers/generateRepository.d.ts +1 -0
- package/dist/src/generate/layers/generateRepositoryMethod.d.ts +1 -0
- package/dist/src/generate/layers/generateUseCases.d.ts +1 -0
- package/dist/src/generate/module/generateDomain.d.ts +8 -0
- package/dist/src/generate/module/generateModule.d.ts +1 -0
- package/dist/src/generate/module/generateModulePreset.d.ts +1 -0
- package/dist/src/generate/module/generatePresenter.d.ts +29 -0
- package/dist/src/generate/module/generateRepository.d.ts +15 -0
- package/dist/src/generate/module/generateRepositoryMethod.d.ts +2 -0
- package/dist/src/generate/module/generateSchemaFromType.d.ts +9 -0
- package/dist/src/generate/module/generateStore.d.ts +1 -0
- package/dist/src/generate/module/generateTypesJson.d.ts +2 -0
- package/dist/src/generate/module/generateUseCases.d.ts +1 -0
- package/dist/src/generate/module/repositoryModuleShared.d.ts +89 -0
- package/dist/src/generate/routes/generateRoutes.d.ts +14 -0
- package/dist/src/generate/ui/generateComponent.d.ts +1 -0
- package/dist/src/generate/ui/generateDesignComponent.d.ts +1 -0
- package/dist/src/generate/ui/generateWidget.d.ts +1 -0
- package/dist/src/git/commit.d.ts +23 -0
- package/dist/src/git/push.d.ts +11 -0
- package/dist/src/git/runGit.d.ts +2 -0
- package/dist/src/init/initAliases.d.ts +12 -0
- package/dist/src/init/initEdges.d.ts +2 -0
- package/dist/src/init/initStructure.d.ts +1 -0
- package/dist/src/install/configureCommitTools.d.ts +1 -0
- package/dist/src/install/installFresh.d.ts +1 -0
- package/dist/src/migrate/migrate.d.ts +22 -0
- package/dist/src/migrate/presenterFactories.d.ts +9 -0
- package/dist/src/migrate/presenters.d.ts +13 -0
- package/dist/src/migrate/translations.d.ts +8 -0
- package/dist/src/plugins/addLint.d.ts +5 -0
- package/dist/src/scaffolds/createCoreSchema.d.ts +1 -0
- package/dist/src/scaffolds/createDatasource.d.ts +1 -0
- package/dist/src/scaffolds/createProgram.d.ts +1 -0
- package/dist/src/scaffolds/createResponse.d.ts +1 -0
- package/dist/src/scaffolds/createSchemaRule.d.ts +1 -0
- package/dist/src/scaffolds/createTranslationManager.d.ts +1 -0
- package/{src/scaffolds/index.ts → dist/src/scaffolds/index.d.ts} +0 -2
- package/dist/src/scaffolds/registerDatasource.d.ts +2 -0
- package/dist/src/scaffolds/updateDefaultFactoryConfig.d.ts +1 -0
- package/dist/src/utils/architecture.d.ts +31 -0
- package/dist/src/utils/contextUtils.d.ts +6 -0
- package/dist/src/utils/fileUtils.d.ts +5 -0
- package/dist/src/utils/flattenConfig.d.ts +2 -0
- package/dist/src/utils/legacyAliases.d.ts +1 -0
- package/dist/src/utils/loadConfig.d.ts +16 -0
- package/dist/src/utils/packageImports.d.ts +4 -0
- package/dist/src/utils/sharedFoundation.d.ts +20 -0
- package/dist/src/utils/viteConfig.d.ts +9 -0
- package/package.json +17 -7
- package/src/checks/architecture.js +154 -0
- package/src/checks/checkDomain.js +124 -154
- package/src/checks/checkFoldersStructure.js +64 -52
- package/src/checks/checkHooks.js +12 -12
- package/src/checks/checkImports.js +26 -142
- package/src/checks/checkInternal.js +27 -24
- package/src/checks/checkLayerBoundaries.js +22 -197
- package/src/checks/checkLayerImports.js +14 -134
- package/src/checks/checkPresenterNames.js +156 -143
- package/src/checks/checkProject.js +27 -26
- package/src/checks/checkProviderGraph.js +182 -166
- package/src/checks/checkProviderNames.js +123 -112
- package/src/generate/layers/generateRepository.js +1 -109
- package/src/generate/layers/generateRepositoryMethod.js +1 -90
- package/src/generate/layers/generateUseCases.js +1 -44
- package/src/generate/module/generateDomain.js +81 -105
- package/src/generate/module/generateModule.js +34 -68
- package/src/generate/module/generateModulePreset.js +188 -248
- package/src/generate/module/generatePresenter.js +441 -369
- package/src/generate/module/generateRepository.js +102 -121
- package/src/generate/module/generateRepositoryMethod.js +117 -0
- package/src/generate/module/generateSchemaFromType.js +283 -280
- package/src/generate/module/generateStore.js +41 -0
- package/src/generate/module/generateTypesJson.js +196 -195
- package/src/generate/module/generateUseCases.js +47 -55
- package/src/generate/module/repositoryModuleShared.js +480 -459
- package/src/generate/routes/generateRoutes.js +4 -7
- package/src/generate/ui/generateComponent.js +44 -64
- package/src/generate/ui/generateDesignComponent.js +21 -41
- package/src/generate/ui/generateWidget.js +21 -41
- package/src/git/commit.js +67 -0
- package/src/git/push.js +19 -0
- package/src/git/runGit.js +11 -0
- package/src/init/initAliases.js +19 -135
- package/src/init/initEdges.js +79 -54
- package/src/init/initStructure.js +31 -145
- package/src/install/configureCommitTools.js +1 -0
- package/src/install/installFresh.js +39 -68
- package/src/migrate/migrate.js +1027 -0
- package/src/migrate/presenterFactories.js +189 -0
- package/src/migrate/presenters.js +373 -0
- package/src/migrate/translations.js +213 -0
- package/src/plugins/addLint.js +52 -219
- package/src/scaffolds/createCoreSchema.js +65 -91
- package/src/scaffolds/createDatasource.js +67 -101
- package/src/scaffolds/createProgram.js +8 -8
- package/src/scaffolds/createResponse.js +29 -29
- package/src/scaffolds/createSchemaRule.js +19 -19
- package/src/scaffolds/createTranslationManager.js +84 -37
- package/src/scaffolds/index.js +0 -2
- package/src/scaffolds/registerDatasource.js +59 -0
- package/src/scaffolds/updateDefaultFactoryConfig.js +31 -0
- package/src/utils/architecture.js +145 -0
- package/src/utils/contextUtils.js +32 -65
- package/src/utils/fileUtils.js +69 -50
- package/src/utils/flattenConfig.js +38 -0
- package/src/utils/legacyAliases.js +75 -0
- package/src/utils/loadConfig.js +76 -40
- package/src/utils/packageImports.js +63 -0
- package/src/utils/sharedFoundation.js +36 -42
- package/src/utils/viteConfig.js +295 -0
- package/.prettierrc +0 -7
- package/bin/azure-net.ts +0 -308
- package/eslint.config.js +0 -34
- package/src/checks/checkDomain.ts +0 -223
- package/src/checks/checkFoldersStructure.ts +0 -85
- package/src/checks/checkHooks.ts +0 -22
- package/src/checks/checkImports.ts +0 -193
- package/src/checks/checkInternal.ts +0 -48
- package/src/checks/checkLayerBoundaries.ts +0 -255
- package/src/checks/checkLayerImports.ts +0 -185
- package/src/checks/checkPresenterNames.ts +0 -203
- package/src/checks/checkProject.ts +0 -45
- package/src/checks/checkProviderGraph.ts +0 -234
- package/src/checks/checkProviderNames.ts +0 -165
- package/src/generate/layers/generateRepository.ts +0 -123
- package/src/generate/layers/generateRepositoryMethod.ts +0 -102
- package/src/generate/layers/generateUseCases.ts +0 -50
- package/src/generate/layers/repositoryShared.js +0 -506
- package/src/generate/layers/repositoryShared.ts +0 -662
- package/src/generate/module/generateDomain.ts +0 -147
- package/src/generate/module/generateModule.ts +0 -84
- package/src/generate/module/generateModulePreset.ts +0 -338
- package/src/generate/module/generatePresenter.ts +0 -498
- package/src/generate/module/generateRepository.ts +0 -158
- package/src/generate/module/generateSchemaFromType.ts +0 -368
- package/src/generate/module/generateTypesJson.ts +0 -259
- package/src/generate/module/generateUseCases.ts +0 -67
- package/src/generate/module/repositoryModuleShared.ts +0 -669
- package/src/generate/routes/generateRoutes.ts +0 -275
- package/src/generate/ui/generateComponent.ts +0 -100
- package/src/generate/ui/generateDesignComponent.ts +0 -71
- package/src/generate/ui/generateWidget.ts +0 -71
- package/src/init/initAliases.ts +0 -172
- package/src/init/initEdges.ts +0 -74
- package/src/init/initStructure.ts +0 -172
- package/src/install/installFresh.ts +0 -93
- package/src/plugins/addLint.ts +0 -274
- package/src/scaffolds/createCoreSchema.ts +0 -130
- package/src/scaffolds/createDatasource.ts +0 -161
- package/src/scaffolds/createDatasourceProvider.js +0 -127
- package/src/scaffolds/createDatasourceProvider.ts +0 -161
- package/src/scaffolds/createPresenterFactory.js +0 -83
- package/src/scaffolds/createPresenterFactory.ts +0 -102
- package/src/scaffolds/createProgram.ts +0 -19
- package/src/scaffolds/createResponse.ts +0 -66
- package/src/scaffolds/createSchemaRule.ts +0 -44
- package/src/scaffolds/createTranslationManager.ts +0 -80
- package/src/utils/contextUtils.ts +0 -88
- package/src/utils/fileUtils.ts +0 -74
- package/src/utils/loadConfig.ts +0 -68
- package/src/utils/sharedFoundation.ts +0 -70
- package/tsconfig.json +0 -19
- /package/{src/checks/index.ts → dist/src/checks/index.d.ts} +0 -0
- /package/{src/plugins/index.ts → dist/src/plugins/index.d.ts} +0 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
const patch = (input, edits) => {
|
|
4
|
+
for (const e of edits.sort((a, b) => b.start - a.start))
|
|
5
|
+
input = input.slice(0, e.start) + e.text + input.slice(e.end);
|
|
6
|
+
return input;
|
|
7
|
+
};
|
|
8
|
+
const internal = new Set(['preloadTranslation', 'initializeClient', 'syncFromState']);
|
|
9
|
+
const translationModules = new Set(['@azure-net/kit/i18n', '@azure-net/edges-translations']);
|
|
10
|
+
/** A conservative migration of known translation wiring, never a project-wide textual replacement. */
|
|
11
|
+
export function migrateTranslationsSources(sources, resolve) {
|
|
12
|
+
const updates = new Map();
|
|
13
|
+
const issues = [];
|
|
14
|
+
const managers = [];
|
|
15
|
+
for (const [file, input] of sources) {
|
|
16
|
+
if (!/\.[jt]s$/.test(file))
|
|
17
|
+
continue;
|
|
18
|
+
const source = ts.createSourceFile(file, input, ts.ScriptTarget.Latest, true);
|
|
19
|
+
const factories = new Set();
|
|
20
|
+
for (const stmt of source.statements) {
|
|
21
|
+
if (!ts.isImportDeclaration(stmt) || !ts.isStringLiteral(stmt.moduleSpecifier) || !translationModules.has(stmt.moduleSpecifier.text))
|
|
22
|
+
continue;
|
|
23
|
+
const bindings = stmt.importClause?.namedBindings;
|
|
24
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
25
|
+
for (const b of bindings.elements)
|
|
26
|
+
if ((b.propertyName ?? b.name).text === 'createTranslations')
|
|
27
|
+
factories.add(b.name.text);
|
|
28
|
+
}
|
|
29
|
+
for (const stmt of source.statements)
|
|
30
|
+
if (ts.isVariableStatement(stmt) && stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword))
|
|
31
|
+
for (const d of stmt.declarationList.declarations) {
|
|
32
|
+
if (ts.isIdentifier(d.name) &&
|
|
33
|
+
d.initializer &&
|
|
34
|
+
ts.isCallExpression(d.initializer) &&
|
|
35
|
+
ts.isIdentifier(d.initializer.expression) &&
|
|
36
|
+
factories.has(d.initializer.expression.text))
|
|
37
|
+
managers.push({ file, name: d.name.text });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!managers.length)
|
|
41
|
+
return { updates, issues };
|
|
42
|
+
if (managers.length !== 1) {
|
|
43
|
+
for (const m of managers)
|
|
44
|
+
issues.push({ file: m.file, reason: 'Multiple translation managers: choose the manager for app.useTranslations manually.' });
|
|
45
|
+
return { updates, issues };
|
|
46
|
+
}
|
|
47
|
+
const manager = managers[0];
|
|
48
|
+
const reaches = (file, seen = new Set()) => {
|
|
49
|
+
if (file === manager.file)
|
|
50
|
+
return true;
|
|
51
|
+
if (seen.has(file))
|
|
52
|
+
return false;
|
|
53
|
+
seen.add(file);
|
|
54
|
+
const source = ts.createSourceFile(file, sources.get(file) ?? '', ts.ScriptTarget.Latest, true);
|
|
55
|
+
return source.statements.some((s) => {
|
|
56
|
+
if ((!ts.isImportDeclaration(s) && !ts.isExportDeclaration(s)) || !s.moduleSpecifier || !ts.isStringLiteral(s.moduleSpecifier))
|
|
57
|
+
return false;
|
|
58
|
+
const target = resolve(file, s.moduleSpecifier.text);
|
|
59
|
+
return !!target && reaches(target, seen);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
let connected = false;
|
|
63
|
+
for (const [file, input] of sources) {
|
|
64
|
+
if (!/\.[jt]s$/.test(file) || !reaches(file))
|
|
65
|
+
continue;
|
|
66
|
+
const source = ts.createSourceFile(file, input, ts.ScriptTarget.Latest, true);
|
|
67
|
+
const creators = new Set();
|
|
68
|
+
for (const stmt of source.statements)
|
|
69
|
+
if (ts.isImportDeclaration(stmt) && ts.isStringLiteral(stmt.moduleSpecifier) && stmt.moduleSpecifier.text === '@azure-net/kit') {
|
|
70
|
+
const b = stmt.importClause?.namedBindings;
|
|
71
|
+
if (b && ts.isNamedImports(b))
|
|
72
|
+
for (const e of b.elements)
|
|
73
|
+
if ((e.propertyName ?? e.name).text === 'createApp')
|
|
74
|
+
creators.add(e.name.text);
|
|
75
|
+
}
|
|
76
|
+
const calls = [];
|
|
77
|
+
const visit = (n) => {
|
|
78
|
+
if (ts.isCallExpression(n) && ts.isIdentifier(n.expression) && creators.has(n.expression.text))
|
|
79
|
+
calls.push(n);
|
|
80
|
+
ts.forEachChild(n, visit);
|
|
81
|
+
};
|
|
82
|
+
visit(source);
|
|
83
|
+
if (!calls.length)
|
|
84
|
+
continue;
|
|
85
|
+
if (calls.length !== 1) {
|
|
86
|
+
issues.push({ file, reason: 'Multiple createApp calls: connect translations manually.' });
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const callback = calls[0].arguments[0];
|
|
90
|
+
if (!callback || !ts.isArrowFunction(callback) || !callback.parameters[0] || !ts.isIdentifier(callback.parameters[0].name)) {
|
|
91
|
+
issues.push({ file, reason: 'Unsupported createApp callback: add useTranslations manually.' });
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
let body = ts.isBlock(callback.body)
|
|
95
|
+
? callback.body.statements.find(ts.isReturnStatement)?.expression
|
|
96
|
+
: callback.body;
|
|
97
|
+
let already = false;
|
|
98
|
+
while (body && ts.isCallExpression(body) && ts.isPropertyAccessExpression(body.expression)) {
|
|
99
|
+
if (body.expression.name.text === 'useTranslations')
|
|
100
|
+
already = true;
|
|
101
|
+
body = body.expression.expression;
|
|
102
|
+
}
|
|
103
|
+
if (already) {
|
|
104
|
+
connected = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (!body || !ts.isIdentifier(body) || body.text !== callback.parameters[0].name.text) {
|
|
108
|
+
issues.push({ file, reason: 'Unsupported createApp chain: add useTranslations manually.' });
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
let local = 'MigratedTranslationManager';
|
|
112
|
+
while (new RegExp(`\\b${local}\\b`).test(input))
|
|
113
|
+
local += '_';
|
|
114
|
+
let spec = path
|
|
115
|
+
.relative(path.dirname(file), manager.file)
|
|
116
|
+
.split(path.sep)
|
|
117
|
+
.join('/')
|
|
118
|
+
.replace(/\.[jt]s$/, '');
|
|
119
|
+
if (!spec.startsWith('.'))
|
|
120
|
+
spec = './' + spec;
|
|
121
|
+
updates.set(file, `import { ${manager.name} as ${local} } from ${JSON.stringify(spec)};\n` +
|
|
122
|
+
input.slice(0, body.end) +
|
|
123
|
+
`.useTranslations(${local})` +
|
|
124
|
+
input.slice(body.end));
|
|
125
|
+
connected = true;
|
|
126
|
+
}
|
|
127
|
+
if (!connected) {
|
|
128
|
+
issues.push({
|
|
129
|
+
file: manager.file,
|
|
130
|
+
reason: 'Translations require app.useTranslations(TranslationManager); no unambiguous connected bootstrap found.'
|
|
131
|
+
});
|
|
132
|
+
return { updates, issues };
|
|
133
|
+
}
|
|
134
|
+
let unsafeLifecycle = false;
|
|
135
|
+
for (const [file, original] of sources) {
|
|
136
|
+
if (!/\.[jt]s$/.test(file))
|
|
137
|
+
continue;
|
|
138
|
+
const input = updates.get(file) ?? original;
|
|
139
|
+
const source = ts.createSourceFile(file, input, ts.ScriptTarget.Latest, true);
|
|
140
|
+
const options = { noLib: true, noResolve: true };
|
|
141
|
+
const host = ts.createCompilerHost(options);
|
|
142
|
+
host.getSourceFile = (name) => (name === file ? source : undefined);
|
|
143
|
+
host.fileExists = (name) => name === file;
|
|
144
|
+
host.readFile = (name) => (name === file ? input : undefined);
|
|
145
|
+
const checker = ts.createProgram([file], options, host).getTypeChecker();
|
|
146
|
+
const accessors = new Set();
|
|
147
|
+
for (const stmt of source.statements)
|
|
148
|
+
if (ts.isImportDeclaration(stmt) &&
|
|
149
|
+
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
150
|
+
resolve(file, stmt.moduleSpecifier.text) === manager.file) {
|
|
151
|
+
const bindings = stmt.importClause?.namedBindings;
|
|
152
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
153
|
+
for (const e of bindings.elements)
|
|
154
|
+
if ((e.propertyName ?? e.name).text === manager.name)
|
|
155
|
+
accessors.add(e.name.text);
|
|
156
|
+
}
|
|
157
|
+
const edits = [];
|
|
158
|
+
let unsafe = false;
|
|
159
|
+
const scan = (node) => {
|
|
160
|
+
if (ts.isVariableDeclaration(node) &&
|
|
161
|
+
ts.isObjectBindingPattern(node.name) &&
|
|
162
|
+
node.initializer &&
|
|
163
|
+
ts.isCallExpression(node.initializer) &&
|
|
164
|
+
ts.isIdentifier(node.initializer.expression) &&
|
|
165
|
+
accessors.has(node.initializer.expression.text)) {
|
|
166
|
+
const removed = node.name.elements.filter((b) => ts.isIdentifier(b.name) && internal.has((b.propertyName ?? b.name).getText(source)));
|
|
167
|
+
for (const b of removed) {
|
|
168
|
+
const binding = checker.getSymbolAtLocation(b.name);
|
|
169
|
+
const refs = [];
|
|
170
|
+
const find = (n) => {
|
|
171
|
+
if (n === node.name)
|
|
172
|
+
return;
|
|
173
|
+
if (binding && ts.isIdentifier(n) && checker.getSymbolAtLocation(n) === binding)
|
|
174
|
+
refs.push(n);
|
|
175
|
+
ts.forEachChild(n, find);
|
|
176
|
+
};
|
|
177
|
+
find(source);
|
|
178
|
+
for (const ref of refs) {
|
|
179
|
+
const call = ref.parent;
|
|
180
|
+
if (!ts.isCallExpression(call) || call.expression !== ref || call.arguments.length) {
|
|
181
|
+
unsafe = true;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
edits.push({ start: call.getStart(source), end: call.end, text: 'Promise.resolve()' });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (removed.length) {
|
|
188
|
+
const kept = node.name.elements.filter((b) => !removed.includes(b));
|
|
189
|
+
edits.push({
|
|
190
|
+
start: node.name.getStart(source),
|
|
191
|
+
end: node.name.end,
|
|
192
|
+
text: `{ ${kept.map((b) => b.getText(source)).join(', ')} }`
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (ts.isImportDeclaration(node) &&
|
|
197
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
198
|
+
node.moduleSpecifier.text === '@azure-net/edges-translations')
|
|
199
|
+
edits.push({ start: node.moduleSpecifier.getStart(source), end: node.moduleSpecifier.end, text: "'@azure-net/kit/i18n'" });
|
|
200
|
+
ts.forEachChild(node, scan);
|
|
201
|
+
};
|
|
202
|
+
scan(source);
|
|
203
|
+
if (unsafe) {
|
|
204
|
+
unsafeLifecycle = true;
|
|
205
|
+
issues.push({ file, reason: 'Custom translation lifecycle arguments/references: migrate these calls manually to useTranslations.' });
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (edits.length)
|
|
209
|
+
updates.set(file, patch(input, edits));
|
|
210
|
+
}
|
|
211
|
+
// Do not connect automatic loading while an old custom locale resolver is still active.
|
|
212
|
+
return { updates: unsafeLifecycle ? new Map() : updates, issues };
|
|
213
|
+
}
|
package/src/plugins/addLint.js
CHANGED
|
@@ -1,222 +1,55 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"options": {
|
|
56
|
-
"parser": "svelte"
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
]
|
|
60
|
-
}
|
|
61
|
-
`;
|
|
62
|
-
const resolveInstallCommand = (manager) => {
|
|
63
|
-
const deps = dependencies.join(' ');
|
|
64
|
-
switch (manager) {
|
|
65
|
-
case 'pnpm':
|
|
66
|
-
return `pnpm add -D ${deps}`;
|
|
67
|
-
case 'npm':
|
|
68
|
-
return `npm install -D ${deps}`;
|
|
69
|
-
case 'yarn':
|
|
70
|
-
return `yarn add -D ${deps}`;
|
|
71
|
-
case 'bun':
|
|
72
|
-
return `bun add -d ${deps}`;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const resolveRunScriptCommand = (manager, script) => {
|
|
76
|
-
switch (manager) {
|
|
77
|
-
case 'pnpm':
|
|
78
|
-
return `pnpm ${script}`;
|
|
79
|
-
case 'npm':
|
|
80
|
-
return `npm run ${script}`;
|
|
81
|
-
case 'yarn':
|
|
82
|
-
return `yarn ${script}`;
|
|
83
|
-
case 'bun':
|
|
84
|
-
return `bun run ${script}`;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
const resolveHuskyCommand = (manager) => {
|
|
88
|
-
switch (manager) {
|
|
89
|
-
case 'pnpm':
|
|
90
|
-
return 'pnpm exec husky';
|
|
91
|
-
case 'npm':
|
|
92
|
-
return 'npx husky';
|
|
93
|
-
case 'yarn':
|
|
94
|
-
return 'yarn husky';
|
|
95
|
-
case 'bun':
|
|
96
|
-
return 'bunx husky';
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
const detectPackageManager = (cwd) => {
|
|
100
|
-
if (fs.existsSync(path.join(cwd, 'bun.lock')) || fs.existsSync(path.join(cwd, 'bun.lockb'))) return 'bun';
|
|
101
|
-
if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) return 'pnpm';
|
|
102
|
-
if (fs.existsSync(path.join(cwd, 'yarn.lock'))) return 'yarn';
|
|
103
|
-
if (fs.existsSync(path.join(cwd, 'package-lock.json'))) return 'npm';
|
|
104
|
-
return undefined;
|
|
105
|
-
};
|
|
106
|
-
const parseConfigState = (cwd) => {
|
|
107
|
-
const tsPath = path.join(cwd, 'azure-net.config.ts');
|
|
108
|
-
const jsPath = path.join(cwd, 'azure-net.config.js');
|
|
109
|
-
const configPath = fs.existsSync(tsPath) ? tsPath : fs.existsSync(jsPath) ? jsPath : null;
|
|
110
|
-
if (!configPath) {
|
|
111
|
-
return {
|
|
112
|
-
configPath: null,
|
|
113
|
-
content: null,
|
|
114
|
-
hasPackageManagerKey: false,
|
|
115
|
-
validPackageManager: null
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
const content = fs.readFileSync(configPath, 'utf-8');
|
|
119
|
-
const hasPackageManagerKey = /packageManager\s*:/.test(content);
|
|
120
|
-
const managerMatch = content.match(/packageManager\s*:\s*['"`](bun|pnpm|npm|yarn)['"`]/);
|
|
121
|
-
const validPackageManager = managerMatch?.[1];
|
|
122
|
-
return {
|
|
123
|
-
configPath,
|
|
124
|
-
content,
|
|
125
|
-
hasPackageManagerKey,
|
|
126
|
-
validPackageManager: validPackageManager ?? null
|
|
127
|
-
};
|
|
128
|
-
};
|
|
129
|
-
const upsertPackageManagerInConfig = (content, manager) => {
|
|
130
|
-
const packageManagerEntry = `packageManager: '${manager}'`;
|
|
131
|
-
const hasExportDefaultObject = /export\s+default\s*\{[\s\S]*\}\s*;?/.test(content);
|
|
132
|
-
if (!hasExportDefaultObject) {
|
|
133
|
-
return `export default {\n\t${packageManagerEntry}\n};\n`;
|
|
134
|
-
}
|
|
135
|
-
if (/packageManager\s*:/.test(content)) {
|
|
136
|
-
return content.replace(/packageManager\s*:\s*['"`][^'"`]+['"`]/, packageManagerEntry);
|
|
137
|
-
}
|
|
138
|
-
return content.replace(/export\s+default\s*\{([\s\S]*?)\}\s*;?/, (_full, body) => {
|
|
139
|
-
const trimmedRight = body.replace(/\s*$/, '');
|
|
140
|
-
if (!trimmedRight.length) {
|
|
141
|
-
return `export default {\n\t${packageManagerEntry}\n};`;
|
|
142
|
-
}
|
|
143
|
-
const withComma = trimmedRight.endsWith(',') ? trimmedRight : `${trimmedRight},`;
|
|
144
|
-
return `export default {${withComma}\n\t${packageManagerEntry}\n};`;
|
|
145
|
-
});
|
|
146
|
-
};
|
|
147
|
-
const localOnlyPrepareScript = `node -e "const local = !process.env.CI && process.env.NODE_ENV !== 'production' && !process.env.DOCKER && !process.env.CONTAINER; process.exit(local ? 1 : 0)" || (husky && svelte-kit sync)`;
|
|
148
|
-
const selectPackageManager = async (cwd, preset) => {
|
|
149
|
-
if (preset) return preset;
|
|
150
|
-
const { default: prompts } = await import('prompts');
|
|
151
|
-
const detected = detectPackageManager(cwd);
|
|
152
|
-
const response = await prompts({
|
|
153
|
-
type: 'select',
|
|
154
|
-
name: 'manager',
|
|
155
|
-
message: 'Choose your package manager:',
|
|
156
|
-
choices: [
|
|
157
|
-
{ title: 'bun', value: 'bun' },
|
|
158
|
-
{ title: 'pnpm', value: 'pnpm' },
|
|
159
|
-
{ title: 'npm', value: 'npm' },
|
|
160
|
-
{ title: 'yarn', value: 'yarn' }
|
|
161
|
-
],
|
|
162
|
-
initial: detected ? ['bun', 'pnpm', 'npm', 'yarn'].indexOf(detected) : 0
|
|
163
|
-
});
|
|
164
|
-
if (!response.manager) {
|
|
165
|
-
throw new Error('Package manager is required');
|
|
166
|
-
}
|
|
167
|
-
return response.manager;
|
|
168
|
-
};
|
|
169
|
-
export default async function addLint(options = {}) {
|
|
170
|
-
const cwd = process.cwd();
|
|
171
|
-
const pkgPath = path.join(cwd, 'package.json');
|
|
172
|
-
if (!fs.existsSync(pkgPath)) {
|
|
173
|
-
console.error('❌ package.json not found in this directory');
|
|
174
|
-
process.exit(1);
|
|
175
|
-
}
|
|
176
|
-
const configState = parseConfigState(cwd);
|
|
177
|
-
const manager = configState.validPackageManager ?? (await selectPackageManager(cwd, options.manager));
|
|
178
|
-
if (!configState.configPath) {
|
|
179
|
-
const newConfigPath = path.join(cwd, 'azure-net.config.ts');
|
|
180
|
-
fs.writeFileSync(newConfigPath, `export default {\n\tpackageManager: '${manager}'\n};\n`);
|
|
181
|
-
} else if (!configState.validPackageManager || !configState.hasPackageManagerKey) {
|
|
182
|
-
const nextConfig = upsertPackageManagerInConfig(configState.content ?? '', manager);
|
|
183
|
-
fs.writeFileSync(configState.configPath, nextConfig);
|
|
184
|
-
}
|
|
185
|
-
const installCommand = resolveInstallCommand(manager);
|
|
186
|
-
const runFormatCommand = resolveRunScriptCommand(manager, 'format');
|
|
187
|
-
const preCommitScript = `${resolveRunScriptCommand(manager, 'lint')} && ${resolveRunScriptCommand(manager, 'check')}`;
|
|
188
|
-
const commitScript = `${resolveRunScriptCommand(manager, 'format')} && git add . && git-cz && git push`;
|
|
189
|
-
const releaseScript = `${resolveRunScriptCommand(manager, 'commit')} && ${resolveRunScriptCommand(manager, 'semantic-release')}`;
|
|
190
|
-
console.log(`📦 Installing packages with ${manager}...`);
|
|
191
|
-
execSync(installCommand, { stdio: 'inherit' });
|
|
192
|
-
console.log('📁 Updating package.json...');
|
|
193
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
194
|
-
pkg.scripts = {
|
|
195
|
-
...(pkg.scripts ?? {}),
|
|
196
|
-
precommit: preCommitScript,
|
|
197
|
-
prepare: localOnlyPrepareScript,
|
|
198
|
-
'semantic-release': 'env-cmd semantic-release',
|
|
199
|
-
commit: commitScript,
|
|
200
|
-
release: releaseScript
|
|
201
|
-
};
|
|
202
|
-
pkg.config = pkg.config ?? {};
|
|
203
|
-
pkg.config.commitizen = {
|
|
204
|
-
path: 'git-cz'
|
|
205
|
-
};
|
|
206
|
-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4));
|
|
207
|
-
fs.writeFileSync(path.join(cwd, 'commitlint.config.ts'), commitlintConfig);
|
|
208
|
-
fs.writeFileSync(path.join(cwd, 'release.config.ts'), releaseConfig);
|
|
209
|
-
fs.writeFileSync(path.join(cwd, '.prettierrc'), prettierConfig);
|
|
210
|
-
console.log('🔧 Initializing Husky...');
|
|
211
|
-
try {
|
|
212
|
-
execSync(resolveHuskyCommand(manager), { stdio: 'inherit' });
|
|
213
|
-
} catch {
|
|
214
|
-
console.warn('⚠️ Husky initialization skipped (command failed in current environment).');
|
|
215
|
-
}
|
|
216
|
-
try {
|
|
217
|
-
execSync(runFormatCommand, { stdio: 'inherit' });
|
|
218
|
-
} catch {
|
|
219
|
-
console.warn('⚠️ Format script execution skipped (script may be missing).');
|
|
220
|
-
}
|
|
221
|
-
console.log(`✅ Linting and release setup completed! Added prepare script: ${localOnlyPrepareScript}`);
|
|
3
|
+
export default async function addLint(_options = {}) {
|
|
4
|
+
// Retain compatibility with the existing --manager option without running a package manager.
|
|
5
|
+
void _options;
|
|
6
|
+
const cwd = process.cwd();
|
|
7
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
8
|
+
if (!fs.existsSync(pkgPath))
|
|
9
|
+
throw new Error('package.json not found in this directory');
|
|
10
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
11
|
+
const configNames = [
|
|
12
|
+
'.prettierrc',
|
|
13
|
+
'.prettierrc.json',
|
|
14
|
+
'.prettierrc.json5',
|
|
15
|
+
'.prettierrc.yaml',
|
|
16
|
+
'.prettierrc.yml',
|
|
17
|
+
'.prettierrc.toml',
|
|
18
|
+
'.prettierrc.js',
|
|
19
|
+
'.prettierrc.cjs',
|
|
20
|
+
'.prettierrc.mjs',
|
|
21
|
+
'.prettierrc.ts',
|
|
22
|
+
'.prettierrc.cts',
|
|
23
|
+
'.prettierrc.mts',
|
|
24
|
+
'prettier.config.js',
|
|
25
|
+
'prettier.config.cjs',
|
|
26
|
+
'prettier.config.mjs',
|
|
27
|
+
'prettier.config.ts',
|
|
28
|
+
'prettier.config.cts',
|
|
29
|
+
'prettier.config.mts'
|
|
30
|
+
];
|
|
31
|
+
const existingConfigs = configNames.filter((name) => fs.existsSync(path.join(cwd, name)));
|
|
32
|
+
if ('prettier' in pkg || existingConfigs.length) {
|
|
33
|
+
const backupRoot = path.join(cwd, '.azure-net', 'PrettierBackups');
|
|
34
|
+
fs.mkdirSync(backupRoot, { recursive: true });
|
|
35
|
+
// Atomic directory creation prevents overwriting backups from rapid repeated runs.
|
|
36
|
+
const backup = fs.mkdtempSync(path.join(backupRoot, 'config-'));
|
|
37
|
+
if ('prettier' in pkg) {
|
|
38
|
+
fs.copyFileSync(pkgPath, path.join(backup, 'package.json'), fs.constants.COPYFILE_EXCL);
|
|
39
|
+
delete pkg.prettier;
|
|
40
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, '\t') + '\n');
|
|
41
|
+
}
|
|
42
|
+
for (const name of existingConfigs) {
|
|
43
|
+
fs.renameSync(path.join(cwd, name), path.join(backup, name));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
fs.writeFileSync(path.join(cwd, '.prettierrc'), JSON.stringify({
|
|
47
|
+
useTabs: true,
|
|
48
|
+
singleQuote: true,
|
|
49
|
+
trailingComma: 'none',
|
|
50
|
+
tabWidth: 2,
|
|
51
|
+
semi: true,
|
|
52
|
+
printWidth: 150
|
|
53
|
+
}, null, '\t') + '\n');
|
|
54
|
+
console.log('Prettier configuration replaced. No packages installed.');
|
|
222
55
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { updateDefaultFactoryConfig } from './updateDefaultFactoryConfig.js';
|
|
1
2
|
import fs from 'node:fs/promises';
|
|
2
3
|
import fsSync from 'node:fs';
|
|
3
4
|
import path from 'node:path';
|
|
@@ -6,102 +7,75 @@ import { writeIfNotExists, updateIndexTs } from '../utils/fileUtils.js';
|
|
|
6
7
|
import { toPascalCase } from '../utils/contextUtils.js';
|
|
7
8
|
import { getFoundationConstructPath, getSharedState } from '../utils/sharedFoundation.js';
|
|
8
9
|
const resolvePresetImportName = (preset) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
switch (preset) {
|
|
11
|
+
case 'ru':
|
|
12
|
+
return 'validationMessagesRu';
|
|
13
|
+
case 'en':
|
|
14
|
+
return 'validationMessagesEn';
|
|
15
|
+
case 'i18n':
|
|
16
|
+
return 'validationMessagesI18n';
|
|
17
|
+
}
|
|
17
18
|
};
|
|
18
|
-
const createSchemaFactoryTemplate = (
|
|
19
|
-
factoryName,
|
|
20
|
-
messagesImportName
|
|
21
|
-
) => `import { createSchemaFactory, createRules, ${messagesImportName} } from '@azure-net/kit/delivery';
|
|
19
|
+
const createSchemaFactoryTemplate = (factoryName, messagesImportName) => `import { createSchemaFactory, createRules, ${messagesImportName} } from '@azure-net/kit/delivery';
|
|
22
20
|
|
|
23
21
|
export const ${factoryName} = createSchemaFactory(createRules(${messagesImportName}));
|
|
24
22
|
`;
|
|
25
23
|
const resolveConfigFile = (cwd) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
const buildDefaultSchemaFactoryConfigEntry = (factoryName) => {
|
|
37
|
-
return `defaultSchemaFactory: '${factoryName}'`;
|
|
38
|
-
};
|
|
39
|
-
const upsertDefaultSchemaFactoryConfig = (content, factoryName) => {
|
|
40
|
-
const hasExportDefaultObject = /export\s+default\s*\{[\s\S]*\}\s*;?/.test(content);
|
|
41
|
-
const defaultFactoryEntry = buildDefaultSchemaFactoryConfigEntry(factoryName);
|
|
42
|
-
if (!hasExportDefaultObject) {
|
|
43
|
-
return `export default {\n\t${defaultFactoryEntry}\n};\n`;
|
|
44
|
-
}
|
|
45
|
-
let nextContent = content;
|
|
46
|
-
if (/defaultSchemaFactory\s*:/.test(nextContent)) {
|
|
47
|
-
nextContent = nextContent.replace(/defaultSchemaFactory\s*:\s*(\{[\s\S]*?\n\s*\}|['"`][^'"`]+['"`])/m, defaultFactoryEntry);
|
|
48
|
-
} else if (/defaultSchema\s*:/.test(nextContent)) {
|
|
49
|
-
nextContent = nextContent.replace(/defaultSchema\s*:\s*(\{[\s\S]*?\n\s*\}|['"`][^'"`]+['"`])/m, defaultFactoryEntry);
|
|
50
|
-
} else {
|
|
51
|
-
nextContent = nextContent.replace(/export\s+default\s*\{([\s\S]*)\}\s*;?/, (_full, body) => {
|
|
52
|
-
const trimmedRight = body.replace(/\s*$/, '');
|
|
53
|
-
if (!trimmedRight.length) {
|
|
54
|
-
return `export default {\n\t${defaultFactoryEntry}\n};`;
|
|
55
|
-
}
|
|
56
|
-
const withComma = trimmedRight.endsWith(',') ? trimmedRight : `${trimmedRight},`;
|
|
57
|
-
return `export default {${withComma}\n\t${defaultFactoryEntry}\n};`;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
return nextContent;
|
|
24
|
+
const tsPath = path.join(cwd, 'azure-net.config.ts');
|
|
25
|
+
const jsPath = path.join(cwd, 'azure-net.config.js');
|
|
26
|
+
if (fsSync.existsSync(tsPath)) {
|
|
27
|
+
return { filepath: tsPath, type: 'ts', exists: true, content: fsSync.readFileSync(tsPath, 'utf-8') };
|
|
28
|
+
}
|
|
29
|
+
if (fsSync.existsSync(jsPath)) {
|
|
30
|
+
return { filepath: jsPath, type: 'js', exists: true, content: fsSync.readFileSync(jsPath, 'utf-8') };
|
|
31
|
+
}
|
|
32
|
+
return { filepath: tsPath, type: 'ts', exists: false, content: '' };
|
|
61
33
|
};
|
|
62
34
|
export default async function createCoreSchema() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
35
|
+
const { schemaFactoryNameRaw } = await prompts({
|
|
36
|
+
type: 'text',
|
|
37
|
+
name: 'schemaFactoryNameRaw',
|
|
38
|
+
message: 'Schema factory name:',
|
|
39
|
+
initial: 'Schema'
|
|
40
|
+
});
|
|
41
|
+
const schemaFactoryName = toPascalCase(String(schemaFactoryNameRaw ?? 'Schema')) || 'Schema';
|
|
42
|
+
const { preset } = await prompts({
|
|
43
|
+
type: 'select',
|
|
44
|
+
name: 'preset',
|
|
45
|
+
message: 'Choose validation messages:',
|
|
46
|
+
choices: [
|
|
47
|
+
{ title: 'Russian', value: 'ru' },
|
|
48
|
+
{ title: 'English', value: 'en' },
|
|
49
|
+
{ title: 'i18n', value: 'i18n' }
|
|
50
|
+
],
|
|
51
|
+
initial: 2
|
|
52
|
+
});
|
|
53
|
+
const selectedPreset = (preset ?? 'i18n');
|
|
54
|
+
const messagesImportName = resolvePresetImportName(selectedPreset);
|
|
55
|
+
const { sharedContext } = await getSharedState();
|
|
56
|
+
const coreSchemaPath = getFoundationConstructPath(sharedContext.name, 'schema');
|
|
57
|
+
const customRulesPath = path.join(coreSchemaPath, 'CustomRules');
|
|
58
|
+
const filePath = path.join(coreSchemaPath, `${schemaFactoryName}.ts`);
|
|
59
|
+
await fs.mkdir(coreSchemaPath, { recursive: true });
|
|
60
|
+
await fs.mkdir(customRulesPath, { recursive: true });
|
|
61
|
+
const created = await writeIfNotExists(filePath, createSchemaFactoryTemplate(schemaFactoryName, messagesImportName));
|
|
62
|
+
if (!created) {
|
|
63
|
+
console.log(`⚠️ Schema factory "${schemaFactoryName}" already exists. File was not overwritten.`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
await updateIndexTs(coreSchemaPath);
|
|
67
|
+
console.log(`✅ Shared schema factory created: ${filePath}`);
|
|
68
|
+
}
|
|
69
|
+
const { useAsDefault } = await prompts({
|
|
70
|
+
type: 'confirm',
|
|
71
|
+
name: 'useAsDefault',
|
|
72
|
+
message: 'Use this schema factory as default for the project?',
|
|
73
|
+
initial: true
|
|
74
|
+
});
|
|
75
|
+
if (!useAsDefault)
|
|
76
|
+
return;
|
|
77
|
+
const config = resolveConfigFile(process.cwd());
|
|
78
|
+
const nextContent = updateDefaultFactoryConfig(config.content, 'defaultSchemaFactory', schemaFactoryName);
|
|
79
|
+
await fs.writeFile(config.filepath, nextContent, 'utf-8');
|
|
80
|
+
console.log(`✅ Default schema factory saved to ${path.basename(config.filepath)}`);
|
|
107
81
|
}
|