UNPKG

radashi-helper

Version:
225 lines (216 loc) 5.99 kB
import { RadashiError, exec, forwardStderrAndRethrow } from "./chunk-JLKIE3A2.js"; import { isString, tryit } from "./chunk-VOEQFXI5.js"; // src/util/debug.ts import createDebug from "debug"; var debug = createDebug("radashi"); // src/util/github.ts async function isGitHubAuthenticated() { if (process.env.GITHUB_TOKEN) { return true; } const { exitCode } = await exec("gh", ["auth", "status"], { reject: false }); return exitCode === 0; } async function prepareGitHubDefaultRepo(cwd) { const radashiUrl = "https://github.com/radashi-org/radashi"; const originUrl = await exec("git", ["remote", "get-url", "origin"], { cwd }).then((r) => r.stdout); debug("originUrl", originUrl); if (originUrl !== radashiUrl) { await exec("git", ["remote", "add", "radashi", radashiUrl], { cwd, reject: false }); if (await isGitHubAuthenticated()) { await exec("gh", ["repo", "set-default", "radashi-org/radashi"], { cwd }).catch(forwardStderrAndRethrow); } } } // src/util/cloneRadashi.ts async function getInstalledRadashiRef(pkg) { const specifier = pkg.dependencies?.radashi; if (!specifier) { throw new RadashiError("No radashi dependency found in package.json"); } let ref = specifier.split("@").pop(); if (ref === "beta") { ref = "main"; } else if (ref !== "next") { ref = await exec("npm", ["view", "radashi@" + ref, "--json"]).then( ({ stdout }) => "v" + JSON.parse(stdout).version ); } return ref; } function isPreReleaseRadashiTag(ref) { return /-(beta|alpha)/.test(ref); } function getRadashiCloneURL(ref) { let cloneUrl = "https://github.com/radashi-org/radashi"; if (isPreReleaseRadashiTag(ref)) { cloneUrl += "-canary"; } return cloneUrl; } async function cloneRadashi(ref, dir, opts = {}) { const cloneUrl = getRadashiCloneURL(ref); await exec( "git", ["clone", cloneUrl, "--branch", ref, "--single-branch", dir], opts ).catch((error) => { if (opts.stdio == null) { forwardStderrAndRethrow(error); } throw error; }); await prepareGitHubDefaultRepo(dir); } // ../../node_modules/.pnpm/escalade@3.1.2/node_modules/escalade/sync/index.mjs import { dirname, resolve } from "path"; import { readdirSync, statSync } from "fs"; function sync_default(start, callback) { let dir = resolve(".", start); let tmp, stats = statSync(dir); if (!stats.isDirectory()) { dir = dirname(dir); } while (true) { tmp = callback(dir, readdirSync(dir)); if (tmp) return resolve(dir, tmp); dir = dirname(tmp = dir); if (tmp === dir) break; } } // src/env.ts import { readFileSync } from "node:fs"; import { join, resolve as resolve2 } from "node:path"; // ../../node_modules/.pnpm/p-lazy@4.0.0/node_modules/p-lazy/index.js var PLazy = class _PLazy extends Promise { constructor(executor) { super((resolve3) => { resolve3(); }); this._executor = executor; } static from(function_) { return new _PLazy((resolve3) => { resolve3(function_()); }); } static resolve(value) { return new _PLazy((resolve3) => { resolve3(value); }); } static reject(error) { return new _PLazy((resolve3, reject) => { reject(error); }); } then(onFulfilled, onRejected) { this._promise = this._promise || new Promise(this._executor); return this._promise.then(onFulfilled, onRejected); } catch(onRejected) { this._promise = this._promise || new Promise(this._executor); return this._promise.catch(onRejected); } }; // src/env.ts function getEnv(root) { let configRequired = false; root = root ? resolve2(root) : sync_default(process.cwd(), (dir, files) => { if (dir.includes("radashi")) { return dir; } if (files.includes("radashi.json")) { configRequired = true; return dir; } }); if (!isString(root)) { throw new RadashiError("Could not find your Radashi root directory"); } const pkg = JSON.parse( readFileSync(join(root, "package.json"), "utf8") ); const originUrl = readOriginSync(root); const [configPath, config] = getConfig(root, configRequired); return { pkg, root, modPath: join(root, "mod.ts"), config, configPath, outDir: process.env.RADASHI_OUT_DIR || join(root, "dist"), radashiDir: !originUrl?.includes("github.com/radashi-org/radashi") ? join(root, ".radashi/upstream") : null, overrideDir: join(root, "overrides"), radashiRef: new PLazy((resolve3, reject) => { getInstalledRadashiRef(pkg).then(resolve3, reject); }) }; } function getConfig(root, configRequired) { const configPath = join(root, "radashi.json"); let userConfig; try { userConfig = JSON.parse(readFileSync(configPath, "utf8")); } catch (error) { if (configRequired) { error.message = "Error parsing radashi.json: " + error.message; throw error; } debug("Failed to read radashi.json, using defaults"); } let editor = userConfig?.editor?.replace( /^\$EDITOR$/, process.env.EDITOR ?? "" ); if (editor === "!") { editor = ""; } const userFormats = userConfig?.formats?.filter( (value) => value === "esm" || value === "cjs" ); const config = { dts: true, ...userConfig, formats: userFormats && userFormats.length > 0 ? userFormats : ["esm"], editor: editor || void 0 }; return [userConfig ? configPath : null, config]; } function readOriginSync(root) { const [err, gitconfig] = tryit( () => readFileSync(join(root, ".git/config"), "utf8") )(); if (err) { return null; } const originSectionIdx = gitconfig.indexOf('[remote "origin"]'); if (originSectionIdx === -1) { return null; } const urlRegex = /url = (.+)/g; urlRegex.lastIndex = originSectionIdx; return urlRegex.exec(gitconfig)?.[1] ?? null; } export { debug, prepareGitHubDefaultRepo, isPreReleaseRadashiTag, getRadashiCloneURL, cloneRadashi, getEnv };