UNPKG

6.68 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 fs = require('fs-extra-promise');
11const path = require('path');
12const util_1 = require('./util');
13let projectConfig = {};
14global['globalConfig'] = {};
15let configValidator = {};
16function getConfig(key) {
17 return __awaiter(this, void 0, void 0, function* () {
18 let result;
19 if (typeof key === "string") {
20 if (projectConfig[key]) {
21 result = projectConfig[key];
22 }
23 if (globalConfig[key]) {
24 result = globalConfig[key];
25 }
26 if (!result) {
27 throw new util_1.CliException(1000, key);
28 }
29 if (configValidator[key]) {
30 var validator = configValidator[key];
31 validator(result);
32 }
33 if (exports.KEY.ANDROID_SUPPORT === key) {
34 if (!testAndroidSupportFilePath(result)) {
35 throw new util_1.CliException(1001, "cannot find android-support path or path illegal!");
36 }
37 }
38 }
39 else {
40 var allConfig = yield getAllConfig();
41 result = key.getter(allConfig);
42 if (key.validator) {
43 key.validator(result);
44 }
45 }
46 return result;
47 });
48}
49exports.getConfig = getConfig;
50exports.KEY = {
51 ANDROID_SUPPORT: "android_support",
52 ANDORID_PROJECT_PATH: "android.project.path",
53 ANDROID_SDK: "android-sdk",
54 ANT_DIR_PATH: "ant-dir-path"
55};
56function getAppDataPath() {
57 var result;
58 switch (process.platform) {
59 case 'darwin':
60 var home = process.env.HOME || ("/Users/" + (process.env.NAME || process.env.LOGNAME));
61 if (!home)
62 return null;
63 result = `${home}/Library/Application Support/`;
64 break;
65 case 'win32':
66 result = process.env.AppData || `${process.env.USERPROFILE}/AppData/Roaming/`;
67 break;
68 default:
69 ;
70 }
71 return result;
72}
73function initConfig(projectPath) {
74 return __awaiter(this, void 0, Promise, function* () {
75 exports.egretProjectPath = projectPath;
76 initProjectConfig();
77 yield initGlobalConfig();
78 });
79}
80exports.initConfig = initConfig;
81function getAllConfig() {
82 return __awaiter(this, void 0, Promise, function* () {
83 return Object.assign({}, projectConfig, globalConfig);
84 });
85}
86exports.getAllConfig = getAllConfig;
87function initProjectConfig() {
88 if (exports.egretProjectPath) {
89 let projectConfigFilename = path.join(exports.egretProjectPath, "egret.config");
90 if (!fs.existsSync(projectConfigFilename)) {
91 projectConfig = {};
92 }
93 else {
94 let content = fs.readFileSync(projectConfigFilename, "utf-8");
95 projectConfig = JSON.parse(content);
96 }
97 }
98}
99function initGlobalConfig() {
100 return __awaiter(this, void 0, void 0, function* () {
101 initConfigValidator(exports.KEY.ANDROID_SUPPORT, androidSupportValidator);
102 var globalConfigFilename = path.join(getAppDataPath(), "egret.config");
103 console.log("global config file:", globalConfigFilename);
104 let isExist = yield fs.existsAsync(globalConfigFilename);
105 if (!isExist) {
106 globalConfig = {};
107 }
108 else {
109 let content = yield fs.readFileAsync(globalConfigFilename, "utf-8");
110 globalConfig = JSON.parse(content);
111 }
112 });
113}
114function initConfigValidator(key, validator) {
115 configValidator[key] = validator;
116}
117let androidSupportValidator = (value) => {
118 var result = fs.existsSync(value);
119 //todo
120 // if (false){
121 // throw new CliException(1001);
122 // }
123 return result;
124};
125function validateConfigs(config, keys) {
126 for (var key of keys) {
127 var value = getConfig(key);
128 }
129 return config;
130}
131exports.validateConfigs = validateConfigs;
132function setProjectConfig(key, value) {
133 return __awaiter(this, void 0, void 0, function* () {
134 console.log("=============", key);
135 if (typeof key === 'string') {
136 projectConfig[key] = value;
137 }
138 else {
139 key.setter(projectConfig, value);
140 }
141 var projectConfigFilename = path.join(exports.egretProjectPath, "egret.config");
142 var content = JSON.stringify(projectConfig, null, "\t");
143 fs.writeFileAsync(projectConfigFilename, content, "utf-8");
144 });
145}
146exports.setProjectConfig = setProjectConfig;
147function testAndroidSupportFilePath(value) {
148 if (fs.existsSync(value)) {
149 let android_support_path = path.join(value, "proj.android");
150 if (fs.existsSync(android_support_path)) {
151 let propertyFilePath = path.join(android_support_path, "project.properties");
152 let manifestFilePath = path.join(android_support_path, "AndroidManifest.xml");
153 if (fs.existsSync(propertyFilePath) &&
154 fs.existsSync(manifestFilePath)) {
155 return true;
156 }
157 }
158 }
159 return false;
160}
161function setGlobaltConfig(key, value) {
162 return __awaiter(this, void 0, void 0, function* () {
163 if (exports.KEY.ANDROID_SUPPORT === key) {
164 if (!testAndroidSupportFilePath(value)) {
165 throw new util_1.CliException(1001, "cannot find android-support path or path illegal!");
166 }
167 }
168 globalConfig[key] = value;
169 var globalConfigFilename = path.join(getAppDataPath(), "egret.config");
170 var content = JSON.stringify(globalConfig, null, "\t");
171 fs.writeFileAsync(globalConfigFilename, content, "utf-8");
172 });
173}
174exports.setGlobaltConfig = setGlobaltConfig;
175var Selector;
176(function (Selector) {
177 Selector.android_support_path = {
178 getter: c => c.androidProjectPath,
179 setter: (c, value) => c.androidProjectPath = value
180 };
181 Selector.channel_sdk = {
182 getter: c => c.sdk,
183 setter: (c, value) => {
184 c.sdk = value;
185 }
186 };
187})(Selector = exports.Selector || (exports.Selector = {}));
188//# sourceMappingURL=config.js.map
\No newline at end of file