|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import { statSync } from 'node:fs' |
| 3 | +import path from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import chalk from 'chalk' |
| 6 | + |
| 7 | +const sourceSrc = path.join(process.cwd(), 'dist') |
| 8 | +const targetSrc = path.join(process.cwd(), 'browserPlugin') |
| 9 | +// fs/promises写法 |
| 10 | +function move(sourceRoot, targetRoot) { |
| 11 | + console.debug(chalk.green(`开始移动${sourceRoot}中的文件至${targetRoot}`)) |
| 12 | + fs.readdir(sourceRoot) |
| 13 | + .then((files) => { |
| 14 | + files.forEach((file) => { |
| 15 | + const sourceFile = path.join(sourceRoot, file) |
| 16 | + const targetFile = path.join(targetRoot, file) |
| 17 | + |
| 18 | + if (statSync(sourceFile).isDirectory()) { |
| 19 | + fs.stat(targetFile) |
| 20 | + .catch((err) => { |
| 21 | + if (err.code === 'ENOENT') { |
| 22 | + // 在targetSrc中新建同名文件夹 |
| 23 | + fs.mkdir(targetFile) |
| 24 | + .then(() => { |
| 25 | + console.debug(chalk.green(`新建${targetFile}文件夹成功`)) |
| 26 | + }) |
| 27 | + .catch((err) => { |
| 28 | + console.debug(chalk.red(`新建失败ERROR:${err}`)) |
| 29 | + }) |
| 30 | + } |
| 31 | + else { |
| 32 | + console.debug(chalk.red(`新建失败ERROR:${err}`)) |
| 33 | + } |
| 34 | + }) |
| 35 | + move(sourceFile, targetFile) |
| 36 | + } |
| 37 | + else { |
| 38 | + const copyFn = () => { |
| 39 | + fs.copyFile(sourceFile, targetFile) |
| 40 | + .catch(() => { |
| 41 | + console.debug(chalk.red('复制失败')) |
| 42 | + }) |
| 43 | + } |
| 44 | + fs.unlink(targetFile) |
| 45 | + .then(() => { |
| 46 | + copyFn() |
| 47 | + }) |
| 48 | + .catch(() => { |
| 49 | + copyFn() |
| 50 | + }) |
| 51 | + } |
| 52 | + }) |
| 53 | + }) |
| 54 | + console.debug(chalk.green('复制成功')) |
| 55 | +} |
| 56 | +// fs写法 |
| 57 | +// fs.readdir(sourceSrc, (err, files) => { |
| 58 | +// if (err) { |
| 59 | +// console.debug("读取文件夹失败"); |
| 60 | +// } else { |
| 61 | +// files.forEach((file) => { |
| 62 | +// const sourceFile = path.join(sourceSrc, file); |
| 63 | +// const targetFile = path.join(targetSrc, file); |
| 64 | +// fs.unlink(targetFile, (err) => { |
| 65 | +// if (!err || err.code !== "ENOENT") { |
| 66 | +// fs.copyFile(sourceFile, targetFile, (err) => { |
| 67 | +// if (err) { |
| 68 | +// console.debug("复制失败"); |
| 69 | +// } else { |
| 70 | +// console.debug("复制成功") |
| 71 | +// } |
| 72 | +// }) |
| 73 | +// } else { |
| 74 | +// console.debug("删除失败"); |
| 75 | +// } |
| 76 | +// }) |
| 77 | + |
| 78 | +// }); |
| 79 | +// } |
| 80 | +// }) |
| 81 | +fs.stat(targetSrc) |
| 82 | + .then((result) => { |
| 83 | + if (result.isDirectory()) |
| 84 | + move(sourceSrc, targetSrc) |
| 85 | + else |
| 86 | + console.debug(chalk.red('browserPlugin不是一个文件夹')) |
| 87 | + }) |
| 88 | + .catch((err) => { |
| 89 | + if (err.code === 'ENOENT') { |
| 90 | + // 新建browserPlugin文件夹 |
| 91 | + fs.mkdir('browserPlugin') |
| 92 | + .then(() => { |
| 93 | + move(sourceSrc, targetSrc) |
| 94 | + }) |
| 95 | + .catch((err) => { |
| 96 | + console.debug(chalk.red(`新建失败ERROR:${err}`)) |
| 97 | + }) |
| 98 | + } |
| 99 | + else { |
| 100 | + console.error(chalk.red('发生错误:', err)) |
| 101 | + } |
| 102 | + }) |
0 commit comments