UNPKG

radashi-helper

Version:
261 lines (213 loc) 9.55 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } 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 _chunkBCKUBJLJcjs = require('./chunk-BCKUBJLJ.cjs'); var _chunk7UCE7HEGcjs = require('./chunk-7UCE7HEG.cjs'); var _chunk3OOKBKZBcjs = require('./chunk-3OOKBKZB.cjs'); var _chunkWVUG42ZOcjs = require('./chunk-WVUG42ZO.cjs'); var _chunk27XA2ZBLcjs = require('./chunk-27XA2ZBL.cjs'); var _chunkIWXG7YVCcjs = require('./chunk-IWXG7YVC.cjs'); var _chunkZP624W2Acjs = require('./chunk-ZP624W2A.cjs'); var _chunkST3J6QTNcjs = require('./chunk-ST3J6QTN.cjs'); var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs'); // src/fn-override.ts var _fs = require('fs'); var _promises = require('fs/promises'); var _path = require('path'); // src/rewired/rewireDependents.ts var import_parser = _chunk5R2KEZLQcjs.__toESM.call(void 0, _chunk7UCE7HEGcjs.require_lib.call(void 0, ), 1); // src/util/getRadashiFuncPaths.ts var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob); async function getRadashiFuncPaths(env) { const srcRoot = _path.join.call(void 0, _nullishCoalesce(env.radashiDir, () => ( env.root)), "src"); return _chunk5R2KEZLQcjs.select.call(void 0, (await _fastglob2.default.call(void 0, "**/*.ts", { cwd: srcRoot })).sort(), (f) => f.replace(/\.ts$/, ""), (f) => f.includes("/") ); } // src/rewired/rewireDependents.ts async function rewireDependents(funcName, env, radashiFuncPaths) { if (!env.radashiDir) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("No upstream repository exists"); } radashiFuncPaths ??= await getRadashiFuncPaths(env); const parseImports = _chunk5R2KEZLQcjs.memo.call(void 0, (filename) => { const fileContents = _fs.readFileSync.call(void 0, filename, "utf8"); const parseResult = (0, import_parser.parse)(fileContents, { plugins: [["typescript", { dts: false }]], sourceType: "module", sourceFilename: filename }); const importedNames = /* @__PURE__ */ new Set(); _chunk5R2KEZLQcjs.traverse.call(void 0, parseResult.program, (node, _key, _parent, context) => { if (_chunk7UCE7HEGcjs.isBabelNode.call(void 0, node)) { context.skip(); if (node.type === "ImportDeclaration" && node.source.value === "radashi") { for (const specifier of node.specifiers) { importedNames.add(_nullishCoalesce(_optionalChain([specifier, 'access', _ => _.imported, 'optionalAccess', _2 => _2.name]), () => ( specifier.local.name))); } } } }); return importedNames; }); const findDependentFiles = _chunk5R2KEZLQcjs.memo.call(void 0, (funcName2, stack = []) => { const selected = _chunk5R2KEZLQcjs.select.call(void 0, radashiFuncPaths, (funcPath) => { const filename = _path.join.call(void 0, env.radashiDir, "src", funcPath + ".ts"); const importedNames = parseImports(filename); if (importedNames.has(funcName2)) { let dependents = []; if (!stack.includes(funcPath)) { stack.push(funcPath); const srcFuncName = funcPath.split("/").at(-1); dependents = findDependentFiles(srcFuncName, stack); stack.pop(); } dependents.unshift(funcPath); return dependents; } return null; } ); return _chunk5R2KEZLQcjs.unique.call(void 0, _chunk5R2KEZLQcjs.flat.call(void 0, selected)); }, { key: (bestMatchName) => bestMatchName } ); const prevRewired = await _chunk7UCE7HEGcjs.loadRewired.call(void 0, env); const dependentFiles = findDependentFiles(funcName).filter((file) => { return !prevRewired.includes(file) && !_fs.existsSync.call(void 0, _path.join.call(void 0, env.overrideDir, "src", file + ".ts")); }).sort(); if (!dependentFiles.length) { return []; } await _promises.writeFile.call(void 0, _path.join.call(void 0, env.overrideDir, "rewired.json"), JSON.stringify([...prevRewired, ...dependentFiles], null, 2) ); await tryCopyFile( _path.join.call(void 0, env.root, "src/tsconfig.json"), _path.join.call(void 0, env.root, "overrides/rewired/tsconfig.json") ); return _chunk5R2KEZLQcjs.reduce.call(void 0, dependentFiles, async (copiedFiles, file) => { if (await _chunk7UCE7HEGcjs.rewire.call(void 0, file, env)) { copiedFiles.push(file); } return copiedFiles; }, [] ); } async function tryCopyFile(src, dst) { _chunkZP624W2Acjs.debug.call(void 0, `Copying ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, src)} to ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, dst)}`); if (_fs.existsSync.call(void 0, src)) { try { await _promises.mkdir.call(void 0, _path.dirname.call(void 0, dst), { recursive: true }); await _promises.copyFile.call(void 0, src, dst); return true; } catch (e) { } } return false; } // src/util/isRepoClean.ts var _child_process = require('child_process'); async function isRepoClean(cwd) { return new Promise((resolve, reject) => { _child_process.exec.call(void 0, "git status --porcelain", { cwd }, (error, stdout, stderr) => { if (error) { reject(new Error(`Error executing git status: ${stderr}`)); } else { resolve(stdout.trim() === ""); } }); }); } // src/util/assertRepoClean.ts async function assertRepoClean(cwd) { if (!await isRepoClean(cwd)) { throw new (0, _chunkST3J6QTNcjs.RadashiError)( "Your repository has uncommitted changes. Please commit or stash them before overriding." ); } } // src/fn-override.ts async function addOverride(query, options = {}) { const env = _nullishCoalesce(options.env, () => ( _chunkZP624W2Acjs.getEnv.call(void 0, options.dir))); const { radashiDir } = env; if (!radashiDir) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("No upstream repository exists"); } await assertRepoClean(env.root); await _chunk3OOKBKZBcjs.pullRadashi.call(void 0, env); let bestMatch; let bestMatchName; await _chunk5R2KEZLQcjs.defer.call(void 0, async (onFinish) => { if (options.fromBranch) { await _chunkST3J6QTNcjs.exec.call(void 0, "git", ["checkout", options.fromBranch], { cwd: radashiDir, stdio: _chunk27XA2ZBLcjs.stdio }); onFinish(async () => { await _chunkST3J6QTNcjs.exec.call(void 0, "git", ["checkout", "-"], { cwd: radashiDir }); }); } const funcPaths = await getRadashiFuncPaths(env); const { funcPath, funcName } = await _chunkBCKUBJLJcjs.queryFuncs.call(void 0, query, funcPaths, { exactMatch: options.exactMatch, message: "Which function do you want to copy?", confirmMessage: 'Is "{funcPath}" the function you want to copy?' }); bestMatch = funcPath; bestMatchName = funcName; let copied = 0; for (const folder of _chunkIWXG7YVCcjs.projectFolders) { const fromPath = _path.join.call(void 0, radashiDir, folder.name, bestMatch + folder.extension ); const outPath = _path.join.call(void 0, env.overrideDir, folder.name, bestMatch + folder.extension ); const success = await tryCopyFile2(fromPath, outPath); if (success) { copied++; if (folder.name === "src" && options.editor !== false) { await _chunkWVUG42ZOcjs.openInEditor.call(void 0, outPath, env, options.editor); } } } copied += (await rewireDependents(bestMatchName, env, funcPaths)).length; _chunkST3J6QTNcjs.log.call(void 0, `${copied} files copied.`); }); const { default: build } = await Promise.resolve().then(() => _interopRequireWildcard(require("./build-D32K7VSY.cjs"))); await build({ env }); _chunkST3J6QTNcjs.log.call(void 0, ""); await _chunkWVUG42ZOcjs.botCommit.call(void 0, `chore: override ${bestMatch}`, { cwd: env.root, add: ["mod.ts", "overrides"] }); } async function tryCopyFile2(src, dst) { _chunkZP624W2Acjs.debug.call(void 0, `Copying ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, src)} to ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, dst)}`); if (_fs.existsSync.call(void 0, src)) { try { await _promises.mkdir.call(void 0, _path.dirname.call(void 0, dst), { recursive: true }); await _promises.copyFile.call(void 0, src, dst); return true; } catch (e2) { } } return false; } exports.assertRepoClean = assertRepoClean; exports.addOverride = addOverride;