UNPKG

radashi-helper

Version:
232 lines (201 loc) 7.49 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(); } } var _chunkQJPNUX4Pcjs = require('./chunk-QJPNUX4P.cjs'); var _chunkN3JPDYAQcjs = require('./chunk-N3JPDYAQ.cjs'); var _chunk3OOKBKZBcjs = require('./chunk-3OOKBKZB.cjs'); var _chunkWVUG42ZOcjs = require('./chunk-WVUG42ZO.cjs'); require('./chunk-27XA2ZBL.cjs'); var _chunk4WZBZWYDcjs = require('./chunk-4WZBZWYD.cjs'); var _chunkZP624W2Acjs = require('./chunk-ZP624W2A.cjs'); var _chunkST3J6QTNcjs = require('./chunk-ST3J6QTN.cjs'); require('./chunk-5R2KEZLQ.cjs'); // src/fn-create.ts var _fs = require('fs'); var _promises = require('fs/promises'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); // src/util/addExportToBarrel.ts function addExportToBarrel(content, specifier) { const addedLine = `export * from '${specifier}'`; const existingLines = content.split("\n"); let insertIndex = Math.max(existingLines.length - 1, 0); for (let i = 0; i < existingLines.length; i++) { if (existingLines[i] && existingLines[i] > addedLine) { if (i === 0) { insertIndex = 0; break; } for (let j = i - 1; j >= 0; j--) { if (existingLines[j].length) { insertIndex = j + 1; break; } } break; } } existingLines.splice(insertIndex, 0, addedLine); return existingLines.join("\n"); } // src/fn-create.ts async function createFunction(funcName, options = {}) { if (funcName == null) { funcName = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "text", name: "funcName", message: "Enter the name for the new function:" }); } if (!funcName) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("Function name cannot be empty"); } if (funcName.includes("/")) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("Function name cannot include slashes"); } const env = _nullishCoalesce(options.env, () => ( _chunkZP624W2Acjs.getEnv.call(void 0, options.dir))); if (env.radashiDir) { await _chunk3OOKBKZBcjs.pullRadashi.call(void 0, env); } let { group, description } = options; if (group == null) { 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 name for the new group:" }); if (!newGroup) { throw new (0, _chunkST3J6QTNcjs.RadashiError)("Group name cannot be empty"); } group = newGroup; } else { group = selectedGroup; } } const directories = { src: _path.join.call(void 0, env.root, "src", group), docs: _path.join.call(void 0, env.root, "docs", group), tests: _path.join.call(void 0, env.root, "tests", group), benchmarks: _path.join.call(void 0, env.root, "benchmarks", group) }; const files = { src: _path.join.call(void 0, directories.src, `${funcName}.ts`), docs: _path.join.call(void 0, directories.docs, `${funcName}.mdx`), tests: _path.join.call(void 0, directories.tests, `${funcName}.test.ts`), benchmarks: _path.join.call(void 0, directories.benchmarks, `${funcName}.bench.ts`) }; if (!_fs.existsSync.call(void 0, files.docs)) { if (description == null) { description = await _chunk4WZBZWYDcjs.prompt.call(void 0, { type: "text", name: "description", message: `Enter a description for ${funcName}:` }); if (description == null) { throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No description provided. Exiting..."); } if (description.trim() === "") { throw new (0, _chunkST3J6QTNcjs.RadashiError)("Function description cannot be empty"); } } await createFile(files.docs, generateDocsContent(funcName, description)); } else { _chunkST3J6QTNcjs.log.error(`Warning: ${files.docs} already exists. Skipping.`); } await createFileIfNotExists(files.src, generateSrcContent(group, funcName)); await createFileIfNotExists(files.tests, generateTestsContent(funcName)); await createFileIfNotExists( files.benchmarks, generateBenchmarksContent(funcName) ); if (options.editor !== false) { await _chunkWVUG42ZOcjs.openInEditor.call(void 0, files.src, env, options.editor); } if (env.radashiDir) { const { default: build } = await Promise.resolve().then(() => _interopRequireWildcard(require("./build-D32K7VSY.cjs"))); await build({ env }); } else { _chunkST3J6QTNcjs.log.call(void 0, "Updating src/mod.ts"); const barrelFile = _path.join.call(void 0, env.root, "src/mod.ts"); _promises.writeFile.call(void 0, barrelFile, addExportToBarrel( _fs.readFileSync.call(void 0, barrelFile, "utf8"), `./${group}/${funcName}.ts` ) ); } } async function createFile(file, content) { await _promises.mkdir.call(void 0, _path.dirname.call(void 0, file), { recursive: true }); await _promises.writeFile.call(void 0, file, content); _chunkST3J6QTNcjs.log.call(void 0, `Created ${_path2.default.relative(process.cwd(), file)}`); } async function createFileIfNotExists(file, content) { if (!_fs.existsSync.call(void 0, file)) { await createFile(file, content); } else { _chunkST3J6QTNcjs.log.error(`Warning: ${file} already exists. Skipping.`); } } function generateDocsContent(funcName, description) { return _chunkQJPNUX4Pcjs.dedent` --- title: ${funcName} description: ${description} --- ### Usage Does a thing. Returns a value. \`\`\`ts import * as _ from 'radashi' _.${funcName}() \`\`\` `; } function generateSrcContent(group, funcName) { return _chunkQJPNUX4Pcjs.dedent` /** * Does a thing. * * @see https://radashi.js.org/reference/${group}/${funcName} * @example * \`\`\`ts * ${funcName}() * \`\`\` */ export function ${funcName}(): void {} `; } function generateTestsContent(funcName) { return _chunkQJPNUX4Pcjs.dedent` import * as _ from 'radashi' describe('${funcName}', () => { test('does a thing', () => { expect(_.${funcName}()).toBe(undefined) }) }) `; } function generateBenchmarksContent(funcName) { return _chunkQJPNUX4Pcjs.dedent` import * as _ from 'radashi' import { bench } from 'vitest' describe('${funcName}', () => { bench('with no arguments', () => { _.${funcName}() }) }) `; } exports.createFunction = createFunction;