1 | "use strict";
|
2 | var __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 | };
|
10 | const resource = require('../resource');
|
11 | const fs = require('fs-extra-promise');
|
12 | const util = require('egret-node-utils');
|
13 | const path = require('path');
|
14 | const ast = require('egret-typescript-ast');
|
15 |
|
16 | const pvrfilter_1 = require('./pvrfilter');
|
17 | let hook = {
|
18 | name: "resource",
|
19 | execute: (env) => __awaiter(this, void 0, void 0, function* () {
|
20 | let result = yield ast.findDecorator(path.join(env.root, "src/Main.ts"));
|
21 | result = result.filter(item => item.name == "RES.mapConfig");
|
22 | yield start(env, result);
|
23 | })
|
24 | };
|
25 | function start(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"];
|
37 | let firstResourceRoot = resourceRoots[0];
|
38 | let mapper = resourceRoot => path.join(sourceRoot, resourceRoot);
|
39 | let list = yield util.walk(resourceRoots.map(mapper), (p) => true);
|
40 | let resPath = path.join(sourceRoot, firstResourceFileName);
|
41 | yield runResourceProgram(resPath, env, firstResourceRoot);
|
42 | });
|
43 | }
|
44 | function runResourceProgram(resourceJsonPath, env, resourceRoot) {
|
45 | return __awaiter(this, void 0, void 0, function* () {
|
46 | var resObj = yield fs.readJSONAsync(resourceJsonPath);
|
47 | resource.parseFromPreviousVersion(resObj);
|
48 | resource.print();
|
49 | let resourcePath = path.join(env.root, resourceRoot);
|
50 | let list = yield util.walk(resourcePath, (p) => true);
|
51 | for (let item of list) {
|
52 | if (!resource.data.resources[item]) {
|
53 | console.warn("以下文件不属于 resource.json,暂时忽略", item);
|
54 | }
|
55 | }
|
56 | yield fs.mkdirpAsync(env.releaseFolder);
|
57 | yield fs.mkdirpAsync(path.join(env.releaseFolder, "resource"));
|
58 | for (let url in resource.data.resources) {
|
59 | console.log("start walkFile:" + url);
|
60 | yield walkEachFile(env, url);
|
61 | }
|
62 | yield fs.writeFileAsync(path.join(env.releaseFolder, resourceRoot, "resource.json"), JSON.stringify(resource.data));
|
63 | console.log('success-1');
|
64 | });
|
65 | }
|
66 | function walkEachFile(env, sourceFilePath) {
|
67 | return __awaiter(this, void 0, void 0, function* () {
|
68 | let filePath = path.join(env.root, "resource", sourceFilePath);
|
69 | let content = yield fs.readFileAsync(filePath);
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | let list = [pvrfilter_1.default];
|
79 | let taskList = list.map(hook => {
|
80 | return (hook) => __awaiter(this, void 0, void 0, function* () {
|
81 | console.log("Promise.all", hook);
|
82 | let e = {
|
83 | filePath: sourceFilePath,
|
84 | content: content,
|
85 | sourceRoot: env.root
|
86 | };
|
87 | if (yield hook.filter(e)) {
|
88 | console.log(hook);
|
89 | content = yield hook.execute(e);
|
90 | }
|
91 | });
|
92 | });
|
93 | console.log("taskList", taskList);
|
94 | yield Promise.all(taskList);
|
95 | let crc32 = require("../../../temp/crc32")(content);
|
96 | let size = content.length;
|
97 | let outputFileBasename = crc32 + "_" + size + path.extname(sourceFilePath);
|
98 | let outputFilePath = path.join(crc32.substring(0, 2), outputFileBasename);
|
99 | let item = resource.getResourceInfo(sourceFilePath);
|
100 | item.size = size;
|
101 | item.url = outputFilePath;
|
102 | item.crc32 = crc32;
|
103 | let fullname = path.join(env.releaseFolder, "resource", outputFilePath);
|
104 | yield fs.mkdirsAsync(path.dirname(fullname));
|
105 | yield fs.writeFileAsync(fullname, content);
|
106 | });
|
107 | }
|
108 | Object.defineProperty(exports, "__esModule", { value: true });
|
109 | exports.default = hook;
|
110 |
|
\ | No newline at end of file |