import _ from 'lodash'; import yargs from 'yargs'; function possibleArray(val: any) { let result; if (!_.isEmpty(val)) { if (_.isArray(val)) { result = val; } else { result = [val]; } } return result; } export function builder(yargs: yargs.Argv) { yargs .option('port', { alias: 'p', description: 'port', default: 3000 }) .options('host', { alias: 'h', description: 'hostname', default: 'localhost' }) .options('dir', { alias: 'd', description: 'root dir', default: process.cwd() }) .options('cms', { alias: 'c', description: 'CMS type', default: '', choices: ['', 'git', 'contentful', 'sanity'], deprecated: true, hidden: true }) .options('contentful-management-token', { description: 'Contentful management token', default: '', deprecated: true, hidden: true }) .options('contentful-space-id', { description: 'Contentful space ID', default: '', deprecated: true, hidden: true }) .options('contentful-preview-token', { description: 'Contentful preview token', default: '', deprecated: true, hidden: true }) .options('sanity-token', { description: 'Sanity token', default: '', deprecated: true, hidden: true }) .options('sanity-project-id', { description: 'Sanity project ID', default: '', deprecated: true, hidden: true }) .options('sanity-studio-path', { description: 'Sanity Studio path', default: '', deprecated: true, hidden: true }) .options('sanity-studio-url', { description: 'Sanity Studio URL', default: '', deprecated: true, hidden: true }) .options('sanity-dataset', { description: 'Sanity dataset', default: '', deprecated: true, hidden: true }) .options('runnable-dir', { description: 'runnable dir', default: '' }) .options('log-level', { description: 'log level', default: 'info' }) .options('open', { description: 'Auto open', type: 'boolean' }) .options('cloudinary-cloud-name', { description: 'Cloudinary Space Name', type: 'string' }) .options('cloudinary-api-key', { description: 'Cloudinary Api Key', type: 'string' }) .options('aprimo-tenant', { description: 'Aprimo Tenant', type: 'string' }) .options('csi-webhook-url', { description: 'CSI Webhook URL. This argument allows debugging webhooks locally. ' + "You need to create a public URL that forwards external webhooks to stackbit dev's internal URL: 'localhost:8090'. " + "You can use a tool like 'ngrok' and run 'ngrok http 8090' to create a public URL that forwards webhooks to stackbit dev. " + 'Ngrok will print the public URL it created (e.g., https://xyz.ngrok.io). ' + "Append the path '/_stackbit/onWebhook' to this URL, and assign it to this argument:\n" + '--csi-webhook-url=https://xyz.ngrok.io/_stackbit/onWebhook', type: 'string' }) .options('help', {}) .check((argv) => { if (argv.cms === 'contentful') { if (!argv.contentfulAccessToken && !argv.contentfulManagementToken) { throw new Error('Missing Contentful management token: --contentful-management-token '); } if (!argv.contentfulPreviewToken) { throw new Error('Missing Contentful preview token: --contentful-preview-token '); } if (!argv.contentfulSpaceId) { throw new Error('Missing Contentful space ID: --contentful-space-id '); } if (possibleArray(argv.contentfulSpaceId)?.length !== possibleArray(argv.contentfulPreviewToken)?.length) { throw new Error("Mismatch between number of space ID's and preview tokens provided."); } } else if (argv.cms === 'sanity') { if (!argv.sanityToken) { throw new Error('Missing Sanity token: --sanity-token '); } if (!argv.sanityProjectId) { throw new Error('Missing Sanity project ID: --sanity-project-id '); } } if (argv.cloudinaryCloudName && !argv.cloudinaryApiKey) { throw new Error('Missing cloudinary api key'); } return true; }); } export function handler(telemetryTrack?: (event: string, data?: any) => void, cliVersion?: string) { return async (argv: any) => { if (argv['log-level']) { process.env.LOG_LEVEL = argv['log-level']; } return require('./dev').start({ ssgPort: argv.port, ssgHost: argv.host, rootDir: argv.dir, autoOpen: argv.open, cmsType: argv.cms, runnableDir: argv['runnable-dir'], logLevel: argv['log-level'], contentfulAccessToken: argv.contentfulManagementToken || argv.contentfulAccessToken, contentfulSpaceIds: possibleArray(argv.contentfulSpaceId), contentfulPreviewTokens: possibleArray(argv.contentfulPreviewToken), sanityToken: argv.sanityToken, sanityStudioPath: argv.sanityStudioPath, sanityStudioUrl: argv.sanityStudioUrl, sanityDataset: argv.sanityDataset, sanityProjectId: argv.sanityProjectId, cloudinaryCloudName: argv.cloudinaryCloudName, cloudinaryApiKey: argv.cloudinaryApiKey, aprimoTenant: argv.aprimoTenant, csiWebhookUrl: argv.csiWebhookUrl, telemetryTrack, cliVersion }); }; }