Skip to content

Commit

Permalink
Add project compiler option #24 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Vinícius Rubido authored May 8, 2019
1 parent 75ba62d commit ccac475
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ tplant --input sample/Classes/Greeter.ts --output sample/Classes/Greeter.puml
### -o, --output <path>
Define the path of the output file. If not defined, it'll output on the STDOUT

### -c, --compositions
### -p, --project <path>
Compile a project given a valid configuration file.
The argument can be a file path to a valid JSON configuration file, or a directory path to a directory containing a tsconfig.json file.

### -C, --compositions
Create not heritage compositions.
Example:
```typescript
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tplant",
"version": "2.1.1",
"version": "2.1.2",
"description": "Typescript to PlantUML",
"keywords": [
"Class Diagram",
Expand Down
8 changes: 4 additions & 4 deletions src/generateDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
} from './ISerializeSymbol';

// tslint:disable-next-line max-func-body-length
export function generateDocumentation(fileNames: ReadonlyArray<string>, options: ts.CompilerOptions = {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2015
}): (ISerializeInterface | ISerializeEnum | ISerializeClass)[] {
export function generateDocumentation(
fileNames: ReadonlyArray<string>,
options: ts.CompilerOptions = ts.getDefaultCompilerOptions()
): (ISerializeInterface | ISerializeEnum | ISerializeClass)[] {

// Build a program using the set of root file names in fileNames
const program: ts.Program = ts.createProgram(fileNames, options);
Expand Down
94 changes: 85 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#!/usr/bin/env node

import commander from 'commander';
import { writeFile } from 'fs';
import fs from 'fs';
import G from 'glob';
import os from 'os';
import path from 'path';
import ts from 'typescript';
import { convertToPlant } from './convertToPlant';
import { generateDocumentation } from './generateDocumentation';

commander
.version('2.1.1')
.version('2.1.2')
.usage('[options]')
.option('-i, --input <path>', 'input file')
.option('-o, --output <path>', 'output file')
.option('-c, --compositions', 'create not heritage compositions')
.option('-I, --only-interfaces', 'only output interfaces')
.option('-i, --input <path>', 'Define the path of the Typescript file')
.option('-o, --output <path>', 'Define the path of the output file. If not defined, it\'ll output on the STDOUT')
.option(
'-p, --project <path>',
'Compile a project given a valid configuration file.' +
' The argument can be a file path to a valid JSON configuration file,' +
' or a directory path to a directory containing a tsconfig.json file.'
)
.option('-C, --compositions', 'Create not heritage compositions')
.option('-I, --only-interfaces', 'Only output interfaces')
.parse(process.argv);

if (!commander.input) {
console.error('missing input file');
console.error('Missing input file');
process.exit(1);
}

Expand All @@ -27,8 +36,10 @@ G(<string>commander.input, {}, (err: Error | null, matches: string[]): void => {
return;
}

const tsConfigFile: string | undefined = findTsConfigFile(<string>commander.input, <string | undefined>commander.tsconfig);

const output: string = convertToPlant(
generateDocumentation(matches),
generateDocumentation(matches, getCompilerOptions(tsConfigFile)),
{
compositions: <boolean>commander.compositions,
onlyInterfaces: <boolean>commander.onlyInterfaces
Expand All @@ -41,7 +52,8 @@ G(<string>commander.input, {}, (err: Error | null, matches: string[]): void => {
return;
}

writeFile(<string>commander.output, output, (errNoException: NodeJS.ErrnoException | null): void => {
// tslint:disable-next-line non-literal-fs-path
fs.writeFile(<string>commander.output, output, (errNoException: NodeJS.ErrnoException | null): void => {
if (errNoException !== null) {
console.error(errNoException);

Expand All @@ -51,3 +63,67 @@ G(<string>commander.input, {}, (err: Error | null, matches: string[]): void => {
console.log('The file was saved!');
});
});

function findTsConfigFile(inputPath: string, tsConfigPath?: string): string | undefined {
if (tsConfigPath !== undefined) {
// tslint:disable-next-line non-literal-fs-path
const tsConfigStats: fs.Stats = fs.statSync(tsConfigPath);
if (tsConfigStats.isFile()) {
return tsConfigPath;
}
if (tsConfigStats.isDirectory()) {
const tsConfigFilePath: string = path.resolve(tsConfigPath, 'tsconfig.json');
// tslint:disable-next-line non-literal-fs-path
if (fs.existsSync(tsConfigFilePath)) {
return tsConfigFilePath;
}
}
}

const localTsConfigFile: string = path.resolve(path.dirname(inputPath), 'tsconfig.json');
// tslint:disable-next-line non-literal-fs-path
if (fs.existsSync(localTsConfigFile)) {
return localTsConfigFile;
}

const cwdTsConfigFile: string = path.resolve(process.cwd(), 'tsconfig.json');
// tslint:disable-next-line non-literal-fs-path
if (fs.existsSync(cwdTsConfigFile)) {
return cwdTsConfigFile;
}
}

function getCompilerOptions(tsConfigFilePath?: string): ts.CompilerOptions {
if (tsConfigFilePath === undefined) {
return ts.getDefaultCompilerOptions();
}

const reader: (path: string) => string | undefined =
// tslint:disable-next-line non-literal-fs-path
(filePath: string): string | undefined => fs.readFileSync(filePath, 'utf8');
const configFile: { config?: { compilerOptions: ts.CompilerOptions }; error?: ts.Diagnostic } =
ts.readConfigFile(tsConfigFilePath, reader);

if (configFile.error !== undefined && configFile.error.category === ts.DiagnosticCategory.Error) {
throw new Error(`unable to read tsconfig.json file at: ${tsConfigFilePath}.
Error: ${ts.flattenDiagnosticMessageText(configFile.error.messageText, os.EOL)}`);
} else if (configFile.config === undefined) {
throw new Error(`unable to read tsconfig.json file at: ${tsConfigFilePath}.`);
}

const convertedCompilerOptions: {
options: ts.CompilerOptions;
errors: ts.Diagnostic[];
} = ts.convertCompilerOptionsFromJson(configFile.config.compilerOptions, path.dirname(tsConfigFilePath));

if (convertedCompilerOptions.errors.length > 0) {
convertedCompilerOptions.errors.forEach((error: ts.Diagnostic): void => {
if (error.category === ts.DiagnosticCategory.Error) {
throw new Error(`unable to read tsconfig.json file at: ${tsConfigFilePath}.
Error: ${ts.flattenDiagnosticMessageText(error.messageText, os.EOL)}`);
}
});
}

return convertedCompilerOptions.options;
}

0 comments on commit ccac475

Please sign in to comment.