zoho-sites-dev-cli 0.0.0-stage → 1.0.0

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