@sevenvip666/rop 0.0.8 → 0.1.0
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/bin/index.mjs +1318 -0
- package/dist/cjs/index.js +523 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.js +370 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/types/flutter/index.d.ts +6 -0
- package/dist/types/ionic/index.d.ts +6 -0
- package/dist/types/types.d.ts +69 -6
- package/dist/types/{upload.d.ts → uploadApi.d.ts} +1 -2
- package/dist/types/utils.d.ts +14 -134
- package/package.json +7 -12
- package/dist/bin/rop.mjs +0 -767
- package/dist/cjs/upload.js +0 -223
- package/dist/cjs/upload.js.map +0 -1
- package/dist/esm/upload.js +0 -166
- package/dist/esm/upload.js.map +0 -1
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var fetch$1 = require('node-fetch');
|
|
5
|
+
var FormData = require('form-data');
|
|
6
|
+
var cliProgress = require('cli-progress');
|
|
7
|
+
var colors = require('ansi-colors');
|
|
8
|
+
var path = require('path');
|
|
9
|
+
var child_process = require('child_process');
|
|
10
|
+
var path$1 = require('node:path');
|
|
11
|
+
var fs$1 = require('node:fs');
|
|
12
|
+
require('@iarna/toml');
|
|
13
|
+
require('globby');
|
|
14
|
+
require('json5');
|
|
15
|
+
require('js-yaml');
|
|
16
|
+
var prompts = require('@inquirer/prompts');
|
|
17
|
+
var os = require('node:os');
|
|
18
|
+
|
|
19
|
+
function _interopNamespaceDefault(e) {
|
|
20
|
+
var n = Object.create(null);
|
|
21
|
+
if (e) {
|
|
22
|
+
Object.keys(e).forEach(function (k) {
|
|
23
|
+
if (k !== 'default') {
|
|
24
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
25
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () { return e[k]; }
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
n.default = e;
|
|
33
|
+
return Object.freeze(n);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var os__namespace = /*#__PURE__*/_interopNamespaceDefault(os);
|
|
37
|
+
|
|
38
|
+
/******************************************************************************
|
|
39
|
+
Copyright (c) Microsoft Corporation.
|
|
40
|
+
|
|
41
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
42
|
+
purpose with or without fee is hereby granted.
|
|
43
|
+
|
|
44
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
45
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
46
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
47
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
48
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
49
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
50
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
51
|
+
***************************************************************************** */
|
|
52
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
56
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
57
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
58
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
59
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
60
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
61
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function __generator(thisArg, body) {
|
|
66
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
67
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
68
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
69
|
+
function step(op) {
|
|
70
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
71
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
72
|
+
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;
|
|
73
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
74
|
+
switch (op[0]) {
|
|
75
|
+
case 0: case 1: t = op; break;
|
|
76
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
77
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
78
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
79
|
+
default:
|
|
80
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
81
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
82
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
83
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
84
|
+
if (t[2]) _.ops.pop();
|
|
85
|
+
_.trys.pop(); continue;
|
|
86
|
+
}
|
|
87
|
+
op = body.call(thisArg, _);
|
|
88
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
89
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
94
|
+
var e = new Error(message);
|
|
95
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
var frameworkList = ['tauri', 'ionic', 'flutter'];
|
|
99
|
+
exports.Framework = void 0;
|
|
100
|
+
(function (Framework) {
|
|
101
|
+
Framework["TAURI"] = "tauri";
|
|
102
|
+
Framework["IONIC"] = "ionic";
|
|
103
|
+
Framework["FLUTTER"] = "flutter";
|
|
104
|
+
})(exports.Framework || (exports.Framework = {}));
|
|
105
|
+
|
|
106
|
+
var getBranch = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
107
|
+
return __generator(this, function (_a) {
|
|
108
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
109
|
+
return child_process.exec('git rev-parse --abbrev-ref HEAD', function (err, stdout, stderr) {
|
|
110
|
+
if (err) {
|
|
111
|
+
console.log("getBranch Error: ".concat(err));
|
|
112
|
+
}
|
|
113
|
+
else if (stdout) {
|
|
114
|
+
resolve(stdout.trim());
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
resolve('none');
|
|
118
|
+
});
|
|
119
|
+
})];
|
|
120
|
+
});
|
|
121
|
+
}); };
|
|
122
|
+
|
|
123
|
+
var RopUploadApi = /** @class */ (function () {
|
|
124
|
+
function RopUploadApi(_a) {
|
|
125
|
+
var baseDomain = _a.baseDomain;
|
|
126
|
+
var _this = this;
|
|
127
|
+
this.checkParams = function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
128
|
+
var branch, _a;
|
|
129
|
+
var _b;
|
|
130
|
+
return __generator(this, function (_c) {
|
|
131
|
+
switch (_c.label) {
|
|
132
|
+
case 0:
|
|
133
|
+
if (!config.accessKey) {
|
|
134
|
+
if (!config.token || !config.projectId) {
|
|
135
|
+
throw new Error('accessKey, token, projectId cannot be empty at the same time');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!((_b = config.branch) !== null && _b !== void 0)) return [3 /*break*/, 1];
|
|
139
|
+
_a = _b;
|
|
140
|
+
return [3 /*break*/, 3];
|
|
141
|
+
case 1: return [4 /*yield*/, getBranch()];
|
|
142
|
+
case 2:
|
|
143
|
+
_a = (_c.sent());
|
|
144
|
+
_c.label = 3;
|
|
145
|
+
case 3:
|
|
146
|
+
branch = _a;
|
|
147
|
+
if (!branch) {
|
|
148
|
+
throw new Error('branch not null');
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
config.branch = branch;
|
|
152
|
+
}
|
|
153
|
+
if (!config.platform) {
|
|
154
|
+
throw new Error('platform not null');
|
|
155
|
+
}
|
|
156
|
+
if (!config.arch) {
|
|
157
|
+
throw new Error('arch not null');
|
|
158
|
+
}
|
|
159
|
+
if (!config.targetPath) {
|
|
160
|
+
throw new Error('target path not null');
|
|
161
|
+
}
|
|
162
|
+
if (!fs.existsSync(config.targetPath)) {
|
|
163
|
+
throw new Error("target file not found, file path: ".concat(config.targetPath));
|
|
164
|
+
}
|
|
165
|
+
if (config.updatePath && !fs.existsSync(config.updatePath)) {
|
|
166
|
+
throw new Error("update file not found\uFF0C file path: ".concat(config.updatePath));
|
|
167
|
+
}
|
|
168
|
+
if (!config.version) {
|
|
169
|
+
throw new Error('version not null');
|
|
170
|
+
}
|
|
171
|
+
return [2 /*return*/, config];
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}); };
|
|
175
|
+
this.baseDomain = baseDomain;
|
|
176
|
+
}
|
|
177
|
+
RopUploadApi.prototype.upload = function (_config) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
179
|
+
var config, formData, fileStream, multibar, bar1, totalUploaded, updateFileStream, bar2_1, totalUpdateUploaded_1, res, body;
|
|
180
|
+
var _a, _b;
|
|
181
|
+
return __generator(this, function (_c) {
|
|
182
|
+
switch (_c.label) {
|
|
183
|
+
case 0: return [4 /*yield*/, this.checkParams(_config)];
|
|
184
|
+
case 1:
|
|
185
|
+
config = _c.sent();
|
|
186
|
+
formData = new FormData();
|
|
187
|
+
fileStream = fs.createReadStream(config.targetPath);
|
|
188
|
+
multibar = new cliProgress.MultiBar({
|
|
189
|
+
format: 'Upload Progress |' + colors.cyan('{bar}') + '| {percentage}% | {filename}',
|
|
190
|
+
barCompleteChar: '\u2588',
|
|
191
|
+
barIncompleteChar: '\u2591',
|
|
192
|
+
hideCursor: true,
|
|
193
|
+
});
|
|
194
|
+
bar1 = multibar.create(fs.statSync(config.targetPath).size, 0);
|
|
195
|
+
totalUploaded = 0;
|
|
196
|
+
fileStream.on('data', function (chunk) {
|
|
197
|
+
totalUploaded += chunk.length;
|
|
198
|
+
bar1.update(totalUploaded, { filename: path.basename(config.targetPath) });
|
|
199
|
+
});
|
|
200
|
+
formData.append('targetFile', fileStream, {
|
|
201
|
+
filename: (_a = config.targetName) !== null && _a !== void 0 ? _a : path.basename(config.targetPath),
|
|
202
|
+
});
|
|
203
|
+
if (config.updatePath) {
|
|
204
|
+
bar2_1 = multibar.create(fs.statSync(config.updatePath).size, 0);
|
|
205
|
+
updateFileStream = fs.createReadStream(config.updatePath);
|
|
206
|
+
totalUpdateUploaded_1 = 0;
|
|
207
|
+
updateFileStream.on('data', function (chunk) {
|
|
208
|
+
totalUpdateUploaded_1 += chunk.length;
|
|
209
|
+
bar2_1.update(totalUpdateUploaded_1, { filename: path.basename(config.updatePath) });
|
|
210
|
+
});
|
|
211
|
+
formData.append('updateFile', updateFileStream, {
|
|
212
|
+
filename: (_b = config.updateName) !== null && _b !== void 0 ? _b : path.basename(config.updatePath),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (config.accessKey) {
|
|
216
|
+
formData.append('accessKey', config.accessKey);
|
|
217
|
+
}
|
|
218
|
+
if (config.projectId) {
|
|
219
|
+
formData.append('projectId', config.projectId);
|
|
220
|
+
}
|
|
221
|
+
if (config.token) {
|
|
222
|
+
formData.append('token', config.token);
|
|
223
|
+
}
|
|
224
|
+
formData.append('branch', config.branch);
|
|
225
|
+
formData.append('version', config.version);
|
|
226
|
+
formData.append('platform', config.platform);
|
|
227
|
+
formData.append('arch', config.arch);
|
|
228
|
+
if (config.signature) {
|
|
229
|
+
formData.append('signature', config.signature);
|
|
230
|
+
}
|
|
231
|
+
return [4 /*yield*/, fetch$1("".concat(this.baseDomain, "/api/upload"), {
|
|
232
|
+
method: 'POST',
|
|
233
|
+
headers: formData.getHeaders(),
|
|
234
|
+
body: formData,
|
|
235
|
+
})];
|
|
236
|
+
case 2:
|
|
237
|
+
res = _c.sent();
|
|
238
|
+
return [4 /*yield*/, res.json()];
|
|
239
|
+
case 3:
|
|
240
|
+
body = _c.sent();
|
|
241
|
+
if (!res.ok) {
|
|
242
|
+
console.log(res.status, res.statusText, body);
|
|
243
|
+
}
|
|
244
|
+
multibar.stop();
|
|
245
|
+
fileStream.destroy();
|
|
246
|
+
updateFileStream === null || updateFileStream === void 0 ? void 0 : updateFileStream.destroy();
|
|
247
|
+
return [2 /*return*/, { success: res.ok, body: body }];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
return RopUploadApi;
|
|
253
|
+
}());
|
|
254
|
+
|
|
255
|
+
var InitApi = /** @class */ (function () {
|
|
256
|
+
function InitApi() {
|
|
257
|
+
}
|
|
258
|
+
InitApi.prototype.initProjectConfigJson = function (framework) {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
260
|
+
var jsonConfigPath, jsonConfigData, str;
|
|
261
|
+
return __generator(this, function (_a) {
|
|
262
|
+
switch (_a.label) {
|
|
263
|
+
case 0:
|
|
264
|
+
jsonConfigPath = path$1.join('.', 'rop.json');
|
|
265
|
+
if (!fs$1.existsSync(jsonConfigPath)) return [3 /*break*/, 2];
|
|
266
|
+
return [4 /*yield*/, prompts.confirm({
|
|
267
|
+
message: "".concat(colors.cyan('rop.json'), " already exists, does it need to be overwritten?"),
|
|
268
|
+
})];
|
|
269
|
+
case 1:
|
|
270
|
+
_a.sent();
|
|
271
|
+
str = fs$1.readFileSync(jsonConfigPath, 'utf8');
|
|
272
|
+
jsonConfigData = JSON.parse(str);
|
|
273
|
+
if (!jsonConfigData.framework) {
|
|
274
|
+
jsonConfigData.framework = {
|
|
275
|
+
name: framework,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
jsonConfigData.framework.name = framework;
|
|
280
|
+
}
|
|
281
|
+
return [3 /*break*/, 3];
|
|
282
|
+
case 2:
|
|
283
|
+
jsonConfigData = {
|
|
284
|
+
framework: {
|
|
285
|
+
name: framework,
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
_a.label = 3;
|
|
289
|
+
case 3:
|
|
290
|
+
fs$1.writeFileSync(jsonConfigPath, JSON.stringify(jsonConfigData, null, 2), 'utf8');
|
|
291
|
+
return [2 /*return*/];
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
InitApi.prototype.initRopConfig = function (projectId) {
|
|
297
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
298
|
+
var ropConfigDirPath, ropConfigPath, ropConfigData;
|
|
299
|
+
return __generator(this, function (_a) {
|
|
300
|
+
ropConfigDirPath = path$1.join('.', '.rop');
|
|
301
|
+
if (!fs$1.existsSync(ropConfigDirPath)) {
|
|
302
|
+
fs$1.mkdirSync(ropConfigDirPath);
|
|
303
|
+
}
|
|
304
|
+
ropConfigPath = path$1.join(ropConfigDirPath, 'config.json');
|
|
305
|
+
ropConfigData = {};
|
|
306
|
+
if (fs$1.existsSync(ropConfigPath)) {
|
|
307
|
+
ropConfigData = JSON.parse(fs$1.readFileSync(ropConfigPath, 'utf8'));
|
|
308
|
+
}
|
|
309
|
+
ropConfigData.projectId = projectId;
|
|
310
|
+
fs$1.writeFileSync(ropConfigPath, JSON.stringify(ropConfigData, null, 2), 'utf8');
|
|
311
|
+
return [2 /*return*/];
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
InitApi.prototype.initGitignore = function () {
|
|
316
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
317
|
+
var gitignorePath, gitignoreData;
|
|
318
|
+
return __generator(this, function (_a) {
|
|
319
|
+
gitignorePath = path$1.join('.', '.gitignore');
|
|
320
|
+
if (fs$1.existsSync(gitignorePath)) {
|
|
321
|
+
gitignoreData = fs$1.readFileSync(gitignorePath, 'utf8');
|
|
322
|
+
if (!gitignoreData.includes('.rop')) {
|
|
323
|
+
fs$1.appendFileSync(gitignorePath, "\n.rop", 'utf8');
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
fs$1.writeFileSync(gitignorePath, ".rop", 'utf8');
|
|
328
|
+
}
|
|
329
|
+
return [2 /*return*/];
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
return InitApi;
|
|
334
|
+
}());
|
|
335
|
+
|
|
336
|
+
var defaultHost = 'http://rop.coolcat.tech:8100';
|
|
337
|
+
|
|
338
|
+
var LOGIN_API = '/api/auth/login';
|
|
339
|
+
var REFRESH_TOKEN_API = '/api/auth/refresh';
|
|
340
|
+
var CHECK_AND_REFRESH_TOKEN_API = '/api/auth/checkAndRefresh';
|
|
341
|
+
var AuthApi = /** @class */ (function () {
|
|
342
|
+
function AuthApi(_a) {
|
|
343
|
+
var baseDomain = _a.baseDomain;
|
|
344
|
+
this.baseDomain = baseDomain !== null && baseDomain !== void 0 ? baseDomain : defaultHost;
|
|
345
|
+
}
|
|
346
|
+
AuthApi.prototype.login = function (req) {
|
|
347
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
348
|
+
var res, resBody, homedir, userFilePath;
|
|
349
|
+
return __generator(this, function (_a) {
|
|
350
|
+
switch (_a.label) {
|
|
351
|
+
case 0: return [4 /*yield*/, fetch("".concat(this.baseDomain).concat(LOGIN_API), {
|
|
352
|
+
method: 'POST',
|
|
353
|
+
headers: {
|
|
354
|
+
'Content-Type': 'application/json;charset=utf-8',
|
|
355
|
+
},
|
|
356
|
+
body: JSON.stringify(req),
|
|
357
|
+
})];
|
|
358
|
+
case 1:
|
|
359
|
+
res = _a.sent();
|
|
360
|
+
return [4 /*yield*/, res.json()];
|
|
361
|
+
case 2:
|
|
362
|
+
resBody = _a.sent();
|
|
363
|
+
if (!res.ok || !resBody.data) {
|
|
364
|
+
if (!resBody.message || res.status != 400) {
|
|
365
|
+
console.log("Login failed: status=".concat(res.status, ", statusText=").concat(res.statusText));
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
console.log(JSON.stringify(resBody));
|
|
369
|
+
}
|
|
370
|
+
return [2 /*return*/];
|
|
371
|
+
}
|
|
372
|
+
homedir = path$1.join(os__namespace.homedir(), '.rop');
|
|
373
|
+
if (!fs.existsSync(homedir)) {
|
|
374
|
+
fs.mkdirSync(homedir);
|
|
375
|
+
}
|
|
376
|
+
userFilePath = path$1.join(homedir, 'user');
|
|
377
|
+
fs.writeFileSync(userFilePath, JSON.stringify(resBody.data, null, 2), 'utf8');
|
|
378
|
+
return [2 /*return*/];
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
AuthApi.prototype.refreshToken = function () {
|
|
384
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
385
|
+
var homedir, userFilePath, user, res, resBody;
|
|
386
|
+
return __generator(this, function (_a) {
|
|
387
|
+
switch (_a.label) {
|
|
388
|
+
case 0:
|
|
389
|
+
homedir = path$1.join(os__namespace.homedir(), '.rop');
|
|
390
|
+
userFilePath = path$1.join(homedir, 'user');
|
|
391
|
+
if (!fs.existsSync(userFilePath)) {
|
|
392
|
+
console.log('Unauthorized. Please execute the login command');
|
|
393
|
+
return [2 /*return*/];
|
|
394
|
+
}
|
|
395
|
+
user = JSON.parse(fs.readFileSync(userFilePath, 'utf8'));
|
|
396
|
+
return [4 /*yield*/, fetch("".concat(this.baseDomain).concat(REFRESH_TOKEN_API), {
|
|
397
|
+
method: 'POST',
|
|
398
|
+
headers: {
|
|
399
|
+
'Content-Type': 'application/json;charset=utf-8',
|
|
400
|
+
},
|
|
401
|
+
body: JSON.stringify({ token: user.auth.refreshToken }),
|
|
402
|
+
})];
|
|
403
|
+
case 1:
|
|
404
|
+
res = _a.sent();
|
|
405
|
+
return [4 /*yield*/, res.json()];
|
|
406
|
+
case 2:
|
|
407
|
+
resBody = _a.sent();
|
|
408
|
+
if (!res.ok || !resBody.data) {
|
|
409
|
+
if (!resBody.message || res.status != 400) {
|
|
410
|
+
console.log("Refresh token failed: status=".concat(res.status, ", statusText=").concat(res.statusText));
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
console.log('Authorization expired please execute the login command');
|
|
414
|
+
fs.rmSync(userFilePath);
|
|
415
|
+
}
|
|
416
|
+
throw new Error('Refresh token failed');
|
|
417
|
+
}
|
|
418
|
+
user.auth = resBody.data;
|
|
419
|
+
fs.writeFileSync(userFilePath, JSON.stringify(user), 'utf8');
|
|
420
|
+
return [2 /*return*/];
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
AuthApi.prototype.getToken = function () {
|
|
426
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
427
|
+
var homedir, userFilePath;
|
|
428
|
+
return __generator(this, function (_a) {
|
|
429
|
+
homedir = path$1.join(os__namespace.homedir(), '.rop');
|
|
430
|
+
userFilePath = path$1.join(homedir, 'user');
|
|
431
|
+
if (!fs.existsSync(userFilePath)) {
|
|
432
|
+
return [2 /*return*/, undefined];
|
|
433
|
+
}
|
|
434
|
+
return [2 /*return*/, JSON.parse(fs.readFileSync(userFilePath, 'utf8'))];
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
};
|
|
438
|
+
AuthApi.prototype.checkAndRefreshToken = function () {
|
|
439
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
440
|
+
var user, res, resBody, homedir, userFilePath;
|
|
441
|
+
return __generator(this, function (_a) {
|
|
442
|
+
switch (_a.label) {
|
|
443
|
+
case 0: return [4 /*yield*/, this.getToken()];
|
|
444
|
+
case 1:
|
|
445
|
+
user = _a.sent();
|
|
446
|
+
if (!user) {
|
|
447
|
+
return [2 /*return*/, undefined];
|
|
448
|
+
}
|
|
449
|
+
return [4 /*yield*/, fetch("".concat(this.baseDomain).concat(CHECK_AND_REFRESH_TOKEN_API), {
|
|
450
|
+
method: 'POST',
|
|
451
|
+
headers: {
|
|
452
|
+
'Content-Type': 'application/json;charset=utf-8',
|
|
453
|
+
},
|
|
454
|
+
body: JSON.stringify(user.auth),
|
|
455
|
+
})];
|
|
456
|
+
case 2:
|
|
457
|
+
res = _a.sent();
|
|
458
|
+
return [4 /*yield*/, res.json()];
|
|
459
|
+
case 3:
|
|
460
|
+
resBody = _a.sent();
|
|
461
|
+
if (!res.ok || !resBody.data) {
|
|
462
|
+
return [2 /*return*/, undefined];
|
|
463
|
+
}
|
|
464
|
+
homedir = path$1.join(os__namespace.homedir(), '.rop');
|
|
465
|
+
userFilePath = path$1.join(homedir, 'user');
|
|
466
|
+
user = JSON.parse(fs.readFileSync(userFilePath, 'utf8'));
|
|
467
|
+
user.auth = resBody.data;
|
|
468
|
+
fs.writeFileSync(userFilePath, JSON.stringify(user), 'utf8');
|
|
469
|
+
return [2 /*return*/, user];
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
};
|
|
474
|
+
AuthApi.prototype.request = function (req) {
|
|
475
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
476
|
+
var user, res, resBody, _a;
|
|
477
|
+
return __generator(this, function (_b) {
|
|
478
|
+
switch (_b.label) {
|
|
479
|
+
case 0: return [4 /*yield*/, this.getToken()];
|
|
480
|
+
case 1:
|
|
481
|
+
user = _b.sent();
|
|
482
|
+
if (!user) {
|
|
483
|
+
console.log('Unauthorized. Please execute the login command');
|
|
484
|
+
return [2 /*return*/, undefined];
|
|
485
|
+
}
|
|
486
|
+
return [4 /*yield*/, req(user.auth.accessToken)];
|
|
487
|
+
case 2:
|
|
488
|
+
res = _b.sent();
|
|
489
|
+
return [4 /*yield*/, res.json()];
|
|
490
|
+
case 3:
|
|
491
|
+
resBody = _b.sent();
|
|
492
|
+
if (!(!res.ok || !resBody.data)) return [3 /*break*/, 10];
|
|
493
|
+
_a = res.status;
|
|
494
|
+
switch (_a) {
|
|
495
|
+
case 400: return [3 /*break*/, 4];
|
|
496
|
+
case 401: return [3 /*break*/, 5];
|
|
497
|
+
}
|
|
498
|
+
return [3 /*break*/, 8];
|
|
499
|
+
case 4:
|
|
500
|
+
console.log(JSON.stringify(resBody));
|
|
501
|
+
return [3 /*break*/, 9];
|
|
502
|
+
case 5: return [4 /*yield*/, this.refreshToken()];
|
|
503
|
+
case 6:
|
|
504
|
+
_b.sent();
|
|
505
|
+
return [4 /*yield*/, this.request(req)];
|
|
506
|
+
case 7: return [2 /*return*/, _b.sent()];
|
|
507
|
+
case 8:
|
|
508
|
+
console.log("request failed: status=".concat(res.status, ", statusText=").concat(res.statusText));
|
|
509
|
+
_b.label = 9;
|
|
510
|
+
case 9: return [2 /*return*/, undefined];
|
|
511
|
+
case 10: return [2 /*return*/, resBody.data];
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
};
|
|
516
|
+
return AuthApi;
|
|
517
|
+
}());
|
|
518
|
+
|
|
519
|
+
exports.AuthApi = AuthApi;
|
|
520
|
+
exports.InitApi = InitApi;
|
|
521
|
+
exports.RopUploadApi = RopUploadApi;
|
|
522
|
+
exports.frameworkList = frameworkList;
|
|
523
|
+
//# sourceMappingURL=index.js.map
|