Skip to content

Commit

Permalink
Added fail-on-class cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
timocov committed Jun 14, 2017
1 parent 7aed6ab commit 85195ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions src/bundle-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { verboseLog, normalLog } from './logger';

export interface GenerationOptions {
outputFilenames?: boolean;
failOnClass?: boolean;
}

const skippedNodes = [
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down

0 comments on commit 85195ea

Please sign in to comment.