@venizia/vulcan 0.3.4 → 0.3.6
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 +2 -2
- package/base/agents.json +232 -0
- package/base/claude/settings.json +12 -0
- package/base/hooks/guard-package.ts +29 -0
- package/base/rules.md +2 -0
- package/base/templates/AGENTS.md +4 -3
- package/package.json +3 -3
- package/src/agents/atlas-block.ts +54 -0
- package/src/agents/common/constants.ts +38 -0
- package/src/agents/common/index.ts +3 -0
- package/src/agents/common/schema.ts +42 -0
- package/src/agents/common/types.ts +30 -0
- package/src/agents/detect.ts +36 -0
- package/src/agents/exclude.ts +87 -0
- package/src/agents/index.ts +9 -0
- package/src/agents/local.ts +65 -0
- package/src/agents/prompt.ts +22 -0
- package/src/agents/render.ts +229 -0
- package/src/agents/resolve.ts +145 -0
- package/src/agents/table.ts +35 -0
- package/src/commands/agents.command.ts +154 -0
- package/src/commands/doctor.command.ts +5 -1
- package/src/commands/help.command.ts +5 -1
- package/src/commands/index.ts +2 -0
- package/src/commands/sync.command.ts +112 -3
- package/src/common/constants.ts +9 -1
- package/src/config/common/constants.ts +12 -1
- package/src/config/packages.ts +2 -0
- package/src/config/schema.ts +16 -1
- package/src/create/add-app.ts +110 -4
- package/src/create/add-lib.ts +317 -0
- package/src/create/common/constants.ts +3 -0
- package/src/create/common/types.ts +18 -2
- package/src/create/dockerignore.ts +36 -0
- package/src/create/init-monorepo.ts +22 -4
- package/src/create/lib-preset.ts +154 -0
- package/src/create/preset.ts +22 -3
- package/src/create/workspace.ts +94 -3
- package/src/doctor/checks.ts +183 -12
- package/src/doctor/common/constants.ts +26 -0
- package/src/doctor/runner.ts +22 -0
- package/src/hooks/guard-package.ts +211 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/session-digest.ts +27 -1
- package/src/init/scaffold.ts +8 -5
- package/src/manifest/render.ts +4 -0
- package/src/router.ts +10 -1
- package/src/sync/agent-files.ts +112 -0
- package/src/sync/agent-removal.ts +241 -0
- package/src/sync/atlas-block.ts +55 -0
- package/src/sync/common/constants.ts +2 -0
- package/src/sync/exclude.ts +89 -0
- package/src/sync/index.ts +5 -0
- package/src/sync/local-choice.ts +55 -0
- package/src/sync/planner.ts +55 -19
- package/src/sync/settings.ts +26 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { ExitCodes } from '../common/index.ts';
|
|
4
|
+
import type { IProjectPackage, TResolvedConfig } from '../config/index.ts';
|
|
5
|
+
import { readWorkspaceGlobs } from '../init/index.ts';
|
|
6
|
+
import { runAppChecks } from './checks.ts';
|
|
7
|
+
import { CreateConstants, CreateFlags } from './common/index.ts';
|
|
8
|
+
import type { IAppsBlock, ILocatedPreset, IPresetSpec } from './common/index.ts';
|
|
9
|
+
import { today, writeFile } from './files.ts';
|
|
10
|
+
import { readLibPreset } from './lib-preset.ts';
|
|
11
|
+
import type { TLibPreset } from './lib-preset.ts';
|
|
12
|
+
import { askWithPrompt, readFlag } from './names.ts';
|
|
13
|
+
import { copyPresetFiles } from './preset.ts';
|
|
14
|
+
import { runBun } from './process.ts';
|
|
15
|
+
import {
|
|
16
|
+
addAppBullet,
|
|
17
|
+
filePresetWarning,
|
|
18
|
+
parseAppsBlock,
|
|
19
|
+
planRootManifest,
|
|
20
|
+
renderKnowledgeIndex,
|
|
21
|
+
renderLibConcept,
|
|
22
|
+
renderLibManifest,
|
|
23
|
+
renderLogEntry,
|
|
24
|
+
replaceAppsBlock,
|
|
25
|
+
} from './workspace.ts';
|
|
26
|
+
|
|
27
|
+
const KNOWLEDGE_DIRECTORY = '.agents/knowledge';
|
|
28
|
+
const SCOPE_PATTERN = /^@[a-z0-9][a-z0-9-]*$/;
|
|
29
|
+
const DIRECTORY_SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
30
|
+
|
|
31
|
+
const fail = (opts: { message: string }): number => {
|
|
32
|
+
process.stderr.write(`vulcan add: ${opts.message}\n`);
|
|
33
|
+
|
|
34
|
+
return ExitCodes.FAILURE;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** A folder from the root, written with slashes, never climbing out or absolute. */
|
|
38
|
+
const isProjectFolder = (opts: { folder: string }): boolean =>
|
|
39
|
+
opts.folder
|
|
40
|
+
.replace(/\/+$/, '')
|
|
41
|
+
.split('/')
|
|
42
|
+
.every(segment => DIRECTORY_SEGMENT.test(segment) && segment !== '..');
|
|
43
|
+
|
|
44
|
+
const scopeOf = (opts: { name: string }): string =>
|
|
45
|
+
opts.name.startsWith('@') ? opts.name.split('/')[0] : '';
|
|
46
|
+
|
|
47
|
+
const rootScope = (opts: { manifest: Record<string, unknown> }): string =>
|
|
48
|
+
typeof opts.manifest.name === 'string' ? scopeOf({ name: opts.manifest.name }) : '';
|
|
49
|
+
|
|
50
|
+
/** Where the new library goes: `--dir`, the layout's shared folder, or the one folder the existing libraries share. */
|
|
51
|
+
const folderOf = (opts: {
|
|
52
|
+
args: string[];
|
|
53
|
+
interactive: boolean;
|
|
54
|
+
lib: string;
|
|
55
|
+
config: TResolvedConfig;
|
|
56
|
+
packages: IProjectPackage[];
|
|
57
|
+
}): { ok: true; folder: string } | { ok: false; error: string } => {
|
|
58
|
+
const given = readFlag({ args: opts.args, flag: CreateFlags.DIR });
|
|
59
|
+
const libFolders = [
|
|
60
|
+
...new Set(
|
|
61
|
+
opts.packages
|
|
62
|
+
.filter(entry => entry.kind === 'lib' && !entry.retired)
|
|
63
|
+
.map(entry => dirname(entry.path)),
|
|
64
|
+
),
|
|
65
|
+
];
|
|
66
|
+
const fallback =
|
|
67
|
+
opts.config.layout?.shared ?? (libFolders.length === 1 ? libFolders[0] : undefined);
|
|
68
|
+
let chosen = given;
|
|
69
|
+
|
|
70
|
+
if (chosen === undefined) {
|
|
71
|
+
if (opts.interactive) {
|
|
72
|
+
const question = fallback === undefined ? 'Folder' : `Folder (${fallback})`;
|
|
73
|
+
const answer = askWithPrompt({ question })?.trim() ?? '';
|
|
74
|
+
|
|
75
|
+
chosen = answer === '' ? fallback : answer;
|
|
76
|
+
} else {
|
|
77
|
+
chosen = fallback;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (chosen === undefined) {
|
|
82
|
+
return { ok: false, error: `where should ${opts.lib} go? pass ${CreateFlags.DIR} <folder>` };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const folder = chosen.replace(/^\.\//, '').replace(/\/+$/, '');
|
|
86
|
+
|
|
87
|
+
return isProjectFolder({ folder })
|
|
88
|
+
? { ok: true, folder }
|
|
89
|
+
: { ok: false, error: `${CreateFlags.DIR} '${chosen}' is not a folder inside the project` };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** The package name: `--scope`, what the packages already in the folder agree on, the project's scope, else none. */
|
|
93
|
+
const nameOf = (opts: {
|
|
94
|
+
args: string[];
|
|
95
|
+
lib: string;
|
|
96
|
+
folder: string;
|
|
97
|
+
packages: IProjectPackage[];
|
|
98
|
+
manifest: Record<string, unknown>;
|
|
99
|
+
}): { ok: true; name: string } | { ok: false; error: string } => {
|
|
100
|
+
const given = readFlag({ args: opts.args, flag: CreateFlags.SCOPE });
|
|
101
|
+
|
|
102
|
+
if (given !== undefined) {
|
|
103
|
+
return SCOPE_PATTERN.test(given)
|
|
104
|
+
? { ok: true, name: `${given}/${opts.lib}` }
|
|
105
|
+
: { ok: false, error: `${CreateFlags.SCOPE} '${given}' is not a scope like @acme` };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const siblings = opts.packages.filter(
|
|
109
|
+
entry => !entry.retired && dirname(entry.path) === opts.folder,
|
|
110
|
+
);
|
|
111
|
+
const scopes = [...new Set(siblings.map(entry => scopeOf({ name: entry.name })))].sort();
|
|
112
|
+
|
|
113
|
+
if (scopes.length > 1) {
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
error: `the packages in ${opts.folder} use more than one scope (${scopes.map(scope => scope || 'unscoped').join(', ')}) - pass ${CreateFlags.SCOPE} <@scope>`,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const scope = scopes.length === 1 ? scopes[0] : rootScope({ manifest: opts.manifest });
|
|
121
|
+
|
|
122
|
+
return { ok: true, name: scope === '' ? opts.lib : `${scope}/${opts.lib}` };
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** The sibling-name rule needs the entries the config holds, not only the folder's directories. */
|
|
126
|
+
export const addLibrary = async (opts: {
|
|
127
|
+
cwd: string;
|
|
128
|
+
args: string[];
|
|
129
|
+
interactive: boolean;
|
|
130
|
+
presetValue: string;
|
|
131
|
+
spec: IPresetSpec;
|
|
132
|
+
located: ILocatedPreset;
|
|
133
|
+
config: TResolvedConfig;
|
|
134
|
+
configFile: string;
|
|
135
|
+
configPath: string;
|
|
136
|
+
manifest: Record<string, unknown>;
|
|
137
|
+
packages: IProjectPackage[];
|
|
138
|
+
libName: string;
|
|
139
|
+
recorded: (opts: { block: IAppsBlock }) => IAppsBlock;
|
|
140
|
+
}): Promise<number> => {
|
|
141
|
+
const { cwd, config, located, libName } = opts;
|
|
142
|
+
|
|
143
|
+
if (!CreateConstants.NAME_PATTERN.test(libName)) {
|
|
144
|
+
return fail({
|
|
145
|
+
message: `'${libName}' is not a name - lowercase letters, digits and -, starting with a letter or digit`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const read = readLibPreset({ located });
|
|
150
|
+
|
|
151
|
+
if (!read.ok) {
|
|
152
|
+
return fail({ message: read.error });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const lib: TLibPreset = read.lib;
|
|
156
|
+
const placed = folderOf({
|
|
157
|
+
args: opts.args,
|
|
158
|
+
interactive: opts.interactive,
|
|
159
|
+
lib: libName,
|
|
160
|
+
config,
|
|
161
|
+
packages: opts.packages,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (!placed.ok) {
|
|
165
|
+
return fail({ message: placed.error });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { folder } = placed;
|
|
169
|
+
const named = nameOf({
|
|
170
|
+
args: opts.args,
|
|
171
|
+
lib: libName,
|
|
172
|
+
folder,
|
|
173
|
+
packages: opts.packages,
|
|
174
|
+
manifest: opts.manifest,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
if (!named.ok) {
|
|
178
|
+
return fail({ message: named.error });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const path = `${folder}/${libName}`;
|
|
182
|
+
|
|
183
|
+
if (
|
|
184
|
+
existsSync(join(cwd, path)) ||
|
|
185
|
+
opts.packages.some(entry => entry.dir === libName || entry.name === named.name)
|
|
186
|
+
) {
|
|
187
|
+
return fail({ message: `${path} already exists` });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const source = readFileSync(opts.configFile, 'utf8');
|
|
191
|
+
const parsed = parseAppsBlock({ source });
|
|
192
|
+
|
|
193
|
+
if (!parsed.ok) {
|
|
194
|
+
return fail({
|
|
195
|
+
message:
|
|
196
|
+
parsed.reason === 'missing'
|
|
197
|
+
? `the vulcan:apps block is gone from ${opts.configPath} - add the package by hand: { dir: '${libName}', name: '${named.name}', path: '${path}', kind: 'lib' }`
|
|
198
|
+
: `the vulcan:apps block in ${opts.configPath} is not the JSON vulcan wrote - add the package by hand: { dir: '${libName}', name: '${named.name}', path: '${path}', kind: 'lib' }`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const block = opts.recorded({
|
|
203
|
+
block: {
|
|
204
|
+
...parsed.block,
|
|
205
|
+
packages: [...parsed.block.packages, { dir: libName, name: named.name, path, kind: 'lib' }],
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
const replaced = replaceAppsBlock({ source, block });
|
|
209
|
+
const nextConfig =
|
|
210
|
+
replaced === undefined || replaced.includes('apps.presets')
|
|
211
|
+
? replaced
|
|
212
|
+
: replaced.replace(
|
|
213
|
+
' packages: apps.packages,\n',
|
|
214
|
+
' packages: apps.packages,\n presets: apps.presets,\n',
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
if (nextConfig === undefined) {
|
|
218
|
+
return fail({ message: `the vulcan:apps block of ${opts.configPath} cannot be rewritten` });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const project =
|
|
222
|
+
config.name ?? (typeof opts.manifest.name === 'string' ? opts.manifest.name : libName);
|
|
223
|
+
const warning = filePresetWarning({ spec: opts.spec, packageName: located.packageName });
|
|
224
|
+
|
|
225
|
+
if (warning !== undefined) {
|
|
226
|
+
process.stderr.write(`${warning}\n`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const written = copyPresetFiles({
|
|
230
|
+
located,
|
|
231
|
+
files: lib.files,
|
|
232
|
+
target: join(cwd, path),
|
|
233
|
+
tokens: { project, lib: libName, package: named.name },
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
for (const file of written) {
|
|
237
|
+
process.stderr.write(` created ${path}/${file}\n`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
writeFile({
|
|
241
|
+
root: cwd,
|
|
242
|
+
path: `${path}/package.json`,
|
|
243
|
+
content: renderLibManifest({ name: named.name, lib }),
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const nextManifest = planRootManifest({
|
|
247
|
+
manifest: opts.manifest,
|
|
248
|
+
globs: readWorkspaceGlobs({ root: cwd }),
|
|
249
|
+
folder,
|
|
250
|
+
additions: { ...lib.dependencies, ...lib.devDependencies, ...lib.peerDependencies },
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
writeFile({
|
|
254
|
+
root: cwd,
|
|
255
|
+
path: 'package.json',
|
|
256
|
+
content: `${JSON.stringify(nextManifest, null, 2)}\n`,
|
|
257
|
+
});
|
|
258
|
+
writeFile({ root: cwd, path: opts.configPath, content: nextConfig });
|
|
259
|
+
writeFile({
|
|
260
|
+
root: cwd,
|
|
261
|
+
path: `${KNOWLEDGE_DIRECTORY}/packages/${libName}.md`,
|
|
262
|
+
content: renderLibConcept({
|
|
263
|
+
name: libName,
|
|
264
|
+
path,
|
|
265
|
+
summary: lib.summary,
|
|
266
|
+
presetPackage: located.packageName,
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const indexPath = join(cwd, KNOWLEDGE_DIRECTORY, 'index.md');
|
|
271
|
+
|
|
272
|
+
writeFile({
|
|
273
|
+
root: cwd,
|
|
274
|
+
path: `${KNOWLEDGE_DIRECTORY}/index.md`,
|
|
275
|
+
content: existsSync(indexPath)
|
|
276
|
+
? addAppBullet({
|
|
277
|
+
source: readFileSync(indexPath, 'utf8'),
|
|
278
|
+
app: { name: libName, summary: lib.summary },
|
|
279
|
+
})
|
|
280
|
+
: renderKnowledgeIndex({ project, apps: [{ name: libName, summary: lib.summary }] }),
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const logPath = join(cwd, KNOWLEDGE_DIRECTORY, 'log.md');
|
|
284
|
+
const logEntry = renderLogEntry({
|
|
285
|
+
date: today(),
|
|
286
|
+
text: `added ${path} from the ${located.packageName} ${located.packageVersion} preset (lib kind)`,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
writeFile({
|
|
290
|
+
root: cwd,
|
|
291
|
+
path: `${KNOWLEDGE_DIRECTORY}/log.md`,
|
|
292
|
+
content: existsSync(logPath)
|
|
293
|
+
? `${readFileSync(logPath, 'utf8').trimEnd()}\n\n${logEntry}`
|
|
294
|
+
: `# ${project} knowledge log\n\n${logEntry}`,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
for (const step of [
|
|
298
|
+
() => runBun({ cwd, args: ['install'], label: 'bun install' }),
|
|
299
|
+
() =>
|
|
300
|
+
runBun({
|
|
301
|
+
cwd,
|
|
302
|
+
args: [join(cwd, 'node_modules', '.bin', 'vulcan'), 'sync'],
|
|
303
|
+
label: 'vulcan sync',
|
|
304
|
+
}),
|
|
305
|
+
() => runAppChecks({ root: cwd, appDirectory: join(cwd, path), checks: lib.checks }),
|
|
306
|
+
]) {
|
|
307
|
+
const code = await step();
|
|
308
|
+
|
|
309
|
+
if (code !== ExitCodes.OK) {
|
|
310
|
+
return code;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
process.stderr.write(`Done. ${path} is added.\n`);
|
|
315
|
+
|
|
316
|
+
return ExitCodes.OK;
|
|
317
|
+
};
|
|
@@ -58,6 +58,8 @@ export interface IPresetSpec {
|
|
|
58
58
|
|
|
59
59
|
export interface ILocatedPreset {
|
|
60
60
|
preset: IPreset;
|
|
61
|
+
/** The lib preset file (`vulcan.lib`), absolute and inside the package; absent when the preset makes apps only. */
|
|
62
|
+
libFile?: string;
|
|
61
63
|
/** The preset package's root directory. */
|
|
62
64
|
packageRoot: string;
|
|
63
65
|
packageName: string;
|
|
@@ -72,9 +74,23 @@ export interface IApp {
|
|
|
72
74
|
folder: string;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* A preset an app of this project came from, as `--preset` takes it back: so an agent that is asked
|
|
79
|
+
* for a new app finds the exact command. Apps only today; a preset has no kind and is an app.
|
|
80
|
+
*/
|
|
81
|
+
export interface IRecordedPreset {
|
|
82
|
+
spec: string;
|
|
83
|
+
side: TAppSide;
|
|
84
|
+
summary: string;
|
|
85
|
+
/** The summary of the preset's lib kind; present when the preset can make a library. */
|
|
86
|
+
lib?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** What `init` and `add` own in the config: the layout, the package list, the presets and the atlas sources. */
|
|
76
90
|
export interface IAppsBlock {
|
|
77
91
|
layout: { backend?: string; frontend?: string; shared?: string };
|
|
78
|
-
packages: { dir: string; name: string; path: string }[];
|
|
92
|
+
packages: { dir: string; name: string; path: string; kind?: 'lib' }[];
|
|
93
|
+
/** Absent in a block written before presets were recorded. */
|
|
94
|
+
presets?: IRecordedPreset[];
|
|
79
95
|
sources: { name: string; repo: string; ref: string }[];
|
|
80
96
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { excludePathsOf, readAgentTable } from '../agents/index.ts';
|
|
2
|
+
import { shippedSkillNames } from '../sync/exclude.ts';
|
|
3
|
+
|
|
4
|
+
/** What no image needs: dependencies, build output, git history and the agents' own folders. */
|
|
5
|
+
const BASE_PATTERNS = [
|
|
6
|
+
'**/node_modules',
|
|
7
|
+
'**/dist',
|
|
8
|
+
'**/*.tsbuildinfo',
|
|
9
|
+
'**/coverage',
|
|
10
|
+
'**/*.log',
|
|
11
|
+
'.git',
|
|
12
|
+
'.claude',
|
|
13
|
+
'.agents/plans',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The root `.dockerignore` of a new project: a Dockerfile is built from the project root, so the
|
|
18
|
+
* context must not carry dependencies, build output or git. Beside the base patterns it lists the
|
|
19
|
+
* per-machine files of every agent, the ones `sync` keeps in `.git/info/exclude`, so a build
|
|
20
|
+
* never depends on which agents a developer chose.
|
|
21
|
+
*/
|
|
22
|
+
export const renderDockerignore = (): string => {
|
|
23
|
+
const agentPaths = excludePathsOf({
|
|
24
|
+
agents: readAgentTable().map(entry => entry.id),
|
|
25
|
+
skillNames: shippedSkillNames(),
|
|
26
|
+
})
|
|
27
|
+
.flatMap(group => group.paths)
|
|
28
|
+
.map(path => path.replace(/^\//, '').replace(/\/$/, ''));
|
|
29
|
+
const patterns = [...new Set([...BASE_PATTERNS, ...agentPaths])];
|
|
30
|
+
|
|
31
|
+
return [
|
|
32
|
+
'# Written by vulcan init; edit it freely, vulcan never overwrites it. .git is excluded, so pass the commit: docker build --build-arg APP_BUILD_COMMIT="$(git rev-parse HEAD)" ...',
|
|
33
|
+
...patterns,
|
|
34
|
+
'',
|
|
35
|
+
].join('\n');
|
|
36
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
3
|
import { ExitCodes, ProjectLayout, VULCAN_VERSION } from '../common/index.ts';
|
|
4
4
|
import { renderTemplate } from '../init/index.ts';
|
|
5
5
|
import { runAppChecks } from './checks.ts';
|
|
@@ -12,6 +12,8 @@ import type {
|
|
|
12
12
|
TAppSide,
|
|
13
13
|
} from './common/index.ts';
|
|
14
14
|
import { today, writeFile } from './files.ts';
|
|
15
|
+
import { recordedPresetOf } from './lib-preset.ts';
|
|
16
|
+
import { renderDockerignore } from './dockerignore.ts';
|
|
15
17
|
import { askWithPrompt, findFolderClash, readFlag, resolveNames, SIDE_FIELDS } from './names.ts';
|
|
16
18
|
import { copyPresetFiles, locatePreset, parsePresetSpec } from './preset.ts';
|
|
17
19
|
import { renderNext } from './next.ts';
|
|
@@ -24,6 +26,7 @@ import {
|
|
|
24
26
|
renderKnowledgeIndex,
|
|
25
27
|
renderLogEntry,
|
|
26
28
|
renderRootManifest,
|
|
29
|
+
presetSpecOf,
|
|
27
30
|
renderVulcanConfig,
|
|
28
31
|
} from './workspace.ts';
|
|
29
32
|
|
|
@@ -70,12 +73,14 @@ const writeMonorepo = (opts: {
|
|
|
70
73
|
root: string;
|
|
71
74
|
project: string;
|
|
72
75
|
spec: IPresetSpec;
|
|
76
|
+
/** `--preset` as the user typed it, recorded so the project can name it again. */
|
|
77
|
+
presetValue: string;
|
|
73
78
|
located: ILocatedPreset;
|
|
74
79
|
folder: string;
|
|
75
80
|
app: string;
|
|
76
81
|
shared: string;
|
|
77
82
|
}): void => {
|
|
78
|
-
const { root, project, spec, located, folder, app, shared } = opts;
|
|
83
|
+
const { root, project, spec, presetValue, located, folder, app, shared } = opts;
|
|
79
84
|
const { preset, packageName, packageVersion } = located;
|
|
80
85
|
const side = preset.side;
|
|
81
86
|
const catalog = {
|
|
@@ -100,6 +105,7 @@ const writeMonorepo = (opts: {
|
|
|
100
105
|
content: `${[...BASE_IGNORES, ...ProjectLayout.GITIGNORE_ENTRIES].join('\n')}\n`,
|
|
101
106
|
});
|
|
102
107
|
writeFile({ root, path: 'README.md', content: `# ${project}\n` });
|
|
108
|
+
writeFile({ root, path: '.dockerignore', content: renderDockerignore() });
|
|
103
109
|
|
|
104
110
|
if (shared !== '') {
|
|
105
111
|
writeFile({ root, path: `${shared}/.gitkeep`, content: '' });
|
|
@@ -110,7 +116,13 @@ const writeMonorepo = (opts: {
|
|
|
110
116
|
const written = copyPresetFiles({
|
|
111
117
|
located,
|
|
112
118
|
target: appDirectory,
|
|
113
|
-
tokens: {
|
|
119
|
+
tokens: {
|
|
120
|
+
project,
|
|
121
|
+
app,
|
|
122
|
+
appPath: `${folder}/${app}`,
|
|
123
|
+
devPort: String(preset.devPort),
|
|
124
|
+
apiUrl: '',
|
|
125
|
+
},
|
|
114
126
|
});
|
|
115
127
|
|
|
116
128
|
for (const path of written) {
|
|
@@ -140,6 +152,12 @@ const writeMonorepo = (opts: {
|
|
|
140
152
|
const block: IAppsBlock = {
|
|
141
153
|
layout: { [side]: folder, ...(shared === '' ? {} : { shared }) },
|
|
142
154
|
packages: [{ dir: app, name: app, path: `${folder}/${app}` }],
|
|
155
|
+
presets: [
|
|
156
|
+
recordedPresetOf({
|
|
157
|
+
spec: presetSpecOf({ value: presetValue, root, cwd: dirname(root) }),
|
|
158
|
+
located,
|
|
159
|
+
}),
|
|
160
|
+
],
|
|
143
161
|
sources: preset.atlas,
|
|
144
162
|
};
|
|
145
163
|
|
|
@@ -293,7 +311,7 @@ const runCreate = async (opts: {
|
|
|
293
311
|
process.stderr.write(`${warning}\n`);
|
|
294
312
|
}
|
|
295
313
|
|
|
296
|
-
writeMonorepo({ root, project, spec, located, folder, app, shared });
|
|
314
|
+
writeMonorepo({ root, project, spec, presetValue, located, folder, app, shared });
|
|
297
315
|
|
|
298
316
|
for (const step of [
|
|
299
317
|
() => runBun({ cwd: root, args: ['install'], label: 'bun install' }),
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { CreateConstants } from './common/index.ts';
|
|
5
|
+
import type { ILocatedPreset, IRecordedPreset } from './common/index.ts';
|
|
6
|
+
|
|
7
|
+
/** `package.json` fields a lib preset may pass through; anything else it generates itself or does not support. */
|
|
8
|
+
const PACKAGE_FIELDS = ['type', 'sideEffects', 'browser', 'engines'];
|
|
9
|
+
const GENERATED_FIELDS: Record<string, string> = {
|
|
10
|
+
name: 'name',
|
|
11
|
+
version: 'version',
|
|
12
|
+
private: 'private',
|
|
13
|
+
exports: 'exports',
|
|
14
|
+
imports: 'imports',
|
|
15
|
+
scripts: 'scripts',
|
|
16
|
+
dependencies: 'dependencies',
|
|
17
|
+
devDependencies: 'devDependencies',
|
|
18
|
+
peerDependencies: 'peerDependencies',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const LIB_SCHEMA = z
|
|
22
|
+
.strictObject({
|
|
23
|
+
presetVersion: z.number(),
|
|
24
|
+
summary: z.string(),
|
|
25
|
+
dependencies: z.record(z.string(), z.string()).default({}),
|
|
26
|
+
devDependencies: z.record(z.string(), z.string()).default({}),
|
|
27
|
+
peerDependencies: z.record(z.string(), z.string()).default({}),
|
|
28
|
+
scripts: z.record(z.string(), z.string()).default({}),
|
|
29
|
+
checks: z.array(z.string()).default([]),
|
|
30
|
+
imports: z.record(z.string(), z.string()).default({}),
|
|
31
|
+
exports: z.record(z.string(), z.unknown()).default({}),
|
|
32
|
+
packageFields: z.record(z.string(), z.unknown()).default({}),
|
|
33
|
+
files: z.string(),
|
|
34
|
+
atlas: z.array(z.object({ name: z.string(), repo: z.string(), ref: z.string() })).default([]),
|
|
35
|
+
})
|
|
36
|
+
.superRefine((value, context) => {
|
|
37
|
+
value.checks.forEach((check, index) => {
|
|
38
|
+
if (!Object.hasOwn(value.scripts, check)) {
|
|
39
|
+
context.addIssue({
|
|
40
|
+
code: 'custom',
|
|
41
|
+
path: ['checks', index],
|
|
42
|
+
message: `"${check}" is not a key of scripts`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
for (const key of Object.keys(value.imports)) {
|
|
48
|
+
if (!key.startsWith('#')) {
|
|
49
|
+
context.addIssue({
|
|
50
|
+
code: 'custom',
|
|
51
|
+
path: ['imports', key],
|
|
52
|
+
message: `"${key}" is not a subpath import: a key starts with #`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const key of Object.keys(value.packageFields)) {
|
|
58
|
+
const generated = GENERATED_FIELDS[key];
|
|
59
|
+
|
|
60
|
+
if (generated !== undefined) {
|
|
61
|
+
context.addIssue({
|
|
62
|
+
code: 'custom',
|
|
63
|
+
path: ['packageFields', key],
|
|
64
|
+
message: `is generated by vulcan - declare it in ${generated} instead`,
|
|
65
|
+
});
|
|
66
|
+
} else if (!PACKAGE_FIELDS.includes(key)) {
|
|
67
|
+
context.addIssue({
|
|
68
|
+
code: 'custom',
|
|
69
|
+
path: ['packageFields', key],
|
|
70
|
+
message: `is not allowed - one of: ${PACKAGE_FIELDS.join(', ')}`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export type TLibPreset = Omit<z.infer<typeof LIB_SCHEMA>, 'files'> & {
|
|
77
|
+
/** The folder holding the files to copy, absolute. */
|
|
78
|
+
files: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type TLibPresetResult = { ok: true; lib: TLibPreset } | { ok: false; error: string };
|
|
82
|
+
|
|
83
|
+
const escapes = (opts: { root: string; path: string }): boolean => {
|
|
84
|
+
const relativePath = relative(opts.root, opts.path);
|
|
85
|
+
|
|
86
|
+
return isAbsolute(relativePath) || relativePath === '..' || relativePath.startsWith(`..${sep}`);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** The lib preset of a located preset package, validated; the app side is never touched by an error here. */
|
|
90
|
+
export const readLibPreset = (opts: { located: ILocatedPreset }): TLibPresetResult => {
|
|
91
|
+
const { located } = opts;
|
|
92
|
+
const shown = `${located.packageName}@${located.packageVersion}`;
|
|
93
|
+
const file = located.libFile;
|
|
94
|
+
|
|
95
|
+
if (file === undefined) {
|
|
96
|
+
return { ok: false, error: `preset ${located.packageName} has no lib kind` };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let raw: unknown;
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
103
|
+
} catch {
|
|
104
|
+
return { ok: false, error: `${shown}: ${file} is missing or not JSON` };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (
|
|
108
|
+
typeof raw !== 'object' ||
|
|
109
|
+
raw === null ||
|
|
110
|
+
!('presetVersion' in raw) ||
|
|
111
|
+
raw.presetVersion !== CreateConstants.PRESET_VERSION
|
|
112
|
+
) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
error: `${shown}: the lib preset has preset version ${String(typeof raw === 'object' && raw !== null && 'presetVersion' in raw ? raw.presetVersion : undefined)}; this VULCAN reads version ${CreateConstants.PRESET_VERSION} - upgrade @venizia/vulcan`,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const parsed = LIB_SCHEMA.safeParse(raw);
|
|
120
|
+
|
|
121
|
+
if (!parsed.success) {
|
|
122
|
+
const issues = parsed.error.issues
|
|
123
|
+
.map(issue => `${issue.path.join('.')} ${issue.message}`)
|
|
124
|
+
.join('; ');
|
|
125
|
+
|
|
126
|
+
return { ok: false, error: `${shown}: lib preset: ${issues}` };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const files = resolve(dirname(file), parsed.data.files);
|
|
130
|
+
|
|
131
|
+
if (escapes({ root: located.packageRoot, path: files }) || !existsSync(files)) {
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
error: `${shown}: lib preset: files ${parsed.data.files} is missing or points outside the package`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { ok: true, lib: { ...parsed.data, files } };
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** What the preset records of itself: its side and summary, and the lib summary when it makes libraries. */
|
|
142
|
+
export const recordedPresetOf = (opts: {
|
|
143
|
+
spec: string;
|
|
144
|
+
located: ILocatedPreset;
|
|
145
|
+
}): IRecordedPreset => {
|
|
146
|
+
const lib = readLibPreset({ located: opts.located });
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
spec: opts.spec,
|
|
150
|
+
side: opts.located.preset.side,
|
|
151
|
+
summary: opts.located.preset.summary,
|
|
152
|
+
...(lib.ok ? { lib: lib.lib.summary } : {}),
|
|
153
|
+
};
|
|
154
|
+
};
|
package/src/create/preset.ts
CHANGED
|
@@ -201,7 +201,23 @@ export const locatePreset = (opts: { spec: IPresetSpec; root: string }): TPreset
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
const preset = { ...parsed.data, files: filesFolder };
|
|
204
|
-
const
|
|
204
|
+
const libField = readProperty({ value: manifest[CreateConstants.PRESET_FIELD], key: 'lib' });
|
|
205
|
+
const libFile =
|
|
206
|
+
typeof libField === 'string'
|
|
207
|
+
? resolveInside({ root: packageRoot, from: packageRoot, path: libField })
|
|
208
|
+
: undefined;
|
|
209
|
+
|
|
210
|
+
if (typeof libField === 'string' && libFile === undefined) {
|
|
211
|
+
return { ok: false, error: `${shown}: vulcan.lib ${libField} points outside the package` };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const located: ILocatedPreset = {
|
|
215
|
+
preset,
|
|
216
|
+
...(libFile === undefined ? {} : { libFile }),
|
|
217
|
+
packageRoot,
|
|
218
|
+
packageName,
|
|
219
|
+
packageVersion,
|
|
220
|
+
};
|
|
205
221
|
|
|
206
222
|
return { ok: true, located };
|
|
207
223
|
};
|
|
@@ -210,11 +226,14 @@ export const copyPresetFiles = (opts: {
|
|
|
210
226
|
located: ILocatedPreset;
|
|
211
227
|
target: string;
|
|
212
228
|
tokens: Record<string, string>;
|
|
229
|
+
/** The folder to copy when it is not the app's: a lib preset's own. */
|
|
230
|
+
files?: string;
|
|
213
231
|
}): string[] => {
|
|
214
232
|
const written: string[] = [];
|
|
233
|
+
const files = opts.files ?? opts.located.preset.files;
|
|
215
234
|
|
|
216
|
-
for (const path of new Glob('**/*').scanSync({ cwd:
|
|
217
|
-
const source = join(
|
|
235
|
+
for (const path of new Glob('**/*').scanSync({ cwd: files, dot: true })) {
|
|
236
|
+
const source = join(files, path);
|
|
218
237
|
|
|
219
238
|
// A link could point at any file on the machine; only real files belong to the preset.
|
|
220
239
|
if (lstatSync(source).isSymbolicLink()) {
|