@cocojs/cli 0.0.1-alpha202511021451
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/bin/coco.js +5 -0
- package/dist/build-dot-coco-process/index.js +822 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +508 -0
- package/dist/webpack-process/index.js +510 -0
- package/package.json +50 -0
- package/runtime-config/tsconfig.json +17 -0
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var process$1 = require('node:process');
|
|
4
|
+
var fs = require('fs');
|
|
5
|
+
var fse = require('fs-extra');
|
|
6
|
+
var path$1 = require('path');
|
|
7
|
+
var path = require('node:path');
|
|
8
|
+
var parser = require('@babel/parser');
|
|
9
|
+
var traverse = require('@babel/traverse');
|
|
10
|
+
var types = require('@babel/types');
|
|
11
|
+
var chokidar = require('chokidar');
|
|
12
|
+
|
|
13
|
+
function _interopNamespaceDefault(e) {
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n.default = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var process__namespace = /*#__PURE__*/_interopNamespaceDefault(process$1);
|
|
31
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
32
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path$1);
|
|
33
|
+
|
|
34
|
+
function _arrayLikeToArray(r, a) {
|
|
35
|
+
(null == a || a > r.length) && (a = r.length);
|
|
36
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
37
|
+
return n;
|
|
38
|
+
}
|
|
39
|
+
function _arrayWithoutHoles(r) {
|
|
40
|
+
if (Array.isArray(r)) return _arrayLikeToArray(r);
|
|
41
|
+
}
|
|
42
|
+
function _classCallCheck(a, n) {
|
|
43
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
44
|
+
}
|
|
45
|
+
function _defineProperties(e, r) {
|
|
46
|
+
for (var t = 0; t < r.length; t++) {
|
|
47
|
+
var o = r[t];
|
|
48
|
+
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function _createClass(e, r, t) {
|
|
52
|
+
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
|
|
53
|
+
writable: false
|
|
54
|
+
}), e;
|
|
55
|
+
}
|
|
56
|
+
function _createForOfIteratorHelper(r, e) {
|
|
57
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
58
|
+
if (!t) {
|
|
59
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
|
|
60
|
+
t && (r = t);
|
|
61
|
+
var n = 0,
|
|
62
|
+
F = function () {};
|
|
63
|
+
return {
|
|
64
|
+
s: F,
|
|
65
|
+
n: function () {
|
|
66
|
+
return n >= r.length ? {
|
|
67
|
+
done: true
|
|
68
|
+
} : {
|
|
69
|
+
done: false,
|
|
70
|
+
value: r[n++]
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
e: function (r) {
|
|
74
|
+
throw r;
|
|
75
|
+
},
|
|
76
|
+
f: F
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
80
|
+
}
|
|
81
|
+
var o,
|
|
82
|
+
a = true,
|
|
83
|
+
u = false;
|
|
84
|
+
return {
|
|
85
|
+
s: function () {
|
|
86
|
+
t = t.call(r);
|
|
87
|
+
},
|
|
88
|
+
n: function () {
|
|
89
|
+
var r = t.next();
|
|
90
|
+
return a = r.done, r;
|
|
91
|
+
},
|
|
92
|
+
e: function (r) {
|
|
93
|
+
u = true, o = r;
|
|
94
|
+
},
|
|
95
|
+
f: function () {
|
|
96
|
+
try {
|
|
97
|
+
a || null == t.return || t.return();
|
|
98
|
+
} finally {
|
|
99
|
+
if (u) throw o;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function _iterableToArray(r) {
|
|
105
|
+
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
|
|
106
|
+
}
|
|
107
|
+
function _nonIterableSpread() {
|
|
108
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
109
|
+
}
|
|
110
|
+
function _regenerator() {
|
|
111
|
+
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
112
|
+
var e,
|
|
113
|
+
t,
|
|
114
|
+
r = "function" == typeof Symbol ? Symbol : {},
|
|
115
|
+
n = r.iterator || "@@iterator",
|
|
116
|
+
o = r.toStringTag || "@@toStringTag";
|
|
117
|
+
function i(r, n, o, i) {
|
|
118
|
+
var c = n && n.prototype instanceof Generator ? n : Generator,
|
|
119
|
+
u = Object.create(c.prototype);
|
|
120
|
+
return _regeneratorDefine(u, "_invoke", function (r, n, o) {
|
|
121
|
+
var i,
|
|
122
|
+
c,
|
|
123
|
+
u,
|
|
124
|
+
f = 0,
|
|
125
|
+
p = o || [],
|
|
126
|
+
y = false,
|
|
127
|
+
G = {
|
|
128
|
+
p: 0,
|
|
129
|
+
n: 0,
|
|
130
|
+
v: e,
|
|
131
|
+
a: d,
|
|
132
|
+
f: d.bind(e, 4),
|
|
133
|
+
d: function (t, r) {
|
|
134
|
+
return i = t, c = 0, u = e, G.n = r, a;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function d(r, n) {
|
|
138
|
+
for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
|
|
139
|
+
var o,
|
|
140
|
+
i = p[t],
|
|
141
|
+
d = G.p,
|
|
142
|
+
l = i[2];
|
|
143
|
+
r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
|
|
144
|
+
}
|
|
145
|
+
if (o || r > 1) return a;
|
|
146
|
+
throw y = true, n;
|
|
147
|
+
}
|
|
148
|
+
return function (o, p, l) {
|
|
149
|
+
if (f > 1) throw TypeError("Generator is already running");
|
|
150
|
+
for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
|
|
151
|
+
i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
|
|
152
|
+
try {
|
|
153
|
+
if (f = 2, i) {
|
|
154
|
+
if (c || (o = "next"), t = i[o]) {
|
|
155
|
+
if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
|
|
156
|
+
if (!t.done) return t;
|
|
157
|
+
u = t.value, c < 2 && (c = 0);
|
|
158
|
+
} else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
|
|
159
|
+
i = e;
|
|
160
|
+
} else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
|
|
161
|
+
} catch (t) {
|
|
162
|
+
i = e, c = 1, u = t;
|
|
163
|
+
} finally {
|
|
164
|
+
f = 1;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
value: t,
|
|
169
|
+
done: y
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
}(r, o, i), true), u;
|
|
173
|
+
}
|
|
174
|
+
var a = {};
|
|
175
|
+
function Generator() {}
|
|
176
|
+
function GeneratorFunction() {}
|
|
177
|
+
function GeneratorFunctionPrototype() {}
|
|
178
|
+
t = Object.getPrototypeOf;
|
|
179
|
+
var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
|
|
180
|
+
return this;
|
|
181
|
+
}), t),
|
|
182
|
+
u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
|
|
183
|
+
function f(e) {
|
|
184
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
|
|
185
|
+
}
|
|
186
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
|
|
187
|
+
return this;
|
|
188
|
+
}), _regeneratorDefine(u, "toString", function () {
|
|
189
|
+
return "[object Generator]";
|
|
190
|
+
}), (_regenerator = function () {
|
|
191
|
+
return {
|
|
192
|
+
w: i,
|
|
193
|
+
m: f
|
|
194
|
+
};
|
|
195
|
+
})();
|
|
196
|
+
}
|
|
197
|
+
function _regeneratorDefine(e, r, n, t) {
|
|
198
|
+
var i = Object.defineProperty;
|
|
199
|
+
try {
|
|
200
|
+
i({}, "", {});
|
|
201
|
+
} catch (e) {
|
|
202
|
+
i = 0;
|
|
203
|
+
}
|
|
204
|
+
_regeneratorDefine = function (e, r, n, t) {
|
|
205
|
+
function o(r, n) {
|
|
206
|
+
_regeneratorDefine(e, r, function (e) {
|
|
207
|
+
return this._invoke(r, n, e);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
r ? i ? i(e, r, {
|
|
211
|
+
value: n,
|
|
212
|
+
enumerable: !t,
|
|
213
|
+
configurable: !t,
|
|
214
|
+
writable: !t
|
|
215
|
+
}) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
|
|
216
|
+
}, _regeneratorDefine(e, r, n, t);
|
|
217
|
+
}
|
|
218
|
+
function _toConsumableArray(r) {
|
|
219
|
+
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
|
|
220
|
+
}
|
|
221
|
+
function _toPrimitive(t, r) {
|
|
222
|
+
if ("object" != typeof t || !t) return t;
|
|
223
|
+
var e = t[Symbol.toPrimitive];
|
|
224
|
+
if (void 0 !== e) {
|
|
225
|
+
var i = e.call(t, r);
|
|
226
|
+
if ("object" != typeof i) return i;
|
|
227
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
228
|
+
}
|
|
229
|
+
return (String )(t);
|
|
230
|
+
}
|
|
231
|
+
function _toPropertyKey(t) {
|
|
232
|
+
var i = _toPrimitive(t, "string");
|
|
233
|
+
return "symbol" == typeof i ? i : i + "";
|
|
234
|
+
}
|
|
235
|
+
function _typeof(o) {
|
|
236
|
+
"@babel/helpers - typeof";
|
|
237
|
+
|
|
238
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
239
|
+
return typeof o;
|
|
240
|
+
} : function (o) {
|
|
241
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
242
|
+
}, _typeof(o);
|
|
243
|
+
}
|
|
244
|
+
function _unsupportedIterableToArray(r, a) {
|
|
245
|
+
if (r) {
|
|
246
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
247
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
248
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
253
|
+
function adopt(value) {
|
|
254
|
+
return value instanceof P ? value : new P(function (resolve) {
|
|
255
|
+
resolve(value);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
259
|
+
function fulfilled(value) {
|
|
260
|
+
try {
|
|
261
|
+
step(generator.next(value));
|
|
262
|
+
} catch (e) {
|
|
263
|
+
reject(e);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function rejected(value) {
|
|
267
|
+
try {
|
|
268
|
+
step(generator["throw"](value));
|
|
269
|
+
} catch (e) {
|
|
270
|
+
reject(e);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function step(result) {
|
|
274
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
275
|
+
}
|
|
276
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
280
|
+
var e = new Error(message);
|
|
281
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
var Project = /*#__PURE__*/function () {
|
|
285
|
+
function Project(projectPath) {
|
|
286
|
+
var _this = this;
|
|
287
|
+
_classCallCheck(this, Project);
|
|
288
|
+
// 项目所在文件夹的绝对路径
|
|
289
|
+
this.absPath = '';
|
|
290
|
+
this.genFullPath = function (name) {
|
|
291
|
+
return path.join(_this.srcAbsPath, name);
|
|
292
|
+
};
|
|
293
|
+
this.absPath = path.join(process.cwd(), projectPath);
|
|
294
|
+
}
|
|
295
|
+
return _createClass(Project, [{
|
|
296
|
+
key: "srcAbsPath",
|
|
297
|
+
get: function get() {
|
|
298
|
+
return path.join(this.absPath, "src");
|
|
299
|
+
}
|
|
300
|
+
}, {
|
|
301
|
+
key: "propertiesAbsPath",
|
|
302
|
+
get: function get() {
|
|
303
|
+
return path.join(this.absPath, "properties");
|
|
304
|
+
}
|
|
305
|
+
}, {
|
|
306
|
+
key: "dotCocoAbsPath",
|
|
307
|
+
get: function get() {
|
|
308
|
+
return path.join(this.srcAbsPath, Project.DOT_COCO_DIR);
|
|
309
|
+
}
|
|
310
|
+
}, {
|
|
311
|
+
key: "applicationTsFileAbsPath",
|
|
312
|
+
get: function get() {
|
|
313
|
+
return path.join(this.srcAbsPath, Project.APPLICATION);
|
|
314
|
+
}
|
|
315
|
+
}]);
|
|
316
|
+
}();
|
|
317
|
+
Project.DOT_COCO_DIR = '.coco';
|
|
318
|
+
Project.APPLICATION = 'application.ts';
|
|
319
|
+
Project.VIEW_DIR = 'view';
|
|
320
|
+
Project.CONFIG_DIR = 'config';
|
|
321
|
+
Project.PAGE_DIR = 'page';
|
|
322
|
+
Project.LAYOUT_DIR = 'layout';
|
|
323
|
+
Project.Flow_DIR = 'flow';
|
|
324
|
+
Project.API_DIR = 'api';
|
|
325
|
+
Project.STORE_DIR = 'store';
|
|
326
|
+
Project.GLOBAL_DATA_DIR = 'global-data';
|
|
327
|
+
Project.COMPONENTS_DIR = 'component';
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 构建.coco文件夹
|
|
331
|
+
*/
|
|
332
|
+
function genIndexTsx(project, iocComponents) {
|
|
333
|
+
// 2. 生成.coco文件
|
|
334
|
+
var importStatements = iocComponents.map(function (_ref) {
|
|
335
|
+
var className = _ref.className,
|
|
336
|
+
filePath = _ref.filePath;
|
|
337
|
+
var relativePath = path$1.relative(project.dotCocoAbsPath, filePath);
|
|
338
|
+
var extLen = path$1.extname(relativePath).length;
|
|
339
|
+
var relativePathNoExt = relativePath.slice(0, relativePath.length - extLen);
|
|
340
|
+
return "export { default as ".concat(className, " } from '").concat(relativePathNoExt, "';");
|
|
341
|
+
});
|
|
342
|
+
var dotCocoDir = project.genFullPath(Project.DOT_COCO_DIR);
|
|
343
|
+
fse.ensureDirSync(dotCocoDir);
|
|
344
|
+
fs.writeFileSync(path$1.join(dotCocoDir, 'index.tsx'), appendExport(importStatements), {
|
|
345
|
+
encoding: 'utf-8'
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
function appendExport(importStatements) {
|
|
349
|
+
var pre = "";
|
|
350
|
+
var append = "\nimport { Application } from \"coco-mvc\";\nimport applicationJson from \"./application.json\";\nexport { Application };\n\n".concat(process__namespace.env.NODE_ENV === 'test' ? "\n// \u6D4B\u8BD5\u65F6\u7531\u6D4B\u8BD5\u7528\u4F8B\u542F\u52A8Application\n" : "\nconst application = new Application(applicationJson);\napplication.start();\n ", "\n ");
|
|
351
|
+
return pre.concat(importStatements.join('\n')).concat(append);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
var cmdDefaultEnvMap = {
|
|
355
|
+
dev: 'dev',
|
|
356
|
+
build: 'prod'
|
|
357
|
+
};
|
|
358
|
+
var defaultPropertiesName = 'application.json';
|
|
359
|
+
/**
|
|
360
|
+
* 综合NODE_ENV和命令得到运行时配置文件名
|
|
361
|
+
* 如果有NODE_ENV,则返回
|
|
362
|
+
* 如果没有,则考虑dev和build默认设定
|
|
363
|
+
*/
|
|
364
|
+
var propertiesFileName = function propertiesFileName(cmd) {
|
|
365
|
+
var env = process$1.env.NODE_ENV || cmdDefaultEnvMap[cmd] || '';
|
|
366
|
+
return env ? "application.".concat(env, ".json") : defaultPropertiesName;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
function readFile(filepath) {
|
|
370
|
+
var content = '{}';
|
|
371
|
+
if (fs.existsSync(filepath)) {
|
|
372
|
+
content = fs.readFileSync(filepath, 'utf8');
|
|
373
|
+
}
|
|
374
|
+
return content;
|
|
375
|
+
}
|
|
376
|
+
function merge(project, cmd) {
|
|
377
|
+
var defaultConfigStr = readFile(path$1.join(project.absPath, "properties/".concat(defaultPropertiesName)));
|
|
378
|
+
var envConfigStr = readFile(path$1.join(project.absPath, "properties/".concat(propertiesFileName(cmd))));
|
|
379
|
+
var defaultConfig = {};
|
|
380
|
+
try {
|
|
381
|
+
defaultConfig = JSON.parse(defaultConfigStr);
|
|
382
|
+
} catch (error) {
|
|
383
|
+
// 写了一半的配置文件
|
|
384
|
+
}
|
|
385
|
+
var envConfig = {};
|
|
386
|
+
try {
|
|
387
|
+
envConfig = JSON.parse(envConfigStr);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
// 写了一半的配置文件
|
|
390
|
+
}
|
|
391
|
+
var config = merge2properties(defaultConfig, envConfig);
|
|
392
|
+
var filePath = path$1.join(project.absPath, 'src/.coco/application.json');
|
|
393
|
+
fs.writeFileSync(filePath, JSON.stringify(config, null, 2), {
|
|
394
|
+
encoding: 'utf-8'
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
// 从前往后依次合并配置对象
|
|
398
|
+
function merge2properties() {
|
|
399
|
+
for (var _len = arguments.length, configs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
400
|
+
configs[_key] = arguments[_key];
|
|
401
|
+
}
|
|
402
|
+
if (configs.length === 0) {
|
|
403
|
+
return {};
|
|
404
|
+
}
|
|
405
|
+
return configs.reduce(function (prev, currentConfig) {
|
|
406
|
+
return doMerge(prev, currentConfig);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
function doMerge(config1, config2) {
|
|
410
|
+
var type1 = _typeof(config1);
|
|
411
|
+
var type2 = _typeof(config2);
|
|
412
|
+
if (type1 !== type2) {
|
|
413
|
+
return config2;
|
|
414
|
+
} else if (type2 === 'string' || type2 === 'boolean' || type2 === 'number' || config2 === null || Array.isArray(config2)) {
|
|
415
|
+
return config2;
|
|
416
|
+
}
|
|
417
|
+
var keys2 = Object.keys(config2);
|
|
418
|
+
var merged = Object.assign({}, config1);
|
|
419
|
+
for (var _i = 0, _keys = keys2; _i < _keys.length; _i++) {
|
|
420
|
+
var key = _keys[_i];
|
|
421
|
+
merged[key] = doMerge(config1[key], config2[key]);
|
|
422
|
+
}
|
|
423
|
+
return merged;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// console.log(numberToLetter(0)); // A
|
|
427
|
+
// console.log(numberToLetter(1)); // B
|
|
428
|
+
// console.log(numberToLetter(25)); // Z
|
|
429
|
+
// console.log(numberToLetter(26)); // AA
|
|
430
|
+
// console.log(numberToLetter(27)); // AB
|
|
431
|
+
// console.log(numberToLetter(51)); // AZ
|
|
432
|
+
// console.log(numberToLetter(52)); // BA
|
|
433
|
+
function numberToLetter(n) {
|
|
434
|
+
var result = '';
|
|
435
|
+
while (n >= 0) {
|
|
436
|
+
result = String.fromCharCode(n % 26 + 65) + result;
|
|
437
|
+
n = Math.floor(n / 26) - 1;
|
|
438
|
+
if (n < 0) break;
|
|
439
|
+
}
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
var PATH_TYPE;
|
|
444
|
+
(function (PATH_TYPE) {
|
|
445
|
+
PATH_TYPE[PATH_TYPE["FOLDER"] = 0] = "FOLDER";
|
|
446
|
+
PATH_TYPE[PATH_TYPE["FILE"] = 1] = "FILE";
|
|
447
|
+
})(PATH_TYPE || (PATH_TYPE = {}));
|
|
448
|
+
var number = 0;
|
|
449
|
+
var RE_DEFAULT_EXPORT = /export\s+default\s+(\w+);?\s?/;
|
|
450
|
+
function scanOneFile(filePath, decorator) {
|
|
451
|
+
var content = fs__namespace.readFileSync(filePath, 'utf-8');
|
|
452
|
+
if (content.includes(decorator) && RE_DEFAULT_EXPORT.test(content)) {
|
|
453
|
+
// todo 需要校验export出来的class名称和注解的是否一致
|
|
454
|
+
// const className = RE_DEFAULT_EXPORT.exec(content)[1];
|
|
455
|
+
return {
|
|
456
|
+
className: numberToLetter(number++),
|
|
457
|
+
filePath: filePath
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
function doScan(type, _path, fileExt, decorator) {
|
|
463
|
+
var result = [];
|
|
464
|
+
if (!fs__namespace.existsSync(_path)) {
|
|
465
|
+
return result;
|
|
466
|
+
}
|
|
467
|
+
if (type === PATH_TYPE.FOLDER) {
|
|
468
|
+
var files = fs__namespace.readdirSync(_path);
|
|
469
|
+
var _iterator = _createForOfIteratorHelper(files),
|
|
470
|
+
_step;
|
|
471
|
+
try {
|
|
472
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
473
|
+
var file = _step.value;
|
|
474
|
+
var filePath = path__namespace.join(_path, file);
|
|
475
|
+
var stat = fs__namespace.statSync(filePath);
|
|
476
|
+
if (stat.isDirectory()) {
|
|
477
|
+
var find = doScan(PATH_TYPE.FOLDER, filePath, fileExt, decorator);
|
|
478
|
+
if (find.length) {
|
|
479
|
+
result.push.apply(result, _toConsumableArray(find));
|
|
480
|
+
}
|
|
481
|
+
} else if (stat.isFile() && path__namespace.extname(filePath) === fileExt) {
|
|
482
|
+
var r = scanOneFile(filePath, decorator);
|
|
483
|
+
if (r) {
|
|
484
|
+
result.push(r);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
} catch (err) {
|
|
489
|
+
_iterator.e(err);
|
|
490
|
+
} finally {
|
|
491
|
+
_iterator.f();
|
|
492
|
+
}
|
|
493
|
+
} else if (type === PATH_TYPE.FILE) {
|
|
494
|
+
var _r = scanOneFile(_path, decorator);
|
|
495
|
+
if (_r) {
|
|
496
|
+
result.push(_r);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return result;
|
|
500
|
+
}
|
|
501
|
+
var scanPathConfig = [{
|
|
502
|
+
// 配置项
|
|
503
|
+
type: PATH_TYPE.FOLDER,
|
|
504
|
+
path: Project.CONFIG_DIR,
|
|
505
|
+
fileExt: '.ts',
|
|
506
|
+
decorator: '@configuration'
|
|
507
|
+
}, {
|
|
508
|
+
// 布局
|
|
509
|
+
type: PATH_TYPE.FOLDER,
|
|
510
|
+
path: Project.LAYOUT_DIR,
|
|
511
|
+
fileExt: '.tsx',
|
|
512
|
+
decorator: '@layout'
|
|
513
|
+
}, {
|
|
514
|
+
// 页面
|
|
515
|
+
type: PATH_TYPE.FOLDER,
|
|
516
|
+
path: Project.PAGE_DIR,
|
|
517
|
+
fileExt: '.tsx',
|
|
518
|
+
decorator: '@page'
|
|
519
|
+
}, {
|
|
520
|
+
// 控制器
|
|
521
|
+
type: PATH_TYPE.FOLDER,
|
|
522
|
+
path: Project.Flow_DIR,
|
|
523
|
+
fileExt: '.ts',
|
|
524
|
+
decorator: '@flow'
|
|
525
|
+
}, {
|
|
526
|
+
// 通用组件
|
|
527
|
+
type: PATH_TYPE.FOLDER,
|
|
528
|
+
path: Project.COMPONENTS_DIR,
|
|
529
|
+
fileExt: '.ts',
|
|
530
|
+
decorator: '@component'
|
|
531
|
+
}, {
|
|
532
|
+
// 接口
|
|
533
|
+
type: PATH_TYPE.FOLDER,
|
|
534
|
+
path: Project.API_DIR,
|
|
535
|
+
fileExt: '.ts',
|
|
536
|
+
decorator: '@api'
|
|
537
|
+
}, {
|
|
538
|
+
// 视图
|
|
539
|
+
type: PATH_TYPE.FOLDER,
|
|
540
|
+
path: Project.VIEW_DIR,
|
|
541
|
+
fileExt: '.tsx',
|
|
542
|
+
decorator: '@view'
|
|
543
|
+
}, {
|
|
544
|
+
// 全局数据
|
|
545
|
+
type: PATH_TYPE.FOLDER,
|
|
546
|
+
path: Project.GLOBAL_DATA_DIR,
|
|
547
|
+
fileExt: '.ts',
|
|
548
|
+
decorator: '@globalData'
|
|
549
|
+
}, {
|
|
550
|
+
// 全局状态
|
|
551
|
+
type: PATH_TYPE.FOLDER,
|
|
552
|
+
path: Project.STORE_DIR,
|
|
553
|
+
fileExt: '.ts',
|
|
554
|
+
decorator: '@store'
|
|
555
|
+
}, {
|
|
556
|
+
// 入口文件
|
|
557
|
+
type: PATH_TYPE.FILE,
|
|
558
|
+
path: Project.APPLICATION,
|
|
559
|
+
fileExt: '.ts',
|
|
560
|
+
decorator: '@webApplication'
|
|
561
|
+
}];
|
|
562
|
+
var scan = function scan(project) {
|
|
563
|
+
return scanPathConfig.reduce(function (prev, curr) {
|
|
564
|
+
prev.push.apply(prev, _toConsumableArray(doScan(curr.type, project.genFullPath(curr.path), curr.fileExt, curr.decorator)));
|
|
565
|
+
return prev;
|
|
566
|
+
}, []);
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
function validateConstructor(project) {
|
|
570
|
+
// 1. 扫描所有ioc组件
|
|
571
|
+
var iocComponents = scan(project);
|
|
572
|
+
// 2. 生成.coco文件
|
|
573
|
+
var importStatements = iocComponents.map(function (_ref) {
|
|
574
|
+
_ref.className;
|
|
575
|
+
var filePath = _ref.filePath;
|
|
576
|
+
return filePath;
|
|
577
|
+
});
|
|
578
|
+
var _iterator = _createForOfIteratorHelper(importStatements),
|
|
579
|
+
_step;
|
|
580
|
+
try {
|
|
581
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
582
|
+
var filePath = _step.value;
|
|
583
|
+
validate(filePath);
|
|
584
|
+
}
|
|
585
|
+
} catch (err) {
|
|
586
|
+
_iterator.e(err);
|
|
587
|
+
} finally {
|
|
588
|
+
_iterator.f();
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
function validate(filePath) {
|
|
592
|
+
var inputCode = fs.readFileSync(filePath, 'utf-8');
|
|
593
|
+
var ast = parser.parse(inputCode, {
|
|
594
|
+
sourceType: 'module',
|
|
595
|
+
plugins: ['jsx', 'typescript', 'decorators-legacy', 'classProperties']
|
|
596
|
+
});
|
|
597
|
+
traverse["default"](ast, {
|
|
598
|
+
ClassDeclaration: function ClassDeclaration(path) {
|
|
599
|
+
var body = path.node.body.body;
|
|
600
|
+
body.forEach(function (method) {
|
|
601
|
+
if (method.kind === 'constructor') {
|
|
602
|
+
var statements = method.body.body;
|
|
603
|
+
statements.forEach(function (statement) {
|
|
604
|
+
if (types.isExpressionStatement(statement) && types.isCallExpression(statement.expression)) {
|
|
605
|
+
if (statement.expression.callee.type === 'Super') {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
var fnName;
|
|
609
|
+
if (statement.expression.callee.type === 'MemberExpression') {
|
|
610
|
+
// todo fix it
|
|
611
|
+
// @ts-ignore
|
|
612
|
+
fnName = statement.expression.callee.property.name;
|
|
613
|
+
} else if (statement.expression.callee.type === 'Identifier') {
|
|
614
|
+
fnName = statement.expression.callee.name;
|
|
615
|
+
} else {
|
|
616
|
+
console.warn('未知函数调用类型', statement.expression.callee);
|
|
617
|
+
fnName = JSON.stringify(statement.expression.callee);
|
|
618
|
+
}
|
|
619
|
+
throw new Error("\u4E0D\u5141\u8BB8\u5728\u6784\u9020\u51FD\u6570\u5185\u6267\u884C\u51FD\u6570\u8C03\u7528\u3010".concat(fnName, "\u3011\u3002"));
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
var DotCocoBuilder = /*#__PURE__*/_createClass(
|
|
629
|
+
/**
|
|
630
|
+
* 目前仅支持单体应用,为了单元测试,可以传入子应用相对项目根的路径
|
|
631
|
+
* @param monorepoPath
|
|
632
|
+
*/
|
|
633
|
+
function DotCocoBuilder() {
|
|
634
|
+
var _this = this;
|
|
635
|
+
var monorepoPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '.';
|
|
636
|
+
_classCallCheck(this, DotCocoBuilder);
|
|
637
|
+
this.ensureEmptyDotCocoFolder = function (project) {
|
|
638
|
+
if (fs.existsSync(project.dotCocoAbsPath)) {
|
|
639
|
+
fs.rmSync(project.dotCocoAbsPath, {
|
|
640
|
+
recursive: true
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
fs.mkdirSync(project.dotCocoAbsPath);
|
|
644
|
+
};
|
|
645
|
+
/**
|
|
646
|
+
* 完成构建前的准备工作
|
|
647
|
+
*/
|
|
648
|
+
this.build = function () {
|
|
649
|
+
_this.ensureEmptyDotCocoFolder(_this.project);
|
|
650
|
+
validateConstructor(_this.project);
|
|
651
|
+
merge(_this.project, _this.cmd);
|
|
652
|
+
genIndexTsx(_this.project, _this.iocComponents);
|
|
653
|
+
};
|
|
654
|
+
this.isTsTsxFile = function (path) {
|
|
655
|
+
return path.endsWith('.ts') || path.endsWith('.tsx');
|
|
656
|
+
};
|
|
657
|
+
this.handleAddFile = function (filePath) {
|
|
658
|
+
if (_this.isTsTsxFile(filePath)) {
|
|
659
|
+
var _path$parse = path.parse(filePath),
|
|
660
|
+
dir = _path$parse.dir,
|
|
661
|
+
ext = _path$parse.ext;
|
|
662
|
+
var match = scanPathConfig.find(function (item) {
|
|
663
|
+
return dir.startsWith(item.path) && item.fileExt === ext;
|
|
664
|
+
});
|
|
665
|
+
if (!match) {
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
var scanRlt = scanOneFile(_this.project.genFullPath(filePath), match.decorator);
|
|
669
|
+
if (scanRlt !== null) {
|
|
670
|
+
if (!_this.iocComponents.find(function (i) {
|
|
671
|
+
return i.filePath === scanRlt.filePath;
|
|
672
|
+
})) {
|
|
673
|
+
// 新增一个组件
|
|
674
|
+
_this.iocComponents.push(scanRlt);
|
|
675
|
+
genIndexTsx(_this.project, _this.iocComponents);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
this.handleDeleteFile = function (filePath) {
|
|
681
|
+
if (_this.isTsTsxFile(filePath)) {
|
|
682
|
+
var fullPath = _this.project.genFullPath(filePath);
|
|
683
|
+
var index = _this.iocComponents.findIndex(function (i) {
|
|
684
|
+
return i.filePath === fullPath;
|
|
685
|
+
});
|
|
686
|
+
if (index > 0) {
|
|
687
|
+
_this.iocComponents.splice(index, 1);
|
|
688
|
+
genIndexTsx(_this.project, _this.iocComponents);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
this.watchSrc = function () {
|
|
693
|
+
_this.srcWatcher = chokidar.watch(_this.project.srcAbsPath, {
|
|
694
|
+
ignored: function ignored(absPath, stats) {
|
|
695
|
+
// 忽略.coco文件夹
|
|
696
|
+
var srcPath = path.relative(_this.project.srcAbsPath, absPath);
|
|
697
|
+
return srcPath.startsWith('.coco');
|
|
698
|
+
},
|
|
699
|
+
ignoreInitial: true,
|
|
700
|
+
cwd: _this.project.srcAbsPath
|
|
701
|
+
});
|
|
702
|
+
_this.srcWatcher.on('add', _this.handleAddFile);
|
|
703
|
+
_this.srcWatcher.on('change', _this.handleAddFile);
|
|
704
|
+
_this.srcWatcher.on('unlink', _this.handleDeleteFile);
|
|
705
|
+
};
|
|
706
|
+
this.watchProperties = function () {
|
|
707
|
+
_this.propertiesWatcher = chokidar.watch(_this.project.propertiesAbsPath, {
|
|
708
|
+
ignored: function ignored(absPath, stats) {
|
|
709
|
+
var filename = path.relative(_this.project.propertiesAbsPath, absPath);
|
|
710
|
+
if (absPath === _this.project.propertiesAbsPath || filename === defaultPropertiesName || filename === propertiesFileName(_this.cmd)) {
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
return true;
|
|
714
|
+
},
|
|
715
|
+
ignoreInitial: true,
|
|
716
|
+
cwd: _this.project.srcAbsPath
|
|
717
|
+
});
|
|
718
|
+
_this.propertiesWatcher.on('add', function () {
|
|
719
|
+
return merge(_this.project, _this.cmd);
|
|
720
|
+
});
|
|
721
|
+
_this.propertiesWatcher.on('change', function () {
|
|
722
|
+
return merge(_this.project, _this.cmd);
|
|
723
|
+
});
|
|
724
|
+
_this.propertiesWatcher.on('unlink', function () {
|
|
725
|
+
return merge(_this.project, _this.cmd);
|
|
726
|
+
});
|
|
727
|
+
};
|
|
728
|
+
this.startWatch = function () {
|
|
729
|
+
_this.watchSrc();
|
|
730
|
+
_this.watchProperties();
|
|
731
|
+
};
|
|
732
|
+
this.stopWatch = function () {
|
|
733
|
+
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee() {
|
|
734
|
+
return _regenerator().w(function (_context) {
|
|
735
|
+
while (1) switch (_context.n) {
|
|
736
|
+
case 0:
|
|
737
|
+
if (!this.srcWatcher) {
|
|
738
|
+
_context.n = 2;
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
_context.n = 1;
|
|
742
|
+
return this.srcWatcher.close();
|
|
743
|
+
case 1:
|
|
744
|
+
this.srcWatcher = null;
|
|
745
|
+
case 2:
|
|
746
|
+
if (!this.propertiesWatcher) {
|
|
747
|
+
_context.n = 4;
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
_context.n = 3;
|
|
751
|
+
return this.propertiesWatcher.close();
|
|
752
|
+
case 3:
|
|
753
|
+
this.propertiesWatcher = null;
|
|
754
|
+
case 4:
|
|
755
|
+
return _context.a(2);
|
|
756
|
+
}
|
|
757
|
+
}, _callee, this);
|
|
758
|
+
}));
|
|
759
|
+
};
|
|
760
|
+
this.project = new Project(monorepoPath);
|
|
761
|
+
this.iocComponents = scan(this.project);
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
function startListening(builder) {
|
|
765
|
+
var watching = false;
|
|
766
|
+
process$1.on('message', function (msg) {
|
|
767
|
+
switch (msg) {
|
|
768
|
+
case 'build-once':
|
|
769
|
+
{
|
|
770
|
+
builder.build();
|
|
771
|
+
process$1.send('build-success');
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
case 'build-and-watch':
|
|
775
|
+
{
|
|
776
|
+
builder.build();
|
|
777
|
+
process$1.send('init-build-success');
|
|
778
|
+
watching = true;
|
|
779
|
+
builder.startWatch();
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
default:
|
|
783
|
+
{
|
|
784
|
+
process$1.send("dot coco process rcv msg:[".concat(msg, "]"));
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
function handleTerminate() {
|
|
790
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee() {
|
|
791
|
+
return _regenerator().w(function (_context) {
|
|
792
|
+
while (1) switch (_context.n) {
|
|
793
|
+
case 0:
|
|
794
|
+
if (!watching) {
|
|
795
|
+
_context.n = 2;
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
_context.n = 1;
|
|
799
|
+
return builder.stopWatch();
|
|
800
|
+
case 1:
|
|
801
|
+
watching = false;
|
|
802
|
+
case 2:
|
|
803
|
+
process$1.exit(0);
|
|
804
|
+
case 3:
|
|
805
|
+
return _context.a(2);
|
|
806
|
+
}
|
|
807
|
+
}, _callee);
|
|
808
|
+
}));
|
|
809
|
+
}
|
|
810
|
+
process$1.on('SIGTERM', handleTerminate);
|
|
811
|
+
process$1.on('SIGINT', handleTerminate);
|
|
812
|
+
}
|
|
813
|
+
function startAsProcess() {
|
|
814
|
+
var secondArgv = process$1.argv[2];
|
|
815
|
+
if (secondArgv === 'run-as-process') {
|
|
816
|
+
var builder = new DotCocoBuilder();
|
|
817
|
+
startListening(builder);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
startAsProcess();
|
|
821
|
+
|
|
822
|
+
module.exports = DotCocoBuilder;
|