@sevenvip666/rop 0.0.7 → 0.0.9

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.
@@ -0,0 +1,813 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+ import * as fs from 'node:fs';
4
+ import fs__default from 'node:fs';
5
+ import { confirm, select, input } from '@inquirer/prompts';
6
+ import colors from 'ansi-colors';
7
+ import path from 'node:path';
8
+ import { exec } from 'child_process';
9
+ import fs$1, { readFileSync, existsSync } from 'fs';
10
+ import path$1, { resolve, join, sep, normalize } from 'path';
11
+ import { parse } from '@iarna/toml';
12
+ import { globbySync } from 'globby';
13
+ import JSON5 from 'json5';
14
+ import fetch from 'node-fetch';
15
+ import FormData from 'form-data';
16
+ import cliProgress from 'cli-progress';
17
+
18
+ /******************************************************************************
19
+ Copyright (c) Microsoft Corporation.
20
+
21
+ Permission to use, copy, modify, and/or distribute this software for any
22
+ purpose with or without fee is hereby granted.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
25
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
26
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
27
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
28
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
29
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30
+ PERFORMANCE OF THIS SOFTWARE.
31
+ ***************************************************************************** */
32
+ /* global Reflect, Promise, SuppressedError, Symbol */
33
+
34
+
35
+ var __assign = function() {
36
+ __assign = Object.assign || function __assign(t) {
37
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
38
+ s = arguments[i];
39
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
40
+ }
41
+ return t;
42
+ };
43
+ return __assign.apply(this, arguments);
44
+ };
45
+
46
+ function __awaiter(thisArg, _arguments, P, generator) {
47
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
+ return new (P || (P = Promise))(function (resolve, reject) {
49
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
51
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
52
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
53
+ });
54
+ }
55
+
56
+ function __generator(thisArg, body) {
57
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
58
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
59
+ function verb(n) { return function (v) { return step([n, v]); }; }
60
+ function step(op) {
61
+ if (f) throw new TypeError("Generator is already executing.");
62
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
63
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
64
+ if (y = 0, t) op = [op[0] & 2, t.value];
65
+ switch (op[0]) {
66
+ case 0: case 1: t = op; break;
67
+ case 4: _.label++; return { value: op[1], done: false };
68
+ case 5: _.label++; y = op[1]; op = [0]; continue;
69
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
70
+ default:
71
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
72
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
73
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
74
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
75
+ if (t[2]) _.ops.pop();
76
+ _.trys.pop(); continue;
77
+ }
78
+ op = body.call(thisArg, _);
79
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
80
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
81
+ }
82
+ }
83
+
84
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
85
+ var e = new Error(message);
86
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
87
+ };
88
+
89
+ var Init = /** @class */ (function () {
90
+ function Init() {
91
+ }
92
+ Init.prototype.initProjectConfigJson = function (framework) {
93
+ return __awaiter(this, void 0, void 0, function () {
94
+ var jsonConfigPath, jsonConfigData, flag, str;
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0:
98
+ jsonConfigPath = path.join('.', 'rop.json');
99
+ if (!fs__default.existsSync(jsonConfigPath)) return [3 /*break*/, 5];
100
+ flag = false;
101
+ _a.label = 1;
102
+ case 1:
103
+ _a.trys.push([1, 3, , 4]);
104
+ return [4 /*yield*/, confirm({
105
+ message: "".concat(colors.cyan('rop.json'), " already exists, does it need to be overwritten?"),
106
+ })];
107
+ case 2:
108
+ flag = _a.sent();
109
+ return [3 /*break*/, 4];
110
+ case 3:
111
+ _a.sent();
112
+ console.log('cancel');
113
+ return [2 /*return*/, false];
114
+ case 4:
115
+ if (!flag) {
116
+ console.log('cancel');
117
+ return [2 /*return*/, false];
118
+ }
119
+ str = fs__default.readFileSync(jsonConfigPath, 'utf8');
120
+ jsonConfigData = JSON.parse(str);
121
+ if (!jsonConfigData.framework) {
122
+ jsonConfigData.framework = {
123
+ name: framework,
124
+ };
125
+ }
126
+ else {
127
+ jsonConfigData.framework.name = framework;
128
+ }
129
+ return [3 /*break*/, 6];
130
+ case 5:
131
+ jsonConfigData = {
132
+ framework: {
133
+ name: framework,
134
+ },
135
+ };
136
+ _a.label = 6;
137
+ case 6:
138
+ fs__default.writeFileSync(jsonConfigPath, JSON.stringify(jsonConfigData, null, 2), 'utf8');
139
+ return [2 /*return*/, true];
140
+ }
141
+ });
142
+ });
143
+ };
144
+ Init.prototype.initRopConfig = function (accessKey) {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var ropConfigDirPath, ropConfigPath, ropConfigData;
147
+ return __generator(this, function (_a) {
148
+ ropConfigDirPath = path.join('.', '.rop');
149
+ if (!fs__default.existsSync(ropConfigDirPath)) {
150
+ fs__default.mkdirSync(ropConfigDirPath);
151
+ }
152
+ ropConfigPath = path.join(ropConfigDirPath, 'config.json');
153
+ ropConfigData = {};
154
+ if (fs__default.existsSync(ropConfigPath)) {
155
+ ropConfigData = JSON.parse(fs__default.readFileSync(ropConfigPath, 'utf8'));
156
+ }
157
+ ropConfigData.accessKey = accessKey;
158
+ fs__default.writeFileSync(ropConfigPath, JSON.stringify(ropConfigData, null, 2), 'utf8');
159
+ return [2 /*return*/];
160
+ });
161
+ });
162
+ };
163
+ Init.prototype.initGitignore = function () {
164
+ return __awaiter(this, void 0, void 0, function () {
165
+ var gitignorePath, gitignoreData;
166
+ return __generator(this, function (_a) {
167
+ gitignorePath = path.join('.', '.gitignore');
168
+ if (fs__default.existsSync(gitignorePath)) {
169
+ gitignoreData = fs__default.readFileSync(gitignorePath, 'utf8');
170
+ if (!gitignoreData.includes('.rop')) {
171
+ fs__default.appendFileSync(gitignorePath, "\n.rop", 'utf8');
172
+ }
173
+ }
174
+ else {
175
+ fs__default.writeFileSync(gitignorePath, ".rop", 'utf8');
176
+ }
177
+ return [2 /*return*/];
178
+ });
179
+ });
180
+ };
181
+ return Init;
182
+ }());
183
+
184
+ var frameworkList = ['tauri'];
185
+ var Framework;
186
+ (function (Framework) {
187
+ Framework["TAURI"] = "TAURI";
188
+ Framework["IONIC"] = "IONIC";
189
+ Framework["FLUTTER"] = "FLUTTER";
190
+ })(Framework || (Framework = {}));
191
+
192
+ function getWorkspaceDir(dir) {
193
+ var _a;
194
+ var rootPath = dir;
195
+ while (dir.length && dir[dir.length - 1] !== sep) {
196
+ var manifestPath = join(dir, 'Cargo.toml');
197
+ if (existsSync(manifestPath)) {
198
+ var toml = parse(readFileSync(manifestPath).toString());
199
+ if ((_a = toml.workspace) === null || _a === void 0 ? void 0 : _a.members) {
200
+ var ignore = ['**/target', '**/node_modules'];
201
+ if (toml.workspace.exclude)
202
+ ignore.push.apply(ignore, toml.workspace.exclude);
203
+ var memberPaths = globbySync(toml.workspace.members, {
204
+ cwd: dir,
205
+ ignore: ignore,
206
+ expandDirectories: false,
207
+ onlyFiles: false,
208
+ });
209
+ if (memberPaths.some(function (m) { return resolve(dir, m) === rootPath; })) {
210
+ return dir;
211
+ }
212
+ }
213
+ }
214
+ dir = normalize(join(dir, '..'));
215
+ }
216
+ return null;
217
+ }
218
+ function getTauriDir(root) {
219
+ var tauriConfPaths = globbySync(['**/tauri.conf.json', '**/tauri.conf.json5', '**/Tauri.toml'], {
220
+ gitignore: true,
221
+ cwd: root,
222
+ // Forcefully ignore target and node_modules dirs
223
+ ignore: ['**/target', '**/node_modules'],
224
+ });
225
+ if (tauriConfPaths.length === 0) {
226
+ return null;
227
+ }
228
+ return resolve(root, tauriConfPaths[0], '..');
229
+ }
230
+ function getCargoManifest(dir) {
231
+ var _a, _b, _c, _d;
232
+ var manifestPath = join(dir, 'Cargo.toml');
233
+ var cargoManifest = parse(readFileSync(manifestPath).toString());
234
+ var name = cargoManifest.package.name;
235
+ var version = cargoManifest.package.version;
236
+ // if the version or name is an object, it means it is a workspace package and we need to traverse up
237
+ if (typeof cargoManifest.package.version == 'object' || typeof cargoManifest.package.name == 'object') {
238
+ var workspaceDir = getWorkspaceDir(dir);
239
+ if (!workspaceDir) {
240
+ throw new Error('Could not find workspace directory, but version and/or name specifies to use workspace package');
241
+ }
242
+ var manifestPath_1 = join(workspaceDir, 'Cargo.toml');
243
+ var workspaceManifest = parse(readFileSync(manifestPath_1).toString());
244
+ if (typeof name === 'object' && ((_b = (_a = workspaceManifest === null || workspaceManifest === void 0 ? void 0 : workspaceManifest.workspace) === null || _a === void 0 ? void 0 : _a.package) === null || _b === void 0 ? void 0 : _b.name) !== undefined) {
245
+ name = workspaceManifest.workspace.package.name;
246
+ }
247
+ if (typeof version === 'object' && ((_d = (_c = workspaceManifest === null || workspaceManifest === void 0 ? void 0 : workspaceManifest.workspace) === null || _c === void 0 ? void 0 : _c.package) === null || _d === void 0 ? void 0 : _d.version) !== undefined) {
248
+ version = workspaceManifest.workspace.package.version;
249
+ }
250
+ }
251
+ return __assign(__assign({}, cargoManifest), { package: __assign(__assign({}, cargoManifest.package), { name: name, version: version }) });
252
+ }
253
+
254
+ function _tryParseJsonConfig(contents) {
255
+ try {
256
+ var config = JSON.parse(contents);
257
+ return config;
258
+ }
259
+ catch (e) {
260
+ // @ts-ignore
261
+ var msg = e.message;
262
+ console.error("Couldn't parse --config flag as inline JSON. This is not an error if it's a file path. Source: \"".concat(msg, "\""));
263
+ return null;
264
+ }
265
+ }
266
+ function _tryParseJson5Config(contents) {
267
+ try {
268
+ var config = JSON5.parse(contents);
269
+ return config;
270
+ }
271
+ catch (e) {
272
+ // @ts-ignore
273
+ var msg = e.message;
274
+ console.error("Couldn't parse --config flag as inline JSON. This is not an error if it's a file path. Source: \"".concat(msg, "\""));
275
+ return null;
276
+ }
277
+ }
278
+ function _tryParseTomlConfig(contents) {
279
+ try {
280
+ var config = parse(contents);
281
+ return config;
282
+ }
283
+ catch (e) {
284
+ // @ts-ignore
285
+ var msg = e.message;
286
+ console.error("Couldn't parse --config flag as inline JSON. This is not an error if it's a file path. Source: \"".concat(msg, "\""));
287
+ return null;
288
+ }
289
+ }
290
+ var TauriConfig = /** @class */ (function () {
291
+ function TauriConfig(identifier) {
292
+ this.identifier = identifier;
293
+ }
294
+ TauriConfig.fromBaseConfig = function (tauriDir) {
295
+ console.log(tauriDir);
296
+ if (existsSync(join(tauriDir, 'tauri.conf.json'))) {
297
+ var contents = readFileSync(join(tauriDir, 'tauri.conf.json')).toString();
298
+ var config = _tryParseJsonConfig(contents);
299
+ if (config) {
300
+ if ('identifier' in config) {
301
+ return this.fromV2Base(config);
302
+ }
303
+ else {
304
+ return this.fromV1Base(config);
305
+ }
306
+ }
307
+ console.error("Found tauri.conf.json file but couldn't parse it as JSON.");
308
+ }
309
+ if (existsSync(join(tauriDir, 'tauri.conf.json5'))) {
310
+ var contents = readFileSync(join(tauriDir, 'tauri.conf.json5')).toString();
311
+ var config = _tryParseJson5Config(contents);
312
+ if (config) {
313
+ if ('identifier' in config) {
314
+ return this.fromV2Base(config);
315
+ }
316
+ else {
317
+ return this.fromV1Base(config);
318
+ }
319
+ }
320
+ console.error("Found tauri.conf.json5 file but couldn't parse it as JSON5.");
321
+ }
322
+ if (existsSync(join(tauriDir, 'Tauri.toml'))) {
323
+ var contents = readFileSync(join(tauriDir, 'Tauri.toml')).toString();
324
+ var config = _tryParseTomlConfig(contents);
325
+ if (config) {
326
+ if ('identifier' in config) {
327
+ return this.fromV2Base(config);
328
+ }
329
+ else {
330
+ return this.fromV1Base(config);
331
+ }
332
+ }
333
+ console.error("Found Tauri.toml file but couldn't parse it as TOML.");
334
+ }
335
+ throw new Error("Couldn't locate or parse tauri config.");
336
+ };
337
+ TauriConfig.fromV1Base = function (config) {
338
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
339
+ if (!((_b = (_a = config.tauri) === null || _a === void 0 ? void 0 : _a.bundle) === null || _b === void 0 ? void 0 : _b.identifier)) {
340
+ throw Error('base config has no bundle identifier.');
341
+ }
342
+ var c = new TauriConfig((_d = (_c = config.tauri) === null || _c === void 0 ? void 0 : _c.bundle) === null || _d === void 0 ? void 0 : _d.identifier);
343
+ c.productName = (_e = config.package) === null || _e === void 0 ? void 0 : _e.productName;
344
+ c.version = (_f = config.package) === null || _f === void 0 ? void 0 : _f.version;
345
+ c.frontendDist = (_g = config.build) === null || _g === void 0 ? void 0 : _g.distDir;
346
+ c.beforeBuildCommand = (_h = config.build) === null || _h === void 0 ? void 0 : _h.beforeBuildCommand;
347
+ c.rpmRelease = (_j = config.tauri.bundle.rpm) === null || _j === void 0 ? void 0 : _j.release;
348
+ c.wixLanguage = (_l = (_k = config.tauri.bundle.windows) === null || _k === void 0 ? void 0 : _k.wix) === null || _l === void 0 ? void 0 : _l.language;
349
+ return c;
350
+ };
351
+ TauriConfig.fromV2Base = function (config) {
352
+ var _a, _b, _c, _d, _e, _f, _g, _h;
353
+ if (!config.identifier) {
354
+ throw Error('base config has no bundle identifier.');
355
+ }
356
+ var c = new TauriConfig(config.identifier);
357
+ c.productName = config.productName;
358
+ c.version = config.version;
359
+ c.frontendDist = (_a = config.build) === null || _a === void 0 ? void 0 : _a.frontendDist;
360
+ c.beforeBuildCommand = (_b = config.build) === null || _b === void 0 ? void 0 : _b.beforeBuildCommand;
361
+ c.rpmRelease = (_e = (_d = (_c = config.bundle) === null || _c === void 0 ? void 0 : _c.linux) === null || _d === void 0 ? void 0 : _d.rpm) === null || _e === void 0 ? void 0 : _e.release;
362
+ c.wixLanguage = (_h = (_g = (_f = config.bundle) === null || _f === void 0 ? void 0 : _f.windows) === null || _g === void 0 ? void 0 : _g.wix) === null || _h === void 0 ? void 0 : _h.language;
363
+ return c;
364
+ };
365
+ return TauriConfig;
366
+ }());
367
+
368
+ var getTauriEnv = function (projectPath) {
369
+ var _a;
370
+ var tauriDir = getTauriDir(projectPath !== null && projectPath !== void 0 ? projectPath : '.');
371
+ if (tauriDir !== null) {
372
+ var name_1;
373
+ var version = void 0;
374
+ var wixLanguage = 'en-US';
375
+ var rpmRelease = '1';
376
+ var config = TauriConfig.fromBaseConfig(tauriDir);
377
+ name_1 = config === null || config === void 0 ? void 0 : config.productName;
378
+ if ((_a = config.version) === null || _a === void 0 ? void 0 : _a.endsWith('.json')) {
379
+ var packageJsonPath = join(tauriDir, config === null || config === void 0 ? void 0 : config.version);
380
+ var contents = readFileSync(packageJsonPath).toString();
381
+ version = JSON.parse(contents).version;
382
+ }
383
+ else {
384
+ version = config === null || config === void 0 ? void 0 : config.version;
385
+ }
386
+ if (!(name_1 && version)) {
387
+ var cargoManifest = getCargoManifest(tauriDir);
388
+ name_1 = name_1 !== null && name_1 !== void 0 ? name_1 : cargoManifest.package.name;
389
+ version = version !== null && version !== void 0 ? version : cargoManifest.package.version;
390
+ }
391
+ if (!(name_1 && version)) {
392
+ console.error('Could not determine package name and version.');
393
+ process.exit(1);
394
+ }
395
+ var wixAppVersion = version.replace(/[-+]/g, '.');
396
+ if (config.wixLanguage) {
397
+ wixLanguage = config.wixLanguage;
398
+ }
399
+ if (config.rpmRelease) {
400
+ rpmRelease = config.rpmRelease;
401
+ }
402
+ return {
403
+ basePath: tauriDir + '/target',
404
+ appName: name_1,
405
+ version: version,
406
+ wixLanguage: wixLanguage,
407
+ wixAppVersion: wixAppVersion,
408
+ rpmRelease: rpmRelease,
409
+ };
410
+ }
411
+ else {
412
+ // This should not actually happen.
413
+ throw Error("Couldn't detect Tauri dir");
414
+ }
415
+ };
416
+
417
+ var tauri = {
418
+ macos: {
419
+ x64: {
420
+ target: {
421
+ path: "${basePath}/x86_64-apple-darwin/release/bundle/dmg/${appName}_${version}_${arch}.dmg"
422
+ },
423
+ update: {
424
+ path: "${basePath}/x86_64-apple-darwin/release/bundle/macos/${appName}.app.tar.gz",
425
+ name: "${appName}_${version}_${arch}.app.tar.gz"
426
+ },
427
+ sig: {
428
+ path: "${basePath}/x86_64-apple-darwin/release/bundle/macos/${appName}.app.tar.gz.sig"
429
+ }
430
+ },
431
+ arm64: {
432
+ target: {
433
+ path: "${basePath}/aarch64-apple-darwin/release/bundle/dmg/${appName}_${version}_arrch64.dmg"
434
+ },
435
+ update: {
436
+ path: "${basePath}/aarch64-apple-darwin/release/bundle/macos/${appName}.app.tar.gz",
437
+ name: "${appName}_${version}_${arch}.app.tar.gz"
438
+ },
439
+ sig: {
440
+ path: "${basePath}/aarch64-apple-darwin/release/bundle/macos/${appName}.app.tar.gz.sig"
441
+ }
442
+ }
443
+ }
444
+ };
445
+ var platformDefaultConfigMap = {
446
+ tauri: tauri
447
+ };
448
+
449
+ var getBranch = function () { return __awaiter(void 0, void 0, void 0, function () {
450
+ return __generator(this, function (_a) {
451
+ return [2 /*return*/, new Promise(function (resolve) {
452
+ return exec('git rev-parse --abbrev-ref HEAD', function (err, stdout, stderr) {
453
+ if (err) {
454
+ console.log("getBranch Error: ".concat(err));
455
+ }
456
+ else if (stdout) {
457
+ resolve(stdout.trim());
458
+ return;
459
+ }
460
+ resolve(undefined);
461
+ });
462
+ })];
463
+ });
464
+ }); };
465
+ // 读取 YAML 文件并替换模版变量
466
+ function loadJsonWithEnv(_a) {
467
+ var _b, _c, _d, _e, _f;
468
+ var platform = _a.platform, arch = _a.arch, optionAccessKey = _a.optionAccessKey;
469
+ var configFilePath = path.join('.', 'rop.json');
470
+ var configData = fs__default.readFileSync(configFilePath, 'utf8');
471
+ var inputConfig = JSON.parse(configData);
472
+ var _arch;
473
+ if (arch == 'x86_64') {
474
+ _arch = 'x64';
475
+ }
476
+ else if (arch) {
477
+ _arch = arch;
478
+ }
479
+ else {
480
+ _arch = 'x64';
481
+ }
482
+ var env = {
483
+ arch: _arch,
484
+ };
485
+ var config;
486
+ if (!inputConfig.platforms) {
487
+ if (((_b = inputConfig.framework) === null || _b === void 0 ? void 0 : _b.name) && frameworkList.includes((_c = inputConfig.framework) === null || _c === void 0 ? void 0 : _c.name)) {
488
+ var framework = inputConfig.framework.name;
489
+ var defaultPlatformConfig = platformDefaultConfigMap[framework];
490
+ config = __assign(__assign({}, inputConfig), { platforms: defaultPlatformConfig });
491
+ }
492
+ }
493
+ else {
494
+ config = inputConfig;
495
+ }
496
+ var _autoEnv = getAutoEnvByFramework((_d = config.framework) === null || _d === void 0 ? void 0 : _d.name, config.projectPath);
497
+ env = __assign(__assign(__assign({}, env), (_autoEnv !== null && _autoEnv !== void 0 ? _autoEnv : {})), ((_f = (_e = config.framework) === null || _e === void 0 ? void 0 : _e.env) !== null && _f !== void 0 ? _f : {}));
498
+ // 确保配置文件中存在 host 属性
499
+ if (!env.version) {
500
+ throw new Error('Missing "version" property in the configuration file.');
501
+ }
502
+ // 递归函数来替换对象中的字符串值
503
+ function replaceEnvInObject(obj) {
504
+ Object.keys(obj).forEach(function (key) {
505
+ if (typeof obj[key] === 'string') {
506
+ // 使用正则表达式替换模版变量(这里假设模版变量是 ${VAR} 格式的)
507
+ // @ts-ignore
508
+ obj[key] = obj[key].replace(/\$\{(\w+)\}/g, function (_, envVar) { return env[envVar] || ''; });
509
+ }
510
+ else if (typeof obj[key] === 'object' && obj[key] !== null) {
511
+ replaceEnvInObject(obj[key]); // 递归处理嵌套对象
512
+ }
513
+ });
514
+ }
515
+ replaceEnvInObject(config);
516
+ var targetInfo = config.platforms[platform]
517
+ ? config.platforms[platform][_arch]
518
+ : undefined;
519
+ if (!targetInfo) {
520
+ throw new Error('not find target file');
521
+ }
522
+ console.log('find target file:');
523
+ console.log(targetInfo);
524
+ var accessKey = getAccessKey(optionAccessKey);
525
+ return { config: config, targetInfo: targetInfo, accessKey: accessKey, version: env.version };
526
+ }
527
+ var getAccessKey = function (accessKey) {
528
+ if (accessKey) {
529
+ return accessKey;
530
+ }
531
+ var configDataStr = fs__default.readFileSync('.rop/config.json', 'utf8');
532
+ var configData = JSON.parse(configDataStr);
533
+ if (configData.accessKey) {
534
+ return configData.accessKey;
535
+ }
536
+ throw new Error('missing access-key');
537
+ };
538
+ var getAutoEnvByFramework = function (framework, projectPath) {
539
+ if (!framework) {
540
+ return undefined;
541
+ }
542
+ switch (framework.toLowerCase()) {
543
+ case Framework.TAURI.toLowerCase():
544
+ return getTauriEnv(projectPath);
545
+ default:
546
+ return undefined;
547
+ }
548
+ };
549
+
550
+ var RopUpload = /** @class */ (function () {
551
+ function RopUpload(_a) {
552
+ var baseDomain = _a.baseDomain;
553
+ var _this = this;
554
+ this.checkParams = function (config) { return __awaiter(_this, void 0, void 0, function () {
555
+ var branch, _a;
556
+ var _b;
557
+ return __generator(this, function (_c) {
558
+ switch (_c.label) {
559
+ case 0:
560
+ if (!config.accessKey) {
561
+ throw new Error('accessKey not null');
562
+ }
563
+ if (!((_b = config.branch) !== null && _b !== void 0)) return [3 /*break*/, 1];
564
+ _a = _b;
565
+ return [3 /*break*/, 3];
566
+ case 1: return [4 /*yield*/, getBranch()];
567
+ case 2:
568
+ _a = (_c.sent());
569
+ _c.label = 3;
570
+ case 3:
571
+ branch = _a;
572
+ if (!branch) {
573
+ throw new Error('branch not null');
574
+ }
575
+ else {
576
+ config.branch = branch;
577
+ }
578
+ if (!config.platform) {
579
+ throw new Error('platform not null');
580
+ }
581
+ if (!config.arch) {
582
+ throw new Error('arch not null');
583
+ }
584
+ if (!config.targetPath) {
585
+ throw new Error('target path not null');
586
+ }
587
+ if (!fs$1.existsSync(config.targetPath)) {
588
+ throw new Error("target file not found, file path: ".concat(config.targetPath));
589
+ }
590
+ if (config.updatePath && !fs$1.existsSync(config.updatePath)) {
591
+ throw new Error("update file not found\uFF0C file path: ".concat(config.updatePath));
592
+ }
593
+ if (!config.version) {
594
+ throw new Error('version not null');
595
+ }
596
+ return [2 /*return*/, config];
597
+ }
598
+ });
599
+ }); };
600
+ this.baseDomain = baseDomain;
601
+ }
602
+ RopUpload.prototype.upload = function (_config) {
603
+ return __awaiter(this, void 0, void 0, function () {
604
+ var config, formData, fileStream, multibar, bar1, totalUploaded, updateFileStream, bar2_1, totalUpdateUploaded_1, res, body;
605
+ var _a, _b;
606
+ return __generator(this, function (_c) {
607
+ switch (_c.label) {
608
+ case 0: return [4 /*yield*/, this.checkParams(_config)];
609
+ case 1:
610
+ config = _c.sent();
611
+ formData = new FormData();
612
+ fileStream = fs$1.createReadStream(config.targetPath);
613
+ multibar = new cliProgress.MultiBar({
614
+ format: 'Upload Progress |' + colors.cyan('{bar}') + '| {percentage}% | {filename}',
615
+ barCompleteChar: '\u2588',
616
+ barIncompleteChar: '\u2591',
617
+ hideCursor: true,
618
+ });
619
+ bar1 = multibar.create(fs$1.statSync(config.targetPath).size, 0);
620
+ totalUploaded = 0;
621
+ fileStream.on('data', function (chunk) {
622
+ totalUploaded += chunk.length;
623
+ bar1.update(totalUploaded, { filename: path$1.basename(config.targetPath) });
624
+ });
625
+ formData.append('targetFile', fileStream, {
626
+ filename: (_a = config.targetName) !== null && _a !== void 0 ? _a : path$1.basename(config.targetPath),
627
+ });
628
+ if (config.updatePath) {
629
+ bar2_1 = multibar.create(fs$1.statSync(config.updatePath).size, 0);
630
+ updateFileStream = fs$1.createReadStream(config.updatePath);
631
+ totalUpdateUploaded_1 = 0;
632
+ updateFileStream.on('data', function (chunk) {
633
+ totalUpdateUploaded_1 += chunk.length;
634
+ bar2_1.update(totalUpdateUploaded_1, { filename: path$1.basename(config.updatePath) });
635
+ });
636
+ formData.append('updateFile', updateFileStream, {
637
+ filename: (_b = config.updateName) !== null && _b !== void 0 ? _b : path$1.basename(config.updatePath),
638
+ });
639
+ }
640
+ formData.append('accessKey', config.accessKey);
641
+ formData.append('branch', config.branch);
642
+ formData.append('version', config.version);
643
+ formData.append('platform', config.platform);
644
+ formData.append('arch', config.arch);
645
+ if (config.signature) {
646
+ formData.append('signature', config.signature);
647
+ }
648
+ return [4 /*yield*/, fetch("".concat(this.baseDomain, "/api/upload"), {
649
+ method: 'POST',
650
+ headers: formData.getHeaders(),
651
+ body: formData,
652
+ })];
653
+ case 2:
654
+ res = _c.sent();
655
+ return [4 /*yield*/, res.json()];
656
+ case 3:
657
+ body = _c.sent();
658
+ if (!res.ok) {
659
+ console.log(res.status, res.statusText, body);
660
+ }
661
+ multibar.stop();
662
+ fileStream.destroy();
663
+ updateFileStream === null || updateFileStream === void 0 ? void 0 : updateFileStream.destroy();
664
+ return [2 /*return*/, { success: res.ok, body: body }];
665
+ }
666
+ });
667
+ });
668
+ };
669
+ return RopUpload;
670
+ }());
671
+
672
+ var program = new Command();
673
+ program.name('rop').description('CLI to some JavaScript string utilities').version('0.1.0');
674
+ program
675
+ .command('init')
676
+ .description('init rop')
677
+ .action(function () { return __awaiter(void 0, void 0, void 0, function () {
678
+ var accessKey, framework, init;
679
+ return __generator(this, function (_a) {
680
+ switch (_a.label) {
681
+ case 0:
682
+ accessKey = '';
683
+ _a.label = 1;
684
+ case 1:
685
+ if (!!accessKey) return [3 /*break*/, 6];
686
+ _a.label = 2;
687
+ case 2:
688
+ _a.trys.push([2, 4, , 5]);
689
+ return [4 /*yield*/, input({ message: "Please enter ".concat(colors.cyan('access-key')) })];
690
+ case 3:
691
+ accessKey = _a.sent();
692
+ return [3 /*break*/, 5];
693
+ case 4:
694
+ _a.sent();
695
+ console.log('cancel');
696
+ return [2 /*return*/];
697
+ case 5: return [3 /*break*/, 1];
698
+ case 6: return [4 /*yield*/, select({
699
+ message: "Please Select ".concat(colors.cyan('framework')),
700
+ choices: [
701
+ {
702
+ name: 'tauri',
703
+ value: 'tauri',
704
+ },
705
+ ],
706
+ })];
707
+ case 7:
708
+ framework = _a.sent();
709
+ init = new Init();
710
+ return [4 /*yield*/, init.initProjectConfigJson(framework)];
711
+ case 8:
712
+ if (!_a.sent()) return [3 /*break*/, 11];
713
+ return [4 /*yield*/, init.initRopConfig(accessKey)];
714
+ case 9:
715
+ _a.sent();
716
+ return [4 /*yield*/, init.initGitignore()];
717
+ case 10:
718
+ _a.sent();
719
+ console.log('done');
720
+ _a.label = 11;
721
+ case 11: return [2 /*return*/];
722
+ }
723
+ });
724
+ }); });
725
+ program
726
+ .command('set-key')
727
+ .description('set access-key')
728
+ .argument('<string>', 'projectAccessKey')
729
+ .action(function (projectAccessKey) { return __awaiter(void 0, void 0, void 0, function () {
730
+ var array, projectName, accessKey, homedir, filePath, content, configData;
731
+ return __generator(this, function (_a) {
732
+ try {
733
+ array = projectAccessKey.split('=');
734
+ if (array.length != 2) {
735
+ throw new Error('parameter format error');
736
+ }
737
+ projectName = array[0], accessKey = array[1];
738
+ homedir = path.join('.', '.rop');
739
+ if (!fs.existsSync(homedir)) {
740
+ fs.mkdirSync(homedir);
741
+ }
742
+ filePath = "".concat(homedir, "/config.json");
743
+ content = {
744
+ accessKey: accessKey,
745
+ };
746
+ if (!fs.existsSync(filePath)) {
747
+ fs.writeFileSync(filePath, JSON.stringify(content, null, 2), 'utf8');
748
+ }
749
+ else {
750
+ configData = fs.readFileSync(filePath, 'utf8');
751
+ content = JSON.parse(configData);
752
+ content.accessKey = accessKey;
753
+ fs.writeFileSync(filePath, JSON.stringify(content, null, 2), 'utf8');
754
+ }
755
+ }
756
+ catch (error) {
757
+ console.error('Error reading configuration file or missing required properties:', error);
758
+ }
759
+ return [2 /*return*/];
760
+ });
761
+ }); });
762
+ program
763
+ .command('upload')
764
+ .description('Upload some framework build')
765
+ .argument('<string>', 'platform')
766
+ .argument('[string]', 'arch')
767
+ .option('-a, --accessKey <string>', 'accessKey path')
768
+ .action(function (platform, arch, options) { return __awaiter(void 0, void 0, void 0, function () {
769
+ var _a, config, targetInfo, accessKey, version, signature, uploadClient;
770
+ var _b, _c, _d, _e, _f;
771
+ return __generator(this, function (_g) {
772
+ switch (_g.label) {
773
+ case 0:
774
+ _a = loadJsonWithEnv({
775
+ platform: platform,
776
+ arch: arch,
777
+ optionAccessKey: options.accessKey,
778
+ }), config = _a.config, targetInfo = _a.targetInfo, accessKey = _a.accessKey, version = _a.version;
779
+ if (!targetInfo) {
780
+ throw new Error('Missing "targetInfo" property in the configuration file.');
781
+ }
782
+ if ((_b = targetInfo.sig) === null || _b === void 0 ? void 0 : _b.path) {
783
+ if (fs.existsSync(targetInfo.sig.path)) {
784
+ signature = fs.readFileSync(targetInfo.sig.path, 'utf8');
785
+ }
786
+ else {
787
+ console.warn('Warn sig file not found');
788
+ }
789
+ }
790
+ uploadClient = new RopUpload({ baseDomain: (_c = config.host) !== null && _c !== void 0 ? _c : 'http://localhost:3000' });
791
+ return [4 /*yield*/, uploadClient.upload({
792
+ accessKey: accessKey,
793
+ arch: arch,
794
+ platform: platform,
795
+ version: version,
796
+ targetPath: targetInfo.target.path,
797
+ targetName: (_d = targetInfo.target) === null || _d === void 0 ? void 0 : _d.name,
798
+ updatePath: (_e = targetInfo.update) === null || _e === void 0 ? void 0 : _e.path,
799
+ updateName: (_f = targetInfo.update) === null || _f === void 0 ? void 0 : _f.name,
800
+ signature: signature,
801
+ })];
802
+ case 1:
803
+ _g.sent();
804
+ return [2 /*return*/];
805
+ }
806
+ });
807
+ }); });
808
+ // 解析命令行参数
809
+ program.parse();
810
+ // // // 如果没有指定任何命令,则显示帮助信息
811
+ // if (process.argv.length <= 2) {
812
+ // program.help();
813
+ // }