@zenstackhq/testtools 3.0.0-alpha.4 → 3.0.0-alpha.7
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/dist/index.cjs +66 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +67 -43
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -31,51 +31,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
// src/index.ts
|
|
32
32
|
var src_exports = {};
|
|
33
33
|
__export(src_exports, {
|
|
34
|
+
createTestProject: () => createTestProject,
|
|
34
35
|
generateTsSchema: () => generateTsSchema,
|
|
35
36
|
generateTsSchemaFromFile: () => generateTsSchemaFromFile
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(src_exports);
|
|
38
39
|
|
|
39
|
-
// src/
|
|
40
|
-
var import_sdk = require("@zenstackhq/sdk");
|
|
41
|
-
var import_glob = require("glob");
|
|
42
|
-
var import_node_child_process = require("child_process");
|
|
40
|
+
// src/project.ts
|
|
43
41
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
44
42
|
var import_node_path = __toESM(require("path"), 1);
|
|
45
43
|
var import_tmp = __toESM(require("tmp"), 1);
|
|
46
|
-
|
|
47
|
-
function makePrelude(provider, dbName) {
|
|
48
|
-
return (0, import_ts_pattern.match)(provider).with("sqlite", () => {
|
|
49
|
-
return `
|
|
50
|
-
datasource db {
|
|
51
|
-
provider = 'sqlite'
|
|
52
|
-
url = '${dbName ?? ":memory:"}'
|
|
53
|
-
}
|
|
54
|
-
`;
|
|
55
|
-
}).with("postgresql", () => {
|
|
56
|
-
return `
|
|
57
|
-
datasource db {
|
|
58
|
-
provider = 'postgresql'
|
|
59
|
-
url = 'postgres://postgres:postgres@localhost:5432/${dbName}'
|
|
60
|
-
}
|
|
61
|
-
`;
|
|
62
|
-
}).exhaustive();
|
|
63
|
-
}
|
|
64
|
-
__name(makePrelude, "makePrelude");
|
|
65
|
-
async function generateTsSchema(schemaText, provider = "sqlite", dbName) {
|
|
44
|
+
function createTestProject() {
|
|
66
45
|
const { name: workDir } = import_tmp.default.dirSync({
|
|
67
46
|
unsafeCleanup: true
|
|
68
47
|
});
|
|
69
|
-
console.log(`Working directory: ${workDir}`);
|
|
70
|
-
const zmodelPath = import_node_path.default.join(workDir, "schema.zmodel");
|
|
71
|
-
const noPrelude = schemaText.includes("datasource ");
|
|
72
|
-
import_node_fs.default.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbName)}
|
|
73
|
-
|
|
74
|
-
${schemaText}`);
|
|
75
|
-
const pluginModelFiles = import_glob.glob.sync(import_node_path.default.resolve(__dirname, "../../runtime/src/plugins/**/plugin.zmodel"));
|
|
76
|
-
const generator = new import_sdk.TsSchemaGenerator();
|
|
77
|
-
const tsPath = import_node_path.default.join(workDir, "schema.ts");
|
|
78
|
-
await generator.generate(zmodelPath, pluginModelFiles, tsPath);
|
|
79
48
|
import_node_fs.default.mkdirSync(import_node_path.default.join(workDir, "node_modules"));
|
|
80
49
|
const nodeModules = import_node_fs.default.readdirSync(import_node_path.default.join(__dirname, "../node_modules"));
|
|
81
50
|
for (const entry of nodeModules) {
|
|
@@ -87,31 +56,86 @@ ${schemaText}`);
|
|
|
87
56
|
const zenstackPackages = [
|
|
88
57
|
"language",
|
|
89
58
|
"sdk",
|
|
90
|
-
"runtime"
|
|
59
|
+
"runtime",
|
|
60
|
+
"cli"
|
|
91
61
|
];
|
|
92
62
|
import_node_fs.default.mkdirSync(import_node_path.default.join(workDir, "node_modules/@zenstackhq"));
|
|
93
63
|
for (const pkg of zenstackPackages) {
|
|
94
|
-
import_node_fs.default.symlinkSync(import_node_path.default.join(__dirname, `../../${pkg}
|
|
64
|
+
import_node_fs.default.symlinkSync(import_node_path.default.join(__dirname, `../../${pkg}`), import_node_path.default.join(workDir, `node_modules/@zenstackhq/${pkg}`), "dir");
|
|
95
65
|
}
|
|
96
66
|
import_node_fs.default.writeFileSync(import_node_path.default.join(workDir, "package.json"), JSON.stringify({
|
|
97
67
|
name: "test",
|
|
98
68
|
version: "1.0.0",
|
|
99
69
|
type: "module"
|
|
100
|
-
}));
|
|
70
|
+
}, null, 4));
|
|
101
71
|
import_node_fs.default.writeFileSync(import_node_path.default.join(workDir, "tsconfig.json"), JSON.stringify({
|
|
102
72
|
compilerOptions: {
|
|
103
73
|
module: "ESNext",
|
|
104
74
|
target: "ESNext",
|
|
105
75
|
moduleResolution: "Bundler",
|
|
106
76
|
esModuleInterop: true,
|
|
107
|
-
skipLibCheck: true
|
|
77
|
+
skipLibCheck: true,
|
|
78
|
+
strict: true
|
|
79
|
+
},
|
|
80
|
+
include: [
|
|
81
|
+
"**/*.ts"
|
|
82
|
+
]
|
|
83
|
+
}, null, 4));
|
|
84
|
+
return workDir;
|
|
85
|
+
}
|
|
86
|
+
__name(createTestProject, "createTestProject");
|
|
87
|
+
|
|
88
|
+
// src/schema.ts
|
|
89
|
+
var import_sdk = require("@zenstackhq/sdk");
|
|
90
|
+
var import_glob = require("glob");
|
|
91
|
+
var import_node_child_process = require("child_process");
|
|
92
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
93
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
94
|
+
var import_ts_pattern = require("ts-pattern");
|
|
95
|
+
function makePrelude(provider, dbName) {
|
|
96
|
+
return (0, import_ts_pattern.match)(provider).with("sqlite", () => {
|
|
97
|
+
return `
|
|
98
|
+
datasource db {
|
|
99
|
+
provider = 'sqlite'
|
|
100
|
+
url = '${dbName ?? ":memory:"}'
|
|
101
|
+
}
|
|
102
|
+
`;
|
|
103
|
+
}).with("postgresql", () => {
|
|
104
|
+
return `
|
|
105
|
+
datasource db {
|
|
106
|
+
provider = 'postgresql'
|
|
107
|
+
url = 'postgres://postgres:postgres@localhost:5432/${dbName}'
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
}).exhaustive();
|
|
111
|
+
}
|
|
112
|
+
__name(makePrelude, "makePrelude");
|
|
113
|
+
async function generateTsSchema(schemaText, provider = "sqlite", dbName, extraSourceFiles) {
|
|
114
|
+
const workDir = createTestProject();
|
|
115
|
+
console.log(`Working directory: ${workDir}`);
|
|
116
|
+
const zmodelPath = import_node_path2.default.join(workDir, "schema.zmodel");
|
|
117
|
+
const noPrelude = schemaText.includes("datasource ");
|
|
118
|
+
import_node_fs2.default.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbName)}
|
|
119
|
+
|
|
120
|
+
${schemaText}`);
|
|
121
|
+
const pluginModelFiles = import_glob.glob.sync(import_node_path2.default.resolve(__dirname, "../../runtime/src/plugins/**/plugin.zmodel"));
|
|
122
|
+
const generator = new import_sdk.TsSchemaGenerator();
|
|
123
|
+
const tsPath = import_node_path2.default.join(workDir, "schema.ts");
|
|
124
|
+
await generator.generate(zmodelPath, pluginModelFiles, tsPath);
|
|
125
|
+
if (extraSourceFiles) {
|
|
126
|
+
for (const [fileName, content] of Object.entries(extraSourceFiles)) {
|
|
127
|
+
const filePath = import_node_path2.default.resolve(workDir, `${fileName}.ts`);
|
|
128
|
+
import_node_fs2.default.mkdirSync(import_node_path2.default.dirname(filePath), {
|
|
129
|
+
recursive: true
|
|
130
|
+
});
|
|
131
|
+
import_node_fs2.default.writeFileSync(filePath, content);
|
|
108
132
|
}
|
|
109
|
-
}
|
|
133
|
+
}
|
|
110
134
|
(0, import_node_child_process.execSync)("npx tsc", {
|
|
111
135
|
cwd: workDir,
|
|
112
136
|
stdio: "inherit"
|
|
113
137
|
});
|
|
114
|
-
const module2 = await import(
|
|
138
|
+
const module2 = await import(import_node_path2.default.join(workDir, "schema.js"));
|
|
115
139
|
return {
|
|
116
140
|
workDir,
|
|
117
141
|
schema: module2.schema
|
|
@@ -119,12 +143,13 @@ ${schemaText}`);
|
|
|
119
143
|
}
|
|
120
144
|
__name(generateTsSchema, "generateTsSchema");
|
|
121
145
|
function generateTsSchemaFromFile(filePath) {
|
|
122
|
-
const schemaText =
|
|
146
|
+
const schemaText = import_node_fs2.default.readFileSync(filePath, "utf8");
|
|
123
147
|
return generateTsSchema(schemaText);
|
|
124
148
|
}
|
|
125
149
|
__name(generateTsSchemaFromFile, "generateTsSchemaFromFile");
|
|
126
150
|
// Annotate the CommonJS export names for ESM import in node:
|
|
127
151
|
0 && (module.exports = {
|
|
152
|
+
createTestProject,
|
|
128
153
|
generateTsSchema,
|
|
129
154
|
generateTsSchemaFromFile
|
|
130
155
|
});
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/schema.ts"],"sourcesContent":["export * from './schema';\n","import { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { glob } from 'glob';\nimport { execSync } from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/project.ts","../src/schema.ts"],"sourcesContent":["export * from './project';\nexport * from './schema';\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport tmp from 'tmp';\n\nexport function createTestProject() {\n const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });\n\n fs.mkdirSync(path.join(workDir, 'node_modules'));\n\n // symlink all entries from \"node_modules\"\n const nodeModules = fs.readdirSync(path.join(__dirname, '../node_modules'));\n for (const entry of nodeModules) {\n if (entry.startsWith('@zenstackhq')) {\n continue;\n }\n fs.symlinkSync(\n path.join(__dirname, '../node_modules', entry),\n path.join(workDir, 'node_modules', entry),\n 'dir',\n );\n }\n\n // in addition, symlink zenstack packages\n const zenstackPackages = ['language', 'sdk', 'runtime', 'cli'];\n fs.mkdirSync(path.join(workDir, 'node_modules/@zenstackhq'));\n for (const pkg of zenstackPackages) {\n fs.symlinkSync(\n path.join(__dirname, `../../${pkg}`),\n path.join(workDir, `node_modules/@zenstackhq/${pkg}`),\n 'dir',\n );\n }\n\n fs.writeFileSync(\n path.join(workDir, 'package.json'),\n JSON.stringify(\n {\n name: 'test',\n version: '1.0.0',\n type: 'module',\n },\n null,\n 4,\n ),\n );\n\n fs.writeFileSync(\n path.join(workDir, 'tsconfig.json'),\n JSON.stringify(\n {\n compilerOptions: {\n module: 'ESNext',\n target: 'ESNext',\n moduleResolution: 'Bundler',\n esModuleInterop: true,\n skipLibCheck: true,\n strict: true,\n },\n include: ['**/*.ts'],\n },\n null,\n 4,\n ),\n );\n\n return workDir;\n}\n","import { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { glob } from 'glob';\nimport { execSync } from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { match } from 'ts-pattern';\nimport { createTestProject } from './project';\n\nfunction makePrelude(provider: 'sqlite' | 'postgresql', dbName?: string) {\n return match(provider)\n .with('sqlite', () => {\n return `\ndatasource db {\n provider = 'sqlite'\n url = '${dbName ?? ':memory:'}'\n}\n`;\n })\n .with('postgresql', () => {\n return `\ndatasource db {\n provider = 'postgresql'\n url = 'postgres://postgres:postgres@localhost:5432/${dbName}'\n}\n`;\n })\n .exhaustive();\n}\n\nexport async function generateTsSchema(\n schemaText: string,\n provider: 'sqlite' | 'postgresql' = 'sqlite',\n dbName?: string,\n extraSourceFiles?: Record<string, string>,\n) {\n const workDir = createTestProject();\n console.log(`Working directory: ${workDir}`);\n\n const zmodelPath = path.join(workDir, 'schema.zmodel');\n const noPrelude = schemaText.includes('datasource ');\n fs.writeFileSync(zmodelPath, `${noPrelude ? '' : makePrelude(provider, dbName)}\\n\\n${schemaText}`);\n\n const pluginModelFiles = glob.sync(path.resolve(__dirname, '../../runtime/src/plugins/**/plugin.zmodel'));\n\n const generator = new TsSchemaGenerator();\n const tsPath = path.join(workDir, 'schema.ts');\n await generator.generate(zmodelPath, pluginModelFiles, tsPath);\n\n if (extraSourceFiles) {\n for (const [fileName, content] of Object.entries(extraSourceFiles)) {\n const filePath = path.resolve(workDir, `${fileName}.ts`);\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n fs.writeFileSync(filePath, content);\n }\n }\n\n // compile the generated TS schema\n execSync('npx tsc', {\n cwd: workDir,\n stdio: 'inherit',\n });\n\n // load the schema module\n const module = await import(path.join(workDir, 'schema.js'));\n return { workDir, schema: module.schema as SchemaDef };\n}\n\nexport function generateTsSchemaFromFile(filePath: string) {\n const schemaText = fs.readFileSync(filePath, 'utf8');\n return generateTsSchema(schemaText);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,qBAAe;AACf,uBAAiB;AACjB,iBAAgB;AAET,SAASA,oBAAAA;AACZ,QAAM,EAAEC,MAAMC,QAAO,IAAKC,WAAAA,QAAIC,QAAQ;IAAEC,eAAe;EAAK,CAAA;AAE5DC,iBAAAA,QAAGC,UAAUC,iBAAAA,QAAKC,KAAKP,SAAS,cAAA,CAAA;AAGhC,QAAMQ,cAAcJ,eAAAA,QAAGK,YAAYH,iBAAAA,QAAKC,KAAKG,WAAW,iBAAA,CAAA;AACxD,aAAWC,SAASH,aAAa;AAC7B,QAAIG,MAAMC,WAAW,aAAA,GAAgB;AACjC;IACJ;AACAR,mBAAAA,QAAGS,YACCP,iBAAAA,QAAKC,KAAKG,WAAW,mBAAmBC,KAAAA,GACxCL,iBAAAA,QAAKC,KAAKP,SAAS,gBAAgBW,KAAAA,GACnC,KAAA;EAER;AAGA,QAAMG,mBAAmB;IAAC;IAAY;IAAO;IAAW;;AACxDV,iBAAAA,QAAGC,UAAUC,iBAAAA,QAAKC,KAAKP,SAAS,0BAAA,CAAA;AAChC,aAAWe,OAAOD,kBAAkB;AAChCV,mBAAAA,QAAGS,YACCP,iBAAAA,QAAKC,KAAKG,WAAW,SAASK,GAAAA,EAAK,GACnCT,iBAAAA,QAAKC,KAAKP,SAAS,4BAA4Be,GAA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tB,QAAMC,SAASb,kBAAAA,QAAKC,KAAKN,SAAS,WAAA;AAClC,QAAMgB,UAAUG,SAASf,YAAYO,kBAAkBO,MAAAA;AAEvD,MAAInB,kBAAkB;AAClB,eAAW,CAACqB,UAAUC,OAAAA,KAAYC,OAAOC,QAAQxB,gBAAAA,GAAmB;AAChE,YAAMyB,WAAWnB,kBAAAA,QAAKS,QAAQd,SAAS,GAAGoB,QAAAA,KAAa;AACvDX,sBAAAA,QAAGgB,UAAUpB,kBAAAA,QAAKqB,QAAQF,QAAAA,GAAW;QAAEG,WAAW;MAAK,CAAA;AACvDlB,sBAAAA,QAAGC,cAAcc,UAAUH,OAAAA;IAC/B;EACJ;AAGAO,0CAAS,WAAW;IAChBC,KAAK7B;IACL8B,OAAO;EACX,CAAA;AAGA,QAAMC,UAAS,MAAM,OAAO1B,kBAAAA,QAAKC,KAAKN,SAAS,WAAA;AAC/C,SAAO;IAAEA;IAASgC,QAAQD,QAAOC;EAAoB;AACzD;AApCsBnC;AAsCf,SAASoC,yBAAyBT,UAAgB;AACrD,QAAM1B,aAAaW,gBAAAA,QAAGyB,aAAaV,UAAU,MAAA;AAC7C,SAAO3B,iBAAiBC,UAAAA;AAC5B;AAHgBmC;","names":["createTestProject","name","workDir","tmp","dirSync","unsafeCleanup","fs","mkdirSync","path","join","nodeModules","readdirSync","__dirname","entry","startsWith","symlinkSync","zenstackPackages","pkg","writeFileSync","JSON","stringify","version","type","compilerOptions","module","target","moduleResolution","esModuleInterop","skipLibCheck","strict","include","import_node_fs","import_node_path","makePrelude","provider","dbName","match","with","exhaustive","generateTsSchema","schemaText","extraSourceFiles","workDir","createTestProject","console","log","zmodelPath","path","join","noPrelude","includes","fs","writeFileSync","pluginModelFiles","glob","sync","resolve","__dirname","generator","TsSchemaGenerator","tsPath","generate","fileName","content","Object","entries","filePath","mkdirSync","dirname","recursive","execSync","cwd","stdio","module","schema","generateTsSchemaFromFile","readFileSync"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SchemaDef } from '@zenstackhq/sdk/schema';
|
|
2
2
|
|
|
3
|
-
declare function
|
|
3
|
+
declare function createTestProject(): string;
|
|
4
|
+
|
|
5
|
+
declare function generateTsSchema(schemaText: string, provider?: 'sqlite' | 'postgresql', dbName?: string, extraSourceFiles?: Record<string, string>): Promise<{
|
|
4
6
|
workDir: string;
|
|
5
7
|
schema: SchemaDef;
|
|
6
8
|
}>;
|
|
@@ -9,4 +11,4 @@ declare function generateTsSchemaFromFile(filePath: string): Promise<{
|
|
|
9
11
|
schema: SchemaDef;
|
|
10
12
|
}>;
|
|
11
13
|
|
|
12
|
-
export { generateTsSchema, generateTsSchemaFromFile };
|
|
14
|
+
export { createTestProject, generateTsSchema, generateTsSchemaFromFile };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SchemaDef } from '@zenstackhq/sdk/schema';
|
|
2
2
|
|
|
3
|
-
declare function
|
|
3
|
+
declare function createTestProject(): string;
|
|
4
|
+
|
|
5
|
+
declare function generateTsSchema(schemaText: string, provider?: 'sqlite' | 'postgresql', dbName?: string, extraSourceFiles?: Record<string, string>): Promise<{
|
|
4
6
|
workDir: string;
|
|
5
7
|
schema: SchemaDef;
|
|
6
8
|
}>;
|
|
@@ -9,4 +11,4 @@ declare function generateTsSchemaFromFile(filePath: string): Promise<{
|
|
|
9
11
|
schema: SchemaDef;
|
|
10
12
|
}>;
|
|
11
13
|
|
|
12
|
-
export { generateTsSchema, generateTsSchemaFromFile };
|
|
14
|
+
export { createTestProject, generateTsSchema, generateTsSchemaFromFile };
|
package/dist/index.js
CHANGED
|
@@ -1,46 +1,14 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import { execSync } from "node:child_process";
|
|
8
|
-
import fs from "node:fs";
|
|
9
|
-
import path from "node:path";
|
|
4
|
+
// src/project.ts
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
10
7
|
import tmp from "tmp";
|
|
11
|
-
|
|
12
|
-
function makePrelude(provider, dbName) {
|
|
13
|
-
return match(provider).with("sqlite", () => {
|
|
14
|
-
return `
|
|
15
|
-
datasource db {
|
|
16
|
-
provider = 'sqlite'
|
|
17
|
-
url = '${dbName ?? ":memory:"}'
|
|
18
|
-
}
|
|
19
|
-
`;
|
|
20
|
-
}).with("postgresql", () => {
|
|
21
|
-
return `
|
|
22
|
-
datasource db {
|
|
23
|
-
provider = 'postgresql'
|
|
24
|
-
url = 'postgres://postgres:postgres@localhost:5432/${dbName}'
|
|
25
|
-
}
|
|
26
|
-
`;
|
|
27
|
-
}).exhaustive();
|
|
28
|
-
}
|
|
29
|
-
__name(makePrelude, "makePrelude");
|
|
30
|
-
async function generateTsSchema(schemaText, provider = "sqlite", dbName) {
|
|
8
|
+
function createTestProject() {
|
|
31
9
|
const { name: workDir } = tmp.dirSync({
|
|
32
10
|
unsafeCleanup: true
|
|
33
11
|
});
|
|
34
|
-
console.log(`Working directory: ${workDir}`);
|
|
35
|
-
const zmodelPath = path.join(workDir, "schema.zmodel");
|
|
36
|
-
const noPrelude = schemaText.includes("datasource ");
|
|
37
|
-
fs.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbName)}
|
|
38
|
-
|
|
39
|
-
${schemaText}`);
|
|
40
|
-
const pluginModelFiles = glob.sync(path.resolve(__dirname, "../../runtime/src/plugins/**/plugin.zmodel"));
|
|
41
|
-
const generator = new TsSchemaGenerator();
|
|
42
|
-
const tsPath = path.join(workDir, "schema.ts");
|
|
43
|
-
await generator.generate(zmodelPath, pluginModelFiles, tsPath);
|
|
44
12
|
fs.mkdirSync(path.join(workDir, "node_modules"));
|
|
45
13
|
const nodeModules = fs.readdirSync(path.join(__dirname, "../node_modules"));
|
|
46
14
|
for (const entry of nodeModules) {
|
|
@@ -52,31 +20,86 @@ ${schemaText}`);
|
|
|
52
20
|
const zenstackPackages = [
|
|
53
21
|
"language",
|
|
54
22
|
"sdk",
|
|
55
|
-
"runtime"
|
|
23
|
+
"runtime",
|
|
24
|
+
"cli"
|
|
56
25
|
];
|
|
57
26
|
fs.mkdirSync(path.join(workDir, "node_modules/@zenstackhq"));
|
|
58
27
|
for (const pkg of zenstackPackages) {
|
|
59
|
-
fs.symlinkSync(path.join(__dirname, `../../${pkg}
|
|
28
|
+
fs.symlinkSync(path.join(__dirname, `../../${pkg}`), path.join(workDir, `node_modules/@zenstackhq/${pkg}`), "dir");
|
|
60
29
|
}
|
|
61
30
|
fs.writeFileSync(path.join(workDir, "package.json"), JSON.stringify({
|
|
62
31
|
name: "test",
|
|
63
32
|
version: "1.0.0",
|
|
64
33
|
type: "module"
|
|
65
|
-
}));
|
|
34
|
+
}, null, 4));
|
|
66
35
|
fs.writeFileSync(path.join(workDir, "tsconfig.json"), JSON.stringify({
|
|
67
36
|
compilerOptions: {
|
|
68
37
|
module: "ESNext",
|
|
69
38
|
target: "ESNext",
|
|
70
39
|
moduleResolution: "Bundler",
|
|
71
40
|
esModuleInterop: true,
|
|
72
|
-
skipLibCheck: true
|
|
41
|
+
skipLibCheck: true,
|
|
42
|
+
strict: true
|
|
43
|
+
},
|
|
44
|
+
include: [
|
|
45
|
+
"**/*.ts"
|
|
46
|
+
]
|
|
47
|
+
}, null, 4));
|
|
48
|
+
return workDir;
|
|
49
|
+
}
|
|
50
|
+
__name(createTestProject, "createTestProject");
|
|
51
|
+
|
|
52
|
+
// src/schema.ts
|
|
53
|
+
import { TsSchemaGenerator } from "@zenstackhq/sdk";
|
|
54
|
+
import { glob } from "glob";
|
|
55
|
+
import { execSync } from "child_process";
|
|
56
|
+
import fs2 from "fs";
|
|
57
|
+
import path2 from "path";
|
|
58
|
+
import { match } from "ts-pattern";
|
|
59
|
+
function makePrelude(provider, dbName) {
|
|
60
|
+
return match(provider).with("sqlite", () => {
|
|
61
|
+
return `
|
|
62
|
+
datasource db {
|
|
63
|
+
provider = 'sqlite'
|
|
64
|
+
url = '${dbName ?? ":memory:"}'
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
}).with("postgresql", () => {
|
|
68
|
+
return `
|
|
69
|
+
datasource db {
|
|
70
|
+
provider = 'postgresql'
|
|
71
|
+
url = 'postgres://postgres:postgres@localhost:5432/${dbName}'
|
|
72
|
+
}
|
|
73
|
+
`;
|
|
74
|
+
}).exhaustive();
|
|
75
|
+
}
|
|
76
|
+
__name(makePrelude, "makePrelude");
|
|
77
|
+
async function generateTsSchema(schemaText, provider = "sqlite", dbName, extraSourceFiles) {
|
|
78
|
+
const workDir = createTestProject();
|
|
79
|
+
console.log(`Working directory: ${workDir}`);
|
|
80
|
+
const zmodelPath = path2.join(workDir, "schema.zmodel");
|
|
81
|
+
const noPrelude = schemaText.includes("datasource ");
|
|
82
|
+
fs2.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbName)}
|
|
83
|
+
|
|
84
|
+
${schemaText}`);
|
|
85
|
+
const pluginModelFiles = glob.sync(path2.resolve(__dirname, "../../runtime/src/plugins/**/plugin.zmodel"));
|
|
86
|
+
const generator = new TsSchemaGenerator();
|
|
87
|
+
const tsPath = path2.join(workDir, "schema.ts");
|
|
88
|
+
await generator.generate(zmodelPath, pluginModelFiles, tsPath);
|
|
89
|
+
if (extraSourceFiles) {
|
|
90
|
+
for (const [fileName, content] of Object.entries(extraSourceFiles)) {
|
|
91
|
+
const filePath = path2.resolve(workDir, `${fileName}.ts`);
|
|
92
|
+
fs2.mkdirSync(path2.dirname(filePath), {
|
|
93
|
+
recursive: true
|
|
94
|
+
});
|
|
95
|
+
fs2.writeFileSync(filePath, content);
|
|
73
96
|
}
|
|
74
|
-
}
|
|
97
|
+
}
|
|
75
98
|
execSync("npx tsc", {
|
|
76
99
|
cwd: workDir,
|
|
77
100
|
stdio: "inherit"
|
|
78
101
|
});
|
|
79
|
-
const module = await import(
|
|
102
|
+
const module = await import(path2.join(workDir, "schema.js"));
|
|
80
103
|
return {
|
|
81
104
|
workDir,
|
|
82
105
|
schema: module.schema
|
|
@@ -84,11 +107,12 @@ ${schemaText}`);
|
|
|
84
107
|
}
|
|
85
108
|
__name(generateTsSchema, "generateTsSchema");
|
|
86
109
|
function generateTsSchemaFromFile(filePath) {
|
|
87
|
-
const schemaText =
|
|
110
|
+
const schemaText = fs2.readFileSync(filePath, "utf8");
|
|
88
111
|
return generateTsSchema(schemaText);
|
|
89
112
|
}
|
|
90
113
|
__name(generateTsSchemaFromFile, "generateTsSchemaFromFile");
|
|
91
114
|
export {
|
|
115
|
+
createTestProject,
|
|
92
116
|
generateTsSchema,
|
|
93
117
|
generateTsSchemaFromFile
|
|
94
118
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schema.ts"],"sourcesContent":["import { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { glob } from 'glob';\nimport { execSync } from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport
|
|
1
|
+
{"version":3,"sources":["../src/project.ts","../src/schema.ts"],"sourcesContent":["import fs from 'node:fs';\nimport path from 'node:path';\nimport tmp from 'tmp';\n\nexport function createTestProject() {\n const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });\n\n fs.mkdirSync(path.join(workDir, 'node_modules'));\n\n // symlink all entries from \"node_modules\"\n const nodeModules = fs.readdirSync(path.join(__dirname, '../node_modules'));\n for (const entry of nodeModules) {\n if (entry.startsWith('@zenstackhq')) {\n continue;\n }\n fs.symlinkSync(\n path.join(__dirname, '../node_modules', entry),\n path.join(workDir, 'node_modules', entry),\n 'dir',\n );\n }\n\n // in addition, symlink zenstack packages\n const zenstackPackages = ['language', 'sdk', 'runtime', 'cli'];\n fs.mkdirSync(path.join(workDir, 'node_modules/@zenstackhq'));\n for (const pkg of zenstackPackages) {\n fs.symlinkSync(\n path.join(__dirname, `../../${pkg}`),\n path.join(workDir, `node_modules/@zenstackhq/${pkg}`),\n 'dir',\n );\n }\n\n fs.writeFileSync(\n path.join(workDir, 'package.json'),\n JSON.stringify(\n {\n name: 'test',\n version: '1.0.0',\n type: 'module',\n },\n null,\n 4,\n ),\n );\n\n fs.writeFileSync(\n path.join(workDir, 'tsconfig.json'),\n JSON.stringify(\n {\n compilerOptions: {\n module: 'ESNext',\n target: 'ESNext',\n moduleResolution: 'Bundler',\n esModuleInterop: true,\n skipLibCheck: true,\n strict: true,\n },\n include: ['**/*.ts'],\n },\n null,\n 4,\n ),\n );\n\n return workDir;\n}\n","import { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { glob } from 'glob';\nimport { execSync } from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { match } from 'ts-pattern';\nimport { createTestProject } from './project';\n\nfunction makePrelude(provider: 'sqlite' | 'postgresql', dbName?: string) {\n return match(provider)\n .with('sqlite', () => {\n return `\ndatasource db {\n provider = 'sqlite'\n url = '${dbName ?? ':memory:'}'\n}\n`;\n })\n .with('postgresql', () => {\n return `\ndatasource db {\n provider = 'postgresql'\n url = 'postgres://postgres:postgres@localhost:5432/${dbName}'\n}\n`;\n })\n .exhaustive();\n}\n\nexport async function generateTsSchema(\n schemaText: string,\n provider: 'sqlite' | 'postgresql' = 'sqlite',\n dbName?: string,\n extraSourceFiles?: Record<string, string>,\n) {\n const workDir = createTestProject();\n console.log(`Working directory: ${workDir}`);\n\n const zmodelPath = path.join(workDir, 'schema.zmodel');\n const noPrelude = schemaText.includes('datasource ');\n fs.writeFileSync(zmodelPath, `${noPrelude ? '' : makePrelude(provider, dbName)}\\n\\n${schemaText}`);\n\n const pluginModelFiles = glob.sync(path.resolve(__dirname, '../../runtime/src/plugins/**/plugin.zmodel'));\n\n const generator = new TsSchemaGenerator();\n const tsPath = path.join(workDir, 'schema.ts');\n await generator.generate(zmodelPath, pluginModelFiles, tsPath);\n\n if (extraSourceFiles) {\n for (const [fileName, content] of Object.entries(extraSourceFiles)) {\n const filePath = path.resolve(workDir, `${fileName}.ts`);\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n fs.writeFileSync(filePath, content);\n }\n }\n\n // compile the generated TS schema\n execSync('npx tsc', {\n cwd: workDir,\n stdio: 'inherit',\n });\n\n // load the schema module\n const module = await import(path.join(workDir, 'schema.js'));\n return { workDir, schema: module.schema as SchemaDef };\n}\n\nexport function generateTsSchemaFromFile(filePath: string) {\n const schemaText = fs.readFileSync(filePath, 'utf8');\n return generateTsSchema(schemaText);\n}\n"],"mappings":";;;;AAAA,OAAOA,QAAQ;AACf,OAAOC,UAAU;AACjB,OAAOC,SAAS;AAET,SAASC,oBAAAA;AACZ,QAAM,EAAEC,MAAMC,QAAO,IAAKC,IAAIC,QAAQ;IAAEC,eAAe;EAAK,CAAA;AAE5DC,KAAGC,UAAUC,KAAKC,KAAKP,SAAS,cAAA,CAAA;AAGhC,QAAMQ,cAAcJ,GAAGK,YAAYH,KAAKC,KAAKG,WAAW,iBAAA,CAAA;AACxD,aAAWC,SAASH,aAAa;AAC7B,QAAIG,MAAMC,WAAW,aAAA,GAAgB;AACjC;IACJ;AACAR,OAAGS,YACCP,KAAKC,KAAKG,WAAW,mBAAmBC,KAAAA,GACxCL,KAAKC,KAAKP,SAAS,gBAAgBW,KAAAA,GACnC,KAAA;EAER;AAGA,QAAMG,mBAAmB;IAAC;IAAY;IAAO;IAAW;;AACxDV,KAAGC,UAAUC,KAAKC,KAAKP,SAAS,0BAAA,CAAA;AAChC,aAAWe,OAAOD,kBAAkB;AAChCV,OAAGS,YACCP,KAAKC,KAAKG,WAAW,SAASK,GAAAA,EAAK,GACnCT,KAAKC,KAAKP,SAAS,4BAA4Be,GAAAA,EAAK,GACpD,KAAA;EAER;AAEAX,KAAGY,cACCV,KAAKC,KAAKP,SAA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,MAAInB,kBAAkB;AAClB,eAAW,CAACqB,UAAUC,OAAAA,KAAYC,OAAOC,QAAQxB,gBAAAA,GAAmB;AAChE,YAAMyB,WAAWnB,MAAKS,QAAQd,SAAS,GAAGoB,QAAAA,KAAa;AACvDX,MAAAA,IAAGgB,UAAUpB,MAAKqB,QAAQF,QAAAA,GAAW;QAAEG,WAAW;MAAK,CAAA;AACvDlB,MAAAA,IAAGC,cAAcc,UAAUH,OAAAA;IAC/B;EACJ;AAGAO,WAAS,WAAW;IAChBC,KAAK7B;IACL8B,OAAO;EACX,CAAA;AAGA,QAAMC,SAAS,MAAM,OAAO1B,MAAKC,KAAKN,SAAS,WAAA;AAC/C,SAAO;IAAEA;IAASgC,QAAQD,OAAOC;EAAoB;AACzD;AApCsBnC;AAsCf,SAASoC,yBAAyBT,UAAgB;AACrD,QAAM1B,aAAaW,IAAGyB,aAAaV,UAAU,MAAA;AAC7C,SAAO3B,iBAAiBC,UAAAA;AAC5B;AAHgBmC;","names":["fs","path","tmp","createTestProject","name","workDir","tmp","dirSync","unsafeCleanup","fs","mkdirSync","path","join","nodeModules","readdirSync","__dirname","entry","startsWith","symlinkSync","zenstackPackages","pkg","writeFileSync","JSON","stringify","version","type","compilerOptions","module","target","moduleResolution","esModuleInterop","skipLibCheck","strict","include","TsSchemaGenerator","glob","execSync","fs","path","match","makePrelude","provider","dbName","match","with","exhaustive","generateTsSchema","schemaText","extraSourceFiles","workDir","createTestProject","console","log","zmodelPath","path","join","noPrelude","includes","fs","writeFileSync","pluginModelFiles","glob","sync","resolve","__dirname","generator","TsSchemaGenerator","tsPath","generate","fileName","content","Object","entries","filePath","mkdirSync","dirname","recursive","execSync","cwd","stdio","module","schema","generateTsSchemaFromFile","readFileSync"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenstackhq/testtools",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.7",
|
|
4
4
|
"description": "ZenStack Test Tools",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [],
|
|
@@ -22,27 +22,27 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@types/node": "^20.0.0",
|
|
26
25
|
"glob": "^11.0.2",
|
|
27
26
|
"tmp": "^0.2.3",
|
|
28
27
|
"ts-pattern": "^5.7.1",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"@zenstackhq/
|
|
32
|
-
"@zenstackhq/
|
|
28
|
+
"prisma": "^6.0.0",
|
|
29
|
+
"typescript": "^5.0.0",
|
|
30
|
+
"@zenstackhq/language": "3.0.0-alpha.7",
|
|
31
|
+
"@zenstackhq/sdk": "3.0.0-alpha.7"
|
|
33
32
|
},
|
|
34
33
|
"peerDependencies": {
|
|
35
34
|
"better-sqlite3": "^11.8.1",
|
|
36
35
|
"pg": "^8.13.1"
|
|
37
36
|
},
|
|
38
37
|
"devDependencies": {
|
|
39
|
-
"@types/tmp": "^0.2.6"
|
|
38
|
+
"@types/tmp": "^0.2.6",
|
|
39
|
+
"@zenstackhq/eslint-config": "3.0.0-alpha.7",
|
|
40
|
+
"@zenstackhq/typescript-config": "3.0.0-alpha.7"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
43
|
"build": "tsup-node",
|
|
43
44
|
"watch": "tsup-node --watch",
|
|
44
45
|
"lint": "eslint src --ext ts",
|
|
45
|
-
"test": "vitest",
|
|
46
46
|
"pack": "pnpm pack"
|
|
47
47
|
}
|
|
48
48
|
}
|