|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { exec } from "node:child_process"; |
| 18 | +import { readdirSync, renameSync, statSync } from "node:fs"; |
| 19 | +import { join, resolve } from "node:path"; |
| 20 | +import { promisify } from "node:util"; |
| 21 | + |
| 22 | +const execAsync = promisify(exec); |
| 23 | + |
| 24 | +async function findGsFiles( |
| 25 | + dir: string, |
| 26 | + fileList: string[] = [], |
| 27 | +): Promise<string[]> { |
| 28 | + const files = readdirSync(dir); |
| 29 | + for (const file of files) { |
| 30 | + const filePath = join(dir, file); |
| 31 | + if ( |
| 32 | + file === "node_modules" || |
| 33 | + file === ".git" || |
| 34 | + file === "dist" || |
| 35 | + file === "target" || |
| 36 | + file === "pkg" |
| 37 | + ) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + const stat = statSync(filePath); |
| 41 | + if (stat.isDirectory()) { |
| 42 | + await findGsFiles(filePath, fileList); |
| 43 | + } else if (file.endsWith(".gs") && file !== "moment.gs") { |
| 44 | + fileList.push(filePath); |
| 45 | + } |
| 46 | + } |
| 47 | + return fileList; |
| 48 | +} |
| 49 | + |
| 50 | +async function main() { |
| 51 | + const command = process.argv[2]; // 'lint' or 'format' |
| 52 | + if (command !== "lint" && command !== "format") { |
| 53 | + console.error("Usage: tsx biome-gs.ts [lint|format]"); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + const rootDir = resolve("."); |
| 58 | + const gsFiles = await findGsFiles(rootDir); |
| 59 | + const renamedFiles: { oldPath: string; newPath: string }[] = []; |
| 60 | + |
| 61 | + const restoreFiles = () => { |
| 62 | + for (const { oldPath, newPath } of renamedFiles) { |
| 63 | + try { |
| 64 | + renameSync(newPath, oldPath); |
| 65 | + } catch (e) { |
| 66 | + console.error(`Failed to restore ${newPath} to ${oldPath}:`, e); |
| 67 | + } |
| 68 | + } |
| 69 | + renamedFiles.length = 0; |
| 70 | + }; |
| 71 | + |
| 72 | + process.on("SIGINT", () => { |
| 73 | + restoreFiles(); |
| 74 | + process.exit(1); |
| 75 | + }); |
| 76 | + process.on("SIGTERM", () => { |
| 77 | + restoreFiles(); |
| 78 | + process.exit(1); |
| 79 | + }); |
| 80 | + process.on("exit", restoreFiles); |
| 81 | + |
| 82 | + try { |
| 83 | + // 1. Rename .gs to .gs.js |
| 84 | + for (const gsFile of gsFiles) { |
| 85 | + const jsFile = `${gsFile}.js`; |
| 86 | + renameSync(gsFile, jsFile); |
| 87 | + renamedFiles.push({ oldPath: gsFile, newPath: jsFile }); |
| 88 | + } |
| 89 | + |
| 90 | + // 2. Run Biome |
| 91 | + const biomeArgs = command === "format" ? "check --write ." : "check ."; |
| 92 | + console.log(`Running biome ${biomeArgs}...`); |
| 93 | + try { |
| 94 | + const { stdout, stderr } = await execAsync( |
| 95 | + `pnpm exec biome ${biomeArgs}`, |
| 96 | + { cwd: rootDir }, |
| 97 | + ); |
| 98 | + if (stdout) console.log(stdout.replace(/\.gs\.js/g, ".gs")); |
| 99 | + if (stderr) console.error(stderr.replace(/\.gs\.js/g, ".gs")); |
| 100 | + } catch (e: unknown) { |
| 101 | + const err = e as { stdout?: string; stderr?: string }; |
| 102 | + if (err.stdout) console.log(err.stdout.replace(/\.gs\.js/g, ".gs")); |
| 103 | + if (err.stderr) console.error(err.stderr.replace(/\.gs\.js/g, ".gs")); |
| 104 | + // Don't exit yet, we need to restore files |
| 105 | + } |
| 106 | + } catch (err) { |
| 107 | + console.error("An error occurred:", err); |
| 108 | + } finally { |
| 109 | + restoreFiles(); |
| 110 | + // Remove listeners to avoid double-running or issues on exit |
| 111 | + process.removeAllListeners("exit"); |
| 112 | + process.removeAllListeners("SIGINT"); |
| 113 | + process.removeAllListeners("SIGTERM"); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +main().catch((err) => { |
| 118 | + console.error(err); |
| 119 | + process.exit(1); |
| 120 | +}); |
0 commit comments