UNPKG

radashi-helper

Version:
225 lines (201 loc) 7.93 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkST3J6QTNcjs = require('./chunk-ST3J6QTN.cjs'); var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs'); // src/util/debug.ts var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "radashi"); // src/util/github.ts async function isGitHubAuthenticated() { if (process.env.GITHUB_TOKEN) { return true; } const { exitCode } = await _chunkST3J6QTNcjs.exec.call(void 0, "gh", ["auth", "status"], { reject: false }); return exitCode === 0; } async function prepareGitHubDefaultRepo(cwd) { const radashiUrl = "https://github.com/radashi-org/radashi"; const originUrl = await _chunkST3J6QTNcjs.exec.call(void 0, "git", ["remote", "get-url", "origin"], { cwd }).then((r) => r.stdout); debug("originUrl", originUrl); if (originUrl !== radashiUrl) { await _chunkST3J6QTNcjs.exec.call(void 0, "git", ["remote", "add", "radashi", radashiUrl], { cwd, reject: false }); if (await isGitHubAuthenticated()) { await _chunkST3J6QTNcjs.exec.call(void 0, "gh", ["repo", "set-default", "radashi-org/radashi"], { cwd }).catch(_chunkST3J6QTNcjs.forwardStderrAndRethrow); } } } // src/util/cloneRadashi.ts async function getInstalledRadashiRef(pkg) { const specifier = _optionalChain([pkg, 'access', _ => _.dependencies, 'optionalAccess', _2 => _2.radashi]); if (!specifier) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("No radashi dependency found in package.json"); } let ref = specifier.split("@").pop(); if (ref === "beta") { ref = "main"; } else if (ref !== "next") { ref = await _chunkST3J6QTNcjs.exec.call(void 0, "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 _chunkST3J6QTNcjs.exec.call(void 0, "git", ["clone", cloneUrl, "--branch", ref, "--single-branch", dir], opts ).catch((error) => { if (opts.stdio == null) { _chunkST3J6QTNcjs.forwardStderrAndRethrow.call(void 0, error); } throw error; }); await prepareGitHubDefaultRepo(dir); } // ../../node_modules/.pnpm/escalade@3.1.2/node_modules/escalade/sync/index.mjs var _path = require('path'); var _fs = require('fs'); function sync_default(start, callback) { let dir = _path.resolve.call(void 0, ".", start); let tmp, stats = _fs.statSync.call(void 0, dir); if (!stats.isDirectory()) { dir = _path.dirname.call(void 0, dir); } while (true) { tmp = callback(dir, _fs.readdirSync.call(void 0, dir)); if (tmp) return _path.resolve.call(void 0, dir, tmp); dir = _path.dirname.call(void 0, tmp = dir); if (tmp === dir) break; } } // src/env.ts // ../../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 ? _path.resolve.call(void 0, root) : sync_default(process.cwd(), (dir, files) => { if (dir.includes("radashi")) { return dir; } if (files.includes("radashi.json")) { configRequired = true; return dir; } }); if (!_chunk5R2KEZLQcjs.isString.call(void 0, root)) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("Could not find your Radashi root directory"); } const pkg = JSON.parse( _fs.readFileSync.call(void 0, _path.join.call(void 0, root, "package.json"), "utf8") ); const originUrl = readOriginSync(root); const [configPath, config] = getConfig(root, configRequired); return { pkg, root, modPath: _path.join.call(void 0, root, "mod.ts"), config, configPath, outDir: process.env.RADASHI_OUT_DIR || _path.join.call(void 0, root, "dist"), radashiDir: !_optionalChain([originUrl, 'optionalAccess', _3 => _3.includes, 'call', _4 => _4("github.com/radashi-org/radashi")]) ? _path.join.call(void 0, root, ".radashi/upstream") : null, overrideDir: _path.join.call(void 0, root, "overrides"), radashiRef: new PLazy((resolve3, reject) => { getInstalledRadashiRef(pkg).then(resolve3, reject); }) }; } function getConfig(root, configRequired) { const configPath = _path.join.call(void 0, root, "radashi.json"); let userConfig; try { userConfig = JSON.parse(_fs.readFileSync.call(void 0, 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 = _optionalChain([userConfig, 'optionalAccess', _5 => _5.editor, 'optionalAccess', _6 => _6.replace, 'call', _7 => _7( /^\$EDITOR$/, _nullishCoalesce(process.env.EDITOR, () => ( "")) )]); if (editor === "!") { editor = ""; } const userFormats = _optionalChain([userConfig, 'optionalAccess', _8 => _8.formats, 'optionalAccess', _9 => _9.filter, 'call', _10 => _10( (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] = _chunk5R2KEZLQcjs.tryit.call(void 0, () => _fs.readFileSync.call(void 0, _path.join.call(void 0, 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 _nullishCoalesce(_optionalChain([urlRegex, 'access', _11 => _11.exec, 'call', _12 => _12(gitconfig), 'optionalAccess', _13 => _13[1]]), () => ( null)); } exports.debug = debug; exports.prepareGitHubDefaultRepo = prepareGitHubDefaultRepo; exports.isPreReleaseRadashiTag = isPreReleaseRadashiTag; exports.getRadashiCloneURL = getRadashiCloneURL; exports.cloneRadashi = cloneRadashi; exports.getEnv = getEnv;