UNPKG

3.23 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 fs = require("fs-extra-promise");
12function run(projectPath) {
13 return __awaiter(this, void 0, void 0, function* () {
14 function copyLibrary() {
15 return __awaiter(this, void 0, void 0, function* () {
16 let folder = path.resolve(__dirname, "../../");
17 let source = path.join(folder, "bin");
18 let target = path.join(projectPath, "bin");
19 yield fs.copyAsync(source, target);
20 });
21 }
22 function convertEgretProperties() {
23 return __awaiter(this, void 0, void 0, function* () {
24 var propertyFile = path.join(projectPath, "egretProperties.json");
25 let propertyData = yield fs.readJsonAsync(propertyFile);
26 delete propertyData.modules['res'];
27 for (let m of propertyData.modules) {
28 if (m.name == "res") {
29 m.name = "resourcemanager";
30 m.path = ".";
31 }
32 }
33 yield fs.writeJSONAsync(propertyFile, propertyData);
34 });
35 }
36 function createDecorator() {
37 return __awaiter(this, void 0, void 0, function* () {
38 var mainSourceFile = path.join(projectPath, "src/Main.ts");
39 let contents = yield fs.readFileAsync(mainSourceFile, "utf-8");
40 if (contents.indexOf("RES.mapConfig") == -1) {
41 var index = contents.indexOf("class Main");
42 if (index == -1) {
43 throw new Error("无法匹配到 class Main,升级失败");
44 }
45 contents = contents.substr(0, index) +
46 `@RES.mapConfig("config.json",()=>"resource",path => {
47 var ext = path.substr(path.lastIndexOf(".") + 1);
48 var typeMap = {
49 "jpg": "image",
50 "png": "image",
51 "webp": "image",
52 "json": "json",
53 "fnt": "font",
54 "pvr": "pvr",
55 "mp3": "sound"
56 }
57 var type = typeMap[ext];
58 if (type == "json") {
59 if (path.indexOf("sheet") >= 0) {
60 type = "sheet";
61 } else if (path.indexOf("movieclip") >= 0) {
62 type = "movieclip";
63 };
64 }
65 return type;
66})\n`
67 + contents.substr(index);
68 }
69 yield fs.writeFileAsync(mainSourceFile, contents, "utf-8");
70 });
71 }
72 yield convertEgretProperties();
73 yield copyLibrary();
74 yield createDecorator();
75 });
76}
77exports.run = run;