UNPKG

@agnostack/next-zcli

Version:

Please contact agnoStack via info@agnostack.com for any questions

167 lines 8.86 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withZendeskCLI = void 0; const chalk_1 = __importDefault(require("chalk")); const deepmerge_1 = __importDefault(require("deepmerge")); const next_plugins_1 = require("@agnostack/next-plugins"); const utils_1 = require("./utils"); // NOTE: this has the same value found in next/constants { PHASE_DEVELOPMENT_SERVER } - w/o hard dep so we dont pin to a version const PHASE_EXPORT = 'phase-export'; const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'; const withZendeskCLI = async (nextPhase, _a) => { var { nextConfig } = _a, config = __rest(_a, ["nextConfig"]); const _b = nextConfig !== null && nextConfig !== void 0 ? nextConfig : {}, { basePath, rewrites: _rewrites, headers: _headers, exportPathMap: _exportPathMap, publicRuntimeConfig: _publicRuntimeConfig = {}, serverRuntimeConfig: _serverRuntimeConfig = {} } = _b, _nextConfig = __rest(_b, ["basePath", "rewrites", "headers", "exportPathMap", "publicRuntimeConfig", "serverRuntimeConfig"]); const _c = config !== null && config !== void 0 ? config : {}, { // eslint-disable-next-line @typescript-eslint/no-unused-vars defaultConfig } = _c, // NOTE: This is here in case we need anything from it pluginConfig = __rest(_c, ["defaultConfig"]); const { translations, data: { asset_url_prefix = utils_1.ASSET_URL_PREFIX, } = {}, } = pluginConfig !== null && pluginConfig !== void 0 ? pluginConfig : {}; const _d = (0, utils_1.getZendeskData)({ asset_url_prefix: utils_1.ASSET_URL_PREFIX, pluginConfig, }), { zendeskData, manifestData } = _d, appData = __rest(_d, ["zendeskData", "manifestData"]); const _e = zendeskData !== null && zendeskData !== void 0 ? zendeskData : {}, { interactive, appRequirements, apiRoute: _apiRoute = '/api/apps' } = _e, _zendeskData = __rest(_e, ["interactive", "appRequirements", "apiRoute"]); const { version: appVersion, defaultLocale: appLocale, signedUrls: appSignedURLs, singleInstall: appSingleInstall, frameworkVersion: appFrameworkVersion, } = manifestData !== null && manifestData !== void 0 ? manifestData : {}; const { installationData: { id: _installationId, app_id: _appId, app_name: _appName, app_locations: appLocations, settings: appSettings, } = {}, safData: { metadata: { stripe_subscription_id: subscriptionId, plan: { name: subscriptionPlan, } = {}, } = {}, } = {}, } = appData !== null && appData !== void 0 ? appData : {}; const { id: installationId, app_id: appId, app_name: appName, apiRoute, } = await (0, next_plugins_1.getPrompts)([ { type: 'text', name: 'id', message: 'Installation ID', initial: _installationId, }, { type: 'text', name: 'app_id', message: 'Application ID', initial: _appId, }, { type: 'text', name: 'app_name', message: 'Application Name', initial: _appName, }, { type: 'text', name: 'apiRoute', message: 'API Route (/apps.json)', initial: _apiRoute, } ], { phase: nextPhase, disabled: !(0, utils_1.isTrue)(interactive) }); const isExportMode = nextPhase === PHASE_EXPORT; const isDevMode = nextPhase === PHASE_DEVELOPMENT_SERVER; // NOTE: this is to fix the issue w/ ZAS requiring local paths to start with /assets: https://github.com/zendesk/zendesk_apps_support/commit/458f077caafce67fb4a0676c26e8666f156f4286 const exportPathMap = async (defaultPathMap) => { const baseExportPathMap = _exportPathMap ? await _exportPathMap(defaultPathMap !== null && defaultPathMap !== void 0 ? defaultPathMap : {}) : defaultPathMap; if (!isExportMode || (asset_url_prefix === null || asset_url_prefix === void 0 ? void 0 : asset_url_prefix.startsWith('http'))) { return baseExportPathMap; } return Object.entries((0, utils_1.ensureObject)(baseExportPathMap)).reduce((pathMap, [path, value]) => { const cleanAssetURLPath = (0, utils_1.removeLeadingSlash)((0, utils_1.removeTrailingSlash)(asset_url_prefix)); const assetPath = (0, utils_1.cleanURL)((path === '/') ? `/${cleanAssetURLPath}/index` : `/${cleanAssetURLPath}${path}`); // eslint-disable-next-line max-len console.info(`${chalk_1.default.yellowBright('next-zcli')} - Added exportPathMap for: '${chalk_1.default.yellowBright(assetPath)}' to '${chalk_1.default.yellowBright(path)}'`); return Object.assign(Object.assign({}, pathMap), { [path]: value, [assetPath]: value }); }, {}); }; const headers = async () => { const baseHeaders = _headers ? await _headers() : []; if (!isDevMode) { return baseHeaders; } return [ ...baseHeaders, { source: '/app.json', headers: [ { key: 'Access-Control-Allow-Methods', value: 'GET', }, { key: 'Access-Control-Allow-Credentials', value: 'false', }, { key: 'Access-Control-Allow-Origin', value: '*', }, { key: 'Access-Control-Allow-Headers', value: '*', } ], } ]; }; // NOTE: the code below ONLY runs when running a local dev server! const rewrites = async () => { const _a = _rewrites ? await _rewrites() : {}, { beforeFiles = [] } = _a, baseRewrites = __rest(_a, ["beforeFiles"]); if (!isDevMode) { return baseRewrites; } const updatedRewrites = Object.assign(Object.assign({}, baseRewrites), { beforeFiles: [ ...beforeFiles, { source: '/app.json', destination: apiRoute, } ] }); return updatedRewrites; }; const _f = translations !== null && translations !== void 0 ? translations : {}, { app } = _f, translationData = __rest(_f, ["app"]); const serverRuntimeData = { jsonData: Object.assign(Object.assign({}, (0, utils_1.objectNotEmpty)(manifestData) && { 'manifest.json': manifestData, }), (0, utils_1.objectNotEmpty)(app) && { 'translations/en.json': Object.assign(Object.assign({}, translationData), { app: (0, deepmerge_1.default)({ short_description: 'Hi', installation_instructions: 'Hi', long_description: 'Hi' }, app) }), }), }; const publicRuntimeConfig = Object.assign(Object.assign({}, _publicRuntimeConfig), appData); const serverRuntimeConfig = Object.assign(Object.assign(Object.assign({}, _serverRuntimeConfig), serverRuntimeData), isDevMode && { subscriptionId, subscriptionPlan, installationId, appId, appName, appSettings, appRequirements, appLocale, appVersion, appSignedURLs, appSingleInstall, appFrameworkVersion, appLocations, }); if (isDevMode) { console.log(`${chalk_1.default.green('ready')} - add ${chalk_1.default.yellowBright(chalk_1.default.bold('?zcli_apps=true'))} to the end of your Zendesk URL to load this app on your Zendesk account!\n`); } return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (basePath && { assetPrefix: `${(0, utils_1.removeTrailingSlash)(basePath)}/`, // NOTE: assetPrefix requires the trailing slash })), _nextConfig), { publicRuntimeConfig, serverRuntimeConfig, basePath, exportPathMap }), !isExportMode && { headers, rewrites, }), (0, utils_1.objectNotEmpty)((0, utils_1.cleanObject)(_zendeskData, false, utils_1.stringEmptyOnly)) && { zendesk: _zendeskData, }); }; exports.withZendeskCLI = withZendeskCLI; //# sourceMappingURL=withZendeskCLI.js.map