zoho-sites-dev-cli 0.0.0-stage → 1.0.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 (53) hide show
  1. package/README.md +396 -2
  2. package/bin/zoho-sites-dev-cli.js +108 -0
  3. package/config/default.json +26 -0
  4. package/index.js +6 -0
  5. package/package.json +55 -4
  6. package/src/commands/auth.js +125 -0
  7. package/src/commands/create.js +175 -0
  8. package/src/commands/deploy.js +409 -0
  9. package/src/commands/generate-zip.js +49 -0
  10. package/src/commands/index.js +23 -0
  11. package/src/commands/init-tailwind.js +57 -0
  12. package/src/commands/init.js +449 -0
  13. package/src/commands/install.js +90 -0
  14. package/src/commands/login.js +253 -0
  15. package/src/commands/pull.js +41 -0
  16. package/src/commands/serve.js +163 -0
  17. package/src/compilers/css-compiler.js +230 -0
  18. package/src/compilers/css-ignore-loader.js +33 -0
  19. package/src/compilers/plugins/index.js +12 -0
  20. package/src/compilers/plugins/root-selector-transform-plugin.js +79 -0
  21. package/src/compilers/tailwind-compiler.js +102 -0
  22. package/src/compilers/webpack-compiler.js +512 -0
  23. package/src/compilers/webpack-config.js +103 -0
  24. package/src/config/constants.js +164 -0
  25. package/src/config/environments/audiences.js +19 -0
  26. package/src/config/environments/index.js +78 -0
  27. package/src/config/environments/internal.js +72 -0
  28. package/src/config/environments/production.js +54 -0
  29. package/src/locales/en.json +216 -0
  30. package/src/services/devServer.js +583 -0
  31. package/src/services/oauthService.js +796 -0
  32. package/src/templates/component/fields.json +6 -0
  33. package/src/templates/component/index.js +11 -0
  34. package/src/templates/component/llm.md +9 -0
  35. package/src/templates/component/meta.json +9 -0
  36. package/src/templates/component/styles.css +3 -0
  37. package/src/templates/oauth/complete.html +41 -0
  38. package/src/templates/oauth/error.html +69 -0
  39. package/src/templates/oauth/invalid-request.html +58 -0
  40. package/src/templates/oauth/loading.html +80 -0
  41. package/src/templates/oauth/not-found.html +33 -0
  42. package/src/templates/oauth/success.html +70 -0
  43. package/src/templates/oauth/timeout.html +56 -0
  44. package/src/templates/oauth/token-error.html +57 -0
  45. package/src/utils/cliName.js +35 -0
  46. package/src/utils/configUtil.js +67 -0
  47. package/src/utils/httpUtil.js +351 -0
  48. package/src/utils/i18n.js +222 -0
  49. package/src/utils/loadingUtil.js +190 -0
  50. package/src/utils/secureConfig.js +75 -0
  51. package/src/utils/secureStorage.js +230 -0
  52. package/src/utils/templateLoader.js +89 -0
  53. package/src/utils/urlUtil.js +372 -0
@@ -0,0 +1,372 @@
1
+ /**
2
+ * URL Utility - Centralized URL management for all environments
3
+ *
4
+ * Environments are resolved from the audience-scoped catalog in
5
+ * `src/config/environments/` (see that folder for the full list + values).
6
+ *
7
+ * Every environment has a fixed `authDomain`. `siteDomain` and
8
+ * `siteBuilderDomainTemplate` are fixed for most environments, but for
9
+ * Dev-1-only "prompt" environments (Csez / Localzoho when audience=sdk) they
10
+ * may be `null`, meaning the value was collected from the user at login time
11
+ * and is read back from SecureStorage (`dev_site_url` / `dev_site_builder_url`).
12
+ */
13
+
14
+ const { readFileSync, existsSync } = require("fs");
15
+ const { resolve } = require("path");
16
+ const { CONFIG_FILE_NAME, AUDIENCE } = require("../config/constants.js");
17
+ const {
18
+ ENVIRONMENT_IDS,
19
+ PROMPT_MODES,
20
+ getEnvironmentsForAudience,
21
+ getEnvironmentById,
22
+ isValidEnvironmentForAudience,
23
+ } = require("../config/environments/index.js");
24
+ const SecureStorage = require("./secureStorage.js");
25
+ const { getCliName } = require("./cliName.js");
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Environments (backward-compatible export; full list depends on audience)
29
+ // ---------------------------------------------------------------------------
30
+ const ENVIRONMENTS = ENVIRONMENT_IDS;
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // API endpoints
34
+ // ---------------------------------------------------------------------------
35
+ const BASE_URL = "api/v3";
36
+
37
+ const API_ENDPOINTS = {
38
+ SITES: {
39
+ LIST: `${BASE_URL}/index`,
40
+ DETAIL: `${BASE_URL}/sites/{siteId}`,
41
+ },
42
+ NAMESPACES: {
43
+ LIST: `${BASE_URL}/namespaces`,
44
+ QUERY: `${BASE_URL}/resources/query`,
45
+ },
46
+ APPS: {
47
+ LIST: `${BASE_URL}/apps`,
48
+ },
49
+ ACCOUNT: {
50
+ GET: `${BASE_URL}/account`,
51
+ },
52
+ WIDGETS: {
53
+ LIST: `${BASE_URL}/widgets`,
54
+ UPLOAD: `${BASE_URL}/widgets`,
55
+ },
56
+ COMPONENT_STATIC: {
57
+ DIST_META: "{namespace}/__widgets/dist/meta.json",
58
+ SRC_FILE: "{namespace}/__widgets/src/{component}/{filename}",
59
+ },
60
+ OAUTH: {
61
+ AUTHORIZE: "oauth/v2/auth",
62
+ TOKEN: "oauth/v2/token",
63
+ PROFILE: "oauth/user/info",
64
+ },
65
+ };
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // UrlUtil
69
+ // ---------------------------------------------------------------------------
70
+ class UrlUtil {
71
+ /**
72
+ * Get the current environment key from SecureStorage.
73
+ * @returns {Promise<string|null>}
74
+ */
75
+ static async getEnvironment() {
76
+ try {
77
+ const environment = await SecureStorage.getEnvironment();
78
+ if (environment && this.isValidEnvironment(environment)) {
79
+ return environment;
80
+ }
81
+ } catch (_) {
82
+ // ignore
83
+ }
84
+ return null;
85
+ }
86
+
87
+ /**
88
+ * Return true if `environment` is selectable for the current build audience.
89
+ */
90
+ static isValidEnvironment(environment) {
91
+ return isValidEnvironmentForAudience(AUDIENCE, environment);
92
+ }
93
+
94
+ /**
95
+ * Return the list of ids selectable for the current build audience.
96
+ * @returns {string[]}
97
+ */
98
+ static getValidEnvironmentIds() {
99
+ return getEnvironmentsForAudience(AUDIENCE).map((env) => env.id);
100
+ }
101
+
102
+ // -------------------------------------------------------------------------
103
+ // Domain URL resolution
104
+ // -------------------------------------------------------------------------
105
+
106
+ /**
107
+ * Resolve the auth domain for the current (or given) environment.
108
+ * Always fixed per environment entry.
109
+ * @param {string} [env] - override; defaults to stored environment
110
+ * @returns {Promise<string>}
111
+ */
112
+ static async resolveAuthDomain(env) {
113
+ const environment = env || (await this.getEnvironment());
114
+ if (!environment) throw new Error(`No environment configured. Please run \`${getCliName()} login\`.`);
115
+
116
+ const cfg = getEnvironmentById(AUDIENCE, environment);
117
+ if (!cfg) throw new Error(`Unknown environment: ${environment}`);
118
+ if (!cfg.authDomain) throw new Error(`Auth domain not configured for environment: ${environment}`);
119
+ return cfg.authDomain;
120
+ }
121
+
122
+ /**
123
+ * Resolve the site domain for the current (or given) environment.
124
+ * Falls back to the user-supplied value (Dev 1 only) when the environment's
125
+ * siteDomain is not fixed.
126
+ * @param {string} [env] - override; defaults to stored environment
127
+ * @returns {Promise<string>}
128
+ */
129
+ static async resolveSiteDomain(env) {
130
+ const environment = env || (await this.getEnvironment());
131
+ if (!environment) throw new Error(`No environment configured. Please run \`${getCliName()} login\`.`);
132
+
133
+ const cfg = getEnvironmentById(AUDIENCE, environment);
134
+ if (!cfg) throw new Error(`Unknown environment: ${environment}`);
135
+
136
+ if (cfg.siteDomain) return cfg.siteDomain;
137
+
138
+ const siteDomain = await SecureStorage.getDevSiteUrl();
139
+ if (!siteDomain) throw new Error(`Dev site URL not set. Please run \`${getCliName()} login\` again and provide the site URL.`);
140
+ return siteDomain;
141
+ }
142
+
143
+ /**
144
+ * Resolve the site builder domain for a given siteId.
145
+ * Falls back to the user-supplied template (Dev 1 only) when the
146
+ * environment's siteBuilderDomainTemplate is not fixed.
147
+ * @param {string} siteId
148
+ * @param {string} [env]
149
+ * @returns {Promise<string>}
150
+ */
151
+ static async resolveSiteBuilderDomain(siteId, env) {
152
+ const environment = env || (await this.getEnvironment());
153
+ if (!environment) throw new Error(`No environment configured. Please run \`${getCliName()} login\`.`);
154
+
155
+ const cfg = getEnvironmentById(AUDIENCE, environment);
156
+ if (!cfg) throw new Error(`Unknown environment: ${environment}`);
157
+
158
+ if (cfg.siteBuilderDomainTemplate) {
159
+ return cfg.siteBuilderDomainTemplate.replace("{siteId}", siteId);
160
+ }
161
+
162
+ // Dev 1 only: user-supplied builder URL, else fall back to the site domain.
163
+ const devSiteBuilderUrl = await SecureStorage.getDevSiteBuilderUrl();
164
+ if (devSiteBuilderUrl) {
165
+ return devSiteBuilderUrl.replace("{siteId}", siteId);
166
+ }
167
+ const siteDomain = await SecureStorage.getDevSiteUrl();
168
+ if (!siteDomain) throw new Error(`Dev site URL not set. Please run \`${getCliName()} login\` again.`);
169
+ return siteDomain;
170
+ }
171
+
172
+ // -------------------------------------------------------------------------
173
+ // Endpoint helpers
174
+ // -------------------------------------------------------------------------
175
+
176
+ /**
177
+ * Get API endpoint with parameter substitution.
178
+ * @param {string} endpointKey - dot-notation key e.g. 'SITES.LIST'
179
+ * @param {Object} params
180
+ * @returns {string}
181
+ */
182
+ static getApiEndpoint(endpointKey, params = {}) {
183
+ let endpoint;
184
+ if (endpointKey.includes(".")) {
185
+ const [category, action] = endpointKey.split(".");
186
+ endpoint = API_ENDPOINTS[category]?.[action];
187
+ } else {
188
+ endpoint = API_ENDPOINTS[endpointKey];
189
+ }
190
+
191
+ if (!endpoint) throw new Error(`Unknown API endpoint: ${endpointKey}`);
192
+
193
+ let finalEndpoint = endpoint;
194
+ for (const [key, value] of Object.entries(params)) {
195
+ finalEndpoint = finalEndpoint.replace(`{${key}}`, value);
196
+ }
197
+
198
+ const unreplaced = finalEndpoint.match(/\{[^}]+\}/g);
199
+ if (unreplaced) {
200
+ throw new Error(`Missing parameters for endpoint ${endpointKey}: ${unreplaced.join(", ")}`);
201
+ }
202
+
203
+ return finalEndpoint;
204
+ }
205
+
206
+ /**
207
+ * Build a complete URL from a base URL, endpoint key, and params.
208
+ * @private
209
+ */
210
+ static _buildUrlWithEndpoint(baseUrl, endpointKey, params = {}) {
211
+ const { query = {}, ...urlParams } = params;
212
+ const endpoint = this.getApiEndpoint(endpointKey, urlParams);
213
+ const fullUrl = `${baseUrl}/${endpoint}`;
214
+
215
+ if (Object.keys(query).length > 0) {
216
+ return `${fullUrl}?${new URLSearchParams(query).toString()}`;
217
+ }
218
+ return fullUrl;
219
+ }
220
+
221
+ // -------------------------------------------------------------------------
222
+ // Public URL builders
223
+ // -------------------------------------------------------------------------
224
+
225
+ /**
226
+ * Build a full auth-domain URL.
227
+ * @param {string} endpointKey
228
+ * @param {Object} params
229
+ * @returns {Promise<string>}
230
+ */
231
+ static async getAuthUrl(endpointKey, params = {}) {
232
+ const baseUrl = await this.resolveAuthDomain();
233
+ return this._buildUrlWithEndpoint(baseUrl, endpointKey, params);
234
+ }
235
+
236
+ /**
237
+ * Build a full auth-domain URL for a specific environment (used during login).
238
+ * @param {string} endpointKey
239
+ * @param {string} environment
240
+ * @param {Object} params
241
+ * @returns {Promise<string>}
242
+ */
243
+ static async getAuthUrlForEnv(endpointKey, environment, params = {}) {
244
+ const baseUrl = await this.resolveAuthDomain(environment);
245
+ return this._buildUrlWithEndpoint(baseUrl, endpointKey, params);
246
+ }
247
+
248
+ /**
249
+ * Build a full site-domain URL.
250
+ * @param {string} endpointKey
251
+ * @param {Object} params
252
+ * @returns {Promise<string>}
253
+ */
254
+ static async getSitesUrl(endpointKey, params = {}) {
255
+ const baseUrl = await this.resolveSiteDomain();
256
+ return this._buildUrlWithEndpoint(baseUrl, endpointKey, params);
257
+ }
258
+
259
+ /**
260
+ * Build a full site-builder-domain URL.
261
+ * @param {string} endpointKey
262
+ * @param {Object} params
263
+ * @returns {Promise<string>}
264
+ */
265
+ static async getSiteBuilderUrl(endpointKey, params = {}) {
266
+ const baseUrl = this.getSiteBuilderDomainUrl();
267
+ return this._buildUrlWithEndpoint(baseUrl, endpointKey, params);
268
+ }
269
+
270
+ /**
271
+ * Build a static component asset URL for a given environment + siteId.
272
+ * @param {string} endpointKey
273
+ * @param {Object} params - must include `environment` and `siteId`
274
+ * @returns {Promise<string>}
275
+ */
276
+ static async getComponentAssetUrlForSite(endpointKey, params = {}) {
277
+ const { environment, siteId, ...templateParams } = params;
278
+ if (!environment || !siteId) throw new Error("Missing required params: environment, siteId");
279
+ const baseUrl = await this.resolveSiteBuilderDomain(siteId, environment);
280
+ const path = this.getApiEndpoint(endpointKey, templateParams);
281
+ return `${baseUrl}/${path}`;
282
+ }
283
+
284
+ /**
285
+ * @deprecated Use resolveSiteBuilderDomain() instead.
286
+ */
287
+ static getSiteBuilderDomainUrlFor(environment, siteId) {
288
+ const cfg = getEnvironmentById(AUDIENCE, environment);
289
+ if (!cfg) throw new Error(`No static config for environment: ${environment}`);
290
+ if (!cfg.siteBuilderDomainTemplate) throw new Error(`No static site builder domain for environment: ${environment}. Use resolveSiteBuilderDomain() for Dev 1 prompt environments.`);
291
+ return cfg.siteBuilderDomainTemplate.replace("{siteId}", siteId);
292
+ }
293
+
294
+ /**
295
+ * Get site domain URL for a specific (static) environment.
296
+ * @param {string} environment
297
+ * @returns {string}
298
+ */
299
+ static getSiteDomainUrlFor(environment) {
300
+ if (!this.isValidEnvironment(environment)) throw new Error(`Invalid environment: ${environment}`);
301
+ const cfg = getEnvironmentById(AUDIENCE, environment);
302
+ if (!cfg || !cfg.siteDomain) throw new Error(`No static site domain for environment: ${environment}. Use resolveSiteDomain() for Dev 1 prompt environments.`);
303
+ return cfg.siteDomain;
304
+ }
305
+
306
+ // -------------------------------------------------------------------------
307
+ // Site builder config (from project .zs file)
308
+ // -------------------------------------------------------------------------
309
+
310
+ static readSiteBuilderConfig() {
311
+ const configPath = resolve(process.cwd(), CONFIG_FILE_NAME);
312
+
313
+ if (!existsSync(configPath)) {
314
+ throw new Error(
315
+ `SiteBuilder configuration not found. Please ensure ${CONFIG_FILE_NAME} file exists in your project directory.`
316
+ );
317
+ }
318
+
319
+ try {
320
+ const configContent = readFileSync(configPath, "utf8");
321
+ const config = JSON.parse(configContent);
322
+
323
+ if (!config.siteBuilderDomain) {
324
+ throw new Error(`SiteBuilder domain not configured in ${CONFIG_FILE_NAME}. Please add 'siteBuilderDomain' property.`);
325
+ }
326
+ return config;
327
+ } catch (error) {
328
+ if (error instanceof SyntaxError) {
329
+ throw new Error(`Invalid JSON in ${CONFIG_FILE_NAME}. Please check the file format.`);
330
+ }
331
+ throw error;
332
+ }
333
+ }
334
+
335
+ static getSiteBuilderDomainUrl() {
336
+ const config = this.readSiteBuilderConfig();
337
+ return config.siteBuilderDomain;
338
+ }
339
+
340
+ // -------------------------------------------------------------------------
341
+ // Environment list for prompts
342
+ // -------------------------------------------------------------------------
343
+
344
+ /**
345
+ * Return choices for the environment selection prompt, scoped to the
346
+ * current build audience.
347
+ */
348
+ static getEnvironmentOptions() {
349
+ return getEnvironmentsForAudience(AUDIENCE).map((env) => ({
350
+ name: env.label,
351
+ value: env.id,
352
+ url: env.authDomain,
353
+ promptMode: env.promptMode,
354
+ }));
355
+ }
356
+
357
+ // -------------------------------------------------------------------------
358
+ // Misc helpers
359
+ // -------------------------------------------------------------------------
360
+
361
+ /**
362
+ * Resolve the fixed auth domain string for a given environment key
363
+ * (synchronous; returns null if unknown for the current audience).
364
+ * Used internally for reverse-lookups and during the OAuth flow.
365
+ */
366
+ static getStaticAuthDomain(environment) {
367
+ const cfg = getEnvironmentById(AUDIENCE, environment);
368
+ return cfg ? cfg.authDomain : null;
369
+ }
370
+ }
371
+
372
+ module.exports = { UrlUtil, ENVIRONMENTS, PROMPT_MODES, API_ENDPOINTS };