UNPKG

sanity

Version:

Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches

40 lines (39 loc) 1.99 kB
import { spawn } from "child_process"; import fs from "fs/promises"; import path from "path"; import readPkgUp from "read-pkg-up"; import { hideBin } from "yargs/helpers"; import yargs from "yargs/yargs"; async function parseCliFlags(args) { return { ...await yargs(hideBin(args.argv || process.argv).slice(2)).option("with-user-token", { type: "boolean", default: !1 }).option("mock-browser-env", { type: "boolean", default: !1 }).argv, script: args.argsWithoutOptions[0] }; } const execScript = async function(args, context) { var _a; const { withUserToken, mockBrowserEnv, script } = await parseCliFlags(args), { workDir } = context, scriptPath = path.resolve(script || ""); if (!script) throw new Error("SCRIPT must be provided. `sanity exec <script>`"); if (!await fs.stat(scriptPath).catch(() => !1)) throw new Error(`${scriptPath} does not exist`); const sanityPkgPath = (_a = await readPkgUp({ cwd: __dirname })) == null ? void 0 : _a.path; if (!sanityPkgPath) throw new Error("Unable to resolve `sanity` module root"); const sanityDir = path.dirname(sanityPkgPath), threadsDir = path.join(sanityDir, "lib", "_internal", "cli", "threads"), esbuildPath = path.join(threadsDir, "esbuild.js"), browserEnvPath = path.join(threadsDir, "registerBrowserEnv.js"), configClientPath = path.join(threadsDir, "configClient.js"); if (!await fs.stat(esbuildPath).catch(() => !1)) throw new Error("`sanity` module build error: missing threads"); const baseArgs = mockBrowserEnv ? ["-r", browserEnvPath] : ["-r", esbuildPath], tokenArgs = withUserToken ? ["-r", configClientPath] : [], nodeArgs = [...baseArgs, ...tokenArgs, scriptPath, ...args.extraArguments]; spawn(process.argv[0], nodeArgs, { stdio: "inherit", env: { // eslint-disable-next-line no-process-env ...process.env, SANITY_BASE_PATH: workDir } }).on("close", process.exit); }; export { execScript as default }; //# sourceMappingURL=execScript.js.map