@ones-open/cli 0.50.0 → 1.0.1-16596.1816

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 (175) hide show
  1. package/LICENSE +201 -674
  2. package/bin/ones.js +4 -0
  3. package/dist/callback.html +65 -0
  4. package/dist/index.cjs +836 -0
  5. package/dist/index.js +835 -0
  6. package/dist/types/actions/build/index.d.ts +4 -0
  7. package/dist/types/actions/build/index.d.ts.map +1 -0
  8. package/dist/types/actions/build/normalize.d.ts +6 -0
  9. package/dist/types/actions/build/normalize.d.ts.map +1 -0
  10. package/dist/types/actions/build/regexp.d.ts +2 -0
  11. package/dist/types/actions/build/regexp.d.ts.map +1 -0
  12. package/dist/types/actions/dev/enums.d.ts +6 -0
  13. package/dist/types/actions/dev/enums.d.ts.map +1 -0
  14. package/dist/types/actions/dev/index.d.ts +4 -0
  15. package/dist/types/actions/dev/index.d.ts.map +1 -0
  16. package/dist/types/actions/dev/normalize.d.ts +6 -0
  17. package/dist/types/actions/dev/normalize.d.ts.map +1 -0
  18. package/dist/types/actions/disable/index.d.ts +4 -0
  19. package/dist/types/actions/disable/index.d.ts.map +1 -0
  20. package/dist/types/actions/disable/normalize.d.ts +6 -0
  21. package/dist/types/actions/disable/normalize.d.ts.map +1 -0
  22. package/dist/types/actions/enable/index.d.ts +4 -0
  23. package/dist/types/actions/enable/index.d.ts.map +1 -0
  24. package/dist/types/actions/enable/normalize.d.ts +6 -0
  25. package/dist/types/actions/enable/normalize.d.ts.map +1 -0
  26. package/dist/types/actions/index.d.ts +15 -0
  27. package/dist/types/actions/index.d.ts.map +1 -0
  28. package/dist/types/actions/install/index.d.ts +4 -0
  29. package/dist/types/actions/install/index.d.ts.map +1 -0
  30. package/dist/types/actions/install/normalize.d.ts +6 -0
  31. package/dist/types/actions/install/normalize.d.ts.map +1 -0
  32. package/dist/types/actions/legacy/index.d.ts +4 -0
  33. package/dist/types/actions/legacy/index.d.ts.map +1 -0
  34. package/dist/types/actions/legacy/normalize.d.ts +6 -0
  35. package/dist/types/actions/legacy/normalize.d.ts.map +1 -0
  36. package/dist/types/actions/login/consts.d.ts +2 -0
  37. package/dist/types/actions/login/consts.d.ts.map +1 -0
  38. package/dist/types/actions/login/index.d.ts +4 -0
  39. package/dist/types/actions/login/index.d.ts.map +1 -0
  40. package/dist/types/actions/login/normalize.d.ts +6 -0
  41. package/dist/types/actions/login/normalize.d.ts.map +1 -0
  42. package/dist/types/actions/login/regexp.d.ts +2 -0
  43. package/dist/types/actions/login/regexp.d.ts.map +1 -0
  44. package/dist/types/actions/logout/index.d.ts +4 -0
  45. package/dist/types/actions/logout/index.d.ts.map +1 -0
  46. package/dist/types/actions/logout/normalize.d.ts +6 -0
  47. package/dist/types/actions/logout/normalize.d.ts.map +1 -0
  48. package/dist/types/actions/tunnel/consts.d.ts +3 -0
  49. package/dist/types/actions/tunnel/consts.d.ts.map +1 -0
  50. package/dist/types/actions/tunnel/index.d.ts +4 -0
  51. package/dist/types/actions/tunnel/index.d.ts.map +1 -0
  52. package/dist/types/actions/tunnel/normalize.d.ts +6 -0
  53. package/dist/types/actions/tunnel/normalize.d.ts.map +1 -0
  54. package/dist/types/actions/tunnel/regexp.d.ts +2 -0
  55. package/dist/types/actions/tunnel/regexp.d.ts.map +1 -0
  56. package/dist/types/actions/tunnel/tunnel.d.ts +2 -0
  57. package/dist/types/actions/tunnel/tunnel.d.ts.map +1 -0
  58. package/dist/types/actions/uninstall/index.d.ts +4 -0
  59. package/dist/types/actions/uninstall/index.d.ts.map +1 -0
  60. package/dist/types/actions/uninstall/normalize.d.ts +6 -0
  61. package/dist/types/actions/uninstall/normalize.d.ts.map +1 -0
  62. package/dist/types/actions/whoami/index.d.ts +4 -0
  63. package/dist/types/actions/whoami/index.d.ts.map +1 -0
  64. package/dist/types/actions/whoami/normalize.d.ts +6 -0
  65. package/dist/types/actions/whoami/normalize.d.ts.map +1 -0
  66. package/dist/types/cli/index.d.ts +2 -0
  67. package/dist/types/cli/index.d.ts.map +1 -0
  68. package/dist/types/command/app/index.d.ts +4 -0
  69. package/dist/types/command/app/index.d.ts.map +1 -0
  70. package/dist/types/command/build/index.d.ts +9 -0
  71. package/dist/types/command/build/index.d.ts.map +1 -0
  72. package/dist/types/command/dev/index.d.ts +9 -0
  73. package/dist/types/command/dev/index.d.ts.map +1 -0
  74. package/dist/types/command/disable/index.d.ts +8 -0
  75. package/dist/types/command/disable/index.d.ts.map +1 -0
  76. package/dist/types/command/enable/index.d.ts +8 -0
  77. package/dist/types/command/enable/index.d.ts.map +1 -0
  78. package/dist/types/command/index.d.ts +16 -0
  79. package/dist/types/command/index.d.ts.map +1 -0
  80. package/dist/types/command/install/index.d.ts +8 -0
  81. package/dist/types/command/install/index.d.ts.map +1 -0
  82. package/dist/types/command/legacy/index.d.ts +8 -0
  83. package/dist/types/command/legacy/index.d.ts.map +1 -0
  84. package/dist/types/command/login/index.d.ts +8 -0
  85. package/dist/types/command/login/index.d.ts.map +1 -0
  86. package/dist/types/command/logout/index.d.ts +8 -0
  87. package/dist/types/command/logout/index.d.ts.map +1 -0
  88. package/dist/types/command/tunnel/index.d.ts +8 -0
  89. package/dist/types/command/tunnel/index.d.ts.map +1 -0
  90. package/dist/types/command/uninstall/index.d.ts +8 -0
  91. package/dist/types/command/uninstall/index.d.ts.map +1 -0
  92. package/dist/types/command/whoami/index.d.ts +8 -0
  93. package/dist/types/command/whoami/index.d.ts.map +1 -0
  94. package/dist/types/common/base/index.d.ts +5 -0
  95. package/dist/types/common/base/index.d.ts.map +1 -0
  96. package/dist/types/common/base/utils.d.ts +7 -0
  97. package/dist/types/common/base/utils.d.ts.map +1 -0
  98. package/dist/types/common/command/index.d.ts +6 -0
  99. package/dist/types/common/command/index.d.ts.map +1 -0
  100. package/dist/types/common/command/types.d.ts +2 -0
  101. package/dist/types/common/command/types.d.ts.map +1 -0
  102. package/dist/types/common/command/utils.d.ts +2 -0
  103. package/dist/types/common/command/utils.d.ts.map +1 -0
  104. package/dist/types/common/config/index.d.ts +6 -0
  105. package/dist/types/common/config/index.d.ts.map +1 -0
  106. package/dist/types/common/config/types.d.ts +6 -0
  107. package/dist/types/common/config/types.d.ts.map +1 -0
  108. package/dist/types/common/config/utils.d.ts +3 -0
  109. package/dist/types/common/config/utils.d.ts.map +1 -0
  110. package/dist/types/common/context/index.d.ts +6 -0
  111. package/dist/types/common/context/index.d.ts.map +1 -0
  112. package/dist/types/common/context/types.d.ts +2 -0
  113. package/dist/types/common/context/types.d.ts.map +1 -0
  114. package/dist/types/common/context/utils.d.ts +2 -0
  115. package/dist/types/common/context/utils.d.ts.map +1 -0
  116. package/dist/types/common/error/enums.d.ts +15 -0
  117. package/dist/types/common/error/enums.d.ts.map +1 -0
  118. package/dist/types/common/error/index.d.ts +7 -0
  119. package/dist/types/common/error/index.d.ts.map +1 -0
  120. package/dist/types/common/error/types.d.ts +2 -0
  121. package/dist/types/common/error/types.d.ts.map +1 -0
  122. package/dist/types/common/error/utils.d.ts +2 -0
  123. package/dist/types/common/error/utils.d.ts.map +1 -0
  124. package/dist/types/common/locales/en/index.d.ts +30 -0
  125. package/dist/types/common/locales/en/index.d.ts.map +1 -0
  126. package/dist/types/common/locales/index.d.ts +6 -0
  127. package/dist/types/common/locales/index.d.ts.map +1 -0
  128. package/dist/types/common/locales/types.d.ts +4 -0
  129. package/dist/types/common/locales/types.d.ts.map +1 -0
  130. package/dist/types/common/locales/utils.d.ts +6 -0
  131. package/dist/types/common/locales/utils.d.ts.map +1 -0
  132. package/dist/types/common/package/index.d.ts +7 -0
  133. package/dist/types/common/package/index.d.ts.map +1 -0
  134. package/dist/types/common/package/schema.d.ts +235 -0
  135. package/dist/types/common/package/schema.d.ts.map +1 -0
  136. package/dist/types/common/package/types.d.ts +2 -0
  137. package/dist/types/common/package/types.d.ts.map +1 -0
  138. package/dist/types/common/package/utils.d.ts +9 -0
  139. package/dist/types/common/package/utils.d.ts.map +1 -0
  140. package/dist/types/common/public/index.d.ts +5 -0
  141. package/dist/types/common/public/index.d.ts.map +1 -0
  142. package/dist/types/common/public/utils.d.ts +2 -0
  143. package/dist/types/common/public/utils.d.ts.map +1 -0
  144. package/dist/types/common/request/consts.d.ts +9 -0
  145. package/dist/types/common/request/consts.d.ts.map +1 -0
  146. package/dist/types/common/request/fetch.d.ts +9 -0
  147. package/dist/types/common/request/fetch.d.ts.map +1 -0
  148. package/dist/types/common/request/index.d.ts +8 -0
  149. package/dist/types/common/request/index.d.ts.map +1 -0
  150. package/dist/types/common/request/types.d.ts +50 -0
  151. package/dist/types/common/request/types.d.ts.map +1 -0
  152. package/dist/types/common/request/utils.d.ts +7 -0
  153. package/dist/types/common/request/utils.d.ts.map +1 -0
  154. package/dist/types/common/store/index.d.ts +6 -0
  155. package/dist/types/common/store/index.d.ts.map +1 -0
  156. package/dist/types/common/store/schema.d.ts +22 -0
  157. package/dist/types/common/store/schema.d.ts.map +1 -0
  158. package/dist/types/common/store/utils.d.ts +11 -0
  159. package/dist/types/common/store/utils.d.ts.map +1 -0
  160. package/dist/types/index.d.ts +2 -0
  161. package/dist/types/index.d.ts.map +1 -0
  162. package/index.d.ts +2 -0
  163. package/index.js +2 -0
  164. package/package.json +54 -38
  165. package/bin/index.es.js +0 -261
  166. package/bin/types/index.d.ts +0 -2
  167. package/bin/types/index.d.ts.map +0 -1
  168. package/bin/types/lib/create.d.ts +0 -4
  169. package/bin/types/lib/create.d.ts.map +0 -1
  170. package/bin/types/lib/env.d.ts +0 -6
  171. package/bin/types/lib/env.d.ts.map +0 -1
  172. package/template/_prettierignore +0 -13
  173. package/template/_prettierrc +0 -6
  174. package/template/browserslist +0 -4
  175. package/template/package.json +0 -5
package/dist/index.js ADDED
@@ -0,0 +1,835 @@
1
+ import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
2
+ import { Command } from "commander";
3
+ import { ErrorCode as ErrorCode$1, throwError, getCommandOptions, addCommandUsage, addCommandOutput, $create, setContext } from "create-ones-app";
4
+ import { get, noop, merge } from "lodash-es";
5
+ import process, { cwd, exit } from "node:process";
6
+ import { dirname, join, resolve } from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+ import { readFileSync } from "node:fs";
9
+ import { z } from "zod";
10
+ import fse from "fs-extra";
11
+ import envPaths from "env-paths";
12
+ import http from "node:http";
13
+ import ora from "ora";
14
+ import open from "open";
15
+ import getPort from "get-port";
16
+ import { v7 } from "uuid";
17
+ import axios from "axios";
18
+ import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
19
+ const en = {
20
+ "desc.ones": "cli for ones",
21
+ "desc.build": "build the application",
22
+ "desc.build.output": "specify the output file path",
23
+ "desc.dev": "develop the application",
24
+ "desc.dev.install": "whether to install the application",
25
+ "desc.tunnel": "start a tunnel to the application",
26
+ "desc.tunnel.port": "specify the port number for serve",
27
+ "desc.login": "login to the environment",
28
+ "desc.login.baseUrl": "specify the environment url for login",
29
+ "desc.logout": "logout from the environment",
30
+ "desc.whoami": "show the current user",
31
+ "desc.app": "app command",
32
+ "desc.install": "install the application",
33
+ "desc.enable": "enable the application",
34
+ "desc.disable": "disable the application",
35
+ "desc.uninstall": "uninstall the application",
36
+ "desc.legacy": "legacy command",
37
+ "error.login.missingBaseURL": "missing base url",
38
+ "error.login.incorrectBaseURL": "incorrect base url",
39
+ "error.tunnel.missingPort": "missing port",
40
+ "error.tunnel.incorrectPort": "incorrect port",
41
+ "error.schema.app.package.parseError": "app package json parse error",
42
+ "error.schema.app.manifest.parseError": "app manifest json parse error",
43
+ "error.build.scriptNotFound": "build script not found",
44
+ "error.dev.scriptNotFound": "dev script not found",
45
+ "error.store.permission": 'permission denied, please check the file permission with "{filePath}"'
46
+ };
47
+ const map = {
48
+ en
49
+ };
50
+ const getCurrentLanguageList = () => {
51
+ return ["en"];
52
+ };
53
+ const translate = (key, params) => {
54
+ const list = getCurrentLanguageList();
55
+ for (let index = 0; index < list.length; index++) {
56
+ const language = list[index];
57
+ let value = get(map[language], key);
58
+ if (value) {
59
+ if (params && typeof value === "string") {
60
+ value = value.replace(/\{(\w+)\}/g, (match, paramKey) => {
61
+ var _params$paramKey;
62
+ const result = (_params$paramKey = params === null || params === void 0 ? void 0 : params[paramKey]) !== null && _params$paramKey !== void 0 ? _params$paramKey : match;
63
+ return String(result);
64
+ });
65
+ }
66
+ return value;
67
+ }
68
+ }
69
+ return key;
70
+ };
71
+ const i18n = {
72
+ t: translate
73
+ };
74
+ var ErrorCode = ((ErrorCode2) => {
75
+ ErrorCode2[ErrorCode2["UNKNOWN_ERROR"] = ErrorCode$1.UNKNOWN_ERROR] = "UNKNOWN_ERROR";
76
+ ErrorCode2[ErrorCode2["COMMAND_ERROR"] = ErrorCode$1.COMMAND_ERROR] = "COMMAND_ERROR";
77
+ ErrorCode2[ErrorCode2["TEMPLATE_NOT_FOUND"] = ErrorCode$1.TEMPLATE_NOT_FOUND] = "TEMPLATE_NOT_FOUND";
78
+ ErrorCode2["MISSING_BASE_URL"] = "E03";
79
+ ErrorCode2["MISSING_PORT"] = "E04";
80
+ ErrorCode2["INCORRECT_BASE_URL"] = "E05";
81
+ ErrorCode2["INCORRECT_PORT"] = "E06";
82
+ ErrorCode2["APP_PACKAGE_JSON_PARSE_ERROR"] = "E07";
83
+ ErrorCode2["APP_MANIFEST_JSON_PARSE_ERROR"] = "E08";
84
+ ErrorCode2["BUILD_SCRIPT_NOT_FOUND"] = "E09";
85
+ ErrorCode2["DEV_SCRIPT_NOT_FOUND"] = "E10";
86
+ ErrorCode2["STORE_PERMISSION"] = "E11";
87
+ return ErrorCode2;
88
+ })(ErrorCode || {});
89
+ const AppPackageJSONSchema = z.object({
90
+ scripts: z.object({
91
+ dev: z.string().optional(),
92
+ build: z.string().optional()
93
+ }).optional()
94
+ });
95
+ const AppManifestJSONSchema = z.object({
96
+ app: z.object({
97
+ id: z.string(),
98
+ name: z.string(),
99
+ version: z.string(),
100
+ logo: z.string().optional(),
101
+ desc: z.string().optional(),
102
+ base_url: z.string().optional(),
103
+ auth: z.object({
104
+ type: z.literal("jwt")
105
+ }),
106
+ ones_version: z.object({
107
+ min: z.string(),
108
+ max: z.string()
109
+ }).optional(),
110
+ lifecycle_callback: z.object({
111
+ install: z.string(),
112
+ enabled: z.string().optional(),
113
+ disabled: z.string().optional(),
114
+ uninstalled: z.string().optional()
115
+ }),
116
+ oauth: z.object({
117
+ type: z.array(z.enum(["app", "user"])).optional(),
118
+ scope: z.array(z.string()),
119
+ redirect_url: z.string().optional()
120
+ }).optional(),
121
+ events: z.object({
122
+ url: z.string(),
123
+ types: z.array(z.object({
124
+ eventType: z.string()
125
+ }))
126
+ }).optional(),
127
+ extensions: z.record(z.unknown()).optional()
128
+ })
129
+ });
130
+ const getPackageJSONPath = () => {
131
+ const __dirname = dirname(fileURLToPath(import.meta.url));
132
+ return join(__dirname, "../package.json");
133
+ };
134
+ const getPackageJSON = () => {
135
+ const path = getPackageJSONPath();
136
+ try {
137
+ const string = readFileSync(path, {
138
+ encoding: "utf8"
139
+ });
140
+ return JSON.parse(string);
141
+ } catch (error) {
142
+ return {};
143
+ }
144
+ };
145
+ const getAppPackageJSONPath = () => {
146
+ return join(cwd(), "./package.json");
147
+ };
148
+ const getAppPackageJSON = () => {
149
+ const path = getAppPackageJSONPath();
150
+ try {
151
+ const string = readFileSync(path, {
152
+ encoding: "utf8"
153
+ });
154
+ const json = JSON.parse(string);
155
+ return AppPackageJSONSchema.parse(json);
156
+ } catch (error) {
157
+ console.error(error);
158
+ return throwError(ErrorCode.APP_PACKAGE_JSON_PARSE_ERROR, i18n.t("error.schema.app.package.parseError"));
159
+ }
160
+ };
161
+ const getAppManifestJSONPath = () => {
162
+ return join(cwd(), "./opkx.json");
163
+ };
164
+ const getAppManifestJSON = () => {
165
+ const path = getAppManifestJSONPath();
166
+ try {
167
+ const string = readFileSync(path, {
168
+ encoding: "utf8"
169
+ });
170
+ const json = JSON.parse(string);
171
+ return AppManifestJSONSchema.parse(json);
172
+ } catch (error) {
173
+ console.error(error);
174
+ return throwError(ErrorCode.APP_MANIFEST_JSON_PARSE_ERROR, i18n.t("error.schema.app.manifest.parseError"));
175
+ }
176
+ };
177
+ const isOPKXFilename = /\.opkx$/;
178
+ const defaultOutputPath = "";
179
+ const normalize$a = async (options) => {
180
+ var _options$output;
181
+ let output = resolve(cwd(), (_options$output = options.output) !== null && _options$output !== void 0 ? _options$output : defaultOutputPath);
182
+ if (isOPKXFilename.test(output))
183
+ ;
184
+ else {
185
+ const appManifestJSON = getAppManifestJSON();
186
+ const defaultOutputFilename = `${appManifestJSON.app.name}.opkx`;
187
+ output = resolve(output, defaultOutputFilename);
188
+ }
189
+ return {
190
+ output
191
+ };
192
+ };
193
+ const build = async function() {
194
+ var _appPackageJSON$scrip;
195
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
196
+ args[_key] = arguments[_key];
197
+ }
198
+ const {
199
+ options
200
+ } = getCommandOptions(args, buildCommandArguments);
201
+ const normalizedOptions = await normalize$a(options);
202
+ console.log("build", normalizedOptions);
203
+ const appPackageJSON = getAppPackageJSON();
204
+ console.log("appPackageJSON", appPackageJSON);
205
+ const appManifestJSON = getAppManifestJSON();
206
+ console.log("appManifestJSON", appManifestJSON);
207
+ if ((_appPackageJSON$scrip = appPackageJSON.scripts) !== null && _appPackageJSON$scrip !== void 0 && _appPackageJSON$scrip.build)
208
+ ;
209
+ else {
210
+ return throwError(ErrorCode.BUILD_SCRIPT_NOT_FOUND, i18n.t("error.build.scriptNotFound"));
211
+ }
212
+ };
213
+ var InstallOptions = /* @__PURE__ */ ((InstallOptions2) => {
214
+ InstallOptions2["AUTO"] = "auto";
215
+ InstallOptions2["TRUE"] = "true";
216
+ InstallOptions2["FALSE"] = "false";
217
+ return InstallOptions2;
218
+ })(InstallOptions || {});
219
+ const defaultInstall = InstallOptions.AUTO;
220
+ const normalize$9 = async (options) => {
221
+ var _options$install;
222
+ let install2 = (_options$install = options.install) !== null && _options$install !== void 0 ? _options$install : defaultInstall;
223
+ switch (true) {
224
+ case install2 === InstallOptions.AUTO:
225
+ break;
226
+ case Boolean(install2):
227
+ install2 = InstallOptions.TRUE;
228
+ break;
229
+ default:
230
+ install2 = InstallOptions.FALSE;
231
+ break;
232
+ }
233
+ return {
234
+ install: install2
235
+ };
236
+ };
237
+ const dev = async function() {
238
+ var _appPackageJSON$scrip;
239
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
240
+ args[_key] = arguments[_key];
241
+ }
242
+ const {
243
+ options
244
+ } = getCommandOptions(args, devCommandArguments);
245
+ const normalizedOptions = await normalize$9(options);
246
+ console.log("dev", normalizedOptions);
247
+ const appPackageJSON = getAppPackageJSON();
248
+ console.log("appPackageJSON", appPackageJSON);
249
+ const appManifestJSON = getAppManifestJSON();
250
+ console.log("appManifestJSON", appManifestJSON);
251
+ if ((_appPackageJSON$scrip = appPackageJSON.scripts) !== null && _appPackageJSON$scrip !== void 0 && _appPackageJSON$scrip.dev) {
252
+ if (normalizedOptions.install)
253
+ ;
254
+ } else {
255
+ return throwError(ErrorCode.DEV_SCRIPT_NOT_FOUND, i18n.t("error.dev.scriptNotFound"));
256
+ }
257
+ };
258
+ const isPortNumber = /^[1-9]\d{0,4}$/;
259
+ const minPortNumber = 1;
260
+ const maxPortNumber = 65535;
261
+ const normalize$8 = async (options) => {
262
+ const portInput = options.port;
263
+ if (portInput)
264
+ ;
265
+ else {
266
+ return throwError(ErrorCode.MISSING_PORT, i18n.t("error.tunnel.missingPort"));
267
+ }
268
+ const portString = String(portInput);
269
+ const portNumber = Number(portString);
270
+ if (isPortNumber.test(portString))
271
+ ;
272
+ else {
273
+ return throwError(ErrorCode.INCORRECT_PORT, i18n.t("error.tunnel.incorrectPort"));
274
+ }
275
+ if (portNumber < minPortNumber || portNumber > maxPortNumber) {
276
+ return throwError(ErrorCode.INCORRECT_PORT, i18n.t("error.tunnel.incorrectPort"));
277
+ }
278
+ const port = portString;
279
+ return {
280
+ port
281
+ };
282
+ };
283
+ const StoreJSONSchema = z.object({
284
+ version: z.string().optional(),
285
+ timestamp: z.number().optional(),
286
+ base_url: z.string().optional(),
287
+ region_url: z.string().optional(),
288
+ ones_token: z.string().optional()
289
+ });
290
+ const {
291
+ ensureFile,
292
+ readJSON,
293
+ writeJSON
294
+ } = fse;
295
+ const name = "@ones-open/cli";
296
+ const paths = envPaths(name);
297
+ const storeDir = paths.config;
298
+ const storePath = join(storeDir, "config.json");
299
+ const getStore = async () => {
300
+ let json = {};
301
+ try {
302
+ await ensureFile(storePath);
303
+ json = await readJSON(storePath, {
304
+ encoding: "utf8"
305
+ });
306
+ } catch (error) {
307
+ const $error = error;
308
+ if ($error.code === "EPERM" || $error.code === "EACCES") {
309
+ return throwError(ErrorCode.STORE_PERMISSION, i18n.t("error.store.permission", {
310
+ filePath: storePath
311
+ }));
312
+ }
313
+ if ($error instanceof SyntaxError) {
314
+ return {};
315
+ }
316
+ throw error;
317
+ }
318
+ try {
319
+ return StoreJSONSchema.parse(json);
320
+ } catch (error) {
321
+ return {};
322
+ }
323
+ };
324
+ const setStore = async (store) => {
325
+ try {
326
+ await ensureFile(storePath);
327
+ const version = `${getPackageJSON().version}`;
328
+ const timestamp = Date.now();
329
+ return await writeJSON(storePath, {
330
+ ...store,
331
+ version,
332
+ timestamp
333
+ }, {
334
+ encoding: "utf8"
335
+ });
336
+ } catch (error) {
337
+ const $error = error;
338
+ if ($error.code === "EPERM" || $error.code === "EACCES") {
339
+ return throwError(ErrorCode.STORE_PERMISSION, i18n.t("error.store.permission", {
340
+ filePath: storePath
341
+ }));
342
+ }
343
+ throw error;
344
+ }
345
+ };
346
+ const mergeStore = async (store) => {
347
+ const currentStore = await getStore();
348
+ return setStore({
349
+ ...currentStore,
350
+ ...store
351
+ });
352
+ };
353
+ const setBaseURL = async (baseURL) => {
354
+ return mergeStore({
355
+ base_url: baseURL
356
+ });
357
+ };
358
+ const getRegionURL = async () => {
359
+ var _store$region_url;
360
+ const store = await getStore();
361
+ return (_store$region_url = store.region_url) !== null && _store$region_url !== void 0 ? _store$region_url : "";
362
+ };
363
+ const setRegionURL = async (regionURL) => {
364
+ return mergeStore({
365
+ region_url: regionURL
366
+ });
367
+ };
368
+ const getONESToken = async () => {
369
+ var _store$ones_token;
370
+ const store = await getStore();
371
+ return (_store$ones_token = store.ones_token) !== null && _store$ones_token !== void 0 ? _store$ones_token : "";
372
+ };
373
+ const setONESToken = async (token) => {
374
+ return mergeStore({
375
+ ones_token: token
376
+ });
377
+ };
378
+ const invokeTunnel = async (port) => {
379
+ console.log("invokeTunnel", port);
380
+ const appManifestJSON = getAppManifestJSON();
381
+ console.log("appManifestJSON", appManifestJSON);
382
+ const regionURL = await getRegionURL();
383
+ const onesToken = await getONESToken();
384
+ console.log("regionURL", regionURL);
385
+ console.log("onesToken", onesToken);
386
+ };
387
+ const tunnel = async function() {
388
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
389
+ args[_key] = arguments[_key];
390
+ }
391
+ const {
392
+ options
393
+ } = getCommandOptions(args, tunnelCommandArguments);
394
+ const normalizedOptions = await normalize$8(options);
395
+ await invokeTunnel(normalizedOptions.port);
396
+ };
397
+ const config = {
398
+ defaultPort: {
399
+ login: 8200
400
+ }
401
+ };
402
+ const getConfig = () => config;
403
+ const getPublicPath = () => {
404
+ const __dirname = dirname(fileURLToPath(import.meta.url));
405
+ return join(__dirname, "../public");
406
+ };
407
+ function createPromise() {
408
+ let resolve2;
409
+ let reject;
410
+ const promise = new Promise((res, rej) => {
411
+ resolve2 = res;
412
+ reject = rej;
413
+ });
414
+ return {
415
+ promise,
416
+ resolve: resolve2,
417
+ reject
418
+ };
419
+ }
420
+ const sleep = (number) => {
421
+ return new Promise((resolve2) => {
422
+ setTimeout(resolve2, number);
423
+ });
424
+ };
425
+ const isURL = /^https?:\/\//;
426
+ const HostBlackList = ["ones.cn", "www.ones.cn", "ones.com", "www.ones.com"];
427
+ const normalize$7 = async (options) => {
428
+ const baseURLInput = options.baseURL;
429
+ if (baseURLInput)
430
+ ;
431
+ else {
432
+ return throwError(ErrorCode.MISSING_BASE_URL, i18n.t("error.login.missingBaseURL"));
433
+ }
434
+ if (isURL.test(baseURLInput))
435
+ ;
436
+ else {
437
+ return throwError(ErrorCode.INCORRECT_BASE_URL, i18n.t("error.login.incorrectBaseURL"));
438
+ }
439
+ let baseURL = "";
440
+ try {
441
+ const url = new URL(baseURLInput);
442
+ const host = url.hostname;
443
+ if (_includesInstanceProperty(HostBlackList).call(HostBlackList, host)) {
444
+ return throwError(ErrorCode.INCORRECT_BASE_URL, i18n.t("error.login.incorrectBaseURL"));
445
+ }
446
+ url.pathname = "";
447
+ url.search = "";
448
+ url.hash = "";
449
+ baseURL = url.toString();
450
+ } catch (error) {
451
+ return throwError(ErrorCode.INCORRECT_BASE_URL, i18n.t("error.login.incorrectBaseURL"));
452
+ }
453
+ return {
454
+ baseURL
455
+ };
456
+ };
457
+ const login = async function() {
458
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
459
+ args[_key] = arguments[_key];
460
+ }
461
+ const {
462
+ options
463
+ } = getCommandOptions(args, loginCommandArguments);
464
+ const normalizedOptions = await normalize$7(options);
465
+ const base = normalizedOptions.baseURL;
466
+ const path = "project/oauth2.html";
467
+ const config2 = getConfig();
468
+ const port = await getPort({
469
+ port: config2.defaultPort.login
470
+ });
471
+ const uuid = v7();
472
+ const search = new URLSearchParams();
473
+ search.set("redirect_path", "/developer/cli");
474
+ search.set("redirect_url", `http://localhost:${port}`);
475
+ search.set("state", uuid);
476
+ const url = `${base}${path}?${search}`;
477
+ const publicPath = getPublicPath();
478
+ const htmlPath = join(publicPath, "callback.html");
479
+ const html = readFileSync(htmlPath, {
480
+ encoding: "utf-8"
481
+ });
482
+ const {
483
+ promise,
484
+ resolve: resolve2
485
+ } = createPromise();
486
+ const server = http.createServer((req, res) => {
487
+ var _req$url$split$, _req$url, _search2$get, _search2$get2;
488
+ const search2 = new URLSearchParams((_req$url$split$ = (_req$url = req.url) === null || _req$url === void 0 ? void 0 : _req$url.split("?")[1]) !== null && _req$url$split$ !== void 0 ? _req$url$split$ : "");
489
+ const state = search2.get("state");
490
+ const token = (_search2$get = search2.get("access_token")) !== null && _search2$get !== void 0 ? _search2$get : "";
491
+ const region = (_search2$get2 = search2.get("region_url")) !== null && _search2$get2 !== void 0 ? _search2$get2 : "";
492
+ if (state === uuid && token && region) {
493
+ resolve2({
494
+ base_url: base,
495
+ ones_token: token,
496
+ region_url: region
497
+ });
498
+ }
499
+ res.writeHead(200, {
500
+ "Content-Type": "text/html"
501
+ });
502
+ res.end(html);
503
+ });
504
+ server.listen(port);
505
+ promise.then(async (store) => {
506
+ var _store$base_url, _store$ones_token, _store$region_url;
507
+ await sleep(100);
508
+ server.closeAllConnections();
509
+ await setBaseURL((_store$base_url = store.base_url) !== null && _store$base_url !== void 0 ? _store$base_url : "");
510
+ await setONESToken((_store$ones_token = store.ones_token) !== null && _store$ones_token !== void 0 ? _store$ones_token : "");
511
+ await setRegionURL((_store$region_url = store.region_url) !== null && _store$region_url !== void 0 ? _store$region_url : "");
512
+ console.log("Logged in successfully!");
513
+ process.exit(0);
514
+ });
515
+ console.log("Logging into your ONES account...");
516
+ console.log(`Opening ${url}`);
517
+ ora("Waiting for authorization...").start();
518
+ await open(url);
519
+ };
520
+ const normalize$6 = async (options) => {
521
+ noop(options);
522
+ return {};
523
+ };
524
+ const logout = async function() {
525
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
526
+ args[_key] = arguments[_key];
527
+ }
528
+ const {
529
+ options
530
+ } = getCommandOptions(args, logoutCommandArguments);
531
+ const normalizedOptions = await normalize$6(options);
532
+ noop(normalizedOptions);
533
+ await setStore({});
534
+ console.log("Logged out successfully!");
535
+ };
536
+ const API = {
537
+ TOKEN_INFO: "/project/api/project/auth/token_info",
538
+ APP_LIST: "/platform/api/app/list",
539
+ APP_INSTALL: "/app/install",
540
+ APP_UNINSTALL: "/app/uninstall",
541
+ APP_ENABLE: "/platform/api/app/:installation_id/enable",
542
+ APP_DISABLE: "/platform/api/app/:installation_id/disable"
543
+ };
544
+ const getPath = (path, map2) => {
545
+ var _context;
546
+ return _reduceInstanceProperty(_context = path.split("/")).call(_context, (base, part) => {
547
+ if (/^:/.test(part)) {
548
+ return `${base}/${map2[part.slice(1)]}`;
549
+ }
550
+ return `${base}/${part}`;
551
+ }, "").slice(1);
552
+ };
553
+ const consoleUnauthorizedMessage = () => {
554
+ console.log("Not logged in");
555
+ console.log('Login with "ones login" command');
556
+ };
557
+ const getURL = async (path, pathMap, queryMap) => {
558
+ const base = await getRegionURL();
559
+ if (base) {
560
+ const query = new URLSearchParams(queryMap !== null && queryMap !== void 0 ? queryMap : {}).toString();
561
+ return `${base}${getPath(path, pathMap !== null && pathMap !== void 0 ? pathMap : {})}${query ? `?${query}` : ""}`;
562
+ }
563
+ consoleUnauthorizedMessage();
564
+ exit(1);
565
+ };
566
+ const getHeaders = async (value) => {
567
+ const token = await getONESToken();
568
+ if (token) {
569
+ return merge({
570
+ Authorization: `Bearer ${token}`
571
+ }, value);
572
+ }
573
+ consoleUnauthorizedMessage();
574
+ exit(1);
575
+ };
576
+ const handleError = (error) => {
577
+ var _error$response;
578
+ if (((_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.status) === 401) {
579
+ console.log(error.response.statusText);
580
+ consoleUnauthorizedMessage();
581
+ } else {
582
+ console.error(error);
583
+ }
584
+ return {};
585
+ };
586
+ const fetchAppBase = async (params) => {
587
+ var _params$url;
588
+ const url = await getURL((_params$url = params.url) !== null && _params$url !== void 0 ? _params$url : "", params.pathMap, params.queryMap);
589
+ const headers = await getHeaders(params.headers);
590
+ const response = await axios({
591
+ ...params,
592
+ url,
593
+ headers
594
+ });
595
+ return response.data;
596
+ };
597
+ const fetchAppList = async (appID) => {
598
+ return await fetchAppBase({
599
+ url: API.APP_LIST,
600
+ method: "GET",
601
+ queryMap: {
602
+ app_id: appID
603
+ }
604
+ }).catch(handleError);
605
+ };
606
+ const fetchTokenInfo = async () => {
607
+ return await fetchAppBase({
608
+ url: API.TOKEN_INFO,
609
+ method: "GET"
610
+ }).catch(handleError);
611
+ };
612
+ const fetchAppInstall = async () => {
613
+ return await fetchAppBase({
614
+ url: API.APP_INSTALL,
615
+ method: "POST"
616
+ }).catch(handleError);
617
+ };
618
+ const fetchAppUninstall = async () => {
619
+ return await fetchAppBase({
620
+ url: API.APP_UNINSTALL,
621
+ method: "POST"
622
+ }).catch(handleError);
623
+ };
624
+ const fetchAppEnable = async () => {
625
+ var _appList$data$0$insta, _appList$data;
626
+ const appID = getAppManifestJSON().app.id;
627
+ const appList = await fetchAppList(appID);
628
+ 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 : "";
629
+ return await fetchAppBase({
630
+ url: API.APP_ENABLE,
631
+ method: "POST",
632
+ pathMap: {
633
+ installation_id: installationID
634
+ }
635
+ }).catch(handleError);
636
+ };
637
+ const fetchAppDisable = async () => {
638
+ var _appList$data$0$insta2, _appList$data2;
639
+ const appID = getAppManifestJSON().app.id;
640
+ const appList = await fetchAppList(appID);
641
+ 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 : "";
642
+ return await fetchAppBase({
643
+ url: API.APP_DISABLE,
644
+ method: "POST",
645
+ pathMap: {
646
+ installation_id: installationID
647
+ }
648
+ }).catch(handleError);
649
+ };
650
+ const normalize$5 = async (options) => {
651
+ noop(options);
652
+ return {};
653
+ };
654
+ const whoami = async function() {
655
+ var _tokenInfo$user, _tokenInfo$user2;
656
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
657
+ args[_key] = arguments[_key];
658
+ }
659
+ const {
660
+ options
661
+ } = getCommandOptions(args, whoamiCommandArguments);
662
+ const normalizedOptions = await normalize$5(options);
663
+ noop(normalizedOptions);
664
+ const tokenInfo = await fetchTokenInfo();
665
+ const name2 = (_tokenInfo$user = tokenInfo.user) === null || _tokenInfo$user === void 0 ? void 0 : _tokenInfo$user.name;
666
+ const email = (_tokenInfo$user2 = tokenInfo.user) === null || _tokenInfo$user2 === void 0 ? void 0 : _tokenInfo$user2.email;
667
+ if (name2 && email) {
668
+ console.log(`${name2} <${email}>`);
669
+ } else {
670
+ consoleUnauthorizedMessage();
671
+ }
672
+ };
673
+ const normalize$4 = async (options) => {
674
+ noop(options);
675
+ return {};
676
+ };
677
+ const install = async function() {
678
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
679
+ args[_key] = arguments[_key];
680
+ }
681
+ const {
682
+ options
683
+ } = getCommandOptions(args, installCommandArguments);
684
+ const normalizedOptions = await normalize$4(options);
685
+ noop(normalizedOptions);
686
+ const result = await fetchAppInstall();
687
+ if (result.code === "OK") {
688
+ console.log("App installed successfully!");
689
+ } else {
690
+ console.error(result);
691
+ }
692
+ };
693
+ const normalize$3 = async (options) => {
694
+ noop(options);
695
+ return {};
696
+ };
697
+ const enable = async function() {
698
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
699
+ args[_key] = arguments[_key];
700
+ }
701
+ const {
702
+ options
703
+ } = getCommandOptions(args, enableCommandArguments);
704
+ const normalizedOptions = await normalize$3(options);
705
+ noop(normalizedOptions);
706
+ const result = await fetchAppEnable();
707
+ if (result.code === "OK") {
708
+ console.log("App enabled successfully!");
709
+ } else {
710
+ console.error(result);
711
+ }
712
+ };
713
+ const normalize$2 = async (options) => {
714
+ noop(options);
715
+ return {};
716
+ };
717
+ const disable = async function() {
718
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
719
+ args[_key] = arguments[_key];
720
+ }
721
+ const {
722
+ options
723
+ } = getCommandOptions(args, disableCommandArguments);
724
+ const normalizedOptions = await normalize$2(options);
725
+ noop(normalizedOptions);
726
+ const result = await fetchAppDisable();
727
+ if (result.code === "OK") {
728
+ console.log("App disabled successfully!");
729
+ } else {
730
+ console.error(result);
731
+ }
732
+ };
733
+ const normalize$1 = async (options) => {
734
+ noop(options);
735
+ return {};
736
+ };
737
+ const uninstall = async function() {
738
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
739
+ args[_key] = arguments[_key];
740
+ }
741
+ const {
742
+ options
743
+ } = getCommandOptions(args, uninstallCommandArguments);
744
+ const normalizedOptions = await normalize$1(options);
745
+ noop(normalizedOptions);
746
+ const result = await fetchAppUninstall();
747
+ if (result.code === "OK") {
748
+ console.log("App uninstalled successfully!");
749
+ } else {
750
+ console.error(result);
751
+ }
752
+ };
753
+ const normalize = async (options) => {
754
+ noop(options);
755
+ return {};
756
+ };
757
+ const legacy = async function() {
758
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
759
+ args[_key] = arguments[_key];
760
+ }
761
+ const {
762
+ options
763
+ } = getCommandOptions(args, legacyCommandArguments);
764
+ const normalizedOptions = await normalize(options);
765
+ console.log("legacy", normalizedOptions);
766
+ };
767
+ const buildCommandArguments = [];
768
+ const $build = new Command("build").description(i18n.t("desc.build")).option("-o, --output [file-path]", i18n.t("desc.build.output")).action(build);
769
+ addCommandUsage($build);
770
+ addCommandOutput($build);
771
+ const devCommandArguments = [];
772
+ const $dev = new Command("dev").description(i18n.t("desc.dev")).option("-i, --install [boolean]", i18n.t("desc.dev.install")).action(dev);
773
+ addCommandUsage($dev);
774
+ addCommandOutput($dev);
775
+ const tunnelCommandArguments = ["port"];
776
+ const $tunnel = new Command("tunnel").description(i18n.t("desc.tunnel")).argument("<port>", i18n.t("desc.tunnel.port")).action(tunnel);
777
+ addCommandUsage($tunnel);
778
+ addCommandOutput($tunnel);
779
+ const loginCommandArguments = ["baseURL"];
780
+ const $login = new Command("login").description(i18n.t("desc.login")).argument("<base-url>", i18n.t("desc.login.baseUrl")).action(login);
781
+ addCommandUsage($login);
782
+ addCommandOutput($login);
783
+ const logoutCommandArguments = [];
784
+ const $logout = new Command("logout").description(i18n.t("desc.logout")).action(logout);
785
+ addCommandUsage($logout);
786
+ addCommandOutput($logout);
787
+ const whoamiCommandArguments = [];
788
+ const $whoami = new Command("whoami").description(i18n.t("desc.whoami")).action(whoami);
789
+ addCommandUsage($whoami);
790
+ addCommandOutput($whoami);
791
+ const installCommandArguments = [];
792
+ const $install = new Command("install").description(i18n.t("desc.install")).action(install);
793
+ addCommandUsage($install);
794
+ addCommandOutput($install);
795
+ const enableCommandArguments = [];
796
+ const $enable = new Command("enable").description(i18n.t("desc.enable")).action(enable);
797
+ addCommandUsage($enable);
798
+ addCommandOutput($enable);
799
+ const disableCommandArguments = [];
800
+ const $disable = new Command("disable").description(i18n.t("desc.disable")).action(disable);
801
+ addCommandUsage($disable);
802
+ addCommandOutput($disable);
803
+ const uninstallCommandArguments = [];
804
+ const $uninstall = new Command("uninstall").description(i18n.t("desc.uninstall")).action(uninstall);
805
+ addCommandUsage($uninstall);
806
+ addCommandOutput($uninstall);
807
+ const $app = new Command("app").description(i18n.t("desc.app"));
808
+ $app.addCommand($install).addCommand($enable).addCommand($disable).addCommand($uninstall);
809
+ addCommandUsage($app);
810
+ addCommandOutput($app);
811
+ const legacyCommandArguments = [];
812
+ const $legacy = new Command("legacy").description(i18n.t("desc.legacy")).action(legacy);
813
+ addCommandUsage($legacy);
814
+ addCommandOutput($legacy);
815
+ const ones = new Command("ones");
816
+ ones.description(i18n.t("desc.ones")).addCommand($create).addCommand($build).addCommand($dev).addCommand($tunnel).addCommand($app).addCommand($login).addCommand($logout).addCommand($whoami).addCommand($legacy).configureHelp({
817
+ visibleCommands: (cmd) => {
818
+ const blackList = ["legacy"];
819
+ return cmd.commands.filter((command) => !_includesInstanceProperty(blackList).call(blackList, command.name()));
820
+ },
821
+ subcommandTerm: (subcommand) => {
822
+ return `${subcommand.name()} ${subcommand.usage()}`.trim();
823
+ }
824
+ });
825
+ addCommandOutput(ones);
826
+ const runCommandONES = async () => {
827
+ const command = ones;
828
+ setContext("command", command);
829
+ const version = `${getPackageJSON().version}`;
830
+ command.version(version);
831
+ await command.parse();
832
+ };
833
+ export {
834
+ runCommandONES
835
+ };