|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -const commander = require('commander') |
| 3 | +import commander from 'commander' |
| 4 | +import chalk from 'chalk' |
| 5 | +import clear from 'clear' |
| 6 | +import figlet from 'figlet' |
| 7 | +import inquirer from 'inquirer' |
| 8 | +import { ALL_REGIONS, EU_CODE, isRegion } from '@storyblok/region-helper' |
| 9 | +import updateNotifier from 'update-notifier' |
| 10 | +import fs from 'fs' |
| 11 | +import tasks from './tasks' |
| 12 | +import { getQuestions, lastStep, api, creds } from './utils' |
| 13 | +import { SYNC_TYPES, COMMANDS } from './constants' |
| 14 | + |
| 15 | +const rawPkg = fs.readFileSync('./package.json') |
| 16 | +const pkg = JSON.parse(rawPkg) |
4 | 17 | const program = new commander.Command() |
5 | | - |
6 | | -const chalk = require('chalk') |
7 | | -const clear = require('clear') |
8 | | -const figlet = require('figlet') |
9 | | -const inquirer = require('inquirer') |
10 | | -const { ALL_REGIONS, EU_CODE, isRegion } = require('@storyblok/region-helper') |
11 | | - |
12 | | -const updateNotifier = require('update-notifier') |
13 | | -const pkg = require('../package.json') |
14 | | - |
15 | | -const tasks = require('./tasks') |
16 | | -const { getQuestions, lastStep, api, creds } = require('./utils') |
17 | | -const { SYNC_TYPES, COMMANDS } = require('./constants') |
18 | 18 | const allRegionsText = ALL_REGIONS.join(', ') |
19 | 19 |
|
20 | 20 | clear() |
@@ -523,6 +523,42 @@ program |
523 | 523 | } |
524 | 524 | }) |
525 | 525 |
|
| 526 | +// Generate Typescript type definitions |
| 527 | +program |
| 528 | + .command(COMMANDS.GENERATE_TYPESCRIPT_TYPEDEFS) |
| 529 | + // Providing backward-compatible flags with Storyblok Generate TS https://github.com/dohomi/storyblok-generate-ts |
| 530 | + .requiredOption('--source, --sourceFilePaths <PATHS>', 'Path(s) to the components JSON file(s) as comma separated values', (paths, _previous) => paths.split(',')) |
| 531 | + .option('--target, --destinationFilePath <PATH>', 'Path to the Typescript file that will be generated (default: `storyblok-component-types.d.ts`)') |
| 532 | + .option('--titlePrefix, --typeNamesPrefix <STRING>', 'A prefix that will be prepended to all the names of the generated types') |
| 533 | + .option('--titleSuffix, --typeNamesSuffix <STRING>', 'A suffix that will be appended to all the names of the generated types (*default*: `Storyblok`)') |
| 534 | + .option('--compilerOptions, --JSONSchemaToTSOptionsPath <PATH>', 'Path to a JSON file with a list of options supported by json-schema-to-typescript') |
| 535 | + .option('--customTypeParser, --customFieldTypesParserPath <PATH>', 'Path to the parser file for Custom Field Types') |
| 536 | + .action((options) => { |
| 537 | + console.log(`${chalk.blue('-')} Executing ${COMMANDS.GENERATE_TYPESCRIPT_TYPEDEFS} task`) |
| 538 | + |
| 539 | + const { |
| 540 | + sourceFilePaths, |
| 541 | + destinationFilePath, |
| 542 | + typeNamesPrefix, |
| 543 | + typeNamesSuffix, |
| 544 | + customFieldTypesParserPath, |
| 545 | + JSONSchemaToTSOptionsPath |
| 546 | + } = options |
| 547 | + |
| 548 | + try { |
| 549 | + tasks.generateTypescriptTypedefs({ |
| 550 | + sourceFilePaths, |
| 551 | + destinationFilePath, |
| 552 | + typeNamesPrefix, |
| 553 | + typeNamesSuffix, |
| 554 | + customFieldTypesParserPath, |
| 555 | + JSONSchemaToTSOptionsPath |
| 556 | + }) |
| 557 | + } catch (e) { |
| 558 | + errorHandler(e, COMMANDS.GENERATE_TYPESCRIPT_TYPEDEFS) |
| 559 | + } |
| 560 | + }) |
| 561 | + |
526 | 562 | program.parse(process.argv) |
527 | 563 |
|
528 | 564 | if (program.rawArgs.length <= 2) { |
|
0 commit comments