Skip to content

Commit

Permalink
新增pnpm run delete
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackWhite2000 committed Dec 4, 2023
1 parent 1bd1f91 commit 549142e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pnpm run build:w # 持续监听执行 pnpm run build,当 src 目录内有变
pnpm run build:public # 将 public 目录内的文件并拷贝至工程内。
pnpm run build:ts # 将 build 目录内的文件编译成js,并拷贝至工程内。
pnpm run build:allTs # 将 src 目录内的文件并拷贝至工程内,与 pnpm run build 区别在于不会合并ts。
pnpm run delete:ts # 删除 pnpm run build、pnpm run build:allTs 生成的ts文件。
pnpm run delete:all # 删除所有生成的文件,包括目录。
```

## 目录说明
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"build:public": "node public/config.mjs",
"build:ts": "tsc -p ./build/tsconfig.json && node scripts/copy_dist.mjs",
"build:allTs": "node scripts/copy_ts.mjs",
"delete:ts": "node scripts/delete_ts.mjs",
"delete:all": "node scripts/delete_all.mjs",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
Expand Down
54 changes: 54 additions & 0 deletions scripts/delete_all.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import process from 'node:process'
import { rm, readdir } from 'node:fs/promises'
import path from 'node:path'
import dotenv from 'dotenv'

// 从 .env 文件中加载环境变量
dotenv.config()

// 获取环境变量
const outputPath = process.env.OUTPUT_PATH
const outputFolderName = process.env.OUTPUT_FOLDER_NAME
const outputName = process.env.OUTPUT_NAME
const env = [
{
var: outputPath,
name: 'OUTPUT_PATH',
type: 'ts合并路径',
},
{
var: outputFolderName,
name: 'OUTPUT_FOLDER_NAME',
type: 'ts目录名称',
},
{
var: outputName,
name: 'OUTPUT_NAME',
type: 'ts文件名称',
},
]
for (const i in env) {
if (!env[i].var) {
console.error(`未找到${env[i].type},请在 .env 文件中设置 ${env[i].name} 变量。`)
process.exit(1)
}
}

const currentWorkingDirectory = process.cwd() // 获取当前工作目录
const destinationDirectory = path.join(currentWorkingDirectory, 'template', outputPath, outputFolderName) // 设置目标目录

// 检查目录是否存在
async function isDirectoryExists(directory) {
try {
const stat = await fs.stat(directory);
return stat.isDirectory();
} catch (error) {
return false;
}
}
if (isDirectoryExists) {
rm(destinationDirectory, { recursive: true, force: true });
console.log('删除操作完成。');
} else {
console.log('目录不存在,无需执行删除操作。');
}
67 changes: 67 additions & 0 deletions scripts/delete_ts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import process from 'node:process'
import { rm, readdir } from 'node:fs/promises'
import path from 'node:path'
import dotenv from 'dotenv'

// 从 .env 文件中加载环境变量
dotenv.config()

// 获取环境变量
const outputPath = process.env.OUTPUT_PATH
const outputFolderName = process.env.OUTPUT_FOLDER_NAME
const outputName = process.env.OUTPUT_NAME
const env = [
{
var: outputPath,
name: 'OUTPUT_PATH',
type: 'ts合并路径',
},
{
var: outputFolderName,
name: 'OUTPUT_FOLDER_NAME',
type: 'ts目录名称',
},
{
var: outputName,
name: 'OUTPUT_NAME',
type: 'ts文件名称',
},
]
for (const i in env) {
if (!env[i].var) {
console.error(`未找到${env[i].type},请在 .env 文件中设置 ${env[i].name} 变量。`)
process.exit(1)
}
}

const currentWorkingDirectory = process.cwd() // 获取当前工作目录
const destinationDirectory = path.join(currentWorkingDirectory, 'template', outputPath, outputFolderName) // 设置目标目录
const directoriesToKeep = ['public', 'dist']; // 删除时忽略的目录

// 检查目录是否存在
async function isDirectoryExists(directory) {
try {
const stat = await fs.stat(directory);
return stat.isDirectory();
} catch (error) {
return false;
}
}
if (isDirectoryExists) {
// 获取目标目录下的所有文件和子目录
const filesAndDirs = await readdir(destinationDirectory);

// 筛选出要保留的目录
const directoriesToDelete = filesAndDirs.filter(
(item) => !directoriesToKeep.includes(item)
);

// 删除不需要的文件和子目录
for (const item of directoriesToDelete) {
const itemPath = path.join(destinationDirectory, item);
await rm(itemPath, { recursive: true, force: true });
}
console.log('删除操作完成。');
} else {
console.log('目录不存在,无需执行删除操作。');
}

0 comments on commit 549142e

Please sign in to comment.