UNPKG

radashi-helper

Version:
170 lines (149 loc) 7.25 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 _chunkN3JPDYAQcjs = require('./chunk-N3JPDYAQ.cjs'); var _chunkDNQVKCXJcjs = require('./chunk-DNQVKCXJ.cjs'); var _chunkIWXG7YVCcjs = require('./chunk-IWXG7YVC.cjs'); var _chunk4WZBZWYDcjs = require('./chunk-4WZBZWYD.cjs'); var _chunkZP624W2Acjs = require('./chunk-ZP624W2A.cjs'); var _chunkST3J6QTNcjs = require('./chunk-ST3J6QTN.cjs'); var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs'); // src/fn-move.ts var _fs = require('fs'); var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises); var _path = require('path'); async function moveFunction(options = {}) { const env = _nullishCoalesce(options.env, () => ( _chunkZP624W2Acjs.getEnv.call(void 0, options.dir))); let oldFuncPath = options.funcPath; const dest = options.dest; if (!oldFuncPath) { const pathsInside = await _chunkDNQVKCXJcjs.findSources.call(void 0, env, ["src", "overrides"]); _chunkZP624W2Acjs.debug.call(void 0, "pathsInside:", pathsInside); const { src: sourceFuncs = [], overrides: overrideFuncs = [] } = _chunk5R2KEZLQcjs.objectify.call(void 0, Object.entries(pathsInside), ([type]) => type, ([type, sourcePaths]) => { const sourceRoot = _path.join.call(void 0, type === "src" ? env.root : env.overrideDir, "src" ); return _optionalChain([sourcePaths, 'optionalAccess', _ => _.map, 'call', _2 => _2( (sourcePath) => _path.relative.call(void 0, sourceRoot, sourcePath).replace(/\.ts$/, "") )]); } ); _chunkZP624W2Acjs.debug.call(void 0, "sourceFuncs:", sourceFuncs); _chunkZP624W2Acjs.debug.call(void 0, "overrideFuncs:", overrideFuncs); const selectedFunc = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "autocomplete", name: "selectedFunc", message: "Select a function to move:", choices: [...sourceFuncs, ...overrideFuncs].sort().map((funcPath) => ({ title: funcPath, value: funcPath })) }); if (!selectedFunc) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No function selected. Exiting..."); } oldFuncPath = selectedFunc; } if (!_fs.existsSync.call(void 0, _path.join.call(void 0, env.root, "src", oldFuncPath + ".ts"))) { throw new (0, _chunkST3J6QTNcjs.RadashiError)( `Function ${oldFuncPath} was not found in ${env.root}/src` ); } const [oldGroup, oldFuncName] = oldFuncPath.split("/"); let [group, funcName] = dest ? dest.includes("/") ? dest.split("/") : [oldGroup, dest] : [oldGroup, oldFuncName]; if (!dest) { const action = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "select", name: "action", message: "What would you like to do?", choices: [ { title: "Move function to a new group", value: "move" }, { title: "Rename function", value: "rename" }, { title: "Both move and rename", value: "both" } ] }); if (!action) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No action selected. Exiting..."); } if (action === "move" || action === "both") { const groups = await _chunkN3JPDYAQcjs.getRadashiGroups.call(void 0, env); const selectedGroup = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "autocomplete", name: "selectedGroup", message: "Select a group for the function:", choices: [ { title: "Create a new group", value: "new" }, ...groups.map((g) => ({ title: g, value: g })) ] }); if (!selectedGroup) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No group selected. Exiting..."); } if (selectedGroup === "new") { const newGroup = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "text", name: "newGroup", message: "Enter the new group name:" }); if (!newGroup) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No new group name provided. Exiting..."); } group = newGroup; } else { group = selectedGroup; } } if (action === "rename" || action === "both") { const newFuncName = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "text", name: "newFuncName", message: "Enter the new function name:" }); if (!newFuncName) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)(); } funcName = newFuncName; } } for (const folder of _chunkIWXG7YVCcjs.projectFolders) { const prevPath = _path.join.call(void 0, env.root, folder.name, oldGroup, oldFuncName + folder.extension ); if (!_fs.existsSync.call(void 0, prevPath)) { continue; } const dest2 = _path.join.call(void 0, env.root, folder.name, group, funcName + folder.extension); if (_fs.existsSync.call(void 0, dest2)) { const overwrite = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "confirm", name: "overwrite", message: `File ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, dest2)} already exists. Do you want to overwrite it?`, initial: false }); if (overwrite == null) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)(); } if (!overwrite) { continue; } } _chunkST3J6QTNcjs.log.call(void 0, `Renaming ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, prevPath)} to ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, dest2)}`); await _promises.mkdir.call(void 0, _path.dirname.call(void 0, dest2), { recursive: true }); await _promises.rename.call(void 0, prevPath, dest2); const prevDir = _path.dirname.call(void 0, prevPath); if ((await _promises.readdir.call(void 0, prevDir)).length === 0) { _chunkST3J6QTNcjs.log.call(void 0, `Removing empty directory: ${_chunkIWXG7YVCcjs.cwdRelative.call(void 0, prevDir)}`); await _promises2.default.rm(prevDir, { recursive: true }); } } if (env.radashiDir) { const { default: build } = await Promise.resolve().then(() => _interopRequireWildcard(require("./build-D32K7VSY.cjs"))); await build({ env }); } } exports.moveFunction = moveFunction;