@codelitdev/platform-cli 0.1.0-alpha.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/LICENSE +21 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +43 -0
- package/dist/codemod-readme-title.d.ts +5 -0
- package/dist/codemod-readme-title.js +21 -0
- package/dist/create.d.ts +6 -0
- package/dist/create.js +124 -0
- package/dist/doctor.d.ts +12 -0
- package/dist/doctor.js +111 -0
- package/dist/fs.d.ts +5 -0
- package/dist/fs.js +42 -0
- package/dist/git.d.ts +1 -0
- package/dist/git.js +19 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/manifest.d.ts +12 -0
- package/dist/manifest.js +31 -0
- package/dist/template.d.ts +2 -0
- package/dist/template.js +23 -0
- package/dist/upgrade.d.ts +13 -0
- package/dist/upgrade.js +97 -0
- package/package.json +41 -0
- package/template/.github/workflows/platform-conformance.yml +28 -0
- package/template/README.md +5 -0
- package/template/apps/api/.env.example +18 -0
- package/template/apps/api/README.md +3 -0
- package/template/apps/api/billing.config.ts +25 -0
- package/template/apps/api/drizzle.config.ts +7 -0
- package/template/apps/api/package.json +53 -0
- package/template/apps/api/scripts/generate-auth-schema.mjs +173 -0
- package/template/apps/api/scripts/migrate.mjs +15 -0
- package/template/apps/api/src/app.test.ts +479 -0
- package/template/apps/api/src/auth/api-keys.ts +131 -0
- package/template/apps/api/src/auth/authenticate.ts +166 -0
- package/template/apps/api/src/auth/better-auth.ts +38 -0
- package/template/apps/api/src/auth/oauth.ts +9 -0
- package/template/apps/api/src/auth/options.ts +69 -0
- package/template/apps/api/src/billing.ts +58 -0
- package/template/apps/api/src/conformance-adapter.ts +335 -0
- package/template/apps/api/src/db/migrate.ts +26 -0
- package/template/apps/api/src/db/migrations/0000_application.sql +72 -0
- package/template/apps/api/src/db/migrations/0000_auth.sql +178 -0
- package/template/apps/api/src/db/migrations/0001_billing.sql +234 -0
- package/template/apps/api/src/db/schema/auth.generated.ts +185 -0
- package/template/apps/api/src/db/schema/billing.generated.ts +447 -0
- package/template/apps/api/src/db/schema/index.ts +3 -0
- package/template/apps/api/src/db/schema/notes.ts +31 -0
- package/template/apps/api/src/db/schema/tenants.ts +83 -0
- package/template/apps/api/src/deps.ts +12 -0
- package/template/apps/api/src/dispatch.ts +476 -0
- package/template/apps/api/src/express-app.ts +161 -0
- package/template/apps/api/src/invitations.ts +217 -0
- package/template/apps/api/src/logger.ts +26 -0
- package/template/apps/api/src/main.ts +98 -0
- package/template/apps/api/src/mcp.ts +127 -0
- package/template/apps/api/src/notes.ts +251 -0
- package/template/apps/api/src/openapi.ts +12 -0
- package/template/apps/api/src/permissions.ts +35 -0
- package/template/apps/api/src/runtime.ts +167 -0
- package/template/apps/api/src/seed.ts +340 -0
- package/template/apps/api/src/tenancy.ts +116 -0
- package/template/apps/api/src/types.ts +20 -0
- package/template/apps/api/src/worker.ts +57 -0
- package/template/apps/api/tsconfig.json +14 -0
- package/template/apps/web/.env.example +7 -0
- package/template/apps/web/app/api/[...path]/route.ts +51 -0
- package/template/apps/web/app/globals.css +156 -0
- package/template/apps/web/app/layout.tsx +34 -0
- package/template/apps/web/app/login/page.tsx +127 -0
- package/template/apps/web/app/notes/page.tsx +93 -0
- package/template/apps/web/app/page.tsx +80 -0
- package/template/apps/web/components/auth-gate.tsx +34 -0
- package/template/apps/web/components/browser-observability.tsx +53 -0
- package/template/apps/web/components/tenant-switcher.tsx +51 -0
- package/template/apps/web/lib/observability-config.ts +15 -0
- package/template/apps/web/next-env.d.ts +2 -0
- package/template/apps/web/next.config.ts +7 -0
- package/template/apps/web/package.json +26 -0
- package/template/apps/web/tsconfig.json +21 -0
- package/template/biome.template.json +50 -0
- package/template/docker-compose.yml +14 -0
- package/template/package.json +40 -0
- package/template/packages/api-contract/package.json +23 -0
- package/template/packages/api-contract/src/index.ts +256 -0
- package/template/packages/api-contract/src/mcp-parity.ts +158 -0
- package/template/packages/api-contract/tsconfig.json +11 -0
- package/template/patches/@anatine__zod-openapi@1.14.2.patch +14 -0
- package/template/tooling/platform/config.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CodeLit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createProduct } from "./create.js";
|
|
3
|
+
import { doctor } from "./doctor.js";
|
|
4
|
+
import { upgradeProduct } from "./upgrade.js";
|
|
5
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
6
|
+
try {
|
|
7
|
+
if (command === "create") {
|
|
8
|
+
const name = rest[0];
|
|
9
|
+
if (!name)
|
|
10
|
+
throw new Error("usage: codelit-platform create <dir>");
|
|
11
|
+
const result = createProduct({
|
|
12
|
+
targetDir: name,
|
|
13
|
+
productName: name,
|
|
14
|
+
});
|
|
15
|
+
process.stdout.write(`created ${result.root}\n`);
|
|
16
|
+
}
|
|
17
|
+
else if (command === "doctor") {
|
|
18
|
+
const report = doctor(process.cwd());
|
|
19
|
+
process.stdout.write(`${JSON.stringify(report)}\n`);
|
|
20
|
+
if (!report.ok)
|
|
21
|
+
process.exitCode = 1;
|
|
22
|
+
}
|
|
23
|
+
else if (command === "upgrade") {
|
|
24
|
+
const dryRun = rest.includes("--dry-run");
|
|
25
|
+
const version = rest.find((arg) => !arg.startsWith("--"));
|
|
26
|
+
const result = upgradeProduct({
|
|
27
|
+
root: process.cwd(),
|
|
28
|
+
version,
|
|
29
|
+
dryRun,
|
|
30
|
+
});
|
|
31
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
32
|
+
if (result.conflicts.length > 0)
|
|
33
|
+
process.exitCode = 1;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
process.stderr.write("usage: codelit-platform create|doctor|upgrade\n");
|
|
37
|
+
process.exitCode = 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
42
|
+
process.exitCode = 1;
|
|
43
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const TARGET = path.join("apps", "api", "README.md");
|
|
4
|
+
export const README_TITLE_CODEMOD = "set-api-readme-title";
|
|
5
|
+
export function applyReadmeTitleCodemod(root, productName) {
|
|
6
|
+
const file = path.join(root, TARGET);
|
|
7
|
+
if (!existsSync(file))
|
|
8
|
+
throw new Error("codemod_input_unsupported");
|
|
9
|
+
const current = readFileSync(file, "utf8");
|
|
10
|
+
const nextTitle = `# ${productName} API`;
|
|
11
|
+
const lines = current.split("\n");
|
|
12
|
+
if (lines[0] === nextTitle) {
|
|
13
|
+
return { changed: false, path: TARGET };
|
|
14
|
+
}
|
|
15
|
+
if (!lines[0]?.startsWith("# ")) {
|
|
16
|
+
throw new Error("codemod_input_unsupported");
|
|
17
|
+
}
|
|
18
|
+
lines[0] = nextTitle;
|
|
19
|
+
writeFileSync(file, lines.join("\n"));
|
|
20
|
+
return { changed: true, path: TARGET };
|
|
21
|
+
}
|
package/dist/create.d.ts
ADDED
package/dist/create.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { loadPresetManifest } from "@codelitdev/platform-preset";
|
|
4
|
+
import { copyTemplate, isEmptyDir, listFiles, removeDir, replaceTokens } from "./fs.js";
|
|
5
|
+
import { sha256File } from "./hash.js";
|
|
6
|
+
import { writeManifest } from "./manifest.js";
|
|
7
|
+
import { resolveTemplateDir, slugify } from "./template.js";
|
|
8
|
+
export function createProduct(input) {
|
|
9
|
+
const target = path.resolve(input.targetDir);
|
|
10
|
+
if (existsSync(target) && !isEmptyDir(target)) {
|
|
11
|
+
throw new Error("create_target_not_empty");
|
|
12
|
+
}
|
|
13
|
+
const slug = slugify(input.productName);
|
|
14
|
+
const staging = `${target}.staging-${process.pid}`;
|
|
15
|
+
removeDir(staging);
|
|
16
|
+
try {
|
|
17
|
+
copyTemplate(resolveTemplateDir(), staging);
|
|
18
|
+
renameSync(path.join(staging, "biome.template.json"), path.join(staging, "biome.json"));
|
|
19
|
+
replaceTokens(staging, {
|
|
20
|
+
__PRODUCT_NAME__: input.productName,
|
|
21
|
+
__PRODUCT_SLUG__: slug,
|
|
22
|
+
});
|
|
23
|
+
pinCodelitVersions(staging, loadPresetManifest());
|
|
24
|
+
const managedRel = path.join("tooling", "platform", "config.ts");
|
|
25
|
+
const managedPath = path.join(staging, managedRel);
|
|
26
|
+
const workflowRel = path.join(".github", "workflows", "platform-conformance.yml");
|
|
27
|
+
const workflowPath = path.join(staging, workflowRel);
|
|
28
|
+
const preset = loadPresetManifest();
|
|
29
|
+
writeManifest(staging, {
|
|
30
|
+
schemaVersion: 1,
|
|
31
|
+
templateVersion: "1.0.0",
|
|
32
|
+
presetVersion: preset.presetVersion,
|
|
33
|
+
capabilities: ["auth", "mcp", "observability", "billing"],
|
|
34
|
+
managedFiles: {
|
|
35
|
+
[managedRel.replaceAll("\\", "/")]: sha256File(managedPath),
|
|
36
|
+
[workflowRel.replaceAll("\\", "/")]: sha256File(workflowPath),
|
|
37
|
+
},
|
|
38
|
+
productOwnedGlobs: ["apps/**", "packages/api-contract/**"],
|
|
39
|
+
appliedUpgrades: ["1.0.0"],
|
|
40
|
+
});
|
|
41
|
+
validateGeneratedMetadata(staging);
|
|
42
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
43
|
+
if (existsSync(target))
|
|
44
|
+
removeDir(target);
|
|
45
|
+
renameSync(staging, target);
|
|
46
|
+
return { root: target };
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
removeDir(staging);
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function validateGeneratedMetadata(root) {
|
|
54
|
+
const files = listFiles(root);
|
|
55
|
+
const required = [
|
|
56
|
+
"package.json",
|
|
57
|
+
"biome.json",
|
|
58
|
+
"apps/api/package.json",
|
|
59
|
+
"apps/web/package.json",
|
|
60
|
+
"packages/api-contract/package.json",
|
|
61
|
+
"apps/api/README.md",
|
|
62
|
+
".github/workflows/platform-conformance.yml",
|
|
63
|
+
];
|
|
64
|
+
if (required.some((file) => !files.includes(file))) {
|
|
65
|
+
throw new Error("create_metadata_invalid");
|
|
66
|
+
}
|
|
67
|
+
for (const file of files) {
|
|
68
|
+
if (!file.endsWith(".json") &&
|
|
69
|
+
!file.endsWith(".ts") &&
|
|
70
|
+
!file.endsWith(".tsx") &&
|
|
71
|
+
!file.endsWith(".md") &&
|
|
72
|
+
!file.endsWith(".yml"))
|
|
73
|
+
continue;
|
|
74
|
+
const content = readFileSync(path.join(root, file), "utf8");
|
|
75
|
+
if (content.includes("__PRODUCT_"))
|
|
76
|
+
throw new Error("create_tokens_unresolved");
|
|
77
|
+
}
|
|
78
|
+
const packageJson = JSON.parse(readFileSync(path.join(root, "package.json"), "utf8"));
|
|
79
|
+
if (packageJson.packageManager !== "bun@1.4.1") {
|
|
80
|
+
throw new Error("create_metadata_invalid");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function pinCodelitVersions(root, preset) {
|
|
84
|
+
for (const rel of listFiles(root)) {
|
|
85
|
+
if (!rel.endsWith("package.json"))
|
|
86
|
+
continue;
|
|
87
|
+
const full = path.join(root, rel);
|
|
88
|
+
const pkg = JSON.parse(readFileSync(full, "utf8"));
|
|
89
|
+
let changed = false;
|
|
90
|
+
for (const field of ["dependencies", "devDependencies"]) {
|
|
91
|
+
const deps = pkg[field];
|
|
92
|
+
if (!deps)
|
|
93
|
+
continue;
|
|
94
|
+
for (const [name, spec] of Object.entries(deps)) {
|
|
95
|
+
const packagePin = preset.packages[name];
|
|
96
|
+
const externalPin = preset.external[name];
|
|
97
|
+
if (packagePin && spec.startsWith("workspace:")) {
|
|
98
|
+
deps[name] = packagePin.recommended;
|
|
99
|
+
changed = true;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (externalPin) {
|
|
103
|
+
const next = externalPin;
|
|
104
|
+
if (spec === next)
|
|
105
|
+
continue;
|
|
106
|
+
deps[name] = next;
|
|
107
|
+
changed = true;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (!name.startsWith("@codelitdev/") || !spec.startsWith("workspace:")) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const pin = packagePin;
|
|
114
|
+
const next = pin?.recommended ?? spec.replace(/^workspace:/, "");
|
|
115
|
+
if (next === "*" || next.startsWith("workspace:"))
|
|
116
|
+
continue;
|
|
117
|
+
deps[name] = next;
|
|
118
|
+
changed = true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (changed)
|
|
122
|
+
writeFileSync(full, `${JSON.stringify(pkg, null, 4)}\n`);
|
|
123
|
+
}
|
|
124
|
+
}
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type DoctorReport = {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
issues: string[];
|
|
4
|
+
};
|
|
5
|
+
export declare function doctor(root: string): DoctorReport;
|
|
6
|
+
/**
|
|
7
|
+
* Read package resolutions from Bun's lockfile without requiring a parser at
|
|
8
|
+
* CLI runtime. Package manifests describe intent; this catches a stale
|
|
9
|
+
* lockfile that would actually install an unsupported package.
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveLockfileVersions(root: string, managedNames: ReadonlySet<string>): Record<string, string>;
|
|
12
|
+
export declare function resolveCodelitVersions(root: string, managedNames?: readonly string[]): Record<string, string>;
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { loadPresetManifest, validateResolvedVersions, } from "@codelitdev/platform-preset";
|
|
4
|
+
import { sha256File } from "./hash.js";
|
|
5
|
+
import { readManifest } from "./manifest.js";
|
|
6
|
+
export function doctor(root) {
|
|
7
|
+
const issues = [];
|
|
8
|
+
try {
|
|
9
|
+
const manifest = readManifest(root);
|
|
10
|
+
const preset = loadPresetManifest();
|
|
11
|
+
for (const [rel, expected] of Object.entries(manifest.managedFiles)) {
|
|
12
|
+
const file = path.join(root, rel);
|
|
13
|
+
if (!existsSync(file)) {
|
|
14
|
+
issues.push(`managed_file_missing:${rel}`);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const actual = sha256File(file);
|
|
18
|
+
if (actual !== expected) {
|
|
19
|
+
issues.push(`managed_hash_mismatch:${rel}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const resolved = resolveCodelitVersions(root, [
|
|
23
|
+
...Object.keys(preset.packages),
|
|
24
|
+
...Object.keys(preset.external),
|
|
25
|
+
]);
|
|
26
|
+
Object.assign(resolved, resolveLockfileVersions(root, new Set(Object.keys(preset.packages))));
|
|
27
|
+
issues.push(...validateResolvedVersions(resolved, preset).map((issue) => `${issue.reason}:${issue.packageName}`));
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
issues.push(error instanceof Error ? error.message : String(error));
|
|
31
|
+
}
|
|
32
|
+
return { ok: issues.length === 0, issues };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Read package resolutions from Bun's lockfile without requiring a parser at
|
|
36
|
+
* CLI runtime. Package manifests describe intent; this catches a stale
|
|
37
|
+
* lockfile that would actually install an unsupported package.
|
|
38
|
+
*/
|
|
39
|
+
export function resolveLockfileVersions(root, managedNames) {
|
|
40
|
+
const lockfile = path.join(root, "bun.lock");
|
|
41
|
+
if (!existsSync(lockfile))
|
|
42
|
+
return {};
|
|
43
|
+
const resolved = {};
|
|
44
|
+
const packagesSection = readFileSync(lockfile, "utf8").split(/\n\s{2}"packages":\s*\{/)[1];
|
|
45
|
+
if (!packagesSection)
|
|
46
|
+
return resolved;
|
|
47
|
+
const entryPattern = /^\s{4}"([^"\\]*(?:\\.[^"\\]*)*)": \["[^"\n]*@(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)/gm;
|
|
48
|
+
for (const match of packagesSection.matchAll(entryPattern)) {
|
|
49
|
+
const name = match[1];
|
|
50
|
+
const version = match[2];
|
|
51
|
+
if (name && version && managedNames.has(name))
|
|
52
|
+
resolved[name] = version;
|
|
53
|
+
}
|
|
54
|
+
return resolved;
|
|
55
|
+
}
|
|
56
|
+
function collectPackageJsonFiles(root) {
|
|
57
|
+
const files = [];
|
|
58
|
+
function walk(dir) {
|
|
59
|
+
for (const entry of readdirSync(dir)) {
|
|
60
|
+
if (entry === "node_modules" || entry === ".git" || entry === "dist") {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const full = path.join(dir, entry);
|
|
64
|
+
if (statSync(full).isDirectory()) {
|
|
65
|
+
walk(full);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (entry === "package.json")
|
|
69
|
+
files.push(full);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (existsSync(root))
|
|
73
|
+
walk(root);
|
|
74
|
+
return files;
|
|
75
|
+
}
|
|
76
|
+
export function resolveCodelitVersions(root, managedNames) {
|
|
77
|
+
const resolved = {};
|
|
78
|
+
const names = managedNames ? new Set(managedNames) : undefined;
|
|
79
|
+
for (const pkgPath of collectPackageJsonFiles(root)) {
|
|
80
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
81
|
+
const specs = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
82
|
+
for (const [name, spec] of Object.entries(specs)) {
|
|
83
|
+
if (!name.startsWith("@codelitdev/") && !names?.has(name))
|
|
84
|
+
continue;
|
|
85
|
+
const installed = findInstalledPackageJson(path.dirname(pkgPath), root, name);
|
|
86
|
+
if (installed) {
|
|
87
|
+
const version = JSON.parse(readFileSync(installed, "utf8")).version;
|
|
88
|
+
resolved[name] = version;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (spec.startsWith("workspace:"))
|
|
92
|
+
continue;
|
|
93
|
+
resolved[name] = spec.replace(/^[\^~]/, "");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return resolved;
|
|
97
|
+
}
|
|
98
|
+
function findInstalledPackageJson(packageDir, root, name) {
|
|
99
|
+
let current = packageDir;
|
|
100
|
+
while (true) {
|
|
101
|
+
const candidate = path.join(current, "node_modules", ...name.split("/"), "package.json");
|
|
102
|
+
if (existsSync(candidate))
|
|
103
|
+
return candidate;
|
|
104
|
+
if (current === root)
|
|
105
|
+
return undefined;
|
|
106
|
+
const parent = path.dirname(current);
|
|
107
|
+
if (parent === current || !parent.startsWith(root))
|
|
108
|
+
return undefined;
|
|
109
|
+
current = parent;
|
|
110
|
+
}
|
|
111
|
+
}
|
package/dist/fs.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function isEmptyDir(dir: string): boolean;
|
|
2
|
+
export declare function listFiles(root: string): string[];
|
|
3
|
+
export declare function replaceTokens(root: string, tokens: Record<string, string>): void;
|
|
4
|
+
export declare function copyTemplate(from: string, to: string): void;
|
|
5
|
+
export declare function removeDir(dir: string): void;
|
package/dist/fs.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function isEmptyDir(dir) {
|
|
4
|
+
if (!existsSync(dir))
|
|
5
|
+
return true;
|
|
6
|
+
return readdirSync(dir).length === 0;
|
|
7
|
+
}
|
|
8
|
+
export function listFiles(root) {
|
|
9
|
+
const out = [];
|
|
10
|
+
function walk(dir) {
|
|
11
|
+
for (const entry of readdirSync(dir)) {
|
|
12
|
+
if (entry === "node_modules" || entry === ".git")
|
|
13
|
+
continue;
|
|
14
|
+
const full = path.join(dir, entry);
|
|
15
|
+
if (statSync(full).isDirectory())
|
|
16
|
+
walk(full);
|
|
17
|
+
else
|
|
18
|
+
out.push(path.relative(root, full));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
walk(root);
|
|
22
|
+
return out.sort();
|
|
23
|
+
}
|
|
24
|
+
export function replaceTokens(root, tokens) {
|
|
25
|
+
for (const rel of listFiles(root)) {
|
|
26
|
+
const full = path.join(root, rel);
|
|
27
|
+
const text = readFileSync(full, "utf8");
|
|
28
|
+
let next = text;
|
|
29
|
+
for (const [token, value] of Object.entries(tokens)) {
|
|
30
|
+
next = next.split(token).join(value);
|
|
31
|
+
}
|
|
32
|
+
if (next !== text)
|
|
33
|
+
writeFileSync(full, next);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function copyTemplate(from, to) {
|
|
37
|
+
mkdirSync(path.dirname(to), { recursive: true });
|
|
38
|
+
cpSync(from, to, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
export function removeDir(dir) {
|
|
41
|
+
rmSync(dir, { recursive: true, force: true });
|
|
42
|
+
}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function assertGitClean(cwd: string): void;
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
export function assertGitClean(cwd) {
|
|
3
|
+
try {
|
|
4
|
+
execFileSync("git", ["rev-parse", "--is-inside-work-tree"], {
|
|
5
|
+
cwd,
|
|
6
|
+
stdio: "pipe",
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
throw new Error("upgrade_requires_git_repository");
|
|
11
|
+
}
|
|
12
|
+
const status = execFileSync("git", ["status", "--porcelain"], {
|
|
13
|
+
cwd,
|
|
14
|
+
encoding: "utf8",
|
|
15
|
+
});
|
|
16
|
+
if (status.trim().length > 0) {
|
|
17
|
+
throw new Error("upgrade_requires_clean_worktree");
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/hash.d.ts
ADDED
package/dist/hash.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
export function sha256File(filePath) {
|
|
4
|
+
return createHash("sha256").update(readFileSync(filePath)).digest("hex");
|
|
5
|
+
}
|
|
6
|
+
export function sha256Text(value) {
|
|
7
|
+
return createHash("sha256").update(value).digest("hex");
|
|
8
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type PlatformManifest = {
|
|
2
|
+
schemaVersion: 1;
|
|
3
|
+
templateVersion: string;
|
|
4
|
+
presetVersion: string;
|
|
5
|
+
capabilities: string[];
|
|
6
|
+
managedFiles: Record<string, string>;
|
|
7
|
+
productOwnedGlobs: string[];
|
|
8
|
+
appliedUpgrades: string[];
|
|
9
|
+
};
|
|
10
|
+
export declare const MANIFEST_NAME = ".codelit-platform.json";
|
|
11
|
+
export declare function readManifest(root: string): PlatformManifest;
|
|
12
|
+
export declare function writeManifest(root: string, manifest: PlatformManifest): void;
|
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export const MANIFEST_NAME = ".codelit-platform.json";
|
|
4
|
+
export function readManifest(root) {
|
|
5
|
+
const file = path.join(root, MANIFEST_NAME);
|
|
6
|
+
if (!existsSync(file))
|
|
7
|
+
throw new Error("platform_manifest_missing");
|
|
8
|
+
const manifest = JSON.parse(readFileSync(file, "utf8"));
|
|
9
|
+
if (manifest.schemaVersion !== 1 ||
|
|
10
|
+
typeof manifest.templateVersion !== "string" ||
|
|
11
|
+
typeof manifest.presetVersion !== "string" ||
|
|
12
|
+
!Array.isArray(manifest.capabilities) ||
|
|
13
|
+
!Array.isArray(manifest.productOwnedGlobs) ||
|
|
14
|
+
!Array.isArray(manifest.appliedUpgrades) ||
|
|
15
|
+
!manifest.managedFiles ||
|
|
16
|
+
typeof manifest.managedFiles !== "object") {
|
|
17
|
+
throw new Error("platform_manifest_invalid");
|
|
18
|
+
}
|
|
19
|
+
for (const [rel, hash] of Object.entries(manifest.managedFiles)) {
|
|
20
|
+
const resolved = path.resolve(root, rel);
|
|
21
|
+
if (path.isAbsolute(rel) ||
|
|
22
|
+
(resolved !== root && !resolved.startsWith(`${root}${path.sep}`)) ||
|
|
23
|
+
!/^[a-f0-9]{64}$/i.test(hash)) {
|
|
24
|
+
throw new Error("platform_manifest_invalid");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return manifest;
|
|
28
|
+
}
|
|
29
|
+
export function writeManifest(root, manifest) {
|
|
30
|
+
writeFileSync(path.join(root, MANIFEST_NAME), `${JSON.stringify(manifest, null, 4)}\n`);
|
|
31
|
+
}
|
package/dist/template.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
export function resolveTemplateDir() {
|
|
5
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const packed = path.join(here, "..", "template");
|
|
7
|
+
const workspace = path.resolve(here, "../../../templates/saas-product");
|
|
8
|
+
if (existsSync(path.join(workspace, "package.json")))
|
|
9
|
+
return workspace;
|
|
10
|
+
if (existsSync(path.join(packed, "package.json")))
|
|
11
|
+
return packed;
|
|
12
|
+
throw new Error("template_missing");
|
|
13
|
+
}
|
|
14
|
+
export function slugify(name) {
|
|
15
|
+
const slug = name
|
|
16
|
+
.trim()
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
19
|
+
.replace(/^-|-$/g, "");
|
|
20
|
+
if (!slug)
|
|
21
|
+
throw new Error("product_name_invalid");
|
|
22
|
+
return slug;
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type UpgradeResult = {
|
|
2
|
+
dryRun: boolean;
|
|
3
|
+
changed: string[];
|
|
4
|
+
conflicts: string[];
|
|
5
|
+
restored?: string[];
|
|
6
|
+
};
|
|
7
|
+
export declare function upgradeProduct(input: {
|
|
8
|
+
root: string;
|
|
9
|
+
version?: string;
|
|
10
|
+
dryRun?: boolean;
|
|
11
|
+
allowDirty?: boolean;
|
|
12
|
+
failAfterWrite?: boolean;
|
|
13
|
+
}): UpgradeResult;
|
package/dist/upgrade.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { assertGitClean } from "./git.js";
|
|
4
|
+
import { sha256File } from "./hash.js";
|
|
5
|
+
import { readManifest, writeManifest } from "./manifest.js";
|
|
6
|
+
export function upgradeProduct(input) {
|
|
7
|
+
const root = path.resolve(input.root);
|
|
8
|
+
if (!input.dryRun && !input.allowDirty) {
|
|
9
|
+
assertGitClean(root);
|
|
10
|
+
}
|
|
11
|
+
const manifest = readManifest(root);
|
|
12
|
+
const changed = [];
|
|
13
|
+
const conflicts = [];
|
|
14
|
+
const plannedWrites = new Map();
|
|
15
|
+
const upgradeVersion = input.version ?? "1.1.0";
|
|
16
|
+
// Compute and validate the complete plan before touching any file. A
|
|
17
|
+
// managed hash mismatch is a conflict, not a best-effort warning.
|
|
18
|
+
for (const [rel, expected] of Object.entries(manifest.managedFiles)) {
|
|
19
|
+
const current = path.join(root, rel);
|
|
20
|
+
if (!existsSync(current) || sha256File(current) !== expected) {
|
|
21
|
+
conflicts.push(rel);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
// Managed files are not automatically source files. A version marker is
|
|
25
|
+
// intentionally owned by the managed TypeScript config only; appending it
|
|
26
|
+
// to every managed file corrupts assets such as GitHub Actions YAML.
|
|
27
|
+
if (rel !== "tooling/platform/config.ts")
|
|
28
|
+
continue;
|
|
29
|
+
const currentText = readFileSync(current, "utf8");
|
|
30
|
+
const marker = /export const platformTemplateVersion = "[^"]*";\n?/;
|
|
31
|
+
const nextText = marker.test(currentText)
|
|
32
|
+
? currentText.replace(marker, `export const platformTemplateVersion = "${upgradeVersion}";\n`)
|
|
33
|
+
: `${currentText.trimEnd()}\nexport const platformTemplateVersion = "${upgradeVersion}";\n`;
|
|
34
|
+
if (nextText !== currentText) {
|
|
35
|
+
plannedWrites.set(rel, nextText);
|
|
36
|
+
changed.push(rel);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const productName = JSON.parse(readFileSync(path.join(root, "package.json"), "utf8")).name ?? "product";
|
|
40
|
+
const readme = path.join(root, "apps/api/README.md");
|
|
41
|
+
if (!existsSync(readme)) {
|
|
42
|
+
throw new Error("codemod_input_unsupported");
|
|
43
|
+
}
|
|
44
|
+
const readmeText = readFileSync(readme, "utf8");
|
|
45
|
+
const expectedTitle = `# ${String(productName)} API`;
|
|
46
|
+
const readmeLines = readmeText.split("\n");
|
|
47
|
+
if (!readmeLines[0]?.startsWith("# ")) {
|
|
48
|
+
throw new Error("codemod_input_unsupported");
|
|
49
|
+
}
|
|
50
|
+
if (readmeLines[0] !== expectedTitle) {
|
|
51
|
+
changed.push("apps/api/README.md");
|
|
52
|
+
if (!input.dryRun) {
|
|
53
|
+
readmeLines[0] = expectedTitle;
|
|
54
|
+
plannedWrites.set("apps/api/README.md", readmeLines.join("\n"));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Never mutate anything if any managed file is conflicted. This also makes
|
|
58
|
+
// the result deterministic for callers that use dry-run to gate a write.
|
|
59
|
+
if (conflicts.length > 0) {
|
|
60
|
+
return { dryRun: Boolean(input.dryRun), changed: [], conflicts };
|
|
61
|
+
}
|
|
62
|
+
if (input.dryRun) {
|
|
63
|
+
return { dryRun: true, changed, conflicts };
|
|
64
|
+
}
|
|
65
|
+
const backups = new Map();
|
|
66
|
+
const manifestPath = path.join(root, ".codelit-platform.json");
|
|
67
|
+
backups.set(".codelit-platform.json", readFileSync(manifestPath, "utf8"));
|
|
68
|
+
try {
|
|
69
|
+
for (const [rel, content] of plannedWrites) {
|
|
70
|
+
const file = path.join(root, rel);
|
|
71
|
+
backups.set(rel, readFileSync(file, "utf8"));
|
|
72
|
+
writeFileSync(file, content);
|
|
73
|
+
if (rel in manifest.managedFiles) {
|
|
74
|
+
manifest.managedFiles[rel] = sha256File(file);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (input.failAfterWrite)
|
|
78
|
+
throw new Error("upgrade_forced_failure");
|
|
79
|
+
if (!manifest.appliedUpgrades.includes(upgradeVersion)) {
|
|
80
|
+
manifest.appliedUpgrades.push(upgradeVersion);
|
|
81
|
+
}
|
|
82
|
+
manifest.templateVersion = upgradeVersion;
|
|
83
|
+
writeManifest(root, manifest);
|
|
84
|
+
return { dryRun: false, changed, conflicts };
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
const restored = [];
|
|
88
|
+
for (const [rel, content] of backups) {
|
|
89
|
+
writeFileSync(path.join(root, rel), content);
|
|
90
|
+
restored.push(rel);
|
|
91
|
+
}
|
|
92
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
93
|
+
throw new Error(`${message}; upgrade_rolled_back:${restored.join(",")}`, {
|
|
94
|
+
cause: error,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|