UNPKG

@wix/cli

Version:

CLI tool for building Wix sites and applications

265 lines (258 loc) 6.62 kB
import { createRequire as _createRequire } from 'node:module'; const require = _createRequire(import.meta.url); import { listFiles, updateFiles } from "./chunk-IZ7XSGRJ.js"; import { AppType, DeploymentOperation, RcLabel, createApp, deployApp } from "./chunk-WYTLYOOV.js"; import { GridAppLayout } from "./chunk-6RDHKDZ4.js"; import { getResolveNpmDependenciesResult, resolveNpmDependencies } from "./chunk-3TTSPNEN.js"; import { useHttpClient } from "./chunk-NPIBHDAX.js"; import { createHttpClient, isHttpError, pRetry, pWaitFor } from "./chunk-PLPLA7N3.js"; import { require_react } from "./chunk-5A5B2WR4.js"; import { getTestOverrides } from "./chunk-WYHHEOWO.js"; import { z } from "./chunk-ZXYGJZOO.js"; import { CliError, CliErrorCode } from "./chunk-D5VYILRO.js"; import { __toESM, init_esm_shims } from "./chunk-4EFJZ3GQ.js"; // ../velo-client/src/useVeloClient.ts init_esm_shims(); var import_react = __toESM(require_react(), 1); // ../velo-client/src/velo-client.ts init_esm_shims(); // ../velo-client/src/schemas.ts init_esm_shims(); var createAppSchema = z.object({ app: z.object({ id: z.string() }) }); var listFilesSchema = z.object({ files: z.array( z.object({ path: z.string(), content: z.string() }) ), directories: z.array( z.object({ path: z.string() }) ) }); var deployAppSchema = z.object({ deploymentInfo: z.object({ deploymentId: z.string().uuid(), deploymentUrl: z.string().url(), deploymentShortUrl: z.string().url().optional(), isPublishPipelineDeployment: z.boolean() }) }); var resolveNpmDependenciesSchema = z.object({ jobId: z.string() }); var getResolveNpmDependenciesResultSchema = z.object({ result: z.object({ npmDependenciesLockFile: z.string().url() }) }); var getWixLockFileSchema = z.string(); // ../velo-client/src/velo-client.ts var { minRetryTimeout, pollInterval, pollTimeout } = getTestOverrides(); var VeloClient = class { constructor(httpClient) { this.httpClient = httpClient; this.retryOptions = { retries: 3, minTimeout: minRetryTimeout, maxTimeout: 3 * 1e3 }; } retryOptions; createMutableApp = async (content) => { try { const { data } = await pRetry( () => this.httpClient.request( createApp({ app: { mutable: true, appType: AppType.VELO_ISOLATED }, content }) ), this.retryOptions ); return createAppSchema.parse(data).app; } catch (e) { throw new CliError({ code: CliErrorCode.FailedToCreateVeloApp(), cause: e }); } }; listAppFiles = async (appId) => { try { const { data } = await pRetry( () => this.httpClient.request( listFiles({ gridAppId: appId, layout: GridAppLayout.GITHUB, hideDeletedPages: true, includeNewPages: false }) ), this.retryOptions ); return listFilesSchema.parse(data); } catch (e) { throw new CliError({ code: CliErrorCode.FailedToGetFilesFromVelo(), cause: e }); } }; updateAppFiles = async (appId, content) => { try { await pRetry( () => this.httpClient.request( updateFiles({ ...content, gridAppId: appId }) ), this.retryOptions ); } catch (e) { throw new CliError({ code: CliErrorCode.FailedToUpdateVeloAppFiles(), cause: e }); } }; deploySitePreview = async (deploymentSourceData) => { try { const { data } = await this.httpClient.request( deployApp({ ...deploymentSourceData, deploymentOperation: DeploymentOperation.RC, appType: AppType.VELO, rcDeploymentParams: { rcLabel: RcLabel.WIX_CLI } }) ); return deployAppSchema.parse(data).deploymentInfo; } catch (e) { throw new CliError({ code: CliErrorCode.FailedToDeploySitePreview(), cause: e }); } }; generateWixLockFile = async (deps) => { const jobId = await this.resolveNpmDependencies(deps); const wixLockFileUrl = await this.getResolveNpmDependenciesResult(jobId); return this.getWixLockFile(wixLockFileUrl); }; resolveNpmDependencies = async (npmPackageInfos) => { try { const { data } = await pRetry( () => this.httpClient.request(resolveNpmDependencies({ npmPackageInfos })), this.retryOptions ); const { jobId } = resolveNpmDependenciesSchema.parse(data); return jobId; } catch (e) { throw new CliError({ code: CliErrorCode.FailedToResolveNpmDependencies(), cause: e }); } }; getResolveNpmDependenciesResult = async (jobId) => { try { return await pWaitFor( async () => { try { const { data } = await pRetry( () => this.httpClient.request( getResolveNpmDependenciesResult({ jobId }) ), this.retryOptions ); const { result } = getResolveNpmDependenciesResultSchema.parse(data); return pWaitFor.resolveWith(result.npmDependenciesLockFile); } catch (e) { if (isHttpError(e) && e.response?.status === 428) { return false; } throw e; } }, { // Poll each 2 seconds interval: pollInterval ?? 2 * 1e3, // Fail if 2 minutes passed timeout: pollTimeout ?? 2 * 60 * 1e3 } ); } catch (e) { throw new CliError({ code: CliErrorCode.FailedToGetResolveNpmDependenciesResult(), cause: e, info: { jobId } }); } }; getWixLockFile = async (url) => { try { const standaloneHttpClient = createHttpClient({ type: "standalone" }); const { data } = await pRetry( () => standaloneHttpClient.get(url), this.retryOptions ); return getWixLockFileSchema.parse(data); } catch (e) { throw new CliError({ code: CliErrorCode.FailedToFetchWixLockFile(), cause: e }); } }; }; // ../velo-client/src/useVeloClient.ts function useVeloClient() { const httpClient = useHttpClient({ type: "code" }); return (0, import_react.useMemo)(() => new VeloClient(httpClient), [httpClient]); } // ../velo-client/src/index.ts init_esm_shims(); export { useVeloClient }; //# sourceMappingURL=chunk-N3R4KOEH.js.map