-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.js
61 lines (58 loc) · 2.47 KB
/
move.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
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 使用说明:
* 1. 命令行执行 npm run move {name};该命令会移动本地打包的文件到生产环境;
* 2. 举例:把打包的文件test.html文件及其对应的静态资源(包括scss,js和图片)移动到生产环境的对应目录, 执行命令 npm run move test;
* 3. tips:移动文件之前必须先执行npm run build,生成打包文件
*/
const fs = require('fs');
const path = require('path');
(function() {
// 开发环境的打包文件目录
const pathDist = './dist';
const pathHtml = './dist/specials/';
const pathStatic = './dist/static/specials/';
// 生产环境的打包文件目录
const pathProHtml = './production/templates/specials/';
const pathProStatic = './production/static/specials/';
// 获取命令行的参数
const name = process.argv[2];
// 读取dist文件夹是否存在
function checkDist() {
const exists = fs.existsSync(path.resolve(__dirname, `${pathDist}`));
if (!exists) throw ('请先执行命令 npm run build 打包文件!');
}
checkDist();
// 检查要移动的文件是否存在
function checkFile() {
const existsHtml = fs.existsSync(path.resolve(__dirname, `${pathHtml}${name}.html`));
const existsCss = fs.existsSync(path.resolve(__dirname, `${pathStatic}css/${name}.css`));
const existsJs = fs.existsSync(path.resolve(__dirname, `${pathStatic}/js/${name}.js`));
if (!existsHtml) throw ('正在移动的文件无法找到!');
// copy html文件
fs.copyFileSync(path.resolve(__dirname, `${pathHtml}${name}.html`),
path.resolve(__dirname, `${pathProHtml}${name}.html`));
if (existsCss) {
// copy css文件
fs.copyFileSync(path.resolve(__dirname, `${pathStatic}css/${name}.css`),
path.resolve(__dirname, `${pathProStatic}css/${name}.css`));
}
if (existsJs) {
// copy js文件
fs.copyFileSync(path.resolve(__dirname, `${pathStatic}js/${name}.js`),
path.resolve(__dirname, `${pathProStatic}js/${name}.js`));
}
// 读取图片文件夹
let imagesName = fs.readdirSync(path.resolve(__dirname, `${pathStatic}images/`));
const reg = new RegExp(`${name}_`);
let filter = imagesName.filter((val) => {
return reg.test(val);
});
filter.forEach((val) => {
// copy image文件
fs.copyFileSync(path.resolve(__dirname, `${pathStatic}images/${val}`),
path.resolve(__dirname, `${pathProStatic}images/${val}`));
});
console.log('文件移动成功!');
}
checkFile();
})();