UNPKG

sensai

Version:

Because even AI needs a master

141 lines (140 loc) 5.04 kB
// utils "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { javascript: function() { return javascript; }, markdown: function() { return markdown; }, prompt: function() { return prompt; }, route: function() { return route; }, unknown: function() { return unknown; } }); const _nodepath = require("node:path"); const _core = require("@swc/core"); const _promises = require("node:fs/promises"); const _options = /*#__PURE__*/ _interop_require_wildcard(require("./options")); const _markdown = require("./markdown"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // the working directory const CWD_PATH = process.cwd(); const javascript = async (filePath, outPath)=>{ const content = await (0, _promises.readFile)(filePath, 'utf-8'); const code = await getJsCode(filePath, content); await write(filePath, outPath, code); }; const route = async (filePath, outPath)=>{ await javascript(filePath, outPath); }; const prompt = async (filePath, outPath)=>{ // TODO we should copy the original file and create a ts file // may be with a unique name (may be based on hash of fileName). const file = await (0, _promises.readFile)(filePath, 'utf-8'); const content = (0, _markdown.getPromptTypescript)(file); const { dir, name } = (0, _nodepath.parse)(filePath); const code = await getJsCode((0, _nodepath.join)(dir, name + '.ts'), content); await write(filePath, outPath, code); }; const markdown = async (filePath, outPath)=>{ const file = await (0, _promises.readFile)(filePath); const content = (0, _markdown.getMarkdownTypescript)(file, false); const { dir, name } = (0, _nodepath.parse)(filePath); const code = await getJsCode((0, _nodepath.join)(dir, name + '.ts'), content); await write(filePath, outPath, code); }; const unknown = async (filePath, outPath)=>{ await copy(filePath, outPath); }; /** * Generate JavaScript off Typescript content. * @param filePath used to get the right compiler options */ const getJsCode = async (filePath, content)=>{ const aliases = (0, _options.getCompilerOptions)(CWD_PATH); const { code } = await (0, _core.transform)(content, (0, _options.default)(filePath, aliases)); return code; }; /** * Write content to the destination directory given the file name (computed from the * file path and destination directory). */ const write = async (filePath, destPath, content)=>{ const path = (0, _nodepath.join)(destPath, (0, _nodepath.relative)(CWD_PATH, filePath)); await (0, _promises.mkdir)((0, _nodepath.dirname)(path), { recursive: true }); return (0, _promises.writeFile)(path, content); }; /** * Copy file to the destination directory given the file name (computed from the * file path and destination directory). * * @notes we might want to use symlinks for further optimization. */ const copy = async (filePath, destPath)=>{ // const path = join(destPath, relative(CWD_PATH, filePath)) // const writeStream = createWriteStream(path) // createReadStream(filePath).pipe(writeStream) // return new Promise((resolve, reject) => { // writeStream.on('finish', resolve) // writeStream.on('error', reject) // }) const path = (0, _nodepath.join)(destPath, (0, _nodepath.relative)(CWD_PATH, filePath)); await (0, _promises.mkdir)((0, _nodepath.dirname)(path), { recursive: true }); await (0, _promises.symlink)(filePath, path); };