UNPKG

10.1 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments)).next());
8 });
9};
10const path = require('path');
11const ast = require('egret-typescript-ast');
12const fs = require('fs-extra-promise');
13const util = require('egret-node-utils');
14const stream = require('stream');
15const resource = require('./resource');
16let hook = { name: "generateVersion", execute: null };
17hook.execute = (env) => __awaiter(this, void 0, void 0, function* () {
18 let result = yield ast.findDecorator(path.join(env.root, "src/Main.ts"));
19 result = result.filter(item => item.name == "RES.mapConfig");
20 yield new GenerateVersion().execute(env, result);
21});
22Object.defineProperty(exports, "__esModule", { value: true });
23exports.default = hook;
24class GenerateVersion {
25 execute(env, decorators) {
26 return __awaiter(this, void 0, Promise, function* () {
27 let sourceRoot = env.root;
28 let releasePath = env.releaseFolder;
29 console.log(`${process.platform}_${process.arch}`);
30 console.log(sourceRoot, releasePath, decorators);
31 if (!decorators || !decorators.length) {
32 throw new Error("no RES.config found");
33 }
34 let resourceFileNames = decorators.map(item => item.paramters[0]);
35 let firstResourceFileName = resourceFileNames[0];
36 let resourceRoots = ["resource"]; //decorators.map(item => item.generics)[0][0];
37 let firstResourceRoot = resourceRoots[0];
38 let mapper = resourceRoot => path.join(sourceRoot, resourceRoot);
39 let list = yield util.walk(resourceRoots.map(mapper), (p) => {
40 if (path.basename(p).charAt(0) == ".") {
41 return false;
42 }
43 return true;
44 });
45 var resPath = path.join(sourceRoot, firstResourceFileName);
46 //TODO:这里报错不会输出
47 try {
48 var resObj = yield fs.readJSONAsync(resPath);
49 yield resource.execute(resPath, env, firstResourceRoot);
50 }
51 catch (e) {
52 console.log(e);
53 }
54 process.exit();
55 var resourceOutputModeList = ["default"]; //, "ios"
56 var length = list.length;
57 let resourceMapper = (element) => __awaiter(this, void 0, void 0, function* () {
58 for (let filePath of list) {
59 var sourceFilePath = path.relative(sourceRoot, filePath);
60 //resource.json文件不被混淆
61 let isResourceFile = false;
62 let isFirstResourceFile = false;
63 let index = resourceFileNames.indexOf(sourceFilePath);
64 if (index == 0) {
65 isFirstResourceFile = true;
66 isResourceFile = true;
67 }
68 else if (index > 0) {
69 isResourceFile = true;
70 }
71 if (isResourceFile) {
72 if (!isFirstResourceFile) {
73 //后续配置文件单纯拷贝
74 //todo没准也要生成crc32一份
75 fs.copySync(filePath, path.join(releasePath, sourceFilePath));
76 }
77 continue;
78 }
79 ;
80 var resRoot = element == "default" ? "resource" : "resource-" + element;
81 var buffer = yield fs.readFileAsync(filePath);
82 let sourceStream = fs.createReadStream(filePath);
83 // let env = {
84 // filePath, content: buffer, sourceRoot
85 // }
86 //
87 // let list: PublishFileHook[] = [euiThemeJsonFileHook];
88 //
89 //
90 // if (await euiThemeJsonFileHook.filter(env)) {
91 // buffer = await euiThemeJsonFileHook.execute(env);
92 // sourceStream.pipe(euiThemeJsonFileHook.transform);
93 // }
94 let transform = new GenerateReleaseFileTransform(resObj, sourceFilePath, resRoot, releasePath);
95 // appendDataToRes(sourceFilePath, txtCrc32, size, resRoot);
96 transform.on("finish", () => console.log("onFinish", this['sourceFilePath']));
97 transform.on("handled", function () {
98 let sourceFilePath = this['sourceFilePath'];
99 console.log("onHandled", sourceFilePath);
100 // console.log('xxx', sourceFilePath);
101 //
102 // console.log('yyy', getConfig(resObj, sourceFilePath, resRoot))
103 // console.log("zzz", resObj)
104 var item = getConfig(resObj, sourceFilePath, resRoot);
105 let outputFileName = item.url;
106 let outputFilePath = path.join(releasePath, outputFileName);
107 // console.log("write : " + outputFileName)
108 try {
109 if (fs.existsSync(path.dirname(outputFilePath)) == false) {
110 fs.mkdirSync(path.dirname(outputFilePath));
111 }
112 }
113 catch (e) {
114 console.log(e);
115 }
116 console.log("write 222: " + outputFileName);
117 // this.pipe(fs.createWriteStream(outputFilePath));
118 fs.writeFile(outputFilePath, item.data);
119 });
120 let stream = sourceStream.pipe(transform);
121 }
122 });
123 yield fs.mkdirpAsync(releasePath);
124 yield fs.mkdirpAsync(path.join(releasePath, "resource"));
125 yield Promise.all(resourceOutputModeList.map(resourceMapper));
126 yield fs.writeFileAsync(path.join(releasePath, firstResourceFileName), JSON.stringify(resObj));
127 });
128 }
129}
130exports.GenerateVersion = GenerateVersion;
131function onFinish(invoker) {
132 return new Promise((resolve, reject) => {
133 invoker.on("finish", resolve);
134 invoker.on("error", reject);
135 });
136}
137function getConfig(resConfig, sourceFilePath, resRoot) {
138 let resourcesList = resConfig.resources;
139 for (let item of resourcesList) {
140 if (item.virtualUrl) {
141 if (item.virtualUrl == sourceFilePath) {
142 return item;
143 }
144 }
145 if (item.url == sourceFilePath) {
146 return item;
147 }
148 }
149 let name = "$egret$" + GenerateReleaseFileTransform.appendDataToResIndex++;
150 let url = sourceFilePath.replace(resRoot + "/", "");
151 let item = { name: name, url: url };
152 resourcesList.push(item);
153 return item;
154}
155class GenerateReleaseFileTransform extends stream.Transform {
156 constructor(resConfig, sourceFilePath, resRoot, releaseRoot) {
157 super();
158 this.resConfig = resConfig;
159 this.sourceFilePath = sourceFilePath;
160 this.resRoot = resRoot;
161 this.releaseRoot = releaseRoot;
162 this.__chunks = [];
163 this.__size = 0;
164 this.on("data", this.onData.bind(this));
165 this.on("end", this.onEnd.bind(this));
166 }
167 onData(data) {
168 // console.log("onData",data);
169 }
170 onEnd() {
171 console.log("onEnd", this.sourceFilePath);
172 // console.log("size:",this.__size);
173 var data = null;
174 switch (this.__chunks.length) {
175 case 0:
176 data = new Buffer(0);
177 break;
178 case 1:
179 data = this.__chunks[0];
180 break;
181 default:
182 data = new Buffer(this.__size);
183 for (var i = 0, pos = 0, l = this.__chunks.length; i < l; i++) {
184 var chunk = this.__chunks[i];
185 chunk.copy(data, pos);
186 pos += chunk.length;
187 }
188 break;
189 }
190 // console.log("onEnd");
191 this.handleData(data);
192 }
193 _transform(chunk, encoding, callback) {
194 console.log("onTransform");
195 // crc32()
196 // this._flush
197 // setTimeout(callback, 300)
198 this.__chunks.push(chunk);
199 this.__size += chunk.length;
200 this.push(chunk);
201 callback();
202 }
203 _flush(callback) {
204 callback();
205 }
206 handleData(chunk) {
207 console.log(chunk);
208 let resObj = this.resConfig;
209 let found = false;
210 let crc32 = require("../../temp/crc32")(chunk);
211 let size = chunk.length;
212 let resourcesList = resObj.resources;
213 let outputFileBasename = crc32 + "_" + size + path.extname(this.sourceFilePath);
214 let url = path.join(this.resRoot, crc32.substring(0, 2), outputFileBasename);
215 let item = getConfig(this.resConfig, this.sourceFilePath, this.resRoot);
216 item.size = size;
217 item.url = url;
218 item.crc32 = crc32;
219 item.virtualUrl = this.sourceFilePath;
220 item.data = chunk;
221 console.log('transfrom complete : ' + url);
222 this.emit("handled");
223 }
224}
225GenerateReleaseFileTransform.appendDataToResIndex = 0;
226class Publish extends fs.WriteStream {
227 constructor(sourceFilePath) {
228 super();
229 this.sourceFilePath = sourceFilePath;
230 }
231}
232//# sourceMappingURL=generate-version.js.map
\No newline at end of file