|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as colors from 'colors'; |
| 5 | +import * as path from 'path'; |
| 6 | +import { FileSystem } from '@microsoft/node-core-library'; |
| 7 | +import { CommandLineAction } from '@microsoft/ts-command-line'; |
| 8 | + |
| 9 | +import { ApiExtractorCommandLine } from './ApiExtractorCommandLine'; |
| 10 | + |
| 11 | +const AE_CONFIG_FILENAME: string = 'api-extractor.json'; |
| 12 | + |
| 13 | +export class InitAction extends CommandLineAction { |
| 14 | + |
| 15 | + constructor(parser: ApiExtractorCommandLine) { |
| 16 | + super({ |
| 17 | + actionName: 'init', |
| 18 | + summary: `Create an ${AE_CONFIG_FILENAME} config file`, |
| 19 | + documentation: `Use this command when setting up API Extractor for a new project. It writes an` |
| 20 | + + ` ${AE_CONFIG_FILENAME} config file template with code comments that describe all the settings.` |
| 21 | + + ` The file will be written in the current directory.` |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + protected onDefineParameters(): void { // override |
| 26 | + // No parameters yet |
| 27 | + } |
| 28 | + |
| 29 | + protected onExecute(): Promise<void> { // override |
| 30 | + const inputFilePath: string = path.resolve(__dirname, '../schemas/api-extractor-template.json'); |
| 31 | + const outputFilePath: string = path.resolve(AE_CONFIG_FILENAME); |
| 32 | + |
| 33 | + if (FileSystem.exists(outputFilePath)) { |
| 34 | + console.log(colors.red('The output file already exists:')); |
| 35 | + console.log('\n ' + outputFilePath + '\n'); |
| 36 | + throw new Error('Unable to write output file'); |
| 37 | + } |
| 38 | + |
| 39 | + console.log(colors.green('Writing file: ') + outputFilePath); |
| 40 | + FileSystem.copyFile({ |
| 41 | + sourcePath: inputFilePath, |
| 42 | + destinationPath: outputFilePath |
| 43 | + }); |
| 44 | + |
| 45 | + console.log('\nThe recommended location for this file is in the project\'s "config" subfolder,\n' |
| 46 | + + 'or else in the top-level folder with package.json.'); |
| 47 | + |
| 48 | + return Promise.resolve(); |
| 49 | + } |
| 50 | +} |
0 commit comments