Skip to content

Commit 025c543

Browse files
committed
feat(tsfmt): add --useTsconfig, --useTsfmt, --useTslint options to CLI closes #67
1 parent a180905 commit 025c543

File tree

22 files changed

+300
-18
lines changed

22 files changed

+300
-18
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ $ tsfmt --help
88

99
Options:
1010

11-
-r, --replace replace .ts file
12-
--verify checking file format
13-
--baseDir <path> config file lookup from <path>
14-
--stdin get formatting content from stdin
15-
--no-tsconfig don't read a tsconfig.json
16-
--no-tslint don't read a tslint.json
17-
--no-editorconfig don't read a .editorconfig
18-
--no-vscode don't read a .vscode/settings.json
19-
--no-tsfmt don't read a tsfmt.json
20-
--verbose makes output more verbose
11+
-r, --replace replace .ts file
12+
--verify checking file format
13+
--baseDir <path> config file lookup from <path>
14+
--stdin get formatting content from stdin
15+
--no-tsconfig don't read a tsconfig.json
16+
--no-tslint don't read a tslint.json
17+
--no-editorconfig don't read a .editorconfig
18+
--no-vscode don't read a .vscode/settings.json
19+
--no-tsfmt don't read a tsfmt.json
20+
--useTsconfig <path> using specified config file insteaf of tsconfig.json
21+
--useTslint <path> using specified config file insteaf of tslint.json
22+
--useTsfmt <path> using specified config file insteaf of tsfmt.json
23+
--verbose makes output more verbose
2124
```
2225
2326
## Installation

lib/cli.ts

+25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
}
1313

1414
import * as fs from "fs";
15+
import * as path from "path";
1516
import * as commandpost from "commandpost";
1617

1718
import * as lib from "./";
@@ -29,6 +30,9 @@ interface RootOptions {
2930
editorconfig: boolean;
3031
vscode: boolean;
3132
tsfmt: boolean;
33+
useTsconfig: string[];
34+
useTslint: string[];
35+
useTsfmt: string[];
3236
verbose: boolean;
3337
}
3438

@@ -48,6 +52,9 @@ let root = commandpost
4852
.option("--no-editorconfig", "don't read a .editorconfig")
4953
.option("--no-vscode", "don't read a .vscode/settings.json")
5054
.option("--no-tsfmt", "don't read a tsfmt.json")
55+
.option("--useTsconfig <path>", "using specified config file insteaf of tsconfig.json")
56+
.option("--useTslint <path>", "using specified config file insteaf of tslint.json")
57+
.option("--useTsfmt <path>", "using specified config file insteaf of tsfmt.json")
5158
.option("--verbose", "makes output more verbose")
5259
.action((opts, args) => {
5360
let replace = !!opts.replace;
@@ -59,6 +66,9 @@ let root = commandpost
5966
let editorconfig = !!opts.editorconfig;
6067
let vscode = !!opts.vscode;
6168
let tsfmt = !!opts.tsfmt;
69+
let tsconfigFile = opts.useTsconfig[0] ? path.join(process.cwd(), opts.useTsconfig[0]) : null;
70+
let tslintFile = opts.useTslint[0] ? path.join(process.cwd(), opts.useTslint[0]) : null;
71+
let tsfmtFile = opts.useTsfmt[0] ? path.join(process.cwd(), opts.useTsfmt[0]) : null;
6272
let verbose = !!opts.verbose;
6373

6474
let files = args.files;
@@ -86,10 +96,19 @@ let root = commandpost
8696
console.log("stdin: " + (stdin ? "ON" : "OFF"));
8797
console.log("files from tsconfig: " + (useTsconfig ? "ON" : "OFF"));
8898
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
99+
if (tsconfigFile) {
100+
console.log("specified tsconfig.json: " + tsconfigFile);
101+
}
89102
console.log("tslint: " + (tslint ? "ON" : "OFF"));
103+
if (tslintFile) {
104+
console.log("specified tslint.json: " + tslintFile);
105+
}
90106
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
91107
console.log("vscode: " + (vscode ? "ON" : "OFF"));
92108
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
109+
if (tsfmtFile) {
110+
console.log("specified tsfmt.json: " + tsfmtFile);
111+
}
93112
}
94113

95114
if (stdin) {
@@ -103,10 +122,13 @@ let root = commandpost
103122
verify: verify,
104123
baseDir: baseDir,
105124
tsconfig: tsconfig,
125+
tsconfigFile: tsconfigFile,
106126
tslint: tslint,
127+
tslintFile: tslintFile,
107128
editorconfig: editorconfig,
108129
vscode: vscode,
109130
tsfmt: tsfmt,
131+
tsfmtFile: tsfmtFile,
110132
verbose: verbose,
111133
})
112134
.then(result => {
@@ -123,10 +145,13 @@ let root = commandpost
123145
verify: verify,
124146
baseDir: baseDir,
125147
tsconfig: tsconfig,
148+
tsconfigFile: tsconfigFile,
126149
tslint: tslint,
150+
tslintFile: tslintFile,
127151
editorconfig: editorconfig,
128152
vscode: vscode,
129153
tsfmt: tsfmt,
154+
tsfmtFile: tsfmtFile,
130155
verbose: verbose,
131156
})
132157
.then(showResultHandler)

lib/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ export interface Options {
1919
replace: boolean;
2020
verify: boolean;
2121
tsconfig: boolean;
22+
tsconfigFile: string | null;
2223
tslint: boolean;
24+
tslintFile: string | null;
2325
editorconfig: boolean;
2426
vscode: boolean;
2527
tsfmt: boolean;
28+
tsfmtFile: string | null;
2629
}
2730

2831
export interface OptionModifier {

lib/provider/base.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ interface TsfmtSettings {
3838

3939
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
4040
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
41-
let configFileName = getConfigFileName(baseDir, "tsfmt.json");
41+
let configFileName: string | null;
42+
if (opts.tsfmtFile && path.isAbsolute(opts.tsfmtFile)) {
43+
configFileName = opts.tsfmtFile;
44+
} else {
45+
configFileName = getConfigFileName(baseDir, opts.tsfmtFile || "tsfmt.json");
46+
}
4247
if (!configFileName) {
4348
return formatSettings;
4449
}

lib/provider/tsconfigjson.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { getConfigFileName, parseJSON } from "../utils";
99
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
1010

1111
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
12-
let configFileName = getConfigFileName(baseDir, "tsconfig.json");
12+
let configFileName: string | null;
13+
if (opts.tsconfigFile && path.isAbsolute(opts.tsconfigFile)) {
14+
configFileName = opts.tsconfigFile;
15+
} else {
16+
configFileName = getConfigFileName(baseDir, opts.tsconfigFile || "tsconfig.json");
17+
}
1318
if (!configFileName) {
1419
return formatSettings;
1520
}

lib/provider/tslintjson.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ export interface AdditionalFormatSettings {
3232
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
3333

3434
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
35-
let configFileName = getConfigFileName(baseDir, "tslint.json");
35+
let configFileName: string | null;
36+
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
37+
configFileName = opts.tslintFile;
38+
} else {
39+
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
40+
}
3641
if (!configFileName) {
3742
return formatSettings;
3843
}
44+
3945
if (opts.verbose) {
4046
console.log(`read ${configFileName} for ${fileName}`);
4147
}
@@ -75,7 +81,12 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
7581
export function postProcess(fileName: string, formattedCode: string, opts: Options, _formatSettings: ts.FormatCodeSettings): string {
7682

7783
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
78-
let configFileName = getConfigFileName(baseDir, "tslint.json");
84+
let configFileName: string | null;
85+
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
86+
configFileName = opts.tslintFile;
87+
} else {
88+
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
89+
}
7990
if (!configFileName) {
8091
return formattedCode;
8192
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\n",
6+
"convertTabsToSpaces": true,
7+
"insertSpaceAfterCommaDelimiter": true,
8+
"insertSpaceAfterSemicolonInForStatements": true,
9+
"insertSpaceBeforeAndAfterBinaryOperators": true,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
15+
"placeOpenBraceOnNewLineForFunctions": false,
16+
"placeOpenBraceOnNewLineForControlBlocks": false
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word: string = "world"): string { return "Hello, " + word; }
3+
}
4+
5+
var s: Sample = new Sample();
6+
if (s === s) { console.log(s.hello()); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\n",
6+
"convertTabsToSpaces": true,
7+
"insertSpaceAfterCommaDelimiter": true,
8+
"insertSpaceAfterSemicolonInForStatements": true,
9+
"insertSpaceBeforeAndAfterBinaryOperators": true,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
15+
"placeOpenBraceOnNewLineForFunctions": true,
16+
"placeOpenBraceOnNewLineForControlBlocks": true
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Sample
2+
{
3+
hello( word: string = "world" ): string { return "Hello, " + word; }
4+
}
5+
6+
var s: Sample = new Sample();
7+
if ( s === s ) { console.log( s.hello() ); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\r\n",
6+
"convertTabsToSpaces": false,
7+
"insertSpaceAfterCommaDelimiter": true,
8+
"insertSpaceAfterSemicolonInForStatements": true,
9+
"insertSpaceBeforeAndAfterBinaryOperators": true,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
15+
"placeOpenBraceOnNewLineForFunctions": false,
16+
"placeOpenBraceOnNewLineForControlBlocks": false
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word: string = "world"): string { return "Hello, " + word; }
3+
}
4+
5+
var s: Sample = new Sample();
6+
if (s === s) { console.log(s.hello()); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"newLine": "LF"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word:string="world"):string{return "Hello, " + word;}
3+
}
4+
5+
var s:Sample=new Sample();
6+
if(s===s){console.log(s.hello());}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"newLine": "CRLF"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\n",
6+
"convertTabsToSpaces": true,
7+
"insertSpaceAfterCommaDelimiter": true,
8+
"insertSpaceAfterSemicolonInForStatements": true,
9+
"insertSpaceBeforeAndAfterBinaryOperators": true,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
15+
"placeOpenBraceOnNewLineForFunctions": true,
16+
"placeOpenBraceOnNewLineForControlBlocks": true
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word:string="world"):string{return "Hello, " + word;}
3+
}
4+
5+
var s:Sample=new Sample();
6+
if(s===s){console.log(s.hello());}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\r\n",
6+
"convertTabsToSpaces": false,
7+
"insertSpaceAfterCommaDelimiter": false,
8+
"insertSpaceAfterSemicolonInForStatements": false,
9+
"insertSpaceBeforeAndAfterBinaryOperators": false,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": false,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
15+
"placeOpenBraceOnNewLineForFunctions": false,
16+
"placeOpenBraceOnNewLineForControlBlocks": false
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"indent": [true, "tabs"]
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word:string="world"):string{return "Hello, " + word;}
3+
}
4+
5+
var s:Sample=new Sample();
6+
if(s===s){console.log(s.hello());}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"indent": [true, "spaces"]
4+
}
5+
}

0 commit comments

Comments
 (0)