UNPKG

4.94 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const fsExtra = require('fs-extra');
4const Time = require('./Time');
5const Match = require('./Match');
6const Bundle = require('./Bundle');
7const createDiff = require('./createDiff');
8
9
10function Packer(option) {
11 this.option = option;
12 this.root = option.root;
13 this.dirs = {
14 curr: option.root,
15 cache: option.cache,
16 temp: path.join(option.cache, 'temp'),
17 diff: path.join(option.cache, 'diff')
18 };
19 this.bundle = {}; // curr: Bundle, diff: Bundle
20 this.difference = {}; // add 新增, del 删除, non 都存在(=> same change)
21 this.rules = new Match(option, option.release);
22 this.resource = null;
23 this.time = new Time();
24 this.initialized = false;
25
26 console.log(this.option)
27 console.log(this.dirs)
28}
29
30Packer.prototype.init = function() {
31 this.time.start('init');
32
33 this.initialized = true;
34
35 this.bundle.curr = new Bundle(this.dirs.curr, 'curr');
36
37 this.dirs.diff = createDiff({
38 root: this.option.root,
39 git: this.option.git,
40 dir: this.dirs.diff,
41 tag: this.option.diff
42 });
43 this.bundle.diff = new Bundle(this.dirs.diff, 'diff');
44
45 this.time.end('init');
46};
47
48Packer.prototype.diff = function() {
49 this.time.start('diff');
50
51 const difference = this.difference = { add: [], del: [], non: [] };
52
53 if (!(this.bundle.curr && this.bundle.diff)) return difference;
54
55 const diff = {};
56 this.bundle.diff.getFile().forEach(item => {
57 diff[item.path] = item;
58 });
59
60 this.bundle.curr.getFile().forEach(item => {
61 const meta = {
62 path: item.path,
63 curr: item,
64 diff: diff[item.path]
65 };
66 if (meta.diff) {
67 difference.non.push(meta);
68 delete diff[item.path];
69 } else {
70 difference.add.push(meta);
71 }
72 });
73
74 Object.keys(diff).forEach(delPath => {
75 difference.del.push({
76 path: delPath,
77 curr: undefined,
78 diff: diff[delPath]
79 })
80 });
81
82 this.time.end('diff');
83
84 return difference;
85};
86
87Packer.prototype.match = function() {
88 this.time.start('match');
89
90 const difference = this.difference;
91
92 this.resource = [];
93
94 if (!(difference.add && difference.del && difference.non)) return this.resource;
95
96 const rules = this.rules;
97 const base = this.root;
98 const temp = this.dirs.temp;
99 this.resource = this.resource.concat(
100 difference.add.map(item => rules.test(item.path) ? item.curr : undefined),
101 difference.del.map(item => rules.test(item.path) ? undefined : item.diff),
102 difference.non.map(item => rules.test(item.path) ? item.curr : item.diff)
103 ).filter(item => !!item).map(file => {
104 const item = {
105 path: file.path,
106 src: file.absolute,
107 tmp: path.join(temp, file.path),
108 tar: path.join(base, file.path),
109 file: file,
110 };
111 return item;
112 });
113
114 this.time.end('match');
115
116 let same = 0;
117 this.resource.forEach(item => {
118 if (item.src === item.tar) {
119 same += 1;
120 } else {
121 }
122 });
123 console.log('total: ', this.resource.length);
124 console.log('same: ', same);
125 console.log('diff: ', this.resource.length - same);
126
127 return this.resource;
128};
129
130Packer.prototype.pack = function() {
131 this.time.start('pack');
132
133 if (!this.resource) return;
134
135 // TODO record log
136
137 // console.log('='.repeat(100));
138 // console.log('create temp bundle');
139 // console.log('-'.repeat(100));
140 this.resource.forEach(item => {
141 // TODO if item.src === item.tar, don't copy/move
142 // console.log('copy', item.src, '=>', item.tmp);
143 try {
144 fsExtra.copySync(item.src, item.tmp);
145 // fsExtra.moveSync(item.src, item.tmp);
146 } catch (e) {
147 throw new Error(e)
148 }
149 });
150
151 // console.log('clean root');
152 this.bundle.curr.getFile().forEach(item => {
153 try {
154 fsExtra.removeSync(item.absolute);
155 } catch (e) {
156 throw new Error(e)
157 }
158 });
159
160 // console.log('='.repeat(100));
161 // console.log('move temp to root');
162 // console.log('-'.repeat(100));
163 this.resource.forEach(item => {
164 // console.log('move', item.tmp, '=>', item.tar);
165 try {
166 fsExtra.moveSync(item.tmp, item.tar);
167 } catch (e) {
168 throw new Error(e)
169 }
170 });
171
172 this.time.end('pack');
173};
174
175Packer.prototype.clean = function() {
176 this.time.start('clean');
177
178 try {
179 fsExtra.removeSync(this.dirs.cache);
180 } catch (e) {
181 throw new Error(e)
182 }
183
184 this.time.end('clean');
185};
186
187Packer.prototype.run = function(callback) {
188 this.init();
189 this.diff();
190 this.match();
191 this.pack();
192 this.clean();
193
194 callback();
195};
196
197
198exports = module.exports = Packer;