-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.js
162 lines (144 loc) · 4.66 KB
/
builder.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Builder.js
* @type {{}}
*/
const Fs = require('fs');
const Path = require('path');
const uglify = require('uglify-js');
const babel = require('babel-core');
const stylus = require('stylus');
// 排除文件夹或文件
// excludes.copy 不复制文件夹或文件列表
// excludes.mini 不压缩文件列表。需要目录文件相对根目录的全路径,如:./script/xx/xx.js
let excludes = {
'copy': [
'.gitignore',
'.DS_Store',
'builder.js',
'dist.zip',
'package.json',
'package-lock.json',
'README.md',
'.git',
'.github',
'.idea',
'node_modules',
'dist',
'style',
'icon'
],
'mini': [
'./scripts/lib/highlight.min.js',
'./scripts/lib/jquery-1.11.0.min.js',
'./scripts/lib/polyfill.min.js',
]
};
(function() {
/**
* 清空目录
* @param targetDir
*/
let delDir = function (targetDir) {
let paths = Fs.readdirSync(targetDir);
if (paths) {
paths.forEach(function (path) {
let targetPath = targetDir + '/' + path,
stat = Fs.lstatSync(targetPath);
if (stat.isFile()) {
// 如果是文件,直接删除
console.log('删除文件: ' + targetPath);
Fs.unlinkSync(targetPath);
} else if (stat.isDirectory()) {
delDir(targetPath);
}
});
Fs.rmdirSync(targetDir);
}
};
/**
* 压缩JS
* @params sourceList 来源JS文件
* @params target 输出目标JS
*/
let jsMini = function (sourceList, target) {
try {
let r = babel.transformFileSync(sourceList, {
presets: ['env']
});
let result = uglify.minify(r.code);
Fs.writeFileSync(target, result['code'], 'utf8');
} catch (e) {
console.error(e);
}
};
/**
* 拷贝目录
* @param sourceDir 源目录
* @param targetDir 目标目录
*/
let deploy = function (sourceDir, targetDir) {
let paths = Fs.readdirSync(sourceDir);
if (paths) {
paths.forEach(function (path) {
if (excludes.copy.indexOf(path) !== -1) {
return false;
}
let sourcePath = sourceDir + '/' + path,
targetPath = targetDir + '/' + path,
stat = Fs.lstatSync(sourcePath);
if (stat.isFile()) {
let extName = Path.extname(sourcePath).slice(1);
let readable, writeable;
if (Fs.existsSync(targetDir)) {
console.log('Copy file: ' + sourcePath + ' => ' + targetPath);
if (extName === 'js' && excludes.mini.indexOf(sourcePath) === -1) {
jsMini(sourcePath, targetPath);
} else {
readable = Fs.createReadStream(sourcePath);
writeable = Fs.createWriteStream(targetPath);
readable.pipe(writeable);
}
} else {
console.log('Mkdir: ' + targetDir);
Fs.mkdirSync(targetDir);
readable = Fs.createReadStream(sourcePath);
writeable = Fs.createWriteStream(targetPath);
readable.pipe(writeable);
}
} else if (stat.isDirectory()) {
if (!Fs.existsSync(targetPath)) {
console.log('Mkdir: ' + targetPath);
Fs.mkdirSync(targetPath);
}
deploy(sourcePath, targetPath);
}
});
}
};
(function() {
console.log('Builder run.');
// check dir
let distDir = 'dist';
if (Fs.existsSync(distDir)) {
//
delDir(distDir);
}
Fs.mkdirSync(distDir);
//
deploy('.', './dist');
})();
// 编译CSS文件
const stylusInput = Fs.readFileSync('style/reader/index.styl','utf8')
stylus(stylusInput)
.set('paths', ['style/reader'])
.set('compress', true)
.render((err, css)=>{
if (err) throw err
css = css.replace(/\n/g, '')
Fs.mkdirSync('dist/style');
Fs.writeFile('dist/style/content.css' , css, (err)=>{
if (err) throw err
console.log('CSS Built => ./dist/style/content.css')
})
})
})();