|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { |
| 4 | + chmodSync, |
| 5 | + cpSync, |
| 6 | + existsSync, |
| 7 | + mkdtempSync, |
| 8 | + mkdirSync, |
| 9 | + readdirSync, |
| 10 | + rmSync, |
| 11 | + statSync, |
| 12 | + symlinkSync, |
| 13 | +} from "node:fs"; |
| 14 | +import os from "node:os"; |
| 15 | +import path from "node:path"; |
| 16 | +import { fileURLToPath } from "node:url"; |
| 17 | +import process from "node:process"; |
| 18 | + |
| 19 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 20 | +const packageRoot = path.resolve(scriptDir, ".."); |
| 21 | +const originalCwd = process.cwd(); |
| 22 | + |
| 23 | +function resolveExistingPath(value) { |
| 24 | + if (path.isAbsolute(value)) { |
| 25 | + return value; |
| 26 | + } |
| 27 | + |
| 28 | + const candidates = [ |
| 29 | + path.join(packageRoot, value), |
| 30 | + value, |
| 31 | + path.join(originalCwd, value), |
| 32 | + process.env.JS_BINARY__EXECROOT |
| 33 | + ? path.join(process.env.JS_BINARY__EXECROOT, value) |
| 34 | + : undefined, |
| 35 | + path.resolve(value), |
| 36 | + ].filter(Boolean); |
| 37 | + |
| 38 | + return candidates.find((candidate) => existsSync(candidate)) ?? value; |
| 39 | +} |
| 40 | + |
| 41 | +function resolvePackageCli() { |
| 42 | + const candidates = [ |
| 43 | + path.join( |
| 44 | + packageRoot, |
| 45 | + "node_modules", |
| 46 | + "@sveltejs", |
| 47 | + "package", |
| 48 | + "src", |
| 49 | + "cli.js", |
| 50 | + ), |
| 51 | + path.join( |
| 52 | + originalCwd, |
| 53 | + "node_modules", |
| 54 | + "@sveltejs", |
| 55 | + "package", |
| 56 | + "src", |
| 57 | + "cli.js", |
| 58 | + ), |
| 59 | + process.env.JS_BINARY__EXECROOT |
| 60 | + ? path.join( |
| 61 | + process.env.JS_BINARY__EXECROOT, |
| 62 | + "node_modules", |
| 63 | + "@sveltejs", |
| 64 | + "package", |
| 65 | + "src", |
| 66 | + "cli.js", |
| 67 | + ) |
| 68 | + : undefined, |
| 69 | + ].filter(Boolean); |
| 70 | + |
| 71 | + const cli = candidates.find((candidate) => existsSync(candidate)); |
| 72 | + if (!cli) { |
| 73 | + throw new Error("Unable to locate @sveltejs/package CLI in Bazel runfiles"); |
| 74 | + } |
| 75 | + return cli; |
| 76 | +} |
| 77 | + |
| 78 | +function linkOrCopy(source, destination, copy = false) { |
| 79 | + rmSync(destination, { recursive: true, force: true }); |
| 80 | + if (copy) { |
| 81 | + cpSync(source, destination, { recursive: true }); |
| 82 | + return; |
| 83 | + } |
| 84 | + symlinkSync(source, destination); |
| 85 | +} |
| 86 | + |
| 87 | +function makeWritable(target) { |
| 88 | + const stats = statSync(target); |
| 89 | + chmodSync(target, stats.isDirectory() ? 0o700 : 0o600); |
| 90 | + |
| 91 | + if (!stats.isDirectory()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + for (const entry of readdirSync(target)) { |
| 96 | + makeWritable(path.join(target, entry)); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function prepareWritableProject(inputDir) { |
| 101 | + const tempRoot = mkdtempSync( |
| 102 | + path.join(os.tmpdir(), "scheduling-kit-package-"), |
| 103 | + ); |
| 104 | + const workDir = mkdirSync(path.join(tempRoot, "work"), { recursive: true }); |
| 105 | + |
| 106 | + linkOrCopy(inputDir, path.join(workDir, "src")); |
| 107 | + for (const file of ["package.json", "svelte.config.js", "tsconfig.json"]) { |
| 108 | + linkOrCopy(resolveExistingPath(file), path.join(workDir, file)); |
| 109 | + } |
| 110 | + |
| 111 | + const svelteKitDir = resolveExistingPath(".svelte-kit"); |
| 112 | + if (existsSync(svelteKitDir)) { |
| 113 | + linkOrCopy(svelteKitDir, path.join(workDir, ".svelte-kit"), true); |
| 114 | + makeWritable(path.join(workDir, ".svelte-kit")); |
| 115 | + } |
| 116 | + |
| 117 | + const nodeModulesDir = resolveExistingPath("node_modules"); |
| 118 | + if (existsSync(nodeModulesDir)) { |
| 119 | + linkOrCopy(nodeModulesDir, path.join(workDir, "node_modules")); |
| 120 | + } |
| 121 | + |
| 122 | + return { tempRoot, workDir }; |
| 123 | +} |
| 124 | + |
| 125 | +const forwardedArgs = []; |
| 126 | +let resolvedInput; |
| 127 | +let outputDir = "dist"; |
| 128 | +for (let index = 0; index < process.argv.length - 2; index += 1) { |
| 129 | + const arg = process.argv[index + 2]; |
| 130 | + |
| 131 | + if (arg === "-i" || arg === "--input") { |
| 132 | + resolvedInput = resolveExistingPath(process.argv[index + 3]); |
| 133 | + forwardedArgs.push(arg, "src"); |
| 134 | + index += 1; |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if (arg.startsWith("--input=")) { |
| 139 | + resolvedInput = resolveExistingPath(arg.slice("--input=".length)); |
| 140 | + forwardedArgs.push("--input=src"); |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + if (arg === "-o" || arg === "--output") { |
| 145 | + outputDir = process.argv[index + 3]; |
| 146 | + forwardedArgs.push(arg, path.resolve(originalCwd, outputDir)); |
| 147 | + index += 1; |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if (arg.startsWith("--output=")) { |
| 152 | + outputDir = arg.slice("--output=".length); |
| 153 | + forwardedArgs.push(`--output=${path.resolve(originalCwd, outputDir)}`); |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + forwardedArgs.push(arg); |
| 158 | +} |
| 159 | + |
| 160 | +if (!resolvedInput) { |
| 161 | + throw new Error("Missing required svelte-package input directory"); |
| 162 | +} |
| 163 | + |
| 164 | +const { tempRoot, workDir } = prepareWritableProject(resolvedInput); |
| 165 | +const result = spawnSync( |
| 166 | + process.execPath, |
| 167 | + [resolvePackageCli(), ...forwardedArgs], |
| 168 | + { |
| 169 | + cwd: workDir, |
| 170 | + stdio: "inherit", |
| 171 | + env: process.env, |
| 172 | + }, |
| 173 | +); |
| 174 | +rmSync(tempRoot, { recursive: true, force: true }); |
| 175 | + |
| 176 | +if (result.error) { |
| 177 | + console.error(result.error.message); |
| 178 | + process.exit(1); |
| 179 | +} |
| 180 | + |
| 181 | +process.exit(result.status ?? 1); |
0 commit comments