// import { join, sep, extname, normalize } from 'node:path'; // import { copyFile, ink, println, scanFolderSync, updateFile } from '@moneko/utils'; // import { JsMinifyOptions, type Options, type Output, transformFile } from '@swc/core'; // function getOutfilePath(outDir: string, file: string, outFileExtension?: string) { // // 处理输出路径 // const parts = file.split(sep); // const baseDir = parts[0] === '' ? parts[1] : parts[0]; // const outputPath = file.replace(baseDir, ''); // const output = join(outDir, outputPath); // if (outFileExtension) { // return output.replace(extname(output), outFileExtension); // } // return output; // } // interface ConvertOptions { // outDir: string; // inputDir: string; // options: Options; // ignore?: RegExp[]; // extensions?: string[]; // copy?: boolean; // quiet?: boolean; // outputExtension?: string; // } // interface ConvertResult { // duration: number; // compiled: string[]; // copied: string[]; // failed: string[]; // } // interface ConvertOutput extends Output { // output?: string; // } // const normalizeExtension: Record = { // '.ts': '.js', // '.tsx': '.js', // '.mts': '.mjs', // '.cts': '.cjs', // '.js': '.js', // '.jsx': '.js', // '.mjs': '.mjs', // '.cjs': '.cjs', // }; // const jscMinify: JsMinifyOptions = { // format: { // comments: false, // }, // ecma: 2015, // compress: { // drop_console: true, // drop_debugger: true, // global_defs: { // '@alert': 'console.log', // }, // pure_funcs: ['console.log', 'console.warn', 'console.error', 'console.info'], // 移除console // ecma: 2015, // toplevel: false, // 为true表示只在顶级作用域压缩清理变量 // module: false, // ie8: false, // keep_classnames: void 0, // keep_fnames: false, // top_retain: [], // keep_infinity: true, // }, // mangle: true, // }; // export async function convert({ // ignore = [], // quiet = true, // copy = true, // outDir, // outputExtension, // inputDir, // options, // extensions = ['.ts', '.js', '.tsx', '.jsx', '.mts', '.mjs', '.cts', '.cjs'], // }: ConvertOptions): Promise { // // 记录运行时间 // const start = process.hrtime(); // const result: ConvertResult = { // duration: 0, // compiled: [], // copied: [], // failed: [], // }; // const allfile = scanFolderSync(inputDir, ['^.*']).map((file) => normalize(file)); // // 获取匹配的文件 // const files: string[] = []; // const copyFiles: string[] = []; // allfile.forEach((file) => { // if (/\.DS_Store/.test(file) || ignore.some((item: RegExp) => item.test(file))) { // return; // } // if (extensions.includes(extname(file))) { // files.push(file); // } else if (copy) { // copyFiles.push(file); // } // }); // const promises = files // .map(async (file) => { // try { // const outExtension = outputExtension || normalizeExtension[extname(file)]; // // 使用 SWC 转换文件 // const { code, map, output } = (await transformFile(file, { // ...options, // jsc: { // externalHelpers: false, // ...options.jsc, // minify: { // ...jscMinify, // ...options.jsc?.minify, // }, // }, // })) as ConvertOutput; // // 处理输出路径 // const outputPath = getOutfilePath(outDir, file, outExtension); // // 写入转换后的代码 // await updateFile(outputPath, code); // // 如果存在 sourcemap,也写入 // if (map) { // await updateFile(`${outputPath}.map`, map); // } // result.compiled.push(`${file} -> ${outputPath}`); // if (output) { // const dtsContent = JSON.parse(output); // if (dtsContent.__swc_isolated_declarations__) { // const ext = extname(file); // const dtsPath = getOutfilePath(outDir, file.replace(ext, `.d${ext}`)); // await updateFile(dtsPath, dtsContent.__swc_isolated_declarations__); // } // } // } catch (error) { // result.failed.push(`Error converting ${file}: ${error}`); // } // }) // .concat( // copyFiles.map(async (file) => { // const outputPath = getOutfilePath(outDir, file); // const isCopy = await copyFile(file, outputPath); // if (isCopy) { // result.copied.push(`${file} -> ${outputPath}`); // } else { // result.failed.push(`Error copying ${file} -> ${outputPath}`); // } // }), // ); // await Promise.all(promises); // const end = process.hrtime(start); // result.duration = end[0] * 1000 + end[1] / 1e6; // // 调用成功回调 // if (quiet) { // const moduleType = { // es6: 'ES6', // commonjs: 'CommonJS', // esm: 'ESM', // amd: 'AMD', // umd: 'UMD', // systemjs: 'SystemJS', // nodenext: 'NodeNext', // }[options.module!.type]; // const list = [ // `${ink('✨ 编译成功!🎉', '82', { bold: true })} ${ink(`(${moduleType})`, '213')}`, // ink(`⏱️ 总耗时:${result.duration.toFixed(3)} ms`, '117'), // result.compiled.length && ink(`📄 编译文件:${result.compiled.length} 个`, '111'), // result.copied.length && ink(`📋 复制文件:${result.copied.length} 个`, '226'), // result.failed.length && ink(`❌ 编译失败:${result.failed.length} 个`, '203'), // ] // .filter(Boolean) // .join('\n- '); // println(list); // } // return result; // } export { };