@aiot-toolkit/shared-utils 1.0.17-binary-beta.2 → 1.0.17-ets-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/buildMode/BuildModeManager.js +1 -273
- package/lib/buildMode/util.js +1 -83
- package/lib/index.js +1 -488
- package/lib/record-client.js +1 -150
- package/package.json +2 -3
- package/lib/protobuf/protobuf.js +0 -284
- package/lib/protobuf/vdom_pb.js +0 -2786
|
@@ -1,274 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
4
|
-
var _path = _interopRequireDefault(require("path"));
|
|
5
|
-
var _index = require("../index");
|
|
6
|
-
var _util = require("./util");
|
|
7
|
-
var _eventBus = _interopRequireDefault(require("../../event-bus"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
-
/**
|
|
10
|
-
* Copyright (C) 2017, hapjs.org. All rights reserved.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
PACKAGER_BUILD_DONE
|
|
15
|
-
} = _eventBus.default;
|
|
16
|
-
/**
|
|
17
|
-
* 编译模式管理器
|
|
18
|
-
* @constructor
|
|
19
|
-
* @example
|
|
20
|
-
* const { BuildModeManager } = require('@aiot-toolkit/shared-utils/lib/BuildModeManager')
|
|
21
|
-
* const bmm = new BuildModeManager(root)
|
|
22
|
-
* const modeOptions = bmm.getConfig() // {current: 0, list: [{}]}
|
|
23
|
-
* bmm.addMode({name, pathName, query, scene}) // {current: 1, list: [{name, pathName, query, scene}]}
|
|
24
|
-
* bmm.update({id: 1, name, pathName, query, scene}) // 更新 id=1 的编译模式
|
|
25
|
-
* bmm.delete(1) // 删除 id=1 的编译模式
|
|
26
|
-
*
|
|
27
|
-
* @param {String} [root] - 项目根目录。可通过 {@link BuildModeManager#setRoot} 重设
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
function BuildModeManager(root) {
|
|
31
|
-
(0, _util.initProjectConfig)(root);
|
|
32
|
-
this.setRoot(root);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 读取配置
|
|
37
|
-
* @private
|
|
38
|
-
* @returns {ProjectConfig}
|
|
39
|
-
*/
|
|
40
|
-
BuildModeManager.prototype._read = function () {
|
|
41
|
-
let config = {};
|
|
42
|
-
try {
|
|
43
|
-
config = (0, _index.readJson)(this._configFile);
|
|
44
|
-
} catch (err) {
|
|
45
|
-
console.log(err.code === 'ENOENT' ? err.message : err);
|
|
46
|
-
// ignore
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!config.modeOptions) {
|
|
50
|
-
config.modeOptions = {
|
|
51
|
-
current: -1,
|
|
52
|
-
list: []
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
const modeOptions = config.modeOptions;
|
|
56
|
-
modeOptions.list = modeOptions.list.filter(m => m.id !== null && m.id !== -1);
|
|
57
|
-
return config;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 写入文件,会移除无效 id 和 重复 id
|
|
62
|
-
*
|
|
63
|
-
* @private
|
|
64
|
-
* @param {Object} config - 项目配置参数
|
|
65
|
-
*/
|
|
66
|
-
BuildModeManager.prototype._write = function (config) {
|
|
67
|
-
const modeOptions = config.modeOptions;
|
|
68
|
-
// -1 为“普通模式”
|
|
69
|
-
modeOptions.list = modeOptions.list.filter(m => m.id !== null && m.id !== -1).reduce((modes, mode) => {
|
|
70
|
-
if (!modes.find(m => m.id === mode.id)) {
|
|
71
|
-
modes.push(mode);
|
|
72
|
-
}
|
|
73
|
-
return modes;
|
|
74
|
-
}, []);
|
|
75
|
-
if (this._configFile) {
|
|
76
|
-
_fs.default.writeFileSync(this._configFile, JSON.stringify(config, null, 2));
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 获取编译模式配置
|
|
82
|
-
*
|
|
83
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
84
|
-
*/
|
|
85
|
-
BuildModeManager.prototype.getConfig = function () {
|
|
86
|
-
const config = this._read();
|
|
87
|
-
return config.modeOptions;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 获取应用的所有页面
|
|
92
|
-
*
|
|
93
|
-
* @returns {Array<String>}
|
|
94
|
-
*/
|
|
95
|
-
BuildModeManager.prototype.getAllPages = function () {
|
|
96
|
-
if (!this.root) {
|
|
97
|
-
return [];
|
|
98
|
-
}
|
|
99
|
-
const manifestPath = _path.default.join(this.root, 'src/manifest.json');
|
|
100
|
-
try {
|
|
101
|
-
const manifest = (0, _index.readJson)(manifestPath);
|
|
102
|
-
const routerPages = manifest.router.pages || {};
|
|
103
|
-
const pages = Object.keys(routerPages);
|
|
104
|
-
return pages;
|
|
105
|
-
} catch (err) {
|
|
106
|
-
console.log(err.code === 'ENOENT' ? err.message : err);
|
|
107
|
-
return [];
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 添加编译模式
|
|
113
|
-
*
|
|
114
|
-
* @private
|
|
115
|
-
* @param {Object} config - 项目配置信息
|
|
116
|
-
* @param {Object} mode - 自定义编译模式条件参数
|
|
117
|
-
* @param {String} mode.name - 名称
|
|
118
|
-
* @param {String} mode.pathName - 启动页面
|
|
119
|
-
* @param {String} [mode.query] - 启动参数
|
|
120
|
-
* @param {String|null} [mode.scene] - 场景值
|
|
121
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
122
|
-
*/
|
|
123
|
-
BuildModeManager.prototype._addMode = function (config, mode, lazy) {
|
|
124
|
-
const modeOptions = config.modeOptions;
|
|
125
|
-
let nextId;
|
|
126
|
-
if (modeOptions.list.length) {
|
|
127
|
-
nextId = Math.max.apply(null, modeOptions.list.map(m => m.id)) + 1;
|
|
128
|
-
} else {
|
|
129
|
-
nextId = 0;
|
|
130
|
-
}
|
|
131
|
-
mode.id = nextId;
|
|
132
|
-
modeOptions.list.push(mode);
|
|
133
|
-
modeOptions.current = mode.id;
|
|
134
|
-
if (!lazy) {
|
|
135
|
-
this._write(config);
|
|
136
|
-
}
|
|
137
|
-
return modeOptions;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 添加编译模式
|
|
142
|
-
*
|
|
143
|
-
* @param {Object} mode - 自定义编译模式条件参数
|
|
144
|
-
* @param {String} mode.name - 名称
|
|
145
|
-
* @param {String} mode.pathName - 启动页面
|
|
146
|
-
* @param {String} [mode.query] - 启动参数
|
|
147
|
-
* @param {String|null} [mode.scene] - 场景值
|
|
148
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
149
|
-
*/
|
|
150
|
-
BuildModeManager.prototype.addMode = function (mode) {
|
|
151
|
-
const config = this._read();
|
|
152
|
-
return this._addMode(config, mode, false);
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 添加多个编译模式
|
|
157
|
-
*
|
|
158
|
-
* @param {Array<Object>} modes - 待添加的编译模式列表
|
|
159
|
-
* @param {String} modes[].name - 名称
|
|
160
|
-
* @param {String} modes[].pathName - 启动页面
|
|
161
|
-
* @param {String} [modes[].query] - 启动参数
|
|
162
|
-
* @param {String|null} [modes[].scene] - 场景值
|
|
163
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
164
|
-
*/
|
|
165
|
-
BuildModeManager.prototype.addModes = function (modes) {
|
|
166
|
-
const config = this._read();
|
|
167
|
-
modes.forEach(mode => {
|
|
168
|
-
this._addMode(config, mode, true);
|
|
169
|
-
});
|
|
170
|
-
this._write(config);
|
|
171
|
-
return config.modeOptions;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* 删除 id={id} 的编译模式
|
|
176
|
-
*
|
|
177
|
-
* @param {Number} id - 编译模式 id
|
|
178
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
179
|
-
*/
|
|
180
|
-
BuildModeManager.prototype.delete = function (id) {
|
|
181
|
-
const config = this._read();
|
|
182
|
-
const modeOptions = config.modeOptions;
|
|
183
|
-
const index = modeOptions.list.findIndex(m => m.id === id);
|
|
184
|
-
modeOptions.current = -1;
|
|
185
|
-
if (index !== -1) {
|
|
186
|
-
modeOptions.list.splice(index, 1);
|
|
187
|
-
this._write(config);
|
|
188
|
-
}
|
|
189
|
-
return modeOptions;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* 更新编译模式
|
|
194
|
-
*
|
|
195
|
-
* @param {Object} mode - 编译模式参数
|
|
196
|
-
* @param {Number} mode.id - 目标模式 id
|
|
197
|
-
* @param {String} mode.name - 名称
|
|
198
|
-
* @param {String} mode.pathName - 启动页面
|
|
199
|
-
* @param {String} [mode.query] - 启动参数
|
|
200
|
-
* @param {String|null} [mode.scene] - 场景值
|
|
201
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
202
|
-
*/
|
|
203
|
-
BuildModeManager.prototype.update = function (mode) {
|
|
204
|
-
const config = this._read();
|
|
205
|
-
const modeOptions = config.modeOptions;
|
|
206
|
-
const index = modeOptions.list.findIndex(m => m.id === mode.id);
|
|
207
|
-
if (index !== -1) {
|
|
208
|
-
modeOptions.list[index] = mode;
|
|
209
|
-
}
|
|
210
|
-
this._write(config);
|
|
211
|
-
return modeOptions;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 选择编译模式
|
|
216
|
-
* @param {Number} id - 目标编译模式 id
|
|
217
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
218
|
-
*/
|
|
219
|
-
BuildModeManager.prototype.select = function (id) {
|
|
220
|
-
const config = this._read();
|
|
221
|
-
const modeOptions = config.modeOptions;
|
|
222
|
-
const index = modeOptions.list.findIndex(m => m.id === id);
|
|
223
|
-
if (index !== -1) {
|
|
224
|
-
modeOptions.current = id;
|
|
225
|
-
this._write(config);
|
|
226
|
-
_eventBus.default.emit(PACKAGER_BUILD_DONE);
|
|
227
|
-
}
|
|
228
|
-
return modeOptions;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 设置为普通编译模式
|
|
233
|
-
* @returns {BuildModeOptions} 当前编译模式配置
|
|
234
|
-
*/
|
|
235
|
-
BuildModeManager.prototype.setToNormal = function () {
|
|
236
|
-
const config = this._read();
|
|
237
|
-
const modeOptions = config.modeOptions;
|
|
238
|
-
modeOptions.current = -1;
|
|
239
|
-
this._write(config);
|
|
240
|
-
_eventBus.default.emit(PACKAGER_BUILD_DONE);
|
|
241
|
-
return modeOptions;
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* 重新设置根目录,便于在多个项目间切换
|
|
246
|
-
* 主要用于内部开发
|
|
247
|
-
*
|
|
248
|
-
* @param {String} [root] - 根目录
|
|
249
|
-
* @returns {BuildModeOptions|null} 当前编译模式配置
|
|
250
|
-
*/
|
|
251
|
-
BuildModeManager.prototype.setRoot = function (root) {
|
|
252
|
-
if (typeof root === 'string') {
|
|
253
|
-
this.root = root;
|
|
254
|
-
this._configFile = _path.default.resolve(root, _util.CONFIG_FILE);
|
|
255
|
-
const config = this._read();
|
|
256
|
-
return config.modeOptions;
|
|
257
|
-
}
|
|
258
|
-
return null;
|
|
259
|
-
};
|
|
260
|
-
module.exports = BuildModeManager;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* @typedef BuildModeOptions
|
|
264
|
-
* @desc 编译模式配置信息
|
|
265
|
-
* @property {Number} current - 当前编译模式的 id, -1 表示默认`普通编译`
|
|
266
|
-
* @property {Array<Object>} list - 编译模式列表
|
|
267
|
-
* @property {Number} list[].current - 编译模式 id
|
|
268
|
-
* @property {String} list[].id - 编译模式 id
|
|
269
|
-
* @property {String} list[].name - 编译模式名称
|
|
270
|
-
* @property {String} list[].pathName - 编译模式启动页面
|
|
271
|
-
* @property {String} list[].query - 编译模式启动参数
|
|
272
|
-
* @property {Number} list[].scene - 编译模式进入场景
|
|
273
|
-
*/
|
|
1
|
+
"use strict";var _fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_index=require("../index"),_util=require("./util"),_eventBus=_interopRequireDefault(require("../../event-bus"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const{PACKAGER_BUILD_DONE:PACKAGER_BUILD_DONE}=_eventBus.default;function BuildModeManager(e){(0,_util.initProjectConfig)(e),this.setRoot(e)}BuildModeManager.prototype._read=function(){let e={};try{e=(0,_index.readJson)(this._configFile)}catch(e){console.log("ENOENT"===e.code?e.message:e)}e.modeOptions||(e.modeOptions={current:-1,list:[]});const t=e.modeOptions;return t.list=t.list.filter((e=>null!==e.id&&-1!==e.id)),e},BuildModeManager.prototype._write=function(e){const t=e.modeOptions;t.list=t.list.filter((e=>null!==e.id&&-1!==e.id)).reduce(((e,t)=>(e.find((e=>e.id===t.id))||e.push(t),e)),[]),this._configFile&&_fs.default.writeFileSync(this._configFile,JSON.stringify(e,null,2))},BuildModeManager.prototype.getConfig=function(){return this._read().modeOptions},BuildModeManager.prototype.getAllPages=function(){if(!this.root)return[];const e=_path.default.join(this.root,"src/manifest.json");try{const t=(0,_index.readJson)(e).router.pages||{};return Object.keys(t)}catch(e){return console.log("ENOENT"===e.code?e.message:e),[]}},BuildModeManager.prototype._addMode=function(e,t,i){const o=e.modeOptions;let n;return n=o.list.length?Math.max.apply(null,o.list.map((e=>e.id)))+1:0,t.id=n,o.list.push(t),o.current=t.id,i||this._write(e),o},BuildModeManager.prototype.addMode=function(e){const t=this._read();return this._addMode(t,e,!1)},BuildModeManager.prototype.addModes=function(e){const t=this._read();return e.forEach((e=>{this._addMode(t,e,!0)})),this._write(t),t.modeOptions},BuildModeManager.prototype.delete=function(e){const t=this._read(),i=t.modeOptions,o=i.list.findIndex((t=>t.id===e));return i.current=-1,-1!==o&&(i.list.splice(o,1),this._write(t)),i},BuildModeManager.prototype.update=function(e){const t=this._read(),i=t.modeOptions,o=i.list.findIndex((t=>t.id===e.id));return-1!==o&&(i.list[o]=e),this._write(t),i},BuildModeManager.prototype.select=function(e){const t=this._read(),i=t.modeOptions;return-1!==i.list.findIndex((t=>t.id===e))&&(i.current=e,this._write(t),_eventBus.default.emit(PACKAGER_BUILD_DONE)),i},BuildModeManager.prototype.setToNormal=function(){const e=this._read(),t=e.modeOptions;return t.current=-1,this._write(e),_eventBus.default.emit(PACKAGER_BUILD_DONE),t},BuildModeManager.prototype.setRoot=function(e){if("string"==typeof e){this.root=e,this._configFile=_path.default.resolve(e,_util.CONFIG_FILE);return this._read().modeOptions}return null},module.exports=BuildModeManager;
|
|
274
2
|
//# sourceMappingURL=BuildModeManager.js.map
|
package/lib/buildMode/util.js
CHANGED
|
@@ -1,84 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.readJson = readJson;
|
|
7
|
-
exports.initProjectConfig = initProjectConfig;
|
|
8
|
-
exports.getLaunchPage = getLaunchPage;
|
|
9
|
-
exports.CONFIG_FILE = void 0;
|
|
10
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
11
|
-
var _path = _interopRequireDefault(require("path"));
|
|
12
|
-
var _config = _interopRequireDefault(require("../../config"));
|
|
13
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
-
/**
|
|
15
|
-
* Copyright (C) 2017, hapjs.org. All rights reserved.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
const CONFIG_FILE = '.quickapp.preview.json';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 读取一份 json 内容
|
|
22
|
-
*
|
|
23
|
-
* @param {String} jsonpath - json 路径
|
|
24
|
-
* @returns {Object} json
|
|
25
|
-
*/
|
|
26
|
-
exports.CONFIG_FILE = CONFIG_FILE;
|
|
27
|
-
function readJson(jsonpath) {
|
|
28
|
-
try {
|
|
29
|
-
return JSON.parse(_fs.default.readFileSync(jsonpath).toString());
|
|
30
|
-
} catch (err) {
|
|
31
|
-
if (err instanceof SyntaxError) {
|
|
32
|
-
throw new SyntaxError(`Failed to parse ${jsonpath}, format error`);
|
|
33
|
-
} else if (err.message.startsWith('ENOENT:')) {
|
|
34
|
-
err.message = `Failed to read ${jsonpath}, the file cannot be found`;
|
|
35
|
-
throw err;
|
|
36
|
-
} else {
|
|
37
|
-
console.error('Failed to read %s', jsonpath);
|
|
38
|
-
throw err;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 检测项目下 .quickapp.preview.json 文件(当前用于记录编译模式),无则新建
|
|
45
|
-
*
|
|
46
|
-
* @param {String} root - 项目路径
|
|
47
|
-
*/
|
|
48
|
-
function initProjectConfig(root) {
|
|
49
|
-
const proConfPath = _path.default.join(root, CONFIG_FILE);
|
|
50
|
-
if (!_fs.default.existsSync(proConfPath)) {
|
|
51
|
-
_fs.default.writeFileSync(proConfPath, JSON.stringify({}, null, 2));
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 获取当前编译模式的启动页面(带参数)
|
|
57
|
-
*
|
|
58
|
-
* @returns {String} page - 启动页面
|
|
59
|
-
*/
|
|
60
|
-
function getLaunchPage() {
|
|
61
|
-
let page = '';
|
|
62
|
-
const proConfPath = _path.default.join(_config.default.projectPath, CONFIG_FILE);
|
|
63
|
-
if (_fs.default.existsSync(proConfPath)) {
|
|
64
|
-
try {
|
|
65
|
-
const {
|
|
66
|
-
modeOptions
|
|
67
|
-
} = readJson(proConfPath);
|
|
68
|
-
if (modeOptions && typeof modeOptions.current === 'number' && modeOptions.list) {
|
|
69
|
-
const mode = modeOptions.list.find(item => {
|
|
70
|
-
return item.id === modeOptions.current;
|
|
71
|
-
});
|
|
72
|
-
if (mode) {
|
|
73
|
-
page = `${mode.pathName}?${mode.query}`;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
} catch (err) {
|
|
77
|
-
console.log(err.code === 'ENOENT' ? err.message : err);
|
|
78
|
-
// ignore
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return page;
|
|
83
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.readJson=readJson,exports.initProjectConfig=initProjectConfig,exports.getLaunchPage=getLaunchPage,exports.CONFIG_FILE=void 0;var _fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_config=_interopRequireDefault(require("../../config"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const CONFIG_FILE=".quickapp.preview.json";function readJson(e){try{return JSON.parse(_fs.default.readFileSync(e).toString())}catch(t){throw t instanceof SyntaxError?new SyntaxError(`Failed to parse ${e}, format error`):t.message.startsWith("ENOENT:")?(t.message=`Failed to read ${e}, the file cannot be found`,t):(console.error("Failed to read %s",e),t)}}function initProjectConfig(e){const t=_path.default.join(e,CONFIG_FILE);_fs.default.existsSync(t)||_fs.default.writeFileSync(t,JSON.stringify({},null,2))}function getLaunchPage(){let e="";const t=_path.default.join(_config.default.projectPath,CONFIG_FILE);if(_fs.default.existsSync(t))try{const{modeOptions:r}=readJson(t);if(r&&"number"==typeof r.current&&r.list){const t=r.list.find((e=>e.id===r.current));t&&(e=`${t.pathName}?${t.query}`)}}catch(e){console.log("ENOENT"===e.code?e.message:e)}return e}exports.CONFIG_FILE=CONFIG_FILE;
|
|
84
2
|
//# sourceMappingURL=util.js.map
|