@ones-open/cli 0.0.11 → 0.0.12-21205.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/index.cjs +327 -85
  2. package/dist/index.js +327 -85
  3. package/dist/types/actions/app/resolve.d.ts +10 -0
  4. package/dist/types/actions/app/resolve.d.ts.map +1 -0
  5. package/dist/types/actions/dev/index.d.ts.map +1 -1
  6. package/dist/types/actions/disable/index.d.ts.map +1 -1
  7. package/dist/types/actions/disable/normalize.d.ts.map +1 -1
  8. package/dist/types/actions/enable/index.d.ts.map +1 -1
  9. package/dist/types/actions/enable/normalize.d.ts.map +1 -1
  10. package/dist/types/actions/index.d.ts +1 -0
  11. package/dist/types/actions/index.d.ts.map +1 -1
  12. package/dist/types/actions/install/index.d.ts.map +1 -1
  13. package/dist/types/actions/install/install.d.ts +2 -1
  14. package/dist/types/actions/install/install.d.ts.map +1 -1
  15. package/dist/types/actions/install/normalize.d.ts.map +1 -1
  16. package/dist/types/actions/logs/index.d.ts +4 -0
  17. package/dist/types/actions/logs/index.d.ts.map +1 -0
  18. package/dist/types/actions/logs/normalize.d.ts +11 -0
  19. package/dist/types/actions/logs/normalize.d.ts.map +1 -0
  20. package/dist/types/actions/uninstall/index.d.ts.map +1 -1
  21. package/dist/types/actions/uninstall/normalize.d.ts.map +1 -1
  22. package/dist/types/command/app/index.d.ts.map +1 -1
  23. package/dist/types/command/disable/index.d.ts +1 -1
  24. package/dist/types/command/disable/index.d.ts.map +1 -1
  25. package/dist/types/command/enable/index.d.ts +1 -1
  26. package/dist/types/command/enable/index.d.ts.map +1 -1
  27. package/dist/types/command/index.d.ts +1 -0
  28. package/dist/types/command/index.d.ts.map +1 -1
  29. package/dist/types/command/install/index.d.ts +1 -1
  30. package/dist/types/command/install/index.d.ts.map +1 -1
  31. package/dist/types/command/logs/index.d.ts +10 -0
  32. package/dist/types/command/logs/index.d.ts.map +1 -0
  33. package/dist/types/command/uninstall/index.d.ts +1 -1
  34. package/dist/types/command/uninstall/index.d.ts.map +1 -1
  35. package/dist/types/common/locales/en/index.d.ts +4 -0
  36. package/dist/types/common/locales/en/index.d.ts.map +1 -1
  37. package/dist/types/common/package/utils.d.ts +1 -0
  38. package/dist/types/common/package/utils.d.ts.map +1 -1
  39. package/dist/types/common/request/consts.d.ts +1 -0
  40. package/dist/types/common/request/consts.d.ts.map +1 -1
  41. package/dist/types/common/request/fetch.d.ts +2 -1
  42. package/dist/types/common/request/fetch.d.ts.map +1 -1
  43. package/dist/types/common/request/types.d.ts +20 -7
  44. package/dist/types/common/request/types.d.ts.map +1 -1
  45. package/package.json +3 -3
package/dist/index.cjs CHANGED
@@ -12,6 +12,7 @@ const lodashEs = require("lodash-es");
12
12
  const process$1 = require("node:process");
13
13
  const node_url = require("node:url");
14
14
  const cosmiconfig = require("cosmiconfig");
15
+ const node_net = require("node:net");
15
16
  const getPort = require("get-port");
16
17
  const envPaths = require("env-paths");
17
18
  const zod = require("zod");
@@ -44,6 +45,10 @@ const en = {
44
45
  "desc.enable": "Enable your ONES App",
45
46
  "desc.disable": "Disable your ONES App",
46
47
  "desc.uninstall": "Uninstall your ONES App",
48
+ "desc.app.appId": "Specify app id",
49
+ "desc.logs": "View runtime logs for your ONES App",
50
+ "desc.logs.fromOpkxJson": "Read app id from local opkx.json when app id is not provided",
51
+ "desc.logs.tail": "Specify latest log lines to fetch (default 100)",
47
52
  "desc.legacy": "Legacy command",
48
53
  "desc.legacy.create": "Create a new ONES plugin",
49
54
  "desc.legacy.create.projectPath": "Specify the project path",
@@ -161,6 +166,18 @@ const getAppManifestJSON = () => {
161
166
  return createOnesApp.throwError(ErrorCode.APP_MANIFEST_JSON_PARSE_ERROR, `${i18n.t("error.schema.app.manifest.parseError")}: ${details}`);
162
167
  }
163
168
  };
169
+ const tryGetAppManifestJSON = function() {
170
+ let path = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : getAppManifestJSONPath();
171
+ try {
172
+ const string = node_fs.readFileSync(path, {
173
+ encoding: "utf8"
174
+ });
175
+ const json = JSON.parse(string);
176
+ return createOnesApp.AppManifestJSONSchema.parse(json);
177
+ } catch (error) {
178
+ return null;
179
+ }
180
+ };
164
181
  const validateAppManifestJSON = () => {
165
182
  const appManifestJSON = getAppManifestJSON();
166
183
  try {
@@ -212,7 +229,7 @@ const getAppRcJSON = async () => {
212
229
  };
213
230
  const isOPKXFilename = /\.opkx$/;
214
231
  const defaultOutputPath = "";
215
- const normalize$a = async (options) => {
232
+ const normalize$b = async (options) => {
216
233
  var _options$output, _options$command;
217
234
  let output = node_path.resolve(process$1.cwd(), (_options$output = options.output) !== null && _options$output !== void 0 ? _options$output : defaultOutputPath);
218
235
  const command = (_options$command = options.command) !== null && _options$command !== void 0 ? _options$command : "";
@@ -241,7 +258,7 @@ const build = async function() {
241
258
  const {
242
259
  options
243
260
  } = createOnesApp.getCommandOptions(args, buildCommandArguments);
244
- const normalizedOptions = await normalize$a(options);
261
+ const normalizedOptions = await normalize$b(options);
245
262
  const appManifestJSON = getAppManifestJSON();
246
263
  const appRcJSON = await getAppRcJSON();
247
264
  const correctCommand = normalizedOptions.command || ((_appRcJSON$dev = appRcJSON.dev) === null || _appRcJSON$dev === void 0 || (_appRcJSON$dev = _appRcJSON$dev.command) === null || _appRcJSON$dev === void 0 ? void 0 : _appRcJSON$dev[0]) || ((_defaultAppRcJSON$dev = defaultAppRcJSON.dev) === null || _defaultAppRcJSON$dev === void 0 || (_defaultAppRcJSON$dev = _defaultAppRcJSON$dev.command) === null || _defaultAppRcJSON$dev === void 0 ? void 0 : _defaultAppRcJSON$dev[0]) || "";
@@ -463,6 +480,7 @@ const API = {
463
480
  APP_ENABLE: "/platform/api/app/:installation_id/enable",
464
481
  APP_DISABLE: "/platform/api/app/:installation_id/disable",
465
482
  OAUTH_TOKEN: "/identity/oauth/token",
483
+ APP_RUNTIME_LOGS: "/platform/runtime_manager/app/runtime/logs",
466
484
  REFRESH_TOKEN: "/identity/oauth/token"
467
485
  };
468
486
  const REQUEST_TIMEOUT = 1e4;
@@ -653,12 +671,36 @@ const fetchHostedAbilityStorageDevDeclare = async (appID, data) => {
653
671
  data
654
672
  }).catch(handleError);
655
673
  };
656
- const fetchAppInstall = async (data) => {
657
- var _appList$data$0$insta, _appList$data;
658
- const appID = getAppManifestJSON().app.id;
674
+ const getAppInstallation = async (appID) => {
675
+ var _appList$data$, _appList$data, _appData$installation, _appData$app_env;
659
676
  const appList = await fetchAppList(appID);
660
- const installationID = (_appList$data$0$insta = (_appList$data = appList.data) === null || _appList$data === void 0 || (_appList$data = _appList$data[0]) === null || _appList$data === void 0 ? void 0 : _appList$data.installation_id) !== null && _appList$data$0$insta !== void 0 ? _appList$data$0$insta : "";
677
+ const appData = (_appList$data$ = (_appList$data = appList.data) === null || _appList$data === void 0 ? void 0 : _appList$data[0]) !== null && _appList$data$ !== void 0 ? _appList$data$ : {};
678
+ return {
679
+ installationID: (_appData$installation = appData.installation_id) !== null && _appData$installation !== void 0 ? _appData$installation : "",
680
+ appEnv: (_appData$app_env = appData.app_env) !== null && _appData$app_env !== void 0 ? _appData$app_env : "development"
681
+ };
682
+ };
683
+ const consoleProductionNotAllowedMessage = (operation) => {
684
+ console.log(`Cannot run "${operation}" in "production" environment`);
685
+ process.exit(1);
686
+ };
687
+ const getDisplayAppEnv = (appEnv) => {
688
+ return appEnv === "production" ? "production" : "development";
689
+ };
690
+ const consoleAppEnvMessage = (operation, appEnv) => {
691
+ console.log(`Operating "${operation}" in "${appEnv}" environment`);
692
+ };
693
+ const fetchAppInstall = async (appID, data) => {
694
+ const {
695
+ installationID,
696
+ appEnv
697
+ } = await getAppInstallation(appID);
661
698
  if (installationID) {
699
+ if (appEnv === "production") {
700
+ consoleProductionNotAllowedMessage("install");
701
+ }
702
+ const displayAppEnv = getDisplayAppEnv(appEnv);
703
+ consoleAppEnvMessage("install", displayAppEnv);
662
704
  return await fetchAppBase({
663
705
  url: API.APP_UPGRADE,
664
706
  method: "POST",
@@ -668,18 +710,24 @@ const fetchAppInstall = async (data) => {
668
710
  data
669
711
  }).catch(handleError);
670
712
  }
713
+ consoleAppEnvMessage("install", "development");
671
714
  return await fetchAppBase({
672
715
  url: API.APP_INSTALL,
673
716
  method: "POST",
674
717
  data
675
718
  }).catch(handleError);
676
719
  };
677
- const fetchAppUninstall = async () => {
678
- var _appList$data$0$insta2, _appList$data2;
679
- const appID = getAppManifestJSON().app.id;
680
- const appList = await fetchAppList(appID);
681
- const installationID = (_appList$data$0$insta2 = (_appList$data2 = appList.data) === null || _appList$data2 === void 0 || (_appList$data2 = _appList$data2[0]) === null || _appList$data2 === void 0 ? void 0 : _appList$data2.installation_id) !== null && _appList$data$0$insta2 !== void 0 ? _appList$data$0$insta2 : "";
720
+ const fetchAppUninstall = async (appID) => {
721
+ const {
722
+ installationID,
723
+ appEnv
724
+ } = await getAppInstallation(appID);
682
725
  if (installationID) {
726
+ if (appEnv === "production") {
727
+ consoleProductionNotAllowedMessage("uninstall");
728
+ }
729
+ const displayAppEnv = getDisplayAppEnv(appEnv);
730
+ consoleAppEnvMessage("uninstall", displayAppEnv);
683
731
  return await fetchAppBase({
684
732
  url: API.APP_UNINSTALL,
685
733
  method: "POST",
@@ -691,12 +739,14 @@ const fetchAppUninstall = async () => {
691
739
  consoleAppNotInstalledMessage();
692
740
  process.exit(1);
693
741
  };
694
- const fetchAppEnable = async () => {
695
- var _appList$data$0$insta3, _appList$data3;
696
- const appID = getAppManifestJSON().app.id;
697
- const appList = await fetchAppList(appID);
698
- const installationID = (_appList$data$0$insta3 = (_appList$data3 = appList.data) === null || _appList$data3 === void 0 || (_appList$data3 = _appList$data3[0]) === null || _appList$data3 === void 0 ? void 0 : _appList$data3.installation_id) !== null && _appList$data$0$insta3 !== void 0 ? _appList$data$0$insta3 : "";
742
+ const fetchAppEnable = async (appID) => {
743
+ const {
744
+ installationID,
745
+ appEnv
746
+ } = await getAppInstallation(appID);
699
747
  if (installationID) {
748
+ const displayAppEnv = getDisplayAppEnv(appEnv);
749
+ consoleAppEnvMessage("enable", displayAppEnv);
700
750
  return await fetchAppBase({
701
751
  url: API.APP_ENABLE,
702
752
  method: "POST",
@@ -708,12 +758,14 @@ const fetchAppEnable = async () => {
708
758
  consoleAppNotInstalledMessage();
709
759
  process.exit(1);
710
760
  };
711
- const fetchAppDisable = async () => {
712
- var _appList$data$0$insta4, _appList$data4;
713
- const appID = getAppManifestJSON().app.id;
714
- const appList = await fetchAppList(appID);
715
- const installationID = (_appList$data$0$insta4 = (_appList$data4 = appList.data) === null || _appList$data4 === void 0 || (_appList$data4 = _appList$data4[0]) === null || _appList$data4 === void 0 ? void 0 : _appList$data4.installation_id) !== null && _appList$data$0$insta4 !== void 0 ? _appList$data$0$insta4 : "";
761
+ const fetchAppDisable = async (appID) => {
762
+ const {
763
+ installationID,
764
+ appEnv
765
+ } = await getAppInstallation(appID);
716
766
  if (installationID) {
767
+ const displayAppEnv = getDisplayAppEnv(appEnv);
768
+ consoleAppEnvMessage("disable", displayAppEnv);
717
769
  return await fetchAppBase({
718
770
  url: API.APP_DISABLE,
719
771
  method: "POST",
@@ -725,6 +777,13 @@ const fetchAppDisable = async () => {
725
777
  consoleAppNotInstalledMessage();
726
778
  process.exit(1);
727
779
  };
780
+ const fetchAppRuntimeLogs = async (data) => {
781
+ return await fetchAppBase({
782
+ url: API.APP_RUNTIME_LOGS,
783
+ method: "POST",
784
+ data
785
+ }).catch(handleError);
786
+ };
728
787
  const checkTokenInfo = async () => {
729
788
  let boolean = true;
730
789
  const regionURL = await getRegionURL().catch(() => "");
@@ -741,8 +800,28 @@ const checkTokenInfo = async () => {
741
800
  }
742
801
  return boolean;
743
802
  };
803
+ const resolveAppInfo = (input) => {
804
+ var _input$appID;
805
+ const appIDFromArgument = (_input$appID = input.appID) !== null && _input$appID !== void 0 ? _input$appID : "";
806
+ if (appIDFromArgument) {
807
+ return {
808
+ appID: appIDFromArgument,
809
+ appName: appIDFromArgument
810
+ };
811
+ }
812
+ const appManifestPath = input.opkxPath ? node_path.resolve(process$1.cwd(), input.opkxPath) : getAppManifestJSONPath();
813
+ const appManifestJSON = tryGetAppManifestJSON(appManifestPath);
814
+ if (appManifestJSON) {
815
+ return {
816
+ appID: appManifestJSON.app.id,
817
+ appName: appManifestJSON.app.name
818
+ };
819
+ }
820
+ console.log('Cannot read local "opkx.json". Please ensure "opkx.json" exists in the current directory, or provide an app ID.');
821
+ process.exit(1);
822
+ };
744
823
  const isPortNumber = /^[1-9]\d{0,4}$/;
745
- const normalize$9 = async (options) => {
824
+ const normalize$a = async (options) => {
746
825
  var _options$clearStorage;
747
826
  const portInput = options.port;
748
827
  if (portInput)
@@ -1106,7 +1185,7 @@ const tunnel = async function() {
1106
1185
  const {
1107
1186
  options
1108
1187
  } = createOnesApp.getCommandOptions(args, tunnelCommandArguments);
1109
- const normalizedOptions = await normalize$9(options);
1188
+ const normalizedOptions = await normalize$a(options);
1110
1189
  const port = Number(normalizedOptions.port);
1111
1190
  const cancelWaiting = startWaiting();
1112
1191
  await invokeTunnel(port, {
@@ -1118,9 +1197,12 @@ const tunnel = async function() {
1118
1197
  const tunnelUrl = await buildTunnelUrl();
1119
1198
  console.log(`Relay endpoint: ${tunnelUrl}`);
1120
1199
  };
1121
- const normalize$8 = async (options) => {
1200
+ const normalize$9 = async (options) => {
1201
+ var _options$appID;
1122
1202
  lodashEs.noop(options);
1123
- return {};
1203
+ return {
1204
+ appID: (_options$appID = options.appID) !== null && _options$appID !== void 0 ? _options$appID : ""
1205
+ };
1124
1206
  };
1125
1207
  const displayAppDetail = async (installationID) => {
1126
1208
  var _tokenInfo$org$uuid, _tokenInfo$org, _tokenInfo$teams$0$uu, _tokenInfo$teams, _tokenInfo$org$visibi, _tokenInfo$org2;
@@ -1138,14 +1220,10 @@ const displayAppDetail = async (installationID) => {
1138
1220
  console.log(`See App detail: ${url}`);
1139
1221
  }
1140
1222
  };
1141
- const invokeInstall = async (manifestUrl) => {
1142
- const {
1143
- app
1144
- } = getAppManifestJSON();
1145
- const appName = app.name;
1146
- const appID = app.id;
1223
+ const invokeInstall = async (manifestUrl, appInfo) => {
1224
+ const appID = appInfo.appID;
1147
1225
  const cancelWaiting = startWaiting();
1148
- const result = await fetchAppInstall({
1226
+ const result = await fetchAppInstall(appID, {
1149
1227
  manifest_url: manifestUrl,
1150
1228
  options: {
1151
1229
  enable: true
@@ -1154,7 +1232,7 @@ const invokeInstall = async (manifestUrl) => {
1154
1232
  cancelWaiting();
1155
1233
  if (result.code === "OK") {
1156
1234
  var _appList$data$0$insta, _appList$data;
1157
- console.log(`App "${appName}" installed successfully!`);
1235
+ console.log(`App "${appID}" installed successfully!`);
1158
1236
  const appList = await fetchAppList(appID);
1159
1237
  const installationID = (_appList$data$0$insta = (_appList$data = appList.data) === null || _appList$data === void 0 || (_appList$data = _appList$data[0]) === null || _appList$data === void 0 ? void 0 : _appList$data.installation_id) !== null && _appList$data$0$insta !== void 0 ? _appList$data$0$insta : "";
1160
1238
  if (installationID) {
@@ -1162,7 +1240,7 @@ const invokeInstall = async (manifestUrl) => {
1162
1240
  }
1163
1241
  return true;
1164
1242
  } else {
1165
- console.log(`App "${appName}" installed failed!`);
1243
+ console.log(`App "${appID}" installed failed!`);
1166
1244
  console.error(JSON.stringify(result, null, 2));
1167
1245
  }
1168
1246
  return false;
@@ -1202,19 +1280,19 @@ const install = async function() {
1202
1280
  const {
1203
1281
  options
1204
1282
  } = createOnesApp.getCommandOptions(args, installCommandArguments);
1205
- const normalizedOptions = await normalize$8(options);
1283
+ const normalizedOptions = await normalize$9(options);
1206
1284
  lodashEs.noop(normalizedOptions);
1207
- const {
1208
- app
1209
- } = getAppManifestJSON();
1210
- const appName = app.name;
1285
+ const appInfo = resolveAppInfo({
1286
+ appID: options.appID
1287
+ });
1288
+ const appID = appInfo.appID;
1211
1289
  const manifestUrl = await getManifestUrl();
1212
1290
  if (!manifestUrl) {
1213
- console.log(`App "${appName}" server not available!`);
1291
+ console.log(`App "${appID}" server not available!`);
1214
1292
  console.log('Use "ones dev" command or "ones tunnel" command to start the server first');
1215
1293
  process.exit(1);
1216
1294
  }
1217
- await invokeInstall(manifestUrl);
1295
+ await invokeInstall(manifestUrl, appInfo);
1218
1296
  };
1219
1297
  var InstallOptions = /* @__PURE__ */ ((InstallOptions2) => {
1220
1298
  InstallOptions2["AUTO"] = "auto";
@@ -1229,7 +1307,7 @@ var DevCommandScripts = /* @__PURE__ */ ((DevCommandScripts2) => {
1229
1307
  return DevCommandScripts2;
1230
1308
  })(DevCommandScripts || {});
1231
1309
  const defaultInstall = InstallOptions.AUTO;
1232
- const normalize$7 = async (options) => {
1310
+ const normalize$8 = async (options) => {
1233
1311
  var _options$install, _options$command, _options$clearStorage;
1234
1312
  let install2 = (_options$install = options.install) !== null && _options$install !== void 0 ? _options$install : defaultInstall;
1235
1313
  const command = (_options$command = options.command) !== null && _options$command !== void 0 ? _options$command : "";
@@ -1251,6 +1329,47 @@ const normalize$7 = async (options) => {
1251
1329
  };
1252
1330
  };
1253
1331
  const IDLE_MS = 2e3;
1332
+ const WAIT_PORT_TIMEOUT_MS = 12e4;
1333
+ const WAIT_PORT_INTERVAL_MS = 500;
1334
+ const WAIT_PORT_CONNECT_TIMEOUT_MS = 1e3;
1335
+ const sleep$1 = (ms) => {
1336
+ return new Promise((resolve) => {
1337
+ setTimeout(resolve, ms);
1338
+ });
1339
+ };
1340
+ const canConnectPort = (port) => {
1341
+ return new Promise((resolve) => {
1342
+ let settled = false;
1343
+ const socket = node_net.createConnection({
1344
+ host: "127.0.0.1",
1345
+ port
1346
+ });
1347
+ const done = (result) => {
1348
+ if (settled)
1349
+ return;
1350
+ settled = true;
1351
+ socket.removeAllListeners();
1352
+ socket.destroy();
1353
+ resolve(result);
1354
+ };
1355
+ socket.setTimeout(WAIT_PORT_CONNECT_TIMEOUT_MS);
1356
+ socket.on("connect", () => done(true));
1357
+ socket.on("timeout", () => done(false));
1358
+ socket.on("error", () => done(false));
1359
+ });
1360
+ };
1361
+ const waitForPortReady = async function(port) {
1362
+ let timeoutMs = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : WAIT_PORT_TIMEOUT_MS;
1363
+ const startedAt = Date.now();
1364
+ while (Date.now() - startedAt < timeoutMs) {
1365
+ const connected = await canConnectPort(port);
1366
+ if (connected) {
1367
+ return true;
1368
+ }
1369
+ await sleep$1(WAIT_PORT_INTERVAL_MS);
1370
+ }
1371
+ return false;
1372
+ };
1254
1373
  const dev = async function() {
1255
1374
  var _appRcJSON$dev, _defaultAppRcJSON$dev, _appRcJSON$dev$comman, _appRcJSON$dev2, _currentCommand$;
1256
1375
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -1259,7 +1378,7 @@ const dev = async function() {
1259
1378
  const {
1260
1379
  options
1261
1380
  } = createOnesApp.getCommandOptions(args, devCommandArguments);
1262
- const normalizedOptions = await normalize$7(options);
1381
+ const normalizedOptions = await normalize$8(options);
1263
1382
  const appRcJSON = await getAppRcJSON();
1264
1383
  const correctCommand = normalizedOptions.command || ((_appRcJSON$dev = appRcJSON.dev) === null || _appRcJSON$dev === void 0 || (_appRcJSON$dev = _appRcJSON$dev.command) === null || _appRcJSON$dev === void 0 ? void 0 : _appRcJSON$dev[0]) || ((_defaultAppRcJSON$dev = defaultAppRcJSON.dev) === null || _defaultAppRcJSON$dev === void 0 || (_defaultAppRcJSON$dev = _defaultAppRcJSON$dev.command) === null || _defaultAppRcJSON$dev === void 0 ? void 0 : _defaultAppRcJSON$dev[0]) || "";
1265
1384
  if (!correctCommand) {
@@ -1293,6 +1412,10 @@ const dev = async function() {
1293
1412
  app
1294
1413
  } = appManifestJSON;
1295
1414
  const appID = app.id;
1415
+ const appInfo = {
1416
+ appID: app.id,
1417
+ appName: app.name
1418
+ };
1296
1419
  const config2 = getConfig();
1297
1420
  const ONES_HOSTED_PORT = await getPort({
1298
1421
  port: config2.defaultPort.hosted
@@ -1349,7 +1472,7 @@ const dev = async function() {
1349
1472
  if (enableTunnel) {
1350
1473
  switch (normalizedOptions.install) {
1351
1474
  case InstallOptions.TRUE:
1352
- await invokeInstall(manifestUrl);
1475
+ await invokeInstall(manifestUrl, appInfo);
1353
1476
  break;
1354
1477
  case InstallOptions.AUTO:
1355
1478
  {
@@ -1359,7 +1482,7 @@ const dev = async function() {
1359
1482
  if (installationID) {
1360
1483
  await displayAppDetail(installationID);
1361
1484
  } else {
1362
- await invokeInstall(manifestUrl);
1485
+ await invokeInstall(manifestUrl, appInfo);
1363
1486
  }
1364
1487
  }
1365
1488
  break;
@@ -1378,6 +1501,28 @@ const dev = async function() {
1378
1501
  const createReadyStatusPipes = (onReady2) => {
1379
1502
  let readyTimer = null;
1380
1503
  let summaryPrinted = false;
1504
+ let readyInProgress = false;
1505
+ const printReady = async () => {
1506
+ if (summaryPrinted || readyInProgress) {
1507
+ return;
1508
+ }
1509
+ readyInProgress = true;
1510
+ try {
1511
+ if (templateCommand) {
1512
+ const ready = await waitForPortReady(ONES_HOSTED_PORT);
1513
+ if (!ready) {
1514
+ console.error(`Timed out waiting for local app server to listen on port ${ONES_HOSTED_PORT}.`);
1515
+ return;
1516
+ }
1517
+ }
1518
+ if (!summaryPrinted) {
1519
+ summaryPrinted = true;
1520
+ await onReady2();
1521
+ }
1522
+ } finally {
1523
+ readyInProgress = false;
1524
+ }
1525
+ };
1381
1526
  const scheduleReady = () => {
1382
1527
  if (summaryPrinted)
1383
1528
  return;
@@ -1385,10 +1530,9 @@ const dev = async function() {
1385
1530
  clearTimeout(readyTimer);
1386
1531
  readyTimer = setTimeout(() => {
1387
1532
  readyTimer = null;
1388
- if (!summaryPrinted) {
1389
- summaryPrinted = true;
1390
- onReady2();
1391
- }
1533
+ printReady().catch((error) => {
1534
+ console.error(error);
1535
+ });
1392
1536
  }, IDLE_MS);
1393
1537
  };
1394
1538
  const pipe2 = (stream, isStderr) => {
@@ -1460,7 +1604,7 @@ const sleep = (number) => {
1460
1604
  };
1461
1605
  const isURL = /^https?:\/\//;
1462
1606
  const HostBlackList = ["ones.cn", "www.ones.cn", "ones.com", "www.ones.com"];
1463
- const normalize$6 = async (options) => {
1607
+ const normalize$7 = async (options) => {
1464
1608
  const baseURLInput = options.baseURL;
1465
1609
  if (baseURLInput)
1466
1610
  ;
@@ -1498,7 +1642,7 @@ const login = async function() {
1498
1642
  const {
1499
1643
  options
1500
1644
  } = createOnesApp.getCommandOptions(args, loginCommandArguments);
1501
- const normalizedOptions = await normalize$6(options);
1645
+ const normalizedOptions = await normalize$7(options);
1502
1646
  const {
1503
1647
  code_verifier,
1504
1648
  code_challenge
@@ -1651,7 +1795,7 @@ const login = async function() {
1651
1795
  return createOnesApp.throwError(ErrorCode.INCORRECT_BASE_URL, i18n.t("error.login.incorrectBaseURL"));
1652
1796
  }
1653
1797
  };
1654
- const normalize$5 = async (options) => {
1798
+ const normalize$6 = async (options) => {
1655
1799
  lodashEs.noop(options);
1656
1800
  return {};
1657
1801
  };
@@ -1662,12 +1806,12 @@ const logout = async function() {
1662
1806
  const {
1663
1807
  options
1664
1808
  } = createOnesApp.getCommandOptions(args, logoutCommandArguments);
1665
- const normalizedOptions = await normalize$5(options);
1809
+ const normalizedOptions = await normalize$6(options);
1666
1810
  lodashEs.noop(normalizedOptions);
1667
1811
  await setStore({});
1668
1812
  console.log("Logged out successfully!");
1669
1813
  };
1670
- const normalize$4 = async (options) => {
1814
+ const normalize$5 = async (options) => {
1671
1815
  lodashEs.noop(options);
1672
1816
  return {};
1673
1817
  };
@@ -1679,7 +1823,7 @@ const whoami = async function() {
1679
1823
  const {
1680
1824
  options
1681
1825
  } = createOnesApp.getCommandOptions(args, whoamiCommandArguments);
1682
- const normalizedOptions = await normalize$4(options);
1826
+ const normalizedOptions = await normalize$5(options);
1683
1827
  lodashEs.noop(normalizedOptions);
1684
1828
  const baseURL = await getBaseURL();
1685
1829
  const cancelWaiting = startWaiting();
@@ -1695,9 +1839,12 @@ const whoami = async function() {
1695
1839
  consoleUnauthorizedMessage();
1696
1840
  }
1697
1841
  };
1698
- const normalize$3 = async (options) => {
1842
+ const normalize$4 = async (options) => {
1843
+ var _options$appID;
1699
1844
  lodashEs.noop(options);
1700
- return {};
1845
+ return {
1846
+ appID: (_options$appID = options.appID) !== null && _options$appID !== void 0 ? _options$appID : ""
1847
+ };
1701
1848
  };
1702
1849
  const enable = async function() {
1703
1850
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -1706,22 +1853,28 @@ const enable = async function() {
1706
1853
  const {
1707
1854
  options
1708
1855
  } = createOnesApp.getCommandOptions(args, enableCommandArguments);
1709
- const normalizedOptions = await normalize$3(options);
1856
+ const normalizedOptions = await normalize$4(options);
1710
1857
  lodashEs.noop(normalizedOptions);
1711
- const appName = getAppManifestJSON().app.name;
1858
+ const appInfo = resolveAppInfo({
1859
+ appID: options.appID
1860
+ });
1861
+ const appID = appInfo.appID;
1712
1862
  const cancelWaiting = startWaiting();
1713
- const result = await fetchAppEnable();
1863
+ const result = await fetchAppEnable(appID);
1714
1864
  cancelWaiting();
1715
1865
  if (result.code === "OK") {
1716
- console.log(`App "${appName}" enabled successfully!`);
1866
+ console.log(`App "${appID}" enabled successfully!`);
1717
1867
  } else {
1718
- console.log(`App "${appName}" enabled failed!`);
1868
+ console.log(`App "${appID}" enabled failed!`);
1719
1869
  console.error(JSON.stringify(result, null, 2));
1720
1870
  }
1721
1871
  };
1722
- const normalize$2 = async (options) => {
1872
+ const normalize$3 = async (options) => {
1873
+ var _options$appID;
1723
1874
  lodashEs.noop(options);
1724
- return {};
1875
+ return {
1876
+ appID: (_options$appID = options.appID) !== null && _options$appID !== void 0 ? _options$appID : ""
1877
+ };
1725
1878
  };
1726
1879
  const disable = async function() {
1727
1880
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -1730,22 +1883,28 @@ const disable = async function() {
1730
1883
  const {
1731
1884
  options
1732
1885
  } = createOnesApp.getCommandOptions(args, disableCommandArguments);
1733
- const normalizedOptions = await normalize$2(options);
1886
+ const normalizedOptions = await normalize$3(options);
1734
1887
  lodashEs.noop(normalizedOptions);
1735
- const appName = getAppManifestJSON().app.name;
1888
+ const appInfo = resolveAppInfo({
1889
+ appID: options.appID
1890
+ });
1891
+ const appID = appInfo.appID;
1736
1892
  const cancelWaiting = startWaiting();
1737
- const result = await fetchAppDisable();
1893
+ const result = await fetchAppDisable(appID);
1738
1894
  cancelWaiting();
1739
1895
  if (result.code === "OK") {
1740
- console.log(`App "${appName}" disabled successfully!`);
1896
+ console.log(`App "${appID}" disabled successfully!`);
1741
1897
  } else {
1742
- console.log(`App "${appName}" disabled failed!`);
1898
+ console.log(`App "${appID}" disabled failed!`);
1743
1899
  console.error(JSON.stringify(result, null, 2));
1744
1900
  }
1745
1901
  };
1746
- const normalize$1 = async (options) => {
1902
+ const normalize$2 = async (options) => {
1903
+ var _options$appID;
1747
1904
  lodashEs.noop(options);
1748
- return {};
1905
+ return {
1906
+ appID: (_options$appID = options.appID) !== null && _options$appID !== void 0 ? _options$appID : ""
1907
+ };
1749
1908
  };
1750
1909
  const uninstall = async function() {
1751
1910
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -1754,18 +1913,97 @@ const uninstall = async function() {
1754
1913
  const {
1755
1914
  options
1756
1915
  } = createOnesApp.getCommandOptions(args, uninstallCommandArguments);
1757
- const normalizedOptions = await normalize$1(options);
1916
+ const normalizedOptions = await normalize$2(options);
1758
1917
  lodashEs.noop(normalizedOptions);
1759
- const appName = getAppManifestJSON().app.name;
1918
+ const appInfo = resolveAppInfo({
1919
+ appID: options.appID
1920
+ });
1921
+ const appID = appInfo.appID;
1760
1922
  const cancelWaiting = startWaiting();
1761
- const result = await fetchAppUninstall();
1923
+ const result = await fetchAppUninstall(appID);
1762
1924
  cancelWaiting();
1763
1925
  if (result.code === "OK") {
1764
- console.log(`App "${appName}" uninstalled successfully!`);
1926
+ console.log(`App "${appID}" uninstalled successfully!`);
1765
1927
  } else {
1766
- console.log(`App "${appName}" uninstalled failed!`);
1928
+ console.log(`App "${appID}" uninstalled failed!`);
1929
+ console.error(JSON.stringify(result, null, 2));
1930
+ }
1931
+ };
1932
+ const defaultTail = 100;
1933
+ const normalize$1 = async (options) => {
1934
+ var _options$appID, _options$fromOpkxJson, _options$tail;
1935
+ const appID = (_options$appID = options.appID) !== null && _options$appID !== void 0 ? _options$appID : "";
1936
+ const fromOpkxJSON = (_options$fromOpkxJson = options.fromOpkxJson) !== null && _options$fromOpkxJson !== void 0 ? _options$fromOpkxJson : false;
1937
+ if (!appID && !fromOpkxJSON) {
1938
+ console.log('App id is required, use "ones app logs <app-id>" or "--from-opkx-json"');
1939
+ process.exit(1);
1940
+ }
1941
+ const appInfo = appID ? resolveAppInfo({
1942
+ appID
1943
+ }) : resolveAppInfo({});
1944
+ const tailString = (_options$tail = options.tail) !== null && _options$tail !== void 0 ? _options$tail : `${defaultTail}`;
1945
+ const tail = Number(tailString);
1946
+ if (!Number.isInteger(tail) || tail <= 0) {
1947
+ console.log("Tail should be a positive integer");
1948
+ process.exit(1);
1949
+ }
1950
+ return {
1951
+ appID: appInfo.appID,
1952
+ appName: appInfo.appName,
1953
+ tail
1954
+ };
1955
+ };
1956
+ const displayLogs = (data) => {
1957
+ if (Array.isArray(data)) {
1958
+ data.forEach((line) => {
1959
+ if (typeof line === "string") {
1960
+ console.log(line);
1961
+ } else {
1962
+ console.log(JSON.stringify(line, null, 2));
1963
+ }
1964
+ });
1965
+ return;
1966
+ }
1967
+ if (typeof data === "string") {
1968
+ console.log(data);
1969
+ return;
1970
+ }
1971
+ console.log(JSON.stringify(data !== null && data !== void 0 ? data : {}, null, 2));
1972
+ };
1973
+ const logs = async function() {
1974
+ var _appList$data;
1975
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1976
+ args[_key] = arguments[_key];
1977
+ }
1978
+ const {
1979
+ options
1980
+ } = createOnesApp.getCommandOptions(args, logsCommandArguments);
1981
+ const normalizedOptions = await normalize$1(options);
1982
+ const {
1983
+ appID,
1984
+ tail
1985
+ } = normalizedOptions;
1986
+ const cancelWaiting = startWaiting();
1987
+ const appList = await fetchAppList(appID);
1988
+ const productionInstallation = (_appList$data = appList.data) === null || _appList$data === void 0 ? void 0 : _appList$data.find((item) => {
1989
+ return item.installation_id && item.app_env === "production";
1990
+ });
1991
+ if (!productionInstallation) {
1992
+ cancelWaiting();
1993
+ console.log(`App "${appID}" is not installed in production environment`);
1994
+ process.exit(1);
1995
+ }
1996
+ const result = await fetchAppRuntimeLogs({
1997
+ app_id: appID,
1998
+ tail
1999
+ });
2000
+ cancelWaiting();
2001
+ if (result.code && result.code !== "OK") {
2002
+ console.log(`App "${appID}" fetch logs failed!`);
1767
2003
  console.error(JSON.stringify(result, null, 2));
2004
+ return;
1768
2005
  }
2006
+ displayLogs(result.data);
1769
2007
  };
1770
2008
  const getTemplatePath = () => {
1771
2009
  const __dirname = node_path.dirname(node_url.fileURLToPath(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.cjs", document.baseURI).href));
@@ -1836,24 +2074,28 @@ const whoamiCommandArguments = [];
1836
2074
  const $whoami = new commander.Command("whoami").description(i18n.t("desc.whoami")).action(whoami);
1837
2075
  createOnesApp.addCommandUsage($whoami);
1838
2076
  createOnesApp.addCommandOutput($whoami);
1839
- const installCommandArguments = [];
1840
- const $install = new commander.Command("install").description(i18n.t("desc.install")).action(install);
2077
+ const installCommandArguments = ["appID"];
2078
+ const $install = new commander.Command("install").description(i18n.t("desc.install")).argument("[app-id]", i18n.t("desc.app.appId")).action(install);
1841
2079
  createOnesApp.addCommandUsage($install);
1842
2080
  createOnesApp.addCommandOutput($install);
1843
- const enableCommandArguments = [];
1844
- const $enable = new commander.Command("enable").description(i18n.t("desc.enable")).action(enable);
2081
+ const enableCommandArguments = ["appID"];
2082
+ const $enable = new commander.Command("enable").description(i18n.t("desc.enable")).argument("[app-id]", i18n.t("desc.app.appId")).action(enable);
1845
2083
  createOnesApp.addCommandUsage($enable);
1846
2084
  createOnesApp.addCommandOutput($enable);
1847
- const disableCommandArguments = [];
1848
- const $disable = new commander.Command("disable").description(i18n.t("desc.disable")).action(disable);
2085
+ const disableCommandArguments = ["appID"];
2086
+ const $disable = new commander.Command("disable").description(i18n.t("desc.disable")).argument("[app-id]", i18n.t("desc.app.appId")).action(disable);
1849
2087
  createOnesApp.addCommandUsage($disable);
1850
2088
  createOnesApp.addCommandOutput($disable);
1851
- const uninstallCommandArguments = [];
1852
- const $uninstall = new commander.Command("uninstall").description(i18n.t("desc.uninstall")).action(uninstall);
2089
+ const uninstallCommandArguments = ["appID"];
2090
+ const $uninstall = new commander.Command("uninstall").description(i18n.t("desc.uninstall")).argument("[app-id]", i18n.t("desc.app.appId")).action(uninstall);
1853
2091
  createOnesApp.addCommandUsage($uninstall);
1854
2092
  createOnesApp.addCommandOutput($uninstall);
2093
+ const logsCommandArguments = ["appID"];
2094
+ const $logs = new commander.Command("logs").description(i18n.t("desc.logs")).argument("[app-id]", i18n.t("desc.app.appId")).option("--from-opkx-json", i18n.t("desc.logs.fromOpkxJson")).option("-t, --tail [number]", i18n.t("desc.logs.tail")).action(logs);
2095
+ createOnesApp.addCommandUsage($logs);
2096
+ createOnesApp.addCommandOutput($logs);
1855
2097
  const $app = new commander.Command("app").description(i18n.t("desc.app"));
1856
- $app.addCommand($install).addCommand($enable).addCommand($disable).addCommand($uninstall);
2098
+ $app.addCommand($install).addCommand($enable).addCommand($disable).addCommand($uninstall).addCommand($logs);
1857
2099
  createOnesApp.addCommandUsage($app);
1858
2100
  createOnesApp.addCommandOutput($app);
1859
2101
  const legacyCommandArguments = ["projectPath"];