diff --git a/README.md b/README.md index 1d5c23a..d35e7a1 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ npm install -g dts-bundle-generator ## Usage ``` -usage: dts-bundle-generator [-h] -o OUTFILE [-v] [--no-check] [--output-source-file] file +usage: dts-bundle-generator [-h] -o OUTFILE [-v] [--no-check] [--output-source-file] + [--fail-on-class] + file Positional arguments: file @@ -60,6 +62,7 @@ Optional arguments: -v, --verbose Enable verbose logging --no-check Skip validation of generated d.ts file --output-source-file Add comment with file path the definitions came from + --fail-on-class Fail if generated dts contains class declaration ``` Example: diff --git a/src/bundle-generator.ts b/src/bundle-generator.ts index 86b1158..6680aff 100644 --- a/src/bundle-generator.ts +++ b/src/bundle-generator.ts @@ -6,6 +6,7 @@ import { verboseLog, normalLog } from './logger'; export interface GenerationOptions { outputFilenames?: boolean; + failOnClass?: boolean; } const skippedNodes = [ @@ -66,6 +67,13 @@ export function generateDtsBundle(filePath: string, options: GenerationOptions = const hasNodeExportKeyword = hasNodeModifier(node, ts.SyntaxKind.ExportKeyword); if (node.kind === ts.SyntaxKind.ClassDeclaration || node.kind === ts.SyntaxKind.EnumDeclaration) { + if (options.failOnClass === true && node.kind === ts.SyntaxKind.ClassDeclaration) { + const classDecl = (node as ts.ClassDeclaration); + const className = classDecl.name ? classDecl.name.text : ''; + const errorMessage = `Class was found in generated dts.\n ${className} from ${sourceFile.fileName}`; + throw new Error(errorMessage); + } + // not all classes and enums can be exported - only exported from root file let shouldNodeHasExportKeyword = isDeclarationExported(rootFileExports, typeChecker, node as (ts.ClassDeclaration | ts.EnumDeclaration)); if (node.kind === ts.SyntaxKind.EnumDeclaration) { diff --git a/src/cli.ts b/src/cli.ts index 1ef6c14..bc39a34 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -49,6 +49,17 @@ parser.addArgument( }, ); +parser.addArgument( + ['--fail-on-class'], + { + action: 'storeTrue', + defaultValue: false, + dest: 'failOnClass', + help: 'Fail if generated dts contains class declaration', + type: Boolean, + }, +); + parser.addArgument(['file'], { nargs: 1 }); const args = parser.parseArgs(); @@ -58,7 +69,11 @@ if (args.verbose) { try { const fileName = args.file[0]; - const generatedDts = generateDtsBundle(fileName, { outputFilenames: args.outputSourceFileName }); + const generatedDts = generateDtsBundle(fileName, { + failOnClass: args.failOnClass, + outputFilenames: args.outputSourceFileName, + }); + normalLog(`Writing generated file to ${args.outFile}...`); fs.writeFileSync(args.outFile, generatedDts);