-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
160 lines (135 loc) · 3.6 KB
/
index.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
var glob = require('glob')
var main = require('./src/main.js')
var path = require('path');
var fs = require('fs');
var utils = require('./src/utils');
global.npm_name = 'publisher';
var yinyuoption = null// fs.readFileSync(process.cwd()+"/.inyusettings", 'utf-8').toString();
if(yinyuoption){
try {
yinyuoption = JSON.parse(yinyuoption);
if(!yinyuoption.url){
throw new Error(".inyusettings 配置错误!")
}
} catch(err) {
console.error(err)
}
}
/**
* 主业务入口
* 接收外部参数option
*/
function controller(options){
this.options = Object.assign({
root: process.cwd(),
iwantcdn:false,
sourceMappingURL:true,
chunk: false,
}, options)
this.options.hostname = options.hostname;
this.options.uploadUrl = options.uploadUrl;
this.options.yinyu = yinyuoption;
if(!this.options.src){
throw new Error("html src (string) is required");
return
}
/**
* 设置临时文件储存地址
*/
this.options.temporary = path.resolve(this.options.dist, './__tmp')
utils.mkdir(this.options.dist);
utils.mkdir(this.options.temporary);
this.mapfile = {
js:[],
css:[],
html:[],
static:[],
jsCount:0,
cssCount:0,
staticCount:0,
count:0,
_:{},
};
this.init();
}
module.exports = controller;
/**
* 初始化搜索
* 依赖glob gulp的内部主要文件,作搜索入口
*/
controller.prototype.init = function(){
glob(this.options.src, {
cwd: path.resolve(this.options.root),
realpath :true,
}, (err, files)=>{
this.files = files;
if(err || this.files.length==0){
throw new Error("glob can't find html in option.src");
}else{
this.htmlpath = path.dirname(files[0])
if(this.options.chunk){
let chunkpath = this.options.chunkFilename;
if(path.isAbsolute(chunkpath)){
chunkpath = "."+chunkpath;
}
this.chunkPath = path.resolve(this.htmlpath, chunkpath);
this.chunkPath = this.chunkPath.replace(/\[.+\]/i,"*")
}
this.mapfile.html = files;
main.scan.call(this)
.then(()=>{
return new Promise((resolve, reject)=>{
if(this.options.onScand){
this.options.onScand.call(this, this.mapfile, resolve)
}
})
})
.then(()=>{
this.move()
})
}
})
}
/**
* 做move处理
* 将map表的对应文件拍平,copy出一份临时文件
* 适应文件处理,例如压缩...
*/
controller.prototype.move = function(){
main.move.call(this)
.then(()=>{
return new Promise((resolve, reject)=>{
if(this.options.onMoved){
this.options.onMoved.call(this, this.mapfile, resolve)
}
})
})
.then(()=>{
this.uploader()
})
}
/**
* uploader
*/
controller.prototype.uploader = function(){
main.uploader.call(this)
.then(()=>{
console.log("rewrite ok!")
this.dist();
})
.then(()=>{
if(this.options.onDone){
this.options.onDone.call(this, this.mapfile)
}
})
}
/**
* 输出最终的html文件
*/
controller.prototype.dist = function(){
main.dist.call(this)
}
process.on('uncaughtException', function (err) {
console.error('An uncaught error occurred!');
console.error(err.stack);
});