Debian Codegraph @

packages/babel-helpers/scripts/generate-helpers.js

raw symbols SCIP
1 import fs from "fs";
2 import { join } from "path";
3 import { URL, fileURLToPath } from "url";
4 import { minify } from "terser"; // eslint-disable-line import/no-extraneous-dependencies
5
6 const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
7 const IGNORED_FILES = new Set(["package.json"]);
8
9 export default async function generateHelpers() {
10 let output = `/*
11 * This file is auto-generated! Do not modify it directly.
12 * To re-generate run 'yarn gulp generate-runtime-helpers'
13 */
14
15 import template from "@babel/template";
16
17 function helper(minVersion: string, source: string) {
18 return Object.freeze({
19 minVersion,
20 ast: () => template.program.ast(source, { preserveComments: true }),
21 })
22 }
23
24 export default Object.freeze({
25 `;
26
27 for (const file of (await fs.promises.readdir(HELPERS_FOLDER)).sort()) {
28 if (IGNORED_FILES.has(file)) continue;
29 if (file.startsWith(".")) continue; // ignore e.g. vim swap files
30
31 const [helperName] = file.split(".");
32
33 const filePath = join(fileURLToPath(HELPERS_FOLDER), file);
34 if (!file.endsWith(".js")) {
35 console.error("ignoring", filePath);
36 continue;
37 }
38
39 const fileContents = await fs.promises.readFile(filePath, "utf8");
40 const minVersionMatch = fileContents.match(
41 /^\s*\/\*\s*@minVersion\s+(?<minVersion>\S+)\s*\*\/\s*$/m
42 );
43 if (!minVersionMatch) {
44 throw new Error(`@minVersion number missing in ${filePath}`);
45 }
46 const { minVersion } = minVersionMatch.groups;
47
48 const source = await minify(fileContents, {
49 mangle: false,
50 // The _typeof helper has a custom directive that we must keep
51 compress: { directives: false },
52 });
53
54 output += `\
55 ${JSON.stringify(helperName)}: helper(
56 ${JSON.stringify(minVersion)},
57 ${JSON.stringify(source.code)},
58 ),
59 `;
60 }
61
62 output += "});";
63 return output;
64 }