MCPcopy
hub / github.com/cloudflare/capnweb / runBuild

Function runBuild

packages/capnweb-validate/src/transform/run.ts:152–219  ·  view source on GitHub ↗
(options: BuildOptions)

Source from the content-addressed store, hash-verified

150}
151
152export async function runBuild(options: BuildOptions): Promise<BuildResult> {
153 let cwd = resolve(options.cwd ?? process.cwd());
154 let out = resolve(cwd, options.out);
155 if (isInsideOrEqual(out, cwd)) {
156 throw new Error(
157 `capnweb-validate: --out must not be the project directory or a parent ` +
158 `of it (cwd=${cwd}, out=${out}).`,
159 );
160 }
161 if (await hasSymlinkParentPathPart(out, cwd)) {
162 throw new Error(
163 `capnweb-validate: --out must not be inside a symlinked directory ` +
164 `(cwd=${cwd}, out=${out}).`,
165 );
166 }
167
168 // Remove stale output before building. Do this before creating the TS Program
169 // so deleted/renamed files from a previous run cannot stay in the source set.
170 await rm(out, { recursive: true, force: true });
171
172 let context = createTransformContext({
173 ...options,
174 tsconfig: options.tsconfig ?? "tsconfig.json",
175 });
176 let transformed = 0;
177 let copied = 0;
178 let skippedOutside: string[] = [];
179 let written = new Set<string>();
180 let assetRefs = new Set<string>();
181 try {
182 for (let id of context.listSourceFiles()) {
183 if (isInsideOrEqual(out, id)) continue;
184 let dest = resolve(out, relative(cwd, id));
185 // A source file outside cwd (e.g. an included sibling dir) maps to a dest
186 // that escapes --out and could clobber unrelated files. Skip it rather
187 // than write outside the requested output tree; warned once below.
188 if (!isInsideOrEqual(out, dest)) {
189 skippedOutside.push(id);
190 continue;
191 }
192 let code = await readFile(id, "utf8");
193 for (let asset of collectAssetRefs(code, id)) assetRefs.add(asset);
194 let result = transformModule(context, id, code);
195 await mkdir(dirname(dest), { recursive: true });
196 if (result) {
197 await writeFile(dest, result.code);
198 transformed++;
199 } else {
200 await writeFile(dest, code);
201 copied++;
202 }
203 written.add(dest);
204 }
205 } finally {
206 context.dispose();
207 }
208
209 copied += await copyAssets(assetRefs, cwd, out, written);

Callers 2

mainFunction · 0.85
plugin.test.tsFile · 0.85

Calls 8

isInsideOrEqualFunction · 0.85
hasSymlinkParentPathPartFunction · 0.85
createTransformContextFunction · 0.85
collectAssetRefsFunction · 0.85
transformModuleFunction · 0.85
copyAssetsFunction · 0.85
listSourceFilesMethod · 0.80
disposeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…