-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathgenerate-sdks.ts
More file actions
197 lines (177 loc) · 5.89 KB
/
generate-sdks.ts
File metadata and controls
197 lines (177 loc) · 5.89 KB
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
import fs from "fs";
import path from "path";
import { COMMENT_BLOCK, COMMENT_LINE, PLATFORMS, copyFromSrcToDest, processMacros, withGeneratorLock, writeFileSyncIfChanged } from "./utils";
/**
* Main function to generate from a template:
* 1. Ensures the destination exists.
* 2. Copies from src to dest (applying a composite editFn).
* 3. Removes any items in dest that aren’t in src.
* 4. Cleans up empty folders.
*
* The composite editFn encapsulates the hard rules:
* - Global ignores (e.g. node_modules, dist, etc.)
* - Skipping source package.json.
* - Renaming package-template.json -> package.json.
* - Inserting header comments into .tsx, .ts, or .js files.
* - Adding a comment field in package.json files.
*
* Custom editFns provided in options can further modify content.
*/
function generateFromTemplate(options: {
src: string;
dest: string;
editFn?: (relativePath: string, content: string) => string;
filterFn?: (relativePath: string) => boolean;
destFn?: (relativePath: string) => string;
}) {
const { src, dest, editFn, filterFn, destFn } = options;
// Composite edit function that applies the hard rules first,
// then defers to any custom edit function.
function compositeEditFn(
relativePath: string,
content: string
): string {
let newContent: string = editFn ? editFn(relativePath, content) : content;
// For .tsx, .ts, or .js files, add header comments.
if (/\.(tsx|ts|js)$/.test(relativePath)) {
const hasShebang =
newContent.startsWith("#") ||
newContent.startsWith('"') ||
newContent.startsWith("'");
let shebangLine = "";
let contentWithoutShebang = newContent;
if (hasShebang) {
const lines = newContent.split("\n");
shebangLine = lines[0] + "\n\n";
contentWithoutShebang = lines.slice(1).join("\n");
}
newContent = shebangLine + COMMENT_BLOCK + contentWithoutShebang;
}
// If the resulting file is package.json, add a comment field to the JSON.
if (path.basename(relativePath) === "package.json") {
const jsonObj = JSON.parse(newContent);
newContent = JSON.stringify({ "//": COMMENT_LINE, ...jsonObj }, null, 2);
}
return newContent;
}
function compositeDestFn(relativePath: string) {
if (relativePath === "package-template.json") {
return "package.json";
}
if (destFn) {
return destFn(relativePath);
}
return relativePath;
}
function compositeFilterFn(relativePath: string) {
const ignores = ["node_modules", "dist", ".turbo", ".gitignore", "package.json"];
for (const ignore of ignores) {
if (relativePath.startsWith(ignore)) {
return false;
}
}
if (filterFn) {
return filterFn(relativePath);
}
return true;
}
// Ensure the destination directory exists.
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
copyFromSrcToDest({
srcDir: src,
destDir: dest,
editFn: compositeEditFn,
filterFn: compositeFilterFn,
destFn: compositeDestFn,
destRemoveSkipFn: (relativePath) => {
return relativePath.startsWith("node_modules") || relativePath.startsWith("dist") || relativePath.startsWith(".turbo");
},
});
}
function processPackageJson(path: string, content: string) {
let jsonObj: any;
try {
jsonObj = JSON.parse(content);
} catch (error) {
throw new Error(`Failed to parse package.json at ${path}`, { cause: error });
}
return JSON.stringify({ "//": `${COMMENT_LINE} (FOR package.json FILES, PLEASE EDIT package-template.json)`, ...jsonObj }, null, 2);
}
function baseEditFn(options: {
relativePath: string,
content: string,
platforms: string[]
}) {
if (options.relativePath.startsWith("src/generated")) {
return options.content;
}
const result = processMacros(options.content, options.platforms);
if (options.relativePath === 'package-template.json') {
return processPackageJson(options.relativePath, result);
}
return result;
}
withGeneratorLock(async () => {
const baseDir = path.resolve(__dirname, "..", "packages");
const srcDir = path.resolve(baseDir, "template");
// Copy package-template.json to package.json in the template,
// applying macros and adding a comment field.
const packageTemplateContent = fs.readFileSync(
path.join(srcDir, "package-template.json"),
"utf-8"
);
const processedPackageJson = processMacros(packageTemplateContent, PLATFORMS["template"]);
writeFileSyncIfChanged(
path.join(srcDir, "package.json"),
processPackageJson(path.join(srcDir, "package-template.json"), processedPackageJson)
);
generateFromTemplate({
src: srcDir,
dest: path.resolve(baseDir, "js"),
editFn: (relativePath, content) => {
return baseEditFn({ relativePath, content, platforms: PLATFORMS["js"] });
},
filterFn: (relativePath) => {
const ignores = [
"postcss.config.js",
"tailwind.config.js",
"quetzal.config.json",
"components.json",
".env",
".env.local",
"scripts/",
"quetzal-translations/",
"src/components/",
"src/components-page/",
"src/generated/",
"src/providers/",
"src/global.css",
"src/global.d.ts",
];
if (ignores.some((ignorePath) => relativePath.startsWith(ignorePath)) || relativePath.endsWith(".tsx")) {
return false;
} else {
return true;
}
},
});
generateFromTemplate({
src: srcDir,
dest: path.resolve(baseDir, "stack"),
editFn: (relativePath, content) => {
return baseEditFn({ relativePath, content, platforms: PLATFORMS["next"] });
},
});
generateFromTemplate({
src: srcDir,
dest: path.resolve(baseDir, "react"),
editFn: (relativePath, content) => {
return baseEditFn({ relativePath, content, platforms: PLATFORMS["react"] });
},
});
}).catch((error) => {
console.error(error);
process.exit(1);
});