swallowkit 1.0.0-beta.3 → 1.0.0-beta.31
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 -21
- package/README.ja.md +353 -215
- package/README.md +406 -216
- package/dist/__tests__/fixtures.d.ts +22 -0
- package/dist/__tests__/fixtures.d.ts.map +1 -0
- package/dist/__tests__/fixtures.js +146 -0
- package/dist/__tests__/fixtures.js.map +1 -0
- package/dist/cli/commands/add-auth.d.ts +10 -0
- package/dist/cli/commands/add-auth.d.ts.map +1 -0
- package/dist/cli/commands/add-auth.js +444 -0
- package/dist/cli/commands/add-auth.js.map +1 -0
- package/dist/cli/commands/add-connector.d.ts +20 -0
- package/dist/cli/commands/add-connector.d.ts.map +1 -0
- package/dist/cli/commands/add-connector.js +163 -0
- package/dist/cli/commands/add-connector.js.map +1 -0
- package/dist/cli/commands/create-model.d.ts +1 -4
- package/dist/cli/commands/create-model.d.ts.map +1 -1
- package/dist/cli/commands/create-model.js +21 -82
- package/dist/cli/commands/create-model.js.map +1 -1
- package/dist/cli/commands/dev-seeds.d.ts +57 -0
- package/dist/cli/commands/dev-seeds.d.ts.map +1 -0
- package/dist/cli/commands/dev-seeds.js +470 -0
- package/dist/cli/commands/dev-seeds.js.map +1 -0
- package/dist/cli/commands/dev.d.ts +33 -0
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/dev.js +628 -146
- package/dist/cli/commands/dev.js.map +1 -1
- package/dist/cli/commands/index.d.ts +1 -0
- package/dist/cli/commands/index.d.ts.map +1 -1
- package/dist/cli/commands/index.js +3 -1
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/init.d.ts +15 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +2696 -1706
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/scaffold.d.ts.map +1 -1
- package/dist/cli/commands/scaffold.js +448 -129
- package/dist/cli/commands/scaffold.js.map +1 -1
- package/dist/cli/index.d.ts +5 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +200 -42
- package/dist/cli/index.js.map +1 -1
- package/dist/core/config.d.ts +8 -2
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +94 -5
- package/dist/core/config.js.map +1 -1
- package/dist/core/mock/connector-mock-server.d.ts +101 -0
- package/dist/core/mock/connector-mock-server.d.ts.map +1 -0
- package/dist/core/mock/connector-mock-server.js +480 -0
- package/dist/core/mock/connector-mock-server.js.map +1 -0
- package/dist/core/mock/zod-mock-generator.d.ts +14 -0
- package/dist/core/mock/zod-mock-generator.d.ts.map +1 -0
- package/dist/core/mock/zod-mock-generator.js +163 -0
- package/dist/core/mock/zod-mock-generator.js.map +1 -0
- package/dist/core/operations/create-model.d.ts +15 -0
- package/dist/core/operations/create-model.d.ts.map +1 -0
- package/dist/core/operations/create-model.js +171 -0
- package/dist/core/operations/create-model.js.map +1 -0
- package/dist/core/operations/runtime.d.ts +32 -0
- package/dist/core/operations/runtime.d.ts.map +1 -0
- package/dist/core/operations/runtime.js +225 -0
- package/dist/core/operations/runtime.js.map +1 -0
- package/dist/core/operations/scaffold-machine.d.ts +16 -0
- package/dist/core/operations/scaffold-machine.d.ts.map +1 -0
- package/dist/core/operations/scaffold-machine.js +63 -0
- package/dist/core/operations/scaffold-machine.js.map +1 -0
- package/dist/core/project/manifest.d.ts +92 -0
- package/dist/core/project/manifest.d.ts.map +1 -0
- package/dist/core/project/manifest.js +321 -0
- package/dist/core/project/manifest.js.map +1 -0
- package/dist/core/project/validation.d.ts +20 -0
- package/dist/core/project/validation.d.ts.map +1 -0
- package/dist/core/project/validation.js +209 -0
- package/dist/core/project/validation.js.map +1 -0
- package/dist/core/scaffold/auth-generator.d.ts +38 -0
- package/dist/core/scaffold/auth-generator.d.ts.map +1 -0
- package/dist/core/scaffold/auth-generator.js +1244 -0
- package/dist/core/scaffold/auth-generator.js.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts +41 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.js +1027 -0
- package/dist/core/scaffold/connector-functions-generator.js.map +1 -0
- package/dist/core/scaffold/functions-generator.d.ts +7 -1
- package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
- package/dist/core/scaffold/functions-generator.js +920 -213
- package/dist/core/scaffold/functions-generator.js.map +1 -1
- package/dist/core/scaffold/model-parser.d.ts +20 -1
- package/dist/core/scaffold/model-parser.d.ts.map +1 -1
- package/dist/core/scaffold/model-parser.js +328 -135
- package/dist/core/scaffold/model-parser.js.map +1 -1
- package/dist/core/scaffold/native-schema-generator.d.ts +13 -0
- package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
- package/dist/core/scaffold/native-schema-generator.js +677 -0
- package/dist/core/scaffold/native-schema-generator.js.map +1 -0
- package/dist/core/scaffold/nextjs-generator.d.ts +8 -0
- package/dist/core/scaffold/nextjs-generator.d.ts.map +1 -1
- package/dist/core/scaffold/nextjs-generator.js +314 -182
- package/dist/core/scaffold/nextjs-generator.js.map +1 -1
- package/dist/core/scaffold/openapi-generator.d.ts +3 -0
- package/dist/core/scaffold/openapi-generator.d.ts.map +1 -0
- package/dist/core/scaffold/openapi-generator.js +190 -0
- package/dist/core/scaffold/openapi-generator.js.map +1 -0
- package/dist/core/scaffold/ui-generator.d.ts +10 -4
- package/dist/core/scaffold/ui-generator.d.ts.map +1 -1
- package/dist/core/scaffold/ui-generator.js +768 -663
- package/dist/core/scaffold/ui-generator.js.map +1 -1
- package/dist/database/base-model.d.ts +3 -3
- package/dist/database/base-model.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/machine/contracts.d.ts +16 -0
- package/dist/machine/contracts.d.ts.map +1 -0
- package/dist/machine/contracts.js +3 -0
- package/dist/machine/contracts.js.map +1 -0
- package/dist/machine/errors.d.ts +11 -0
- package/dist/machine/errors.d.ts.map +1 -0
- package/dist/machine/errors.js +34 -0
- package/dist/machine/errors.js.map +1 -0
- package/dist/machine/index.d.ts +3 -0
- package/dist/machine/index.d.ts.map +1 -0
- package/dist/machine/index.js +156 -0
- package/dist/machine/index.js.map +1 -0
- package/dist/mcp/index.d.ts +25 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +184 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/types/index.d.ts +65 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/package-manager.d.ts +109 -0
- package/dist/utils/package-manager.d.ts.map +1 -0
- package/dist/utils/package-manager.js +215 -0
- package/dist/utils/package-manager.js.map +1 -0
- package/dist/utils/python-uv.d.ts +21 -0
- package/dist/utils/python-uv.d.ts.map +1 -0
- package/dist/utils/python-uv.js +111 -0
- package/dist/utils/python-uv.js.map +1 -0
- package/package.json +85 -73
- package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +1139 -0
- package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +194 -0
- package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +532 -0
- package/src/__tests__/auth.test.ts +654 -0
- package/src/__tests__/config.test.ts +274 -0
- package/src/__tests__/connector-functions-generator.test.ts +288 -0
- package/src/__tests__/connector-mock-server.test.ts +439 -0
- package/src/__tests__/connector-model-bff.test.ts +162 -0
- package/src/__tests__/dev-seeds.test.ts +173 -0
- package/src/__tests__/dev.test.ts +252 -0
- package/src/__tests__/fixtures.ts +144 -0
- package/src/__tests__/functions-generator.test.ts +237 -0
- package/src/__tests__/init.test.ts +115 -0
- package/src/__tests__/machine.test.ts +251 -0
- package/src/__tests__/mcp.test.ts +117 -0
- package/src/__tests__/model-parser.test.ts +52 -0
- package/src/__tests__/nextjs-generator.test.ts +97 -0
- package/src/__tests__/openapi-generator.test.ts +43 -0
- package/src/__tests__/package-manager.test.ts +189 -0
- package/src/__tests__/python-uv.test.ts +48 -0
- package/src/__tests__/scaffold.test.ts +67 -0
- package/src/__tests__/string-utils.test.ts +75 -0
- package/src/__tests__/ui-generator.test.ts +144 -0
- package/src/__tests__/zod-mock-generator.test.ts +132 -0
- package/src/cli/commands/add-auth.ts +500 -0
- package/src/cli/commands/add-connector.ts +158 -0
- package/src/cli/commands/create-model.ts +62 -0
- package/src/cli/commands/dev-seeds.ts +614 -0
- package/src/cli/commands/dev.ts +1134 -0
- package/src/cli/commands/index.ts +9 -0
- package/src/cli/commands/init.ts +3480 -0
- package/src/cli/commands/provision.ts +193 -0
- package/src/cli/commands/scaffold.ts +1001 -0
- package/src/cli/index.ts +196 -0
- package/src/core/config.ts +312 -0
- package/src/core/mock/connector-mock-server.ts +555 -0
- package/src/core/mock/zod-mock-generator.ts +205 -0
- package/src/core/operations/create-model.ts +174 -0
- package/src/core/operations/runtime.ts +235 -0
- package/src/core/operations/scaffold-machine.ts +91 -0
- package/src/core/project/manifest.ts +402 -0
- package/src/core/project/validation.ts +229 -0
- package/src/core/scaffold/auth-generator.ts +1284 -0
- package/src/core/scaffold/connector-functions-generator.ts +1128 -0
- package/src/core/scaffold/functions-generator.ts +970 -0
- package/src/core/scaffold/model-parser.ts +841 -0
- package/src/core/scaffold/native-schema-generator.ts +798 -0
- package/src/core/scaffold/nextjs-generator.ts +370 -0
- package/src/core/scaffold/openapi-generator.ts +212 -0
- package/src/core/scaffold/ui-generator.ts +1061 -0
- package/src/database/base-model.ts +184 -0
- package/src/database/client.ts +140 -0
- package/src/database/repository.ts +104 -0
- package/src/database/runtime-check.ts +25 -0
- package/src/index.ts +27 -0
- package/src/machine/contracts.ts +17 -0
- package/src/machine/errors.ts +34 -0
- package/src/machine/index.ts +173 -0
- package/src/mcp/index.ts +185 -0
- package/src/types/index.ts +134 -0
- package/src/utils/package-manager.ts +229 -0
- package/src/utils/python-uv.ts +96 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as readline from "readline";
|
|
2
|
+
import { createModelOperation } from "../../core/operations/create-model";
|
|
3
|
+
import { detectFromProject, getCommands } from "../../utils/package-manager";
|
|
4
|
+
|
|
5
|
+
interface CreateModelOptions {
|
|
6
|
+
names: string[]; // モデル名のリスト(例: ["todo", "user", "post"])
|
|
7
|
+
modelsDir?: string; // モデルディレクトリ(デフォルト: "shared/models")
|
|
8
|
+
connector?: string; // コネクタ名(例: "mysql", "backlog")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ユーザーに確認を求める
|
|
13
|
+
*/
|
|
14
|
+
function askConfirmation(question: string): Promise<boolean> {
|
|
15
|
+
const rl = readline.createInterface({
|
|
16
|
+
input: process.stdin,
|
|
17
|
+
output: process.stdout,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
rl.question(question, (answer) => {
|
|
22
|
+
rl.close();
|
|
23
|
+
resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* create-model コマンド
|
|
30
|
+
*/
|
|
31
|
+
export async function createModelCommand(options: CreateModelOptions) {
|
|
32
|
+
console.log("🏗️ SwallowKit Create-Model: Generating model templates...\n");
|
|
33
|
+
|
|
34
|
+
const result = await createModelOperation({
|
|
35
|
+
names: options.names,
|
|
36
|
+
modelsDir: options.modelsDir,
|
|
37
|
+
connector: options.connector,
|
|
38
|
+
overwriteMode: "prompt",
|
|
39
|
+
confirmOverwrite: async (filePath) => askConfirmation(`⚠️ File ${filePath} already exists. Overwrite? (y/N): `),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// サマリー表示
|
|
43
|
+
console.log("\n📋 Summary:");
|
|
44
|
+
if (result.connectorType && options.connector) {
|
|
45
|
+
console.log(` 🔌 Connector: ${options.connector} (${result.connectorType})`);
|
|
46
|
+
}
|
|
47
|
+
if (result.createdFiles.length > 0) {
|
|
48
|
+
console.log(` ✅ Created ${result.createdFiles.length} model(s): ${result.createdFiles.join(", ")}`);
|
|
49
|
+
}
|
|
50
|
+
if (result.skippedFiles.length > 0) {
|
|
51
|
+
console.log(` ⏭️ Skipped ${result.skippedFiles.length} model(s): ${result.skippedFiles.join(", ")}`);
|
|
52
|
+
}
|
|
53
|
+
if (result.updatedIndex) {
|
|
54
|
+
console.log(" 📦 Updated shared/index.ts");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (result.createdFiles.length > 0) {
|
|
58
|
+
console.log("\n📝 Next steps:");
|
|
59
|
+
console.log(" 1. Customize the generated model fields in shared/models/");
|
|
60
|
+
console.log(` 2. Run '${getCommands(detectFromProject()).dlx} swallowkit scaffold <model>' to generate CRUD code`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { CosmosClient, Database, PartitionKeyKind } from "@azure/cosmos";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { ensureSwallowKitProject } from "../../core/config";
|
|
6
|
+
import { FieldInfo, ModelInfo, getAllModels, toKebabCase } from "../../core/scaffold/model-parser";
|
|
7
|
+
|
|
8
|
+
export type SeedDocument = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
interface GenerateDevSeedTemplatesOptions {
|
|
11
|
+
environment: string;
|
|
12
|
+
modelsDir?: string;
|
|
13
|
+
seedsDir?: string;
|
|
14
|
+
force?: boolean;
|
|
15
|
+
fromEmulator?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ApplyDevSeedEnvironmentOptions {
|
|
19
|
+
client: CosmosClient;
|
|
20
|
+
databaseName: string;
|
|
21
|
+
environment: string;
|
|
22
|
+
models: ModelInfo[];
|
|
23
|
+
seedsDir?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface LoadedSeedFile {
|
|
27
|
+
model: ModelInfo;
|
|
28
|
+
containerName: string;
|
|
29
|
+
documents: SeedDocument[];
|
|
30
|
+
filePath: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface LocalCosmosConnectionInfo {
|
|
34
|
+
endpoint: string;
|
|
35
|
+
key: string;
|
|
36
|
+
databaseName: string;
|
|
37
|
+
localSettingsPath: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ResolveLocalCosmosConnectionResult =
|
|
41
|
+
| { ok: true; value: LocalCosmosConnectionInfo }
|
|
42
|
+
| {
|
|
43
|
+
ok: false;
|
|
44
|
+
reason: "missing-local-settings" | "missing-connection-string" | "invalid-connection-string";
|
|
45
|
+
localSettingsPath: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
interface ExportedContainerDocuments {
|
|
49
|
+
documents: SeedDocument[];
|
|
50
|
+
containerExists: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const COSMOS_SYSTEM_PROPERTIES = new Set(["_rid", "_self", "_etag", "_attachments", "_ts"]);
|
|
54
|
+
|
|
55
|
+
export function getContainerNameForModel(model: Pick<ModelInfo, "name">): string {
|
|
56
|
+
return model.name.endsWith('s') ? model.name : `${model.name}s`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function getSeedEnvironmentDir(environment: string, seedsDir: string = "dev-seeds"): string {
|
|
60
|
+
return path.join(process.cwd(), seedsDir, environment);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function normalizeSeedIdentifier(value: string): string {
|
|
64
|
+
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function parseSeedDocuments(content: string, filePath: string): SeedDocument[] {
|
|
68
|
+
const parsed: unknown = JSON.parse(content);
|
|
69
|
+
|
|
70
|
+
if (Array.isArray(parsed)) {
|
|
71
|
+
return parsed.map((item, index) => {
|
|
72
|
+
if (!isSeedDocument(item)) {
|
|
73
|
+
throw new Error(`${filePath} must contain only JSON objects. Invalid item at index ${index}.`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return item;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isSeedDocument(parsed)) {
|
|
81
|
+
return [parsed];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
throw new Error(`${filePath} must contain a JSON object or an array of JSON objects.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function loadProjectModels(modelsDir: string = "shared/models"): Promise<ModelInfo[]> {
|
|
88
|
+
return getAllModels(modelsDir);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function buildDefaultCosmosDatabaseName(packageName: string): string {
|
|
92
|
+
const normalizedName = packageName.trim();
|
|
93
|
+
const baseName = normalizedName.length > 0 ? normalizedName : "App";
|
|
94
|
+
return `${baseName.charAt(0).toUpperCase()}${baseName.slice(1)}Database`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function getDefaultCosmosDatabaseName(projectDir: string = process.cwd()): string {
|
|
98
|
+
const packageJsonPath = path.join(projectDir, "package.json");
|
|
99
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as { name?: string };
|
|
100
|
+
return buildDefaultCosmosDatabaseName(packageJson.name || "App");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function parseCosmosConnectionString(
|
|
104
|
+
connectionString: string
|
|
105
|
+
): Pick<LocalCosmosConnectionInfo, "endpoint" | "key"> | null {
|
|
106
|
+
const endpointMatch = connectionString.match(/AccountEndpoint=([^;]+)/);
|
|
107
|
+
const keyMatch = connectionString.match(/AccountKey=([^;]+)/);
|
|
108
|
+
|
|
109
|
+
if (!endpointMatch || !keyMatch) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
endpoint: endpointMatch[1],
|
|
115
|
+
key: keyMatch[1],
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function resolveLocalCosmosConnectionInfo(
|
|
120
|
+
defaultDatabaseName: string,
|
|
121
|
+
functionsDir: string = path.join(process.cwd(), "functions")
|
|
122
|
+
): ResolveLocalCosmosConnectionResult {
|
|
123
|
+
const localSettingsPath = path.join(functionsDir, "local.settings.json");
|
|
124
|
+
if (!fs.existsSync(localSettingsPath)) {
|
|
125
|
+
return { ok: false, reason: "missing-local-settings", localSettingsPath };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const localSettings = JSON.parse(fs.readFileSync(localSettingsPath, "utf-8")) as {
|
|
129
|
+
Values?: {
|
|
130
|
+
CosmosDBConnection?: string;
|
|
131
|
+
COSMOS_DB_DATABASE_NAME?: string;
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
const connectionString = localSettings.Values?.CosmosDBConnection;
|
|
135
|
+
|
|
136
|
+
if (!connectionString) {
|
|
137
|
+
return { ok: false, reason: "missing-connection-string", localSettingsPath };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const parsedConnection = parseCosmosConnectionString(connectionString);
|
|
141
|
+
if (!parsedConnection) {
|
|
142
|
+
return { ok: false, reason: "invalid-connection-string", localSettingsPath };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
ok: true,
|
|
147
|
+
value: {
|
|
148
|
+
...parsedConnection,
|
|
149
|
+
databaseName: localSettings.Values?.COSMOS_DB_DATABASE_NAME || defaultDatabaseName,
|
|
150
|
+
localSettingsPath,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function buildSeedTemplateDocument(
|
|
156
|
+
model: ModelInfo,
|
|
157
|
+
allModels: ModelInfo[] = [model],
|
|
158
|
+
seenModels: Set<string> = new Set()
|
|
159
|
+
): SeedDocument {
|
|
160
|
+
const modelLookup = new Map(allModels.map((candidate) => [candidate.name, candidate]));
|
|
161
|
+
return buildSeedTemplateDocumentInternal(model, modelLookup, seenModels);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function loadDevSeedFiles(
|
|
165
|
+
environment: string,
|
|
166
|
+
models: ModelInfo[],
|
|
167
|
+
seedsDir: string = "dev-seeds"
|
|
168
|
+
): Promise<LoadedSeedFile[]> {
|
|
169
|
+
const environmentDir = getSeedEnvironmentDir(environment, seedsDir);
|
|
170
|
+
if (!fs.existsSync(environmentDir)) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const modelAliases = buildModelAliasMap(models);
|
|
175
|
+
const seedFiles = fs.readdirSync(environmentDir).filter((entry) => entry.endsWith(".json"));
|
|
176
|
+
const loaded: LoadedSeedFile[] = [];
|
|
177
|
+
|
|
178
|
+
for (const fileName of seedFiles) {
|
|
179
|
+
const filePath = path.join(environmentDir, fileName);
|
|
180
|
+
const fileStem = path.basename(fileName, ".json");
|
|
181
|
+
const model = modelAliases.get(normalizeSeedIdentifier(fileStem));
|
|
182
|
+
|
|
183
|
+
if (!model) {
|
|
184
|
+
console.warn(`⚠️ Skipping seed file without matching schema: ${filePath}`);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const documents = parseSeedDocuments(fs.readFileSync(filePath, "utf-8"), filePath);
|
|
189
|
+
validateSeedDocuments(documents, filePath);
|
|
190
|
+
|
|
191
|
+
loaded.push({
|
|
192
|
+
model,
|
|
193
|
+
containerName: getContainerNameForModel(model),
|
|
194
|
+
documents,
|
|
195
|
+
filePath,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return loaded;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function applyDevSeedEnvironment({
|
|
203
|
+
client,
|
|
204
|
+
databaseName,
|
|
205
|
+
environment,
|
|
206
|
+
models,
|
|
207
|
+
seedsDir = "dev-seeds",
|
|
208
|
+
}: ApplyDevSeedEnvironmentOptions): Promise<boolean> {
|
|
209
|
+
const environmentDir = getSeedEnvironmentDir(environment, seedsDir);
|
|
210
|
+
if (!fs.existsSync(environmentDir)) {
|
|
211
|
+
console.log(`ℹ️ Seed environment "${environment}" not found at ${environmentDir}. Keeping existing Cosmos DB data.`);
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const seedFiles = await loadDevSeedFiles(environment, models, seedsDir);
|
|
216
|
+
if (seedFiles.length === 0) {
|
|
217
|
+
console.log(`ℹ️ No seed files found for environment "${environment}". Keeping existing Cosmos DB data.`);
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const database = client.database(databaseName);
|
|
222
|
+
console.log(`🧪 Applying Cosmos DB seed data for environment "${environment}"...`);
|
|
223
|
+
|
|
224
|
+
for (const seedFile of seedFiles) {
|
|
225
|
+
await recreateContainer(database, seedFile.containerName, seedFile.model.partitionKey);
|
|
226
|
+
const container = database.container(seedFile.containerName);
|
|
227
|
+
|
|
228
|
+
for (const document of seedFile.documents) {
|
|
229
|
+
await container.items.create(document);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
console.log(
|
|
233
|
+
`✅ Seeded "${seedFile.containerName}" with ${seedFile.documents.length} item(s) from ${path.basename(seedFile.filePath)}`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
console.log("✅ Cosmos DB seed replacement complete\n");
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export async function generateDevSeedTemplates({
|
|
242
|
+
environment,
|
|
243
|
+
modelsDir = "shared/models",
|
|
244
|
+
seedsDir = "dev-seeds",
|
|
245
|
+
force = false,
|
|
246
|
+
}: GenerateDevSeedTemplatesOptions): Promise<void> {
|
|
247
|
+
ensureSwallowKitProject("create-dev-seeds");
|
|
248
|
+
|
|
249
|
+
console.log(`🧪 Generating dev seed templates for environment "${environment}"...\n`);
|
|
250
|
+
const models = await loadProjectModels(modelsDir);
|
|
251
|
+
|
|
252
|
+
if (models.length === 0) {
|
|
253
|
+
console.log("⚠️ No schemas found under shared/models. Nothing was generated.");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const environmentDir = getSeedEnvironmentDir(environment, seedsDir);
|
|
258
|
+
fs.mkdirSync(environmentDir, { recursive: true });
|
|
259
|
+
|
|
260
|
+
const writtenFiles: string[] = [];
|
|
261
|
+
const skippedFiles: string[] = [];
|
|
262
|
+
|
|
263
|
+
for (const model of models) {
|
|
264
|
+
const filePath = path.join(environmentDir, `${toKebabCase(model.name)}.json`);
|
|
265
|
+
if (!force && fs.existsSync(filePath)) {
|
|
266
|
+
skippedFiles.push(filePath);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const content = JSON.stringify([buildSeedTemplateDocument(model, models)], null, 2) + "\n";
|
|
271
|
+
fs.writeFileSync(filePath, content, "utf-8");
|
|
272
|
+
writtenFiles.push(filePath);
|
|
273
|
+
console.log(`✅ Created: ${filePath}`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (skippedFiles.length > 0) {
|
|
277
|
+
console.log("");
|
|
278
|
+
console.log(`⏭️ Skipped ${skippedFiles.length} existing file(s). Use --force to overwrite them.`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
console.log("");
|
|
282
|
+
console.log("📝 Next steps:");
|
|
283
|
+
console.log(` 1. Edit JSON files under ${environmentDir}`);
|
|
284
|
+
console.log(` 2. Run 'swallowkit dev --seed-env ${environment}' to replace emulator data before startup`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function stripCosmosSystemProperties(document: SeedDocument): SeedDocument {
|
|
288
|
+
const sanitized: SeedDocument = {};
|
|
289
|
+
|
|
290
|
+
for (const [key, value] of Object.entries(document)) {
|
|
291
|
+
if (!COSMOS_SYSTEM_PROPERTIES.has(key)) {
|
|
292
|
+
sanitized[key] = value;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return sanitized;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function prepareSeedDocumentsForExport(documents: SeedDocument[], filePath: string): SeedDocument[] {
|
|
300
|
+
const preparedDocuments = documents
|
|
301
|
+
.map((document) => stripCosmosSystemProperties(document))
|
|
302
|
+
.sort(compareSeedDocumentsForExport);
|
|
303
|
+
|
|
304
|
+
validateSeedDocuments(preparedDocuments, filePath);
|
|
305
|
+
return preparedDocuments;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function exportDevSeedsFromEmulator({
|
|
309
|
+
environment,
|
|
310
|
+
modelsDir = "shared/models",
|
|
311
|
+
seedsDir = "dev-seeds",
|
|
312
|
+
force = false,
|
|
313
|
+
}: GenerateDevSeedTemplatesOptions): Promise<void> {
|
|
314
|
+
ensureSwallowKitProject("create-dev-seeds");
|
|
315
|
+
|
|
316
|
+
console.log(`🧪 Exporting dev seeds from local Cosmos DB Emulator for environment "${environment}"...\n`);
|
|
317
|
+
const models = await loadProjectModels(modelsDir);
|
|
318
|
+
|
|
319
|
+
if (models.length === 0) {
|
|
320
|
+
console.log("⚠️ No schemas found under shared/models. Nothing was exported.");
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const connectionInfoResult = resolveLocalCosmosConnectionInfo(getDefaultCosmosDatabaseName());
|
|
325
|
+
if (!connectionInfoResult.ok) {
|
|
326
|
+
throw new Error(buildLocalCosmosConnectionError(connectionInfoResult));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const { endpoint, key, databaseName, localSettingsPath } = connectionInfoResult.value;
|
|
330
|
+
console.log(`🗄️ Using local Cosmos DB settings from ${localSettingsPath}`);
|
|
331
|
+
console.log(`📦 Export source database: "${databaseName}"\n`);
|
|
332
|
+
|
|
333
|
+
const client = new CosmosClient({ endpoint, key });
|
|
334
|
+
const database = client.database(databaseName);
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
await database.read();
|
|
338
|
+
} catch (error: unknown) {
|
|
339
|
+
if (isCosmosNotFoundError(error)) {
|
|
340
|
+
throw new Error(`Cosmos DB database "${databaseName}" was not found in the local emulator.`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const environmentDir = getSeedEnvironmentDir(environment, seedsDir);
|
|
347
|
+
fs.mkdirSync(environmentDir, { recursive: true });
|
|
348
|
+
|
|
349
|
+
let writtenFiles = 0;
|
|
350
|
+
let skippedFiles = 0;
|
|
351
|
+
let totalDocuments = 0;
|
|
352
|
+
|
|
353
|
+
for (const model of models) {
|
|
354
|
+
const containerName = getContainerNameForModel(model);
|
|
355
|
+
const filePath = path.join(environmentDir, `${toKebabCase(model.name)}.json`);
|
|
356
|
+
|
|
357
|
+
if (!force && fs.existsSync(filePath)) {
|
|
358
|
+
skippedFiles += 1;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const exported = await readContainerSeedDocuments(database, containerName, filePath);
|
|
363
|
+
fs.writeFileSync(filePath, JSON.stringify(exported.documents, null, 2) + "\n", "utf-8");
|
|
364
|
+
writtenFiles += 1;
|
|
365
|
+
totalDocuments += exported.documents.length;
|
|
366
|
+
|
|
367
|
+
const detail = exported.containerExists
|
|
368
|
+
? `${exported.documents.length} item(s)`
|
|
369
|
+
: "container not found; wrote empty seed";
|
|
370
|
+
console.log(`✅ Exported "${containerName}" to ${filePath} (${detail})`);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (skippedFiles > 0) {
|
|
374
|
+
console.log("");
|
|
375
|
+
console.log(`⏭️ Skipped ${skippedFiles} existing file(s). Use --force to overwrite them.`);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
console.log("");
|
|
379
|
+
console.log(`🧾 Export complete: ${writtenFiles} file(s), ${totalDocuments} item(s).`);
|
|
380
|
+
console.log(`📝 Next step: Run 'swallowkit dev --seed-env ${environment}' to replay the exported data.`);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export const devSeedsCommand = new Command()
|
|
384
|
+
.name("create-dev-seeds")
|
|
385
|
+
.description("Generate dev seed JSON files from shared/models schemas or export current local Cosmos DB Emulator data")
|
|
386
|
+
.argument("<environment>", "Seed environment name")
|
|
387
|
+
.option("--models-dir <dir>", "Models directory", "shared/models")
|
|
388
|
+
.option("--seeds-dir <dir>", "Dev seeds directory", "dev-seeds")
|
|
389
|
+
.option("--force", "Overwrite existing seed JSON files", false)
|
|
390
|
+
.option("--from-emulator", "Export current data from the local Cosmos DB Emulator instead of generating templates", false)
|
|
391
|
+
.action(async (environment: string, options: { modelsDir?: string; seedsDir?: string; force?: boolean; fromEmulator?: boolean }) => {
|
|
392
|
+
if (options.fromEmulator) {
|
|
393
|
+
await exportDevSeedsFromEmulator({
|
|
394
|
+
environment,
|
|
395
|
+
modelsDir: options.modelsDir,
|
|
396
|
+
seedsDir: options.seedsDir,
|
|
397
|
+
force: options.force,
|
|
398
|
+
});
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
await generateDevSeedTemplates({
|
|
403
|
+
environment,
|
|
404
|
+
modelsDir: options.modelsDir,
|
|
405
|
+
seedsDir: options.seedsDir,
|
|
406
|
+
force: options.force,
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
function buildSeedTemplateDocumentInternal(
|
|
411
|
+
model: ModelInfo,
|
|
412
|
+
modelLookup: Map<string, ModelInfo>,
|
|
413
|
+
seenModels: Set<string>
|
|
414
|
+
): SeedDocument {
|
|
415
|
+
const nextSeen = new Set(seenModels);
|
|
416
|
+
nextSeen.add(model.name);
|
|
417
|
+
|
|
418
|
+
const document: SeedDocument = {};
|
|
419
|
+
|
|
420
|
+
for (const field of model.fields) {
|
|
421
|
+
document[field.name] = buildTemplateValueForField(model, field, modelLookup, nextSeen);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return document;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function buildTemplateValueForField(
|
|
428
|
+
model: ModelInfo,
|
|
429
|
+
field: FieldInfo,
|
|
430
|
+
modelLookup: Map<string, ModelInfo>,
|
|
431
|
+
seenModels: Set<string>
|
|
432
|
+
): unknown {
|
|
433
|
+
if (field.isNestedSchema && field.nestedModelName) {
|
|
434
|
+
const nestedModel = modelLookup.get(field.nestedModelName);
|
|
435
|
+
if (nestedModel) {
|
|
436
|
+
if (seenModels.has(nestedModel.name)) {
|
|
437
|
+
const fallback = { id: `${toKebabCase(nestedModel.name)}-001` };
|
|
438
|
+
return field.isArray ? [fallback] : fallback;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const nestedDocument = buildSeedTemplateDocumentInternal(nestedModel, modelLookup, seenModels);
|
|
442
|
+
return field.isArray ? [nestedDocument] : nestedDocument;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (field.isArray) {
|
|
447
|
+
return [buildScalarTemplateValue(model, field)];
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return buildScalarTemplateValue(model, field);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function buildScalarTemplateValue(model: ModelInfo, field: FieldInfo): unknown {
|
|
454
|
+
if (field.name === "id") {
|
|
455
|
+
return `${toKebabCase(model.name)}-001`;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (field.enumValues && field.enumValues.length > 0) {
|
|
459
|
+
return field.enumValues[0];
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (field.type === "number") {
|
|
463
|
+
return 0;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (field.type === "boolean") {
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (field.type === "date" || field.name.endsWith("At")) {
|
|
471
|
+
return "2026-01-01T00:00:00.000Z";
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (field.type === "object") {
|
|
475
|
+
return {};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return `${toKebabCase(model.name)}-${toKebabCase(field.name)}-sample`;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function buildModelAliasMap(models: ModelInfo[]): Map<string, ModelInfo> {
|
|
482
|
+
const aliases = new Map<string, ModelInfo>();
|
|
483
|
+
|
|
484
|
+
for (const model of models) {
|
|
485
|
+
const candidates = [
|
|
486
|
+
model.name,
|
|
487
|
+
model.schemaName,
|
|
488
|
+
toKebabCase(model.name),
|
|
489
|
+
path.basename(model.filePath, path.extname(model.filePath)),
|
|
490
|
+
];
|
|
491
|
+
|
|
492
|
+
for (const candidate of candidates) {
|
|
493
|
+
const normalized = normalizeSeedIdentifier(candidate);
|
|
494
|
+
if (!aliases.has(normalized)) {
|
|
495
|
+
aliases.set(normalized, model);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return aliases;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function isSeedDocument(value: unknown): value is SeedDocument {
|
|
504
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function validateSeedDocuments(documents: SeedDocument[], filePath: string): void {
|
|
508
|
+
const ids = new Set<string>();
|
|
509
|
+
|
|
510
|
+
documents.forEach((document, index) => {
|
|
511
|
+
if (typeof document.id !== "string" || document.id.trim().length === 0) {
|
|
512
|
+
throw new Error(`${filePath} item at index ${index} must contain a non-empty string id.`);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (ids.has(document.id)) {
|
|
516
|
+
throw new Error(`${filePath} contains duplicate id "${document.id}".`);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
ids.add(document.id);
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function compareSeedDocumentsForExport(left: SeedDocument, right: SeedDocument): number {
|
|
524
|
+
const leftId = typeof left.id === "string" ? left.id : "";
|
|
525
|
+
const rightId = typeof right.id === "string" ? right.id : "";
|
|
526
|
+
|
|
527
|
+
if (leftId.length === 0 && rightId.length === 0) {
|
|
528
|
+
return 0;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (leftId.length === 0) {
|
|
532
|
+
return 1;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (rightId.length === 0) {
|
|
536
|
+
return -1;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return leftId.localeCompare(rightId);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function buildLocalCosmosConnectionError(
|
|
543
|
+
result: Exclude<ResolveLocalCosmosConnectionResult, { ok: true; value: LocalCosmosConnectionInfo }>
|
|
544
|
+
): string {
|
|
545
|
+
switch (result.reason) {
|
|
546
|
+
case "missing-local-settings":
|
|
547
|
+
return `local.settings.json not found at ${result.localSettingsPath}. Start from a SwallowKit app with Azure Functions configured.`;
|
|
548
|
+
case "missing-connection-string":
|
|
549
|
+
return `CosmosDBConnection not found in ${result.localSettingsPath}.`;
|
|
550
|
+
case "invalid-connection-string":
|
|
551
|
+
return `Invalid CosmosDBConnection format in ${result.localSettingsPath}.`;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
throw new Error(`Unhandled local Cosmos connection error: ${JSON.stringify(result)}`);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
async function readContainerSeedDocuments(
|
|
558
|
+
database: Database,
|
|
559
|
+
containerName: string,
|
|
560
|
+
filePath: string
|
|
561
|
+
): Promise<ExportedContainerDocuments> {
|
|
562
|
+
const container = database.container(containerName);
|
|
563
|
+
|
|
564
|
+
try {
|
|
565
|
+
await container.read();
|
|
566
|
+
} catch (error: unknown) {
|
|
567
|
+
if (isCosmosNotFoundError(error)) {
|
|
568
|
+
return { documents: [], containerExists: false };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
throw error;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const { resources } = await container.items.query<SeedDocument>("SELECT * FROM c").fetchAll();
|
|
575
|
+
return {
|
|
576
|
+
documents: prepareSeedDocumentsForExport(resources, filePath),
|
|
577
|
+
containerExists: true,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function isCosmosNotFoundError(error: unknown): boolean {
|
|
582
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === 404;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
async function recreateContainer(database: Database, containerName: string, partitionKeyPath: string = '/id'): Promise<void> {
|
|
586
|
+
try {
|
|
587
|
+
await database.container(containerName).delete();
|
|
588
|
+
} catch (error: any) {
|
|
589
|
+
if (error?.code !== 404) {
|
|
590
|
+
throw error;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
try {
|
|
595
|
+
await database.containers.createIfNotExists({
|
|
596
|
+
id: containerName,
|
|
597
|
+
partitionKey: {
|
|
598
|
+
paths: [partitionKeyPath],
|
|
599
|
+
kind: PartitionKeyKind.Hash,
|
|
600
|
+
version: 2,
|
|
601
|
+
},
|
|
602
|
+
});
|
|
603
|
+
} catch (error: unknown) {
|
|
604
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
605
|
+
console.log(`⚠️ Failed to recreate "${containerName}" with full partition key definition: ${message}`);
|
|
606
|
+
console.log("🔄 Retrying with simple partition key...");
|
|
607
|
+
await database.containers.createIfNotExists({
|
|
608
|
+
id: containerName,
|
|
609
|
+
partitionKey: {
|
|
610
|
+
paths: [partitionKeyPath],
|
|
611
|
+
},
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
}
|