-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
48 lines (43 loc) · 1.46 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import path from 'path'
import fs from 'fs'
import dotenv from 'dotenv'
dotenv.config()
// 复制临时目录的文件到目标文件夹
const copyTemp2Build = (tempDir, tarDir) => {
if (!fs.existsSync(tarDir)) {
fs.mkdir(tarDir) // 如果不存在目标目录就创建目录
}
fs.readdirSync(tempDir).forEach(file => {
const tempPath = path.join(tempDir, file)
const tarPath = path.join(tarDir, file)
if (fs.lstatSync(tempPath).isDirectory()) {
copyTemp2Build(tempPath, tarPath)
} else {
fs.copyFileSync(tempPath, tarPath)
}
})
}
// 删除临时目录
const deleteTempDir = tempDir => {
if (fs.existsSync(tempDir)) {
fs.readdirSync(tempDir).forEach(file => {
const tempPath = path.join(tempDir, file)
if (fs.lstatSync(tempPath).isDirectory()) {
deleteTempDir(tempPath)
} else {
fs.unlinkSync(tempPath)
}
})
fs.rmdirSync(tempDir)
}
}
// content-script 临时打包目录
const tempContentDir = path.resolve(process.cwd(), process.env.TEMP_CONTENT_DIR)
const tempBackgroundDir = path.resolve(process.cwd(), process.env.TEMP_BACKGROUND_DIR)
const targetBuildDir = path.resolve(process.cwd(), process.env.TARGET_BUILD_DIR)
// 复制 content-script 和 background-script 的build 文件到最终build 目录中
copyTemp2Build(tempContentDir, targetBuildDir)
copyTemp2Build(tempBackgroundDir, targetBuildDir)
// 删除临时目录
deleteTempDir(tempContentDir)
deleteTempDir(tempBackgroundDir)