deno.land / x / pothos@release-1713397530 / scripts / build.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as fs from 'fs/promises';import { statSync, existsSync } from 'fs';import * as path from 'path';import * as ts from 'typescript';
const packageDir = path.resolve(__dirname, '../../');const targetDir = path.resolve(__dirname, '../packages');const excludedPackages = [ 'converter', 'deno', 'plugin-example', 'plugin-prisma', 'plugin-prisma-utils', 'test-utils', 'plugin-federation', 'tracing-opentelemetry', 'tracing-sentry', 'tracing-newrelic', 'tracing-xray',];const excludedDirs = ['esm', 'lib', 'test', 'tests', 'node_modules', 'prisma'];const excludedFiles = [ 'package.json', 'tsconfig.json', 'tsconfig.type.json', 'tsconfig.tsbuildinfo', 'tsconfig.type.tsbuildinfo', 'CHANGELOG.md', '.npmignore', 'babel.config.js',];
const moduleMap: Record<string, string> = { graphql: 'https://cdn.skypack.dev/graphql?dts', zod: 'https://cdn.skypack.dev/zod@v1.11.17?dts', dataloader: 'https://cdn.skypack.dev/dataloader?dts', '@pothos/core': './core/index.ts', '@pothos/plugin-directives': './plugin-directives/index.ts',};
type LoadedFile = { path: string; content: Buffer;};
const modFile = `import Default from './index.ts';export * from './index.ts';export default Default;`;
async function getFiles(dir: string, root = false): Promise<string[]> { const results = await fs.readdir(dir, { withFileTypes: true, });
const paths = await Promise.all( results .filter( (entry) => !excludedFiles.includes(entry.name) && (!root || !excludedDirs.includes(entry.name)), ) .map((entry): string[] | Promise<string[]> => { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { return getFiles(fullPath); }
return [fullPath]; }), );
return paths.flatMap((entry) => entry);}
async function getPackages() { const entries = await fs.readdir(packageDir);
return entries.filter((entry) => !excludedPackages.includes(entry));}
async function loadFile(file: string): Promise<LoadedFile> { const newPath = path.resolve(targetDir, path.relative(packageDir, file)).replace('/src', ''); const printer = ts.createPrinter();
if (file.endsWith('.ts')) { const source = ts.createSourceFile( file, await fs.readFile(file, 'utf-8'), ts.ScriptTarget.ESNext, ); const result = ts.transform(source, [importTransformer]);
return { path: newPath, content: Buffer.from(`// @ts-nocheck\n${printer.printFile(result.transformed[0])}`, 'utf8'), }; } else { return { path: newPath, content: await fs.readFile(file), }; }}
async function getAllFiles() { const projects = await getPackages();
const results = await Promise.all( projects.map((dir) => getFiles(path.join(packageDir, dir), true)), );
const files = results.flatMap((entry) => entry); const modFiles = projects.map((dir) => ({ path: path.join(targetDir, dir, 'mod.ts'), content: Buffer.from(modFile, 'utf8'), }));
return [...(await Promise.all(files.map((file) => loadFile(file)))), ...modFiles];}
const importTransformer: ts.TransformerFactory<ts.SourceFile> = (context) => { return (sourceFile) => { const visitor = (node: ts.Node): ts.Node => { if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) { const { moduleSpecifier } = node; if (moduleSpecifier && ts.isStringLiteralLike(moduleSpecifier)) { const dirName = path.dirname(sourceFile.fileName); let mod = moduleSpecifier.text;
if (mod.startsWith('.')) { const modulePath = path.resolve(dirName, mod); const tsPath = modulePath + '.ts'; const dtsPath = modulePath + '.d.ts';
const stat = existsSync(modulePath) && statSync(modulePath); if (stat && stat.isDirectory()) { mod = path.join(modulePath, 'index.ts'); } else if (existsSync(tsPath)) { mod = tsPath; } else if (existsSync(dtsPath)) { mod = dtsPath; } else if (!existsSync(modulePath)) { throw new Error(`Unable to resolve modulePath ${modulePath}`); }
mod = path.relative(dirName, mod).replace(/^([^\.])/, './$1'); } else if (moduleMap[mod]) { const newMod = moduleMap[mod];
if (newMod.startsWith('.')) { mod = path.relative(dirName.replace('/src', ''), path.resolve(packageDir, newMod)); } else { mod = moduleMap[mod]; } } else { throw new Error(`Unknown module ${mod} in ${sourceFile.fileName}`); }
if (ts.isImportDeclaration(node)) { return ts.factory.updateImportDeclaration( node, node.modifiers, node.importClause, ts.factory.createStringLiteral(mod, true), undefined, ); }
return ts.factory.updateExportDeclaration( node, node.modifiers, node.isTypeOnly, node.exportClause, ts.factory.createStringLiteral(mod, true), undefined, ); } }
return ts.visitEachChild(node, visitor, context); };
return ts.visitNode(sourceFile, visitor); };};
async function build() { await fs.rm(targetDir, { recursive: true, force: true });
const files = await getAllFiles(); const results = files.map(async (file) => { await fs.mkdir(path.dirname(file.path), { recursive: true }); await fs.writeFile(file.path, file.content); }); await Promise.all(results);}
build().catch((error) => { throw error;});
pothos

Version Info

Tagged at
a month ago