mbler 0.2.0 → 0.2.2
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/dist/build.d.ts +145 -0
- package/dist/build.esm.js +1312 -0
- package/dist/build.esm.js.map +1 -0
- package/dist/build.js +1339 -0
- package/dist/build.js.map +1 -0
- package/dist/index.d.ts +11 -144
- package/dist/index.esm.js +150 -964
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +148 -965
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -8,19 +8,6 @@ var os = require('node:os');
|
|
|
8
8
|
var readline = require('readline');
|
|
9
9
|
var npmRegistryFetch = require('npm-registry-fetch');
|
|
10
10
|
var node_child_process = require('node:child_process');
|
|
11
|
-
var mcxDef = require('@mbler/mcx-core');
|
|
12
|
-
var _chalk = require('chalk');
|
|
13
|
-
var jsonPlugin = require('@rollup/plugin-json');
|
|
14
|
-
var resolvePlugin = require('@rollup/plugin-node-resolve');
|
|
15
|
-
var minifyPlugin = require('@rollup/plugin-terser');
|
|
16
|
-
var chokidar = require('chokidar');
|
|
17
|
-
var rollup = require('rollup');
|
|
18
|
-
var crypto = require('node:crypto');
|
|
19
|
-
var AdmZip = require('adm-zip');
|
|
20
|
-
var commonjs = require('@rollup/plugin-commonjs');
|
|
21
|
-
var mcxServer = require('@mbler/mcx-server');
|
|
22
|
-
var typescript = require('@rollup/plugin-typescript');
|
|
23
|
-
var runTsc = require('@volar/typescript/lib/quickstart/runTsc');
|
|
24
11
|
|
|
25
12
|
function _interopNamespaceDefault(e) {
|
|
26
13
|
var n = Object.create(null);
|
|
@@ -43,11 +30,9 @@ var fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
|
43
30
|
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
44
31
|
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs$1);
|
|
45
32
|
var readline__namespace = /*#__PURE__*/_interopNamespaceDefault(readline);
|
|
46
|
-
var mcxDef__namespace = /*#__PURE__*/_interopNamespaceDefault(mcxDef);
|
|
47
|
-
var rollup__namespace = /*#__PURE__*/_interopNamespaceDefault(rollup);
|
|
48
33
|
|
|
49
34
|
const LanguageNames = ['zh', 'en'];
|
|
50
|
-
const cmdList = ['c', 'work', 'help', 'h', 'init', 'version', 'build', 'watch', 'lang', 'set-work-dir'];
|
|
35
|
+
const cmdList = ['c', 'work', 'help', 'h', 'init', 'version', 'build', 'watch', 'lang', 'set-work-dir', 'publish', 'unpublish', 'install', 'uninstall', 'login'];
|
|
51
36
|
const templateMblerConfig = {
|
|
52
37
|
name: 'demo',
|
|
53
38
|
description: 'demo',
|
|
@@ -71,12 +56,12 @@ var types = /*#__PURE__*/Object.freeze({
|
|
|
71
56
|
templateMblerConfig: templateMblerConfig
|
|
72
57
|
});
|
|
73
58
|
|
|
74
|
-
var MBLERVersion = { commit: `commit
|
|
59
|
+
var MBLERVersion = { commit: `commit 81c6f8bb0b33367bd219c7c9168f18ae83e1f2e1
|
|
75
60
|
Author: Ruanhor <3915264929@qq.com>
|
|
76
|
-
Date: Sun Apr 19
|
|
61
|
+
Date: Sun Apr 19 16:33:01 2026 +0800
|
|
77
62
|
|
|
78
|
-
|
|
79
|
-
`, version: "0.2.
|
|
63
|
+
fix: init and add plan command
|
|
64
|
+
`, version: "0.2.2" };
|
|
80
65
|
|
|
81
66
|
var ZhLang = {
|
|
82
67
|
description: `MBLER
|
|
@@ -101,7 +86,12 @@ git https://github.com/RuanhoR/mbler/`,
|
|
|
101
86
|
watch: "mbler watch\n - 启动构建并开启监视模式,文件变化会自动重新构建",
|
|
102
87
|
lang: 'mbler lang\n- 无参数:显示当前语言\n- 跟 zh 或 en(如 mbler lang en): 设置语言',
|
|
103
88
|
version: "mbler version - 版本管理命令\n- 无参数:显示当前版本和提交哈希\n- --show=<commit|version>:筛选显示内容\n- <新版本号>:更新package.json和配置文件的版本",
|
|
104
|
-
'set-work-dir': "mbler set-work-dir <on|off>\n - on: 启用工作目录功能\n - off: 禁用工作目录功能,直接使用 process.cwd()"
|
|
89
|
+
'set-work-dir': "mbler set-work-dir <on|off>\n - on: 启用工作目录功能\n - off: 禁用工作目录功能,直接使用 process.cwd()",
|
|
90
|
+
publish: 'mbler publish\n - 将你的附加包发布到 pmnx\n- 参数:\n- -tag: 版本标签名称',
|
|
91
|
+
uninstall: 'mbler uninstall @<scope>/<name>@version\n从你的游戏中移除一个包',
|
|
92
|
+
install: 'mbler install @<scope>/<name>@version\n将一个包安装到你的游戏中',
|
|
93
|
+
unpublish: 'mbler unpublish @<scope>/<name>@version\n从 pmnx 取消发布一个包',
|
|
94
|
+
login: 'mbler login <?:token>\n使用 token 登录你的 pmnx 账户\n- 如果没有 token,会向你请求'
|
|
105
95
|
},
|
|
106
96
|
init: {
|
|
107
97
|
useUI: '使用UI模块? (y/n): ',
|
|
@@ -146,7 +136,12 @@ var enLang = {
|
|
|
146
136
|
watch: 'mbler watch\n - run build and enter watch mode; changes will trigger rebuilds',
|
|
147
137
|
lang: 'mbler lang [?:languare]\n- No args: show current languare\n- languare = "zh” | “en": set languare',
|
|
148
138
|
version: 'mbler version\n - Version control command\n- No args: Shows version + commit hash\n- --show=<commit|version>: Filters output\n- <new_version>: Updates version in both package.json and config file',
|
|
149
|
-
'set-work-dir': "mbler set-work-dir <on|off>\n - on: Enable work dir feature\n - off: Disable work dir feature, use process.cwd() directly"
|
|
139
|
+
'set-work-dir': "mbler set-work-dir <on|off>\n - on: Enable work dir feature\n - off: Disable work dir feature, use process.cwd() directly",
|
|
140
|
+
publish: 'mbler publish\n - Publish your package to pmnx\n- Params:\n- -tag: version tag name',
|
|
141
|
+
uninstall: 'mbler uninstall @<scope>/<name>@version\nRemove a package from your game',
|
|
142
|
+
install: 'mbler install @<scope>/<name>@version\nInstall a package to your game',
|
|
143
|
+
unpublish: 'mbler unpublish @<scope>/<name>@version\nUnpublish a package from pmnx',
|
|
144
|
+
login: 'mbler login <?:token>\nUse token login your pmnx account'
|
|
150
145
|
},
|
|
151
146
|
init: {
|
|
152
147
|
useUI: 'Use UI? (y/n): ',
|
|
@@ -238,7 +233,7 @@ function Export(lang) {
|
|
|
238
233
|
lang.init();
|
|
239
234
|
Export(lang);
|
|
240
235
|
|
|
241
|
-
var index$
|
|
236
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
242
237
|
__proto__: null,
|
|
243
238
|
default: i18n
|
|
244
239
|
});
|
|
@@ -367,7 +362,7 @@ process.stdin.on("keypress", (str, key) => {
|
|
|
367
362
|
}, str);
|
|
368
363
|
});
|
|
369
364
|
|
|
370
|
-
var index
|
|
365
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
371
366
|
__proto__: null,
|
|
372
367
|
Input: Input,
|
|
373
368
|
click: click,
|
|
@@ -375,76 +370,7 @@ var index$1 = /*#__PURE__*/Object.freeze({
|
|
|
375
370
|
});
|
|
376
371
|
|
|
377
372
|
const BuildConfig = {
|
|
378
|
-
ConfigFile: "mbler.config.js"
|
|
379
|
-
salt: {
|
|
380
|
-
header: "d61e721d-a2c9-4535-8054-0183bce24767",
|
|
381
|
-
sapi: "33e2c698-908f-45ab-8a9f-66018f8486ed",
|
|
382
|
-
module: "cbbacfa4-8b1e-4a9c-9cbd-7a0d2e5f0b3c",
|
|
383
|
-
},
|
|
384
|
-
behavior: "behavior",
|
|
385
|
-
resources: "resources",
|
|
386
|
-
includes: {
|
|
387
|
-
public: {
|
|
388
|
-
"pack_icon.png": "file",
|
|
389
|
-
"manifest.json": "file",
|
|
390
|
-
},
|
|
391
|
-
resources: {
|
|
392
|
-
"biomes_client.json": "file",
|
|
393
|
-
"blocks.json": "file",
|
|
394
|
-
"bug_pack_icon.png": "file",
|
|
395
|
-
"contents.json": "file",
|
|
396
|
-
"loading_messages.json": "file",
|
|
397
|
-
"manifest_publish.json": "file",
|
|
398
|
-
"signatures.json": "file",
|
|
399
|
-
"sounds.json": "file",
|
|
400
|
-
"splashes.json": "file",
|
|
401
|
-
animation_controllers: "directory",
|
|
402
|
-
animations: "directory",
|
|
403
|
-
attachables: "directory",
|
|
404
|
-
biomes: "directory",
|
|
405
|
-
cameras: "directory",
|
|
406
|
-
entity: "directory",
|
|
407
|
-
fogs: "directory",
|
|
408
|
-
font: "directory",
|
|
409
|
-
items: "directory",
|
|
410
|
-
library: "directory",
|
|
411
|
-
materials: "directory",
|
|
412
|
-
models: "directory",
|
|
413
|
-
particles: "directory",
|
|
414
|
-
render_controllers: "directory",
|
|
415
|
-
sounds: "directory",
|
|
416
|
-
texts: "directory",
|
|
417
|
-
textures: "directory",
|
|
418
|
-
},
|
|
419
|
-
behavior: {
|
|
420
|
-
aim_assist: "directory",
|
|
421
|
-
animation_controllers: "directory",
|
|
422
|
-
animations: "directory",
|
|
423
|
-
behavior_trees: "directory",
|
|
424
|
-
biomes: "directory",
|
|
425
|
-
blocks: "directory",
|
|
426
|
-
cameras: "directory",
|
|
427
|
-
dimensions: "directory",
|
|
428
|
-
entities: "directory",
|
|
429
|
-
feature_rules: "directory",
|
|
430
|
-
features: "directory",
|
|
431
|
-
functions: "directory",
|
|
432
|
-
item_catalog: "directory",
|
|
433
|
-
items: "directory",
|
|
434
|
-
loot_tables: "directory",
|
|
435
|
-
recipes: "directory",
|
|
436
|
-
scripts: "skip", // special handling
|
|
437
|
-
spawn_rules: "directory",
|
|
438
|
-
structures: "directory",
|
|
439
|
-
texts: "directory",
|
|
440
|
-
trading: "directory",
|
|
441
|
-
worldgen: "directory",
|
|
442
|
-
"contents.json": "file",
|
|
443
|
-
"manifest_publish.json": "file",
|
|
444
|
-
"signatures.json": "file",
|
|
445
|
-
},
|
|
446
|
-
},
|
|
447
|
-
};
|
|
373
|
+
ConfigFile: "mbler.config.js"};
|
|
448
374
|
|
|
449
375
|
async function FileExsit(file) {
|
|
450
376
|
try {
|
|
@@ -457,19 +383,6 @@ async function FileExsit(file) {
|
|
|
457
383
|
}
|
|
458
384
|
return false;
|
|
459
385
|
}
|
|
460
|
-
function join(baseDir, inputPath) {
|
|
461
|
-
return path__namespace.isAbsolute(inputPath) ? inputPath : path__namespace.join(baseDir, inputPath);
|
|
462
|
-
}
|
|
463
|
-
async function ReadProjectMblerConfig(project) {
|
|
464
|
-
const fileExport = await import(path__namespace.join(project, BuildConfig.ConfigFile));
|
|
465
|
-
const file = fileExport.default;
|
|
466
|
-
for (const key in file) {
|
|
467
|
-
if (!(key in templateMblerConfig)) {
|
|
468
|
-
throw new Error(`[read config]: read config from '${project}' error: Unexpected '${key}'`);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
return file;
|
|
472
|
-
}
|
|
473
386
|
async function readFileAsJson(filePath) {
|
|
474
387
|
try {
|
|
475
388
|
const content = await fs__namespace.readFile(filePath, 'utf-8');
|
|
@@ -519,12 +432,6 @@ function showText(text, needNextLine = true) {
|
|
|
519
432
|
});
|
|
520
433
|
}
|
|
521
434
|
}
|
|
522
|
-
function stringToNumberArray(str) {
|
|
523
|
-
return str
|
|
524
|
-
.split('.')
|
|
525
|
-
.map((s) => parseInt(s, 10))
|
|
526
|
-
.slice(0, 3);
|
|
527
|
-
}
|
|
528
435
|
async function writeJSON(filePath, data) {
|
|
529
436
|
const content = JSON.stringify(data, null, 2);
|
|
530
437
|
if (!(await FileExsit(path__namespace.dirname(filePath)))) {
|
|
@@ -691,28 +598,98 @@ class Logger {
|
|
|
691
598
|
}
|
|
692
599
|
}
|
|
693
600
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
601
|
+
async function tryMkdir(point) {
|
|
602
|
+
try {
|
|
603
|
+
await fs$1.mkdir(point);
|
|
604
|
+
return true;
|
|
605
|
+
}
|
|
606
|
+
catch {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
class WorkDirManage {
|
|
611
|
+
cacheDir;
|
|
612
|
+
currentWorkPoint = null;
|
|
613
|
+
enabledPath = path.join(os.homedir(), ".cache/mbler/workdir_enabled.db");
|
|
614
|
+
constructor(cacheDir = path.join(os.homedir(), ".cache/mbler/mp.db")) {
|
|
615
|
+
this.cacheDir = cacheDir;
|
|
616
|
+
}
|
|
617
|
+
async isDisabled() {
|
|
618
|
+
try {
|
|
619
|
+
await fs$1.readFile(this.enabledPath, "utf-8");
|
|
620
|
+
return false; // 文件存在,表示启用
|
|
621
|
+
}
|
|
622
|
+
catch {
|
|
623
|
+
return true; // 文件不存在,默认禁用
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
async setDisabled(disabled) {
|
|
627
|
+
if (!disabled) {
|
|
628
|
+
// 启用:创建文件
|
|
629
|
+
await fs$1.writeFile(this.enabledPath, "1", { encoding: "utf-8" });
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
try {
|
|
633
|
+
await fs$1.unlink(this.enabledPath);
|
|
634
|
+
}
|
|
635
|
+
catch {
|
|
636
|
+
// 文件不存在,忽略
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
async set(newPointDir) {
|
|
641
|
+
// check
|
|
642
|
+
try {
|
|
643
|
+
const s = await fs$1.stat(newPointDir);
|
|
644
|
+
if (!s.isDirectory()) {
|
|
645
|
+
throw new Error("Not Dir (0xcvb)");
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
catch (err) {
|
|
649
|
+
const code = err.code;
|
|
650
|
+
if (err.message && err.message.includes("0xcvb"))
|
|
651
|
+
return i18n.workdir.nfound;
|
|
652
|
+
if (code == "ENOENT") {
|
|
653
|
+
const res = tryMkdir(newPointDir);
|
|
654
|
+
if (!res) {
|
|
655
|
+
return i18n.workdir.nfound;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
try {
|
|
660
|
+
if (!(await FileExsit(path.dirname(this.cacheDir)))) {
|
|
661
|
+
const isC = await tryMkdir(path.dirname(this.cacheDir));
|
|
662
|
+
if (!isC)
|
|
663
|
+
return i18n.workdir.nfound;
|
|
664
|
+
}
|
|
665
|
+
await fs$1.writeFile(this.cacheDir, newPointDir, {
|
|
666
|
+
encoding: "utf-8",
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
catch (err) {
|
|
670
|
+
Logger.e("WorkDir", err.stack);
|
|
671
|
+
}
|
|
672
|
+
return i18n.workdir.set + newPointDir;
|
|
673
|
+
}
|
|
674
|
+
async get() {
|
|
675
|
+
if (await this.isDisabled()) {
|
|
676
|
+
return node_process.cwd();
|
|
677
|
+
}
|
|
678
|
+
if (this.currentWorkPoint) {
|
|
679
|
+
return this.currentWorkPoint;
|
|
680
|
+
}
|
|
681
|
+
const file = await fs$1.readFile(this.cacheDir, "utf-8").catch((e) => {
|
|
682
|
+
this.set(node_process.cwd());
|
|
683
|
+
return node_process.cwd();
|
|
684
|
+
});
|
|
685
|
+
return (this.currentWorkPoint = file);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
712
688
|
|
|
713
689
|
const config = {
|
|
714
690
|
tmpdir: path__namespace.join(os.tmpdir(), ".mbler"),
|
|
715
|
-
mcxVersion: "0.0.2-beta.r7"
|
|
691
|
+
mcxVersion: "0.0.2-beta.r7",
|
|
692
|
+
mcxCoreVersion: "0.0.6"
|
|
716
693
|
};
|
|
717
694
|
|
|
718
695
|
/**
|
|
@@ -841,833 +818,6 @@ const exp = (function () {
|
|
|
841
818
|
};
|
|
842
819
|
})();
|
|
843
820
|
|
|
844
|
-
async function generateManifest(config, type) {
|
|
845
|
-
const manifest = {
|
|
846
|
-
format_version: 2,
|
|
847
|
-
header: {
|
|
848
|
-
name: config.name,
|
|
849
|
-
description: config.description,
|
|
850
|
-
uuid: fromString(config.name, BuildConfig.salt.header + type),
|
|
851
|
-
version: stringToNumberArray(config.version),
|
|
852
|
-
min_engine_version: stringToNumberArray(typeof config.mcVersion === "string"
|
|
853
|
-
? config.mcVersion
|
|
854
|
-
: (() => {
|
|
855
|
-
throw new Error("mcVersion in mblerconfig should be a string");
|
|
856
|
-
})()),
|
|
857
|
-
},
|
|
858
|
-
modules: [
|
|
859
|
-
{
|
|
860
|
-
type: type,
|
|
861
|
-
uuid: fromString(config.name, BuildConfig.salt.module + type),
|
|
862
|
-
description: `From Mbler(https://github.com/RuanhoR/mbler). welcome to star and contribute!`,
|
|
863
|
-
version: stringToNumberArray(config.version),
|
|
864
|
-
},
|
|
865
|
-
],
|
|
866
|
-
};
|
|
867
|
-
if (type === "data" && config.script) {
|
|
868
|
-
manifest.modules.push({
|
|
869
|
-
type: "script",
|
|
870
|
-
uuid: fromString(config.name, BuildConfig.salt.sapi + type),
|
|
871
|
-
description: `sapi generate by mbler, weclome to download and star at https://github.com/RuanhoR/mbler`,
|
|
872
|
-
version: stringToNumberArray(config.version),
|
|
873
|
-
});
|
|
874
|
-
manifest.capabilities = ["script_eval"];
|
|
875
|
-
manifest.dependencies = [
|
|
876
|
-
{
|
|
877
|
-
module_name: "@minecraft/server",
|
|
878
|
-
version: (await exp.generateVersion("@minecraft/server", config.mcVersion, config.script?.UseBeta || false)).split("-")[0], // only major.minor.patch, remove -beta or -rc
|
|
879
|
-
},
|
|
880
|
-
];
|
|
881
|
-
if (config.script.ui) {
|
|
882
|
-
manifest.dependencies.push({
|
|
883
|
-
module_name: "@minecraft/server-ui",
|
|
884
|
-
version: (await exp.generateVersion("@minecraft/server-ui", config.mcVersion, config.script?.UseBeta || false)).split("-")[0], // only major.minor.patch, remove -beta or -rc
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
return manifest;
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
function createFullZip(dir) {
|
|
892
|
-
const zip = new AdmZip();
|
|
893
|
-
zip.addLocalFolder(dir);
|
|
894
|
-
return zip;
|
|
895
|
-
}
|
|
896
|
-
async function createZipWithMoreFolder(dir) {
|
|
897
|
-
const zip = new AdmZip();
|
|
898
|
-
for (const folder of dir) {
|
|
899
|
-
await zip.addLocalFolderPromise(folder[0], {
|
|
900
|
-
zipPath: folder[1]
|
|
901
|
-
});
|
|
902
|
-
}
|
|
903
|
-
return zip;
|
|
904
|
-
}
|
|
905
|
-
async function generateRelease(build) {
|
|
906
|
-
if (!build.srcDirs)
|
|
907
|
-
throw new Error("invaild Build");
|
|
908
|
-
if (node_process.env.BUILD_MODULE !== "release")
|
|
909
|
-
return;
|
|
910
|
-
let zip;
|
|
911
|
-
if (build.module == "all") {
|
|
912
|
-
zip = await createZipWithMoreFolder([
|
|
913
|
-
[build.srcDirs?.behavior, "behavior"],
|
|
914
|
-
[build.srcDirs.resources, "resources"]
|
|
915
|
-
]);
|
|
916
|
-
}
|
|
917
|
-
else if (build.module == "behavior") {
|
|
918
|
-
zip = createFullZip(build.srcDirs.behavior);
|
|
919
|
-
}
|
|
920
|
-
else {
|
|
921
|
-
zip = createFullZip(build.srcDirs.resources);
|
|
922
|
-
}
|
|
923
|
-
await zip.writeZipPromise(build.outdirs?.dist);
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
// cjs support
|
|
927
|
-
const chalk$1 = _chalk instanceof Function ? _chalk : _chalk.default;
|
|
928
|
-
class Postgress {
|
|
929
|
-
max;
|
|
930
|
-
constructor(max) {
|
|
931
|
-
this.max = max;
|
|
932
|
-
}
|
|
933
|
-
update(current) {
|
|
934
|
-
const percentage = Math.min(current, this.max) / this.max;
|
|
935
|
-
const barWidth = 30;
|
|
936
|
-
const filledWidth = Math.round(barWidth * percentage);
|
|
937
|
-
const emptyWidth = barWidth - filledWidth;
|
|
938
|
-
const filledBar = chalk$1.green('█'.repeat(filledWidth));
|
|
939
|
-
const emptyBar = chalk$1.white('█'.repeat(emptyWidth));
|
|
940
|
-
const progressBar = `${filledBar}${emptyBar}`;
|
|
941
|
-
const percentText = chalk$1.blue(`${Math.round(percentage * 100)}%`);
|
|
942
|
-
const progressText = `\n\u001B[1A\r[${progressBar}] ${percentText} (${current}/${this.max})`;
|
|
943
|
-
showText(progressText, false);
|
|
944
|
-
if (current == this.max) {
|
|
945
|
-
showText("", true);
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
/**
|
|
951
|
-
* 运行 MCX TypeScript 编译器
|
|
952
|
-
* 为 .mcx 文件提供 TypeScript 类型检查支持
|
|
953
|
-
*/
|
|
954
|
-
function runTSC(tscpath = require.resolve("typescript/lib/tsc")) {
|
|
955
|
-
runTsc.runTsc(tscpath, {
|
|
956
|
-
extraSupportedExtensions: ['.mcx'],
|
|
957
|
-
extraExtensionsToRemove: ['.mcx'],
|
|
958
|
-
}, (ts) => {
|
|
959
|
-
return [mcxServer.createMCXLanguagePlugin(ts)];
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
// cjs support
|
|
964
|
-
const chalk = _chalk instanceof Function ? _chalk : _chalk.default;
|
|
965
|
-
class Build {
|
|
966
|
-
baseBuildDir;
|
|
967
|
-
resolve;
|
|
968
|
-
isWatch;
|
|
969
|
-
currentConfig = null;
|
|
970
|
-
srcDirs = null;
|
|
971
|
-
outdirs = null;
|
|
972
|
-
mcxTs;
|
|
973
|
-
mcxLanguagePluginCreator = null;
|
|
974
|
-
constructor(opts, baseBuildDir, resolve, isWatch = false) {
|
|
975
|
-
this.baseBuildDir = baseBuildDir;
|
|
976
|
-
this.resolve = resolve;
|
|
977
|
-
this.isWatch = isWatch;
|
|
978
|
-
// 初始化 MCX 语言插件创建器,传入 tsHook.ts 使用
|
|
979
|
-
try {
|
|
980
|
-
const tsModule = require("typescript");
|
|
981
|
-
this.mcxLanguagePluginCreator = mcxServer.createMCXLanguagePlugin;
|
|
982
|
-
this.mcxTs = tsModule;
|
|
983
|
-
Logger.i("Build", "MCX Volar language plugin creator initialized successfully");
|
|
984
|
-
}
|
|
985
|
-
catch (error) {
|
|
986
|
-
this.mcxTs = require("typescript");
|
|
987
|
-
Logger.w("Build", `Failed to initialize MCX language plugin: ${error}`);
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Start the watch mode.
|
|
992
|
-
* This will perform an initial build (if not already done) and then
|
|
993
|
-
* start filesystem and rollup watchers.
|
|
994
|
-
* Returns the watcher handles once they are created so that callers
|
|
995
|
-
* (for example tests) can clean them up later.
|
|
996
|
-
*/
|
|
997
|
-
async watch() {
|
|
998
|
-
try {
|
|
999
|
-
onEnd(() => {
|
|
1000
|
-
if (this.watchers) {
|
|
1001
|
-
this.watchers.chokidar.close();
|
|
1002
|
-
this.watchers.rollup.close();
|
|
1003
|
-
}
|
|
1004
|
-
});
|
|
1005
|
-
await this._watch();
|
|
1006
|
-
}
|
|
1007
|
-
catch (e) {
|
|
1008
|
-
if (e instanceof Error) {
|
|
1009
|
-
Logger.e('Watcher', e.stack || e.message);
|
|
1010
|
-
}
|
|
1011
|
-
else {
|
|
1012
|
-
Logger.e('Watcher', e + '');
|
|
1013
|
-
}
|
|
1014
|
-
showText('MBLER__ERR__WATCHER: ' + e + ' Log at ' + Logger.LogFile);
|
|
1015
|
-
this.resolve(1);
|
|
1016
|
-
return null;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
async start() {
|
|
1020
|
-
try {
|
|
1021
|
-
return await this.build();
|
|
1022
|
-
}
|
|
1023
|
-
catch (e) {
|
|
1024
|
-
if (e instanceof Error) {
|
|
1025
|
-
Logger.e('Build', e.stack || e.message);
|
|
1026
|
-
}
|
|
1027
|
-
else {
|
|
1028
|
-
Logger.e('Build', e + '');
|
|
1029
|
-
}
|
|
1030
|
-
showText('MBLER__ERR__BUILD: ' + e.stack + ' Log at ' + Logger.LogFile);
|
|
1031
|
-
this.resolve(1);
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
/**
|
|
1035
|
-
* Handles returned from the currently-active watchers.
|
|
1036
|
-
* Set by {@link createWatcher} and exposed via {@link getWatchers}
|
|
1037
|
-
* so that external callers can close them when necessary (e.g. tests).
|
|
1038
|
-
*/
|
|
1039
|
-
watchers = null;
|
|
1040
|
-
/**
|
|
1041
|
-
* Returns the watcher handles if watch mode has been started.
|
|
1042
|
-
* Can be safely called even before `watch()` has been invoked.
|
|
1043
|
-
*/
|
|
1044
|
-
getWatchers() {
|
|
1045
|
-
return this.watchers;
|
|
1046
|
-
}
|
|
1047
|
-
/**
|
|
1048
|
-
* Close any active watchers. The build process does not automatically
|
|
1049
|
-
* terminate the watchers unless the process exits; tests or CLI wrappers
|
|
1050
|
-
* can call this method to clean up resources.
|
|
1051
|
-
*/
|
|
1052
|
-
closeWatchers() {
|
|
1053
|
-
if (this.watchers) {
|
|
1054
|
-
this.watchers.chokidar.close();
|
|
1055
|
-
this.watchers.rollup.close();
|
|
1056
|
-
this.watchers = null;
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
rollupPlugin = null;
|
|
1060
|
-
init = false;
|
|
1061
|
-
/**
|
|
1062
|
-
* Which modules are present in the current project.
|
|
1063
|
-
* - "behavior" when only behavior code exists
|
|
1064
|
-
* - "resources" when only resource files exist
|
|
1065
|
-
* - "all" when both are present
|
|
1066
|
-
* This field is populated during `handlerOtherAddon`.
|
|
1067
|
-
*/
|
|
1068
|
-
module = null;
|
|
1069
|
-
/**
|
|
1070
|
-
* Determine whether a path refers to a regular file or a directory.
|
|
1071
|
-
* Follows symbolic links recursively. Throws if the path exists but
|
|
1072
|
-
* is not one of the expected types.
|
|
1073
|
-
*
|
|
1074
|
-
* @param filePath file system path to inspect
|
|
1075
|
-
* @returns "file" or "directory"
|
|
1076
|
-
*/
|
|
1077
|
-
async fileType(filePath) {
|
|
1078
|
-
const stat = await fs__namespace.lstat(filePath);
|
|
1079
|
-
if (stat.isFile()) {
|
|
1080
|
-
return 'file';
|
|
1081
|
-
}
|
|
1082
|
-
if (stat.isDirectory()) {
|
|
1083
|
-
return 'directory';
|
|
1084
|
-
}
|
|
1085
|
-
if (stat.isSymbolicLink()) {
|
|
1086
|
-
return await this.fileType(await fs__namespace.readlink(filePath));
|
|
1087
|
-
}
|
|
1088
|
-
throw new Error('[build addon]: invaild file type');
|
|
1089
|
-
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Perform a single build of the project located at {@link baseBuildDir}.
|
|
1092
|
-
* The process is roughly:
|
|
1093
|
-
* 1. load and validate the configuration file
|
|
1094
|
-
* 2. prepare source and output directory information
|
|
1095
|
-
* 3. copy addon files (behavior/resources)
|
|
1096
|
-
* 4. generate manifest.json files
|
|
1097
|
-
* 5. run rollup to bundle any script entry point
|
|
1098
|
-
*
|
|
1099
|
-
* If anything goes wrong the promise returned by the public wrapper
|
|
1100
|
-
* (`build()` function exported at the bottom of this file) will be
|
|
1101
|
-
* resolved with a non-zero code and appropriate log entries will be
|
|
1102
|
-
* emitted.
|
|
1103
|
-
*/
|
|
1104
|
-
async build() {
|
|
1105
|
-
const progress = new Postgress(100);
|
|
1106
|
-
this.init = true;
|
|
1107
|
-
if (!path.isAbsolute(this.baseBuildDir)) {
|
|
1108
|
-
throw new Error('[init build]: build dir is not absolute path');
|
|
1109
|
-
}
|
|
1110
|
-
this.currentConfig = await ReadProjectMblerConfig(this.baseBuildDir);
|
|
1111
|
-
this.loadData();
|
|
1112
|
-
if (!this.isWatch)
|
|
1113
|
-
progress.update(10);
|
|
1114
|
-
await this.handlerOtherAddon();
|
|
1115
|
-
await this.handlerManifest();
|
|
1116
|
-
if (!this.isWatch)
|
|
1117
|
-
progress.update(30);
|
|
1118
|
-
const rBuild = (await this.createRollup());
|
|
1119
|
-
if (!this.rollupPlugin || !this.outdirs) {
|
|
1120
|
-
throw new Error(`[build addon]: can't resolve rollup instance`);
|
|
1121
|
-
}
|
|
1122
|
-
if (!this.isWatch)
|
|
1123
|
-
progress.update(50);
|
|
1124
|
-
// write script
|
|
1125
|
-
let output = this.currentConfig.script?.main;
|
|
1126
|
-
if (!output)
|
|
1127
|
-
output = "index.js";
|
|
1128
|
-
if (path.extname(output) !== "js")
|
|
1129
|
-
output = output.slice(0, output.length - path.extname(output).length) + ".js";
|
|
1130
|
-
if (this.currentConfig.script)
|
|
1131
|
-
await rBuild.write({
|
|
1132
|
-
file: join(path.join(this.outdirs.behavior, "scripts"), output),
|
|
1133
|
-
format: 'esm',
|
|
1134
|
-
sourcemap: false,
|
|
1135
|
-
});
|
|
1136
|
-
if (!this.isWatch)
|
|
1137
|
-
progress.update(70);
|
|
1138
|
-
await generateRelease(this);
|
|
1139
|
-
if (!this.isWatch)
|
|
1140
|
-
progress.update(80);
|
|
1141
|
-
if (!this.isWatch)
|
|
1142
|
-
this.resolve(0);
|
|
1143
|
-
if (!this.isWatch)
|
|
1144
|
-
progress.update(100);
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Create and return a Rollup build instance configured for the
|
|
1148
|
-
* project's script. The Rollup configuration mirrors the options
|
|
1149
|
-
* used by the CLI when running manual builds.
|
|
1150
|
-
*
|
|
1151
|
-
* Returns undefined if the project does not define a script section
|
|
1152
|
-
* (in which case nothing needs to be bundled).
|
|
1153
|
-
*/
|
|
1154
|
-
async createRollup() {
|
|
1155
|
-
if (!this.currentConfig || !this.srcDirs || !this.outdirs)
|
|
1156
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1157
|
-
if (!this.currentConfig.script)
|
|
1158
|
-
return;
|
|
1159
|
-
const main = path.join(this.srcDirs.behavior, 'scripts', this.currentConfig.script.main);
|
|
1160
|
-
if (!(await FileExsit(main))) {
|
|
1161
|
-
throw new Error(`[build addon]: main script ${main} is not exist: can't resolve entry`);
|
|
1162
|
-
}
|
|
1163
|
-
const plugin = [
|
|
1164
|
-
jsonPlugin(),
|
|
1165
|
-
resolvePlugin({
|
|
1166
|
-
extensions: ['.ts', '.js', '.json'],
|
|
1167
|
-
}),
|
|
1168
|
-
commonjs()
|
|
1169
|
-
];
|
|
1170
|
-
const moduleDir = path.join(this.baseBuildDir, 'node_modules');
|
|
1171
|
-
if (!(await FileExsit(moduleDir))) {
|
|
1172
|
-
throw new Error(`[build addon]: node_modules is not exist in project root: can't resolve node_modules for rollup: ${moduleDir}`);
|
|
1173
|
-
}
|
|
1174
|
-
if (this.currentConfig.minify) {
|
|
1175
|
-
plugin.push(minifyPlugin({
|
|
1176
|
-
format: {
|
|
1177
|
-
comments: false,
|
|
1178
|
-
},
|
|
1179
|
-
compress: {
|
|
1180
|
-
unused: true,
|
|
1181
|
-
},
|
|
1182
|
-
}));
|
|
1183
|
-
}
|
|
1184
|
-
if (this.currentConfig.script.lang == "ts") {
|
|
1185
|
-
const tsconfigPath = path.join(this.baseBuildDir, 'tsconfig.json');
|
|
1186
|
-
if (!(await FileExsit(tsconfigPath))) {
|
|
1187
|
-
throw new Error(`[build addon]: ts-lang: tsconfig.json is not exist in project root: can't resolve tsconfig for rollup: ${tsconfigPath}`);
|
|
1188
|
-
}
|
|
1189
|
-
plugin.push(typescript({
|
|
1190
|
-
sourceMap: false,
|
|
1191
|
-
tsconfig: tsconfigPath,
|
|
1192
|
-
exclude: [
|
|
1193
|
-
this.outdirs.behavior,
|
|
1194
|
-
this.outdirs.resources
|
|
1195
|
-
],
|
|
1196
|
-
include: [
|
|
1197
|
-
this.srcDirs.behavior
|
|
1198
|
-
]
|
|
1199
|
-
}));
|
|
1200
|
-
}
|
|
1201
|
-
if (this.currentConfig.script?.lang == 'mcx') {
|
|
1202
|
-
try {
|
|
1203
|
-
const tsconfigPath = path.join(this.baseBuildDir, 'tsconfig.json');
|
|
1204
|
-
if (!(await FileExsit(tsconfigPath))) {
|
|
1205
|
-
throw new Error(`[build addon]: ts-lang: tsconfig.json is not exist in project root: can't resolve tsconfig for rollup: ${tsconfigPath}`);
|
|
1206
|
-
}
|
|
1207
|
-
const pluginConfig = {
|
|
1208
|
-
moduleDir: moduleDir,
|
|
1209
|
-
tsconfigPath: tsconfigPath,
|
|
1210
|
-
sourcemap: false,
|
|
1211
|
-
ts: this.mcxTs,
|
|
1212
|
-
mcxLanguagePlugin: this.mcxLanguagePluginCreator
|
|
1213
|
-
};
|
|
1214
|
-
if (this.mcxLanguagePluginCreator) {
|
|
1215
|
-
pluginConfig.mcxLanguagePlugin = this.mcxLanguagePluginCreator;
|
|
1216
|
-
}
|
|
1217
|
-
plugin.push(mcxDef__namespace.plugin(pluginConfig, this.outdirs));
|
|
1218
|
-
}
|
|
1219
|
-
catch (err) {
|
|
1220
|
-
throw new Error(`[build addon]: mcx plugin is required but '@mbler/mcx-core' could not be loaded: ${err}`);
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
// save plugin array for watcher re-use
|
|
1224
|
-
this.rollupPlugin = plugin;
|
|
1225
|
-
return await rollup__namespace.rollup({
|
|
1226
|
-
input: main,
|
|
1227
|
-
external: ['@minecraft/server', '@minecraft/server-ui'],
|
|
1228
|
-
plugins: plugin,
|
|
1229
|
-
});
|
|
1230
|
-
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Internal helper invoked by {@link watch}.
|
|
1233
|
-
* Ensures a build has been run before starting the watchers.
|
|
1234
|
-
*/
|
|
1235
|
-
async _watch() {
|
|
1236
|
-
// init build
|
|
1237
|
-
if (!this.init) {
|
|
1238
|
-
await this.build();
|
|
1239
|
-
}
|
|
1240
|
-
this.createWatcher();
|
|
1241
|
-
// watchers field is populated by createWatcher
|
|
1242
|
-
}
|
|
1243
|
-
isParent(parent, dir) {
|
|
1244
|
-
const relative = path.relative(parent, dir);
|
|
1245
|
-
return (!!relative && !relative.startsWith('..') && !path.isAbsolute(relative));
|
|
1246
|
-
}
|
|
1247
|
-
isChange(oldObj, newObj, checkKeys) {
|
|
1248
|
-
for (const key of checkKeys) {
|
|
1249
|
-
if (typeof oldObj[key] === 'object' &&
|
|
1250
|
-
typeof newObj[key] === 'object' &&
|
|
1251
|
-
oldObj[key] !== null &&
|
|
1252
|
-
newObj[key] !== null) {
|
|
1253
|
-
if (this.isChange(oldObj[key], newObj[key], Object.getOwnPropertyNames(oldObj[key]))) {
|
|
1254
|
-
return true;
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
else if (oldObj[key] !== newObj[key]) {
|
|
1258
|
-
return true;
|
|
1259
|
-
}
|
|
1260
|
-
}
|
|
1261
|
-
return false;
|
|
1262
|
-
}
|
|
1263
|
-
createRollupWatcher() {
|
|
1264
|
-
if (!this.srcDirs ||
|
|
1265
|
-
!this.outdirs ||
|
|
1266
|
-
!this.currentConfig ||
|
|
1267
|
-
!this.rollupPlugin)
|
|
1268
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1269
|
-
let output = this.currentConfig.script?.main;
|
|
1270
|
-
if (!output)
|
|
1271
|
-
output = "index.js";
|
|
1272
|
-
if (path.extname(output) !== "js")
|
|
1273
|
-
output = output.slice(0, output.length - path.extname(output).length) + ".js";
|
|
1274
|
-
const rollupWatcher = rollup__namespace.watch({
|
|
1275
|
-
input: path.join(this.srcDirs.behavior, 'scripts', this.currentConfig?.script?.main || ''),
|
|
1276
|
-
external: ['@minecraft/server', '@minecraft/server-ui'],
|
|
1277
|
-
plugins: this.rollupPlugin,
|
|
1278
|
-
output: {
|
|
1279
|
-
file: join(path.join(this.outdirs.behavior, "scripts"), output),
|
|
1280
|
-
format: 'esm',
|
|
1281
|
-
sourcemap: false,
|
|
1282
|
-
},
|
|
1283
|
-
cache: true,
|
|
1284
|
-
watch: {
|
|
1285
|
-
clearScreen: false,
|
|
1286
|
-
include: path.join(this.srcDirs.behavior, 'scripts/**/*'),
|
|
1287
|
-
exclude: [
|
|
1288
|
-
path.join(this.baseBuildDir, 'node_modules/**/*'),
|
|
1289
|
-
this.outdirs.behavior,
|
|
1290
|
-
this.outdirs.resources,
|
|
1291
|
-
this.outdirs.dist,
|
|
1292
|
-
],
|
|
1293
|
-
},
|
|
1294
|
-
});
|
|
1295
|
-
rollupWatcher.on('change', async (filePath) => {
|
|
1296
|
-
Logger.i('Watcher', `file changed: ${filePath}, start rebuild`);
|
|
1297
|
-
});
|
|
1298
|
-
rollupWatcher.on('event', async (event) => {
|
|
1299
|
-
if (event.code === 'ERROR') {
|
|
1300
|
-
Logger.e('Watcher', `rollup error: ${event.error.stack || event.error}`);
|
|
1301
|
-
showText('MBLER__ERR__ROLLUP: ' +
|
|
1302
|
-
(event.error.stack || event.error) +
|
|
1303
|
-
' Log at ' +
|
|
1304
|
-
Logger.LogFile);
|
|
1305
|
-
}
|
|
1306
|
-
else if (event.code === 'END') {
|
|
1307
|
-
Logger.i('Watcher', `rebuild success`);
|
|
1308
|
-
}
|
|
1309
|
-
});
|
|
1310
|
-
return rollupWatcher;
|
|
1311
|
-
}
|
|
1312
|
-
async onChange(filePath) {
|
|
1313
|
-
if (!this.srcDirs ||
|
|
1314
|
-
!this.outdirs ||
|
|
1315
|
-
!this.currentConfig ||
|
|
1316
|
-
!this.rollupPlugin ||
|
|
1317
|
-
!this.watchers)
|
|
1318
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1319
|
-
const isConfigChange = path.relative(path.join(this.baseBuildDir, 'mbler.config.json'), filePath) === '';
|
|
1320
|
-
const isBehaviorChange = this.isParent(this.srcDirs.behavior, filePath) && !this.isParent(path.join(this.srcDirs.behavior, 'scripts'), filePath);
|
|
1321
|
-
const isResourcesChange = this.isParent(this.srcDirs.resources, filePath);
|
|
1322
|
-
if (isConfigChange) {
|
|
1323
|
-
const oldConfig = this.currentConfig;
|
|
1324
|
-
Logger.i('Watcher', 'detected mbler.config.json change, reload config');
|
|
1325
|
-
this.currentConfig = await ReadProjectMblerConfig(this.baseBuildDir);
|
|
1326
|
-
this.loadData();
|
|
1327
|
-
if (this.isChange(oldConfig, this.currentConfig, [
|
|
1328
|
-
'name',
|
|
1329
|
-
'version',
|
|
1330
|
-
'description',
|
|
1331
|
-
'mcVersion',
|
|
1332
|
-
])) {
|
|
1333
|
-
await this.handlerManifest();
|
|
1334
|
-
}
|
|
1335
|
-
if (this.isChange(oldConfig, this.currentConfig, ['script', 'outdir'])) {
|
|
1336
|
-
this.watchers.rollup.close();
|
|
1337
|
-
await this.createRollup();
|
|
1338
|
-
this.watchers.rollup = rollup__namespace.watch({
|
|
1339
|
-
input: path.join(this.srcDirs.behavior, 'scripts', this.currentConfig?.script?.main || ''),
|
|
1340
|
-
external: ['@minecraft/server', '@minecraft/server-ui'],
|
|
1341
|
-
plugins: this.rollupPlugin,
|
|
1342
|
-
output: {
|
|
1343
|
-
file: path.join(this.outdirs.behavior, 'scripts', this.currentConfig?.script?.main || ''),
|
|
1344
|
-
format: 'esm',
|
|
1345
|
-
},
|
|
1346
|
-
});
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
// if behavior or resources change, we can just copy the changed file instead of copy all files again.
|
|
1350
|
-
if (isBehaviorChange || isResourcesChange) {
|
|
1351
|
-
const handlerBP = async () => {
|
|
1352
|
-
if (!this.srcDirs || !this.outdirs)
|
|
1353
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1354
|
-
const relativePath = path.relative(this.srcDirs.behavior, filePath);
|
|
1355
|
-
await fs__namespace.cp(path.join(this.srcDirs.behavior, relativePath), path.join(this.outdirs.behavior, relativePath), {
|
|
1356
|
-
recursive: true,
|
|
1357
|
-
force: true,
|
|
1358
|
-
});
|
|
1359
|
-
};
|
|
1360
|
-
const handlerRP = async () => {
|
|
1361
|
-
if (!this.srcDirs || !this.outdirs)
|
|
1362
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1363
|
-
const relativePath = path.relative(this.srcDirs.resources, filePath);
|
|
1364
|
-
await fs__namespace.cp(path.join(this.srcDirs.resources, relativePath), path.join(this.outdirs.resources, relativePath), {
|
|
1365
|
-
recursive: true,
|
|
1366
|
-
force: true,
|
|
1367
|
-
});
|
|
1368
|
-
};
|
|
1369
|
-
if (isBehaviorChange) {
|
|
1370
|
-
await handlerBP();
|
|
1371
|
-
}
|
|
1372
|
-
if (isResourcesChange) {
|
|
1373
|
-
await handlerRP();
|
|
1374
|
-
}
|
|
1375
|
-
}
|
|
1376
|
-
showText(`[${chalk.green('mbler')}] ${chalk.bgYellow(`file changed: ${filePath}`)}`);
|
|
1377
|
-
}
|
|
1378
|
-
createWatcher() {
|
|
1379
|
-
if (!this.srcDirs || !this.outdirs || !this.rollupPlugin)
|
|
1380
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1381
|
-
const chokidar$1 = chokidar.watch(this.baseBuildDir, {
|
|
1382
|
-
ignored: [
|
|
1383
|
-
this.outdirs.behavior,
|
|
1384
|
-
this.outdirs.resources,
|
|
1385
|
-
this.outdirs.dist,
|
|
1386
|
-
path.join(this.baseBuildDir, 'node_modules'),
|
|
1387
|
-
],
|
|
1388
|
-
ignoreInitial: true,
|
|
1389
|
-
interval: 100,
|
|
1390
|
-
});
|
|
1391
|
-
const onChange = async (filePath) => {
|
|
1392
|
-
await this.onChange(filePath);
|
|
1393
|
-
};
|
|
1394
|
-
chokidar$1.on('change', onChange);
|
|
1395
|
-
const rollupWatcher = this.createRollupWatcher();
|
|
1396
|
-
this.watchers = {
|
|
1397
|
-
chokidar: chokidar$1,
|
|
1398
|
-
rollup: rollupWatcher,
|
|
1399
|
-
};
|
|
1400
|
-
}
|
|
1401
|
-
async handlerManifest() {
|
|
1402
|
-
if (!this.currentConfig || !this.outdirs || !this.srcDirs || !this.module)
|
|
1403
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1404
|
-
const otherManifestOption = {
|
|
1405
|
-
behavior: {},
|
|
1406
|
-
resources: {},
|
|
1407
|
-
};
|
|
1408
|
-
const handlerBP = async () => {
|
|
1409
|
-
if (!this.outdirs || !this.currentConfig)
|
|
1410
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1411
|
-
const manifest = await generateManifest(this.currentConfig, 'data');
|
|
1412
|
-
await writeJSON(path.join(this.outdirs.behavior, 'manifest.json'), {
|
|
1413
|
-
...manifest,
|
|
1414
|
-
...otherManifestOption.behavior,
|
|
1415
|
-
});
|
|
1416
|
-
};
|
|
1417
|
-
const handlerRP = async () => {
|
|
1418
|
-
if (!this.outdirs || !this.currentConfig)
|
|
1419
|
-
throw new Error(`[build addon]: can't first can this method`);
|
|
1420
|
-
const manifest = await generateManifest(this.currentConfig, 'resources');
|
|
1421
|
-
await writeJSON(path.join(this.outdirs.resources, 'manifest.json'), {
|
|
1422
|
-
...manifest,
|
|
1423
|
-
...otherManifestOption.resources,
|
|
1424
|
-
});
|
|
1425
|
-
};
|
|
1426
|
-
if (this.module == 'behavior' || this.module == 'all') {
|
|
1427
|
-
const filePath = path.join(this.srcDirs.behavior, 'manifest.json');
|
|
1428
|
-
if (await FileExsit(filePath)) {
|
|
1429
|
-
try {
|
|
1430
|
-
const content = await fs__namespace.readFile(filePath, 'utf-8');
|
|
1431
|
-
const json = JSON.parse(content);
|
|
1432
|
-
otherManifestOption.behavior = json;
|
|
1433
|
-
}
|
|
1434
|
-
catch (err) {
|
|
1435
|
-
Logger.w('Build', 'invalid manifest.json in behavior');
|
|
1436
|
-
}
|
|
1437
|
-
}
|
|
1438
|
-
await handlerBP();
|
|
1439
|
-
}
|
|
1440
|
-
if (this.module == 'resources' || this.module == 'all') {
|
|
1441
|
-
const filePath = path.join(this.srcDirs.resources, 'manifest.json');
|
|
1442
|
-
if (await FileExsit(filePath)) {
|
|
1443
|
-
try {
|
|
1444
|
-
const content = await fs__namespace.readFile(filePath, 'utf-8');
|
|
1445
|
-
const json = JSON.parse(content);
|
|
1446
|
-
otherManifestOption.resources = json;
|
|
1447
|
-
}
|
|
1448
|
-
catch (err) {
|
|
1449
|
-
Logger.w('Build', 'invalid manifest.json in resources');
|
|
1450
|
-
}
|
|
1451
|
-
}
|
|
1452
|
-
await handlerRP();
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
loadData() {
|
|
1456
|
-
// check run time
|
|
1457
|
-
if (!this.currentConfig || !this.baseBuildDir)
|
|
1458
|
-
throw new Error("[build data]: can't resolve again");
|
|
1459
|
-
// source code dir
|
|
1460
|
-
this.srcDirs = {
|
|
1461
|
-
behavior: path.join(this.baseBuildDir, BuildConfig.behavior),
|
|
1462
|
-
resources: path.join(this.baseBuildDir, BuildConfig.resources), // res
|
|
1463
|
-
};
|
|
1464
|
-
// output dir
|
|
1465
|
-
this.outdirs = {
|
|
1466
|
-
behavior: this.currentConfig.outdir?.behavior
|
|
1467
|
-
? join(this.baseBuildDir, this.currentConfig.outdir.behavior)
|
|
1468
|
-
: path.join(this.baseBuildDir, 'dist/dep'),
|
|
1469
|
-
resources: this.currentConfig.outdir?.resources
|
|
1470
|
-
? join(this.baseBuildDir, this.currentConfig.outdir.resources)
|
|
1471
|
-
: path.join(this.baseBuildDir, 'dist/res'),
|
|
1472
|
-
dist: this.currentConfig.outdir?.dist
|
|
1473
|
-
? join(this.baseBuildDir, this.currentConfig.outdir.dist)
|
|
1474
|
-
: path.join(this.baseBuildDir, 'dist-pkg'),
|
|
1475
|
-
};
|
|
1476
|
-
}
|
|
1477
|
-
/**
|
|
1478
|
-
* Copy the various files (behavior/resources) into the corresponding
|
|
1479
|
-
* output directories and determine which modules exist in the project
|
|
1480
|
-
* by inspecting the source directories.
|
|
1481
|
-
*/
|
|
1482
|
-
async handlerOtherAddon() {
|
|
1483
|
-
if (!this.srcDirs)
|
|
1484
|
-
throw new Error("[build addon]: can't first can this method");
|
|
1485
|
-
const isHasBp = await FileExsit(this.srcDirs.behavior);
|
|
1486
|
-
if (!isHasBp)
|
|
1487
|
-
throw new Error("[build addon]: can't resolve behavior");
|
|
1488
|
-
// init copy resources
|
|
1489
|
-
const handlerBP = async () => {
|
|
1490
|
-
if (!this.srcDirs || !this.outdirs)
|
|
1491
|
-
throw new Error("[build addon]: can't first can this method");
|
|
1492
|
-
for (const f of await fs__namespace.readdir(this.srcDirs.behavior)) {
|
|
1493
|
-
const fType = await this.fileType(path.join(this.srcDirs.behavior, f));
|
|
1494
|
-
const includeType = BuildConfig.includes.behavior[f] || BuildConfig.includes.public[f];
|
|
1495
|
-
if (includeType == fType) {
|
|
1496
|
-
await fs__namespace.cp(path.join(this.srcDirs.behavior, f), path.join(this.outdirs.behavior, f), {
|
|
1497
|
-
recursive: true,
|
|
1498
|
-
force: true,
|
|
1499
|
-
});
|
|
1500
|
-
}
|
|
1501
|
-
else if (includeType == 'skip') {
|
|
1502
|
-
continue;
|
|
1503
|
-
}
|
|
1504
|
-
else {
|
|
1505
|
-
throw new Error(`[build addon]: invaild file: ${path.join(this.srcDirs.behavior, f)}: type: ${fType}`);
|
|
1506
|
-
}
|
|
1507
|
-
}
|
|
1508
|
-
};
|
|
1509
|
-
const handlerRP = async () => {
|
|
1510
|
-
if (!this.srcDirs || !this.outdirs)
|
|
1511
|
-
throw new Error("[build addon]: can't first can this method");
|
|
1512
|
-
for (const f of await fs__namespace.readdir(this.srcDirs.resources)) {
|
|
1513
|
-
const fType = await this.fileType(path.join(this.srcDirs.resources, f));
|
|
1514
|
-
const includeType = BuildConfig.includes.resources[f] || BuildConfig.includes.public[f];
|
|
1515
|
-
if (includeType == fType) {
|
|
1516
|
-
await fs__namespace.cp(path.join(this.srcDirs.resources, f), path.join(this.outdirs.resources, f), {
|
|
1517
|
-
recursive: true,
|
|
1518
|
-
force: true,
|
|
1519
|
-
});
|
|
1520
|
-
}
|
|
1521
|
-
else if (includeType == 'skip') {
|
|
1522
|
-
continue;
|
|
1523
|
-
}
|
|
1524
|
-
else {
|
|
1525
|
-
throw new Error(`[build addon]: invaild file: ${path.join(this.srcDirs.resources, f)}: type: ${fType}`);
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
};
|
|
1529
|
-
const tasks = [];
|
|
1530
|
-
if (await FileExsit(this.srcDirs.behavior)) {
|
|
1531
|
-
this.module = 'behavior';
|
|
1532
|
-
tasks.push(handlerBP());
|
|
1533
|
-
}
|
|
1534
|
-
if (await FileExsit(this.srcDirs.resources)) {
|
|
1535
|
-
if (this.module == 'behavior') {
|
|
1536
|
-
this.module = 'all';
|
|
1537
|
-
}
|
|
1538
|
-
else {
|
|
1539
|
-
this.module = 'resources';
|
|
1540
|
-
}
|
|
1541
|
-
tasks.push(handlerRP());
|
|
1542
|
-
}
|
|
1543
|
-
if (!this.module) {
|
|
1544
|
-
throw new Error("[build addon]: couldn't resolve source code(your behaivor or reources code is not found)");
|
|
1545
|
-
}
|
|
1546
|
-
await Promise.all(tasks);
|
|
1547
|
-
}
|
|
1548
|
-
}
|
|
1549
|
-
function build(cliParam, work) {
|
|
1550
|
-
return new Promise((resolve) => {
|
|
1551
|
-
new Build(cliParam.opts, work, resolve).start();
|
|
1552
|
-
});
|
|
1553
|
-
}
|
|
1554
|
-
function watch(cliParam, work) {
|
|
1555
|
-
return new Promise((resolve, reject) => {
|
|
1556
|
-
try {
|
|
1557
|
-
const build = new Build(cliParam.opts, work, resolve, true);
|
|
1558
|
-
build.start().then(() => {
|
|
1559
|
-
build.watch();
|
|
1560
|
-
showText(`[${chalk.green('mbler')}] ${chalk.bgYellow('watching for file changes...')}`);
|
|
1561
|
-
});
|
|
1562
|
-
}
|
|
1563
|
-
catch (err) {
|
|
1564
|
-
if (err instanceof Error) {
|
|
1565
|
-
reject(`[watcher]: error ${err.stack || err.message}`);
|
|
1566
|
-
}
|
|
1567
|
-
else {
|
|
1568
|
-
reject(err);
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
});
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
var index = /*#__PURE__*/Object.freeze({
|
|
1575
|
-
__proto__: null,
|
|
1576
|
-
Build: Build,
|
|
1577
|
-
McxTsc: runTSC,
|
|
1578
|
-
build: build,
|
|
1579
|
-
default: Build,
|
|
1580
|
-
watch: watch
|
|
1581
|
-
});
|
|
1582
|
-
|
|
1583
|
-
async function tryMkdir(point) {
|
|
1584
|
-
try {
|
|
1585
|
-
await fs$1.mkdir(point);
|
|
1586
|
-
return true;
|
|
1587
|
-
}
|
|
1588
|
-
catch {
|
|
1589
|
-
return false;
|
|
1590
|
-
}
|
|
1591
|
-
}
|
|
1592
|
-
class WorkDirManage {
|
|
1593
|
-
cacheDir;
|
|
1594
|
-
currentWorkPoint = null;
|
|
1595
|
-
enabledPath = path.join(os.homedir(), ".cache/mbler/workdir_enabled.db");
|
|
1596
|
-
constructor(cacheDir = path.join(os.homedir(), ".cache/mbler/mp.db")) {
|
|
1597
|
-
this.cacheDir = cacheDir;
|
|
1598
|
-
}
|
|
1599
|
-
async isDisabled() {
|
|
1600
|
-
try {
|
|
1601
|
-
await fs$1.readFile(this.enabledPath, "utf-8");
|
|
1602
|
-
return false; // 文件存在,表示启用
|
|
1603
|
-
}
|
|
1604
|
-
catch {
|
|
1605
|
-
return true; // 文件不存在,默认禁用
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
async setDisabled(disabled) {
|
|
1609
|
-
if (!disabled) {
|
|
1610
|
-
// 启用:创建文件
|
|
1611
|
-
await fs$1.writeFile(this.enabledPath, "1", { encoding: "utf-8" });
|
|
1612
|
-
}
|
|
1613
|
-
else {
|
|
1614
|
-
try {
|
|
1615
|
-
await fs$1.unlink(this.enabledPath);
|
|
1616
|
-
}
|
|
1617
|
-
catch {
|
|
1618
|
-
// 文件不存在,忽略
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
1621
|
-
}
|
|
1622
|
-
async set(newPointDir) {
|
|
1623
|
-
// check
|
|
1624
|
-
try {
|
|
1625
|
-
const s = await fs$1.stat(newPointDir);
|
|
1626
|
-
if (!s.isDirectory()) {
|
|
1627
|
-
throw new Error("Not Dir (0xcvb)");
|
|
1628
|
-
}
|
|
1629
|
-
}
|
|
1630
|
-
catch (err) {
|
|
1631
|
-
const code = err.code;
|
|
1632
|
-
if (err.message && err.message.includes("0xcvb"))
|
|
1633
|
-
return i18n.workdir.nfound;
|
|
1634
|
-
if (code == "ENOENT") {
|
|
1635
|
-
const res = tryMkdir(newPointDir);
|
|
1636
|
-
if (!res) {
|
|
1637
|
-
return i18n.workdir.nfound;
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
try {
|
|
1642
|
-
if (!(await FileExsit(path.dirname(this.cacheDir)))) {
|
|
1643
|
-
const isC = await tryMkdir(path.dirname(this.cacheDir));
|
|
1644
|
-
if (!isC)
|
|
1645
|
-
return i18n.workdir.nfound;
|
|
1646
|
-
}
|
|
1647
|
-
await fs$1.writeFile(this.cacheDir, newPointDir, {
|
|
1648
|
-
encoding: "utf-8",
|
|
1649
|
-
});
|
|
1650
|
-
}
|
|
1651
|
-
catch (err) {
|
|
1652
|
-
Logger.e("WorkDir", err.stack);
|
|
1653
|
-
}
|
|
1654
|
-
return i18n.workdir.set + newPointDir;
|
|
1655
|
-
}
|
|
1656
|
-
async get() {
|
|
1657
|
-
if (await this.isDisabled()) {
|
|
1658
|
-
return node_process.cwd();
|
|
1659
|
-
}
|
|
1660
|
-
if (this.currentWorkPoint) {
|
|
1661
|
-
return this.currentWorkPoint;
|
|
1662
|
-
}
|
|
1663
|
-
const file = await fs$1.readFile(this.cacheDir, "utf-8").catch((e) => {
|
|
1664
|
-
this.set(node_process.cwd());
|
|
1665
|
-
return node_process.cwd();
|
|
1666
|
-
});
|
|
1667
|
-
return (this.currentWorkPoint = file);
|
|
1668
|
-
}
|
|
1669
|
-
}
|
|
1670
|
-
|
|
1671
821
|
async function isInit(dir) {
|
|
1672
822
|
return (await Promise.all([BuildConfig.ConfigFile, 'package.json', 'behavior'].map((item) => {
|
|
1673
823
|
return FileExsit(path.join(dir, item));
|
|
@@ -1682,12 +832,12 @@ async function findTemplatedir() {
|
|
|
1682
832
|
return path.join(__dirname, './template');
|
|
1683
833
|
}
|
|
1684
834
|
async function initCommand(cliParam, workdir) {
|
|
835
|
+
node_process.stdout.write(i18n.init.welcome + "\n");
|
|
1685
836
|
await exp.refresh();
|
|
1686
837
|
const cmdParams = cliParam.params.slice(1);
|
|
1687
838
|
if (await isInit(workdir)) {
|
|
1688
839
|
return 0;
|
|
1689
840
|
}
|
|
1690
|
-
showText(i18n.init.welcome);
|
|
1691
841
|
const initOpts = {
|
|
1692
842
|
name: cmdParams[0] || (await input(i18n.init.name)),
|
|
1693
843
|
description: cmdParams[1] || (await input(i18n.init.description)),
|
|
@@ -1775,12 +925,14 @@ async function initCommand(cliParam, workdir) {
|
|
|
1775
925
|
if (initOpts.lang == 'mcx') {
|
|
1776
926
|
mblerConfig.script.main = 'index.mjs';
|
|
1777
927
|
packageJSON.devDependencies['@mbler/mcx'] = config.mcxVersion;
|
|
928
|
+
packageJSON.devDependencies['@mbler/mcx-core'] = config.mcxCoreVersion;
|
|
1778
929
|
}
|
|
1779
930
|
if (initOpts.lang !== 'js') {
|
|
1780
931
|
await writeJSON(tsconfigPath, tsconfig);
|
|
1781
932
|
}
|
|
1782
933
|
await writeJSON(packageJSONPath, packageJSON);
|
|
1783
|
-
|
|
934
|
+
const mblerConfigContent = `import { defineConfig } from "mbler";\nexport default ${JSON.stringify(mblerConfig, null, 2)}`;
|
|
935
|
+
await fs$1.writeFile(mblerConfigPath, mblerConfigContent);
|
|
1784
936
|
// write template
|
|
1785
937
|
const templatedir = await findTemplatedir();
|
|
1786
938
|
if (!templatedir) {
|
|
@@ -1864,6 +1016,31 @@ function langCommand(cliParam, workdir) {
|
|
|
1864
1016
|
return 0;
|
|
1865
1017
|
}
|
|
1866
1018
|
|
|
1019
|
+
function unpublishCommand(cliParam, work) {
|
|
1020
|
+
showText("unpublish is planing");
|
|
1021
|
+
return 1;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
function publishCommand(cliParam, work) {
|
|
1025
|
+
showText("publish is planing");
|
|
1026
|
+
return 1;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
function uninstallCommand(cliParam, work) {
|
|
1030
|
+
showText("uninstall is planing");
|
|
1031
|
+
return 1;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
function installCommand(cliParam, work) {
|
|
1035
|
+
showText("install is planing");
|
|
1036
|
+
return 1;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
function loginCommand(cliParam, work) {
|
|
1040
|
+
showText("login is planing");
|
|
1041
|
+
return 1;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1867
1044
|
// `showText` moved to `utils` to avoid circular dependency with `build`.
|
|
1868
1045
|
const main = (function () {
|
|
1869
1046
|
let currentWDManage;
|
|
@@ -1980,9 +1157,11 @@ const main = (function () {
|
|
|
1980
1157
|
return async function cli() {
|
|
1981
1158
|
const cliParam = parseParam();
|
|
1982
1159
|
const handlerBuild = async (cliParam, workDir) => {
|
|
1160
|
+
const { build } = require("mbler/build");
|
|
1983
1161
|
return await build(cliParam, workDir);
|
|
1984
1162
|
};
|
|
1985
1163
|
const handlerWatch = async (cliParam, workDir) => {
|
|
1164
|
+
const { watch } = require("mbler/build");
|
|
1986
1165
|
return await watch(cliParam, workDir);
|
|
1987
1166
|
};
|
|
1988
1167
|
const cmdMap = {
|
|
@@ -1995,7 +1174,12 @@ const main = (function () {
|
|
|
1995
1174
|
init: initCommand,
|
|
1996
1175
|
version: handlerVersion,
|
|
1997
1176
|
lang: langCommand,
|
|
1998
|
-
'set-work-dir': handlerSetWorkDirCommand
|
|
1177
|
+
'set-work-dir': handlerSetWorkDirCommand,
|
|
1178
|
+
unpublish: unpublishCommand,
|
|
1179
|
+
publish: publishCommand,
|
|
1180
|
+
uninstall: uninstallCommand,
|
|
1181
|
+
install: installCommand,
|
|
1182
|
+
login: loginCommand
|
|
1999
1183
|
};
|
|
2000
1184
|
const cmd = cliParam.params[0];
|
|
2001
1185
|
if (cliParam.opts.cwp) {
|
|
@@ -2035,10 +1219,9 @@ function defineConfig(config) {
|
|
|
2035
1219
|
return config;
|
|
2036
1220
|
}
|
|
2037
1221
|
|
|
2038
|
-
exports.Build = index;
|
|
2039
1222
|
exports.Types = types;
|
|
2040
1223
|
exports.cli = main;
|
|
2041
|
-
exports.commander = index
|
|
1224
|
+
exports.commander = index;
|
|
2042
1225
|
exports.defineConfig = defineConfig;
|
|
2043
|
-
exports.i18n = index$
|
|
1226
|
+
exports.i18n = index$1;
|
|
2044
1227
|
//# sourceMappingURL=index.js.map
|