Skip to content

Commit

Permalink
feat(tsfmt): add --useTsconfig, --useTsfmt, --useTslint options to CLI
Browse files Browse the repository at this point in the history
…closes #67
  • Loading branch information
vvakame committed Feb 27, 2017
1 parent a180905 commit 025c543
Show file tree
Hide file tree
Showing 22 changed files with 300 additions and 18 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ $ tsfmt --help

Options:

-r, --replace replace .ts file
--verify checking file format
--baseDir <path> config file lookup from <path>
--stdin get formatting content from stdin
--no-tsconfig don't read a tsconfig.json
--no-tslint don't read a tslint.json
--no-editorconfig don't read a .editorconfig
--no-vscode don't read a .vscode/settings.json
--no-tsfmt don't read a tsfmt.json
--verbose makes output more verbose
-r, --replace replace .ts file
--verify checking file format
--baseDir <path> config file lookup from <path>
--stdin get formatting content from stdin
--no-tsconfig don't read a tsconfig.json
--no-tslint don't read a tslint.json
--no-editorconfig don't read a .editorconfig
--no-vscode don't read a .vscode/settings.json
--no-tsfmt don't read a tsfmt.json
--useTsconfig <path> using specified config file insteaf of tsconfig.json
--useTslint <path> using specified config file insteaf of tslint.json
--useTsfmt <path> using specified config file insteaf of tsfmt.json
--verbose makes output more verbose
```
## Installation
Expand Down
25 changes: 25 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ try {
}

import * as fs from "fs";
import * as path from "path";
import * as commandpost from "commandpost";

import * as lib from "./";
Expand All @@ -29,6 +30,9 @@ interface RootOptions {
editorconfig: boolean;
vscode: boolean;
tsfmt: boolean;
useTsconfig: string[];
useTslint: string[];
useTsfmt: string[];
verbose: boolean;
}

Expand All @@ -48,6 +52,9 @@ let root = commandpost
.option("--no-editorconfig", "don't read a .editorconfig")
.option("--no-vscode", "don't read a .vscode/settings.json")
.option("--no-tsfmt", "don't read a tsfmt.json")
.option("--useTsconfig <path>", "using specified config file insteaf of tsconfig.json")
.option("--useTslint <path>", "using specified config file insteaf of tslint.json")
.option("--useTsfmt <path>", "using specified config file insteaf of tsfmt.json")
.option("--verbose", "makes output more verbose")
.action((opts, args) => {
let replace = !!opts.replace;
Expand All @@ -59,6 +66,9 @@ let root = commandpost
let editorconfig = !!opts.editorconfig;
let vscode = !!opts.vscode;
let tsfmt = !!opts.tsfmt;
let tsconfigFile = opts.useTsconfig[0] ? path.join(process.cwd(), opts.useTsconfig[0]) : null;
let tslintFile = opts.useTslint[0] ? path.join(process.cwd(), opts.useTslint[0]) : null;
let tsfmtFile = opts.useTsfmt[0] ? path.join(process.cwd(), opts.useTsfmt[0]) : null;
let verbose = !!opts.verbose;

let files = args.files;
Expand Down Expand Up @@ -86,10 +96,19 @@ let root = commandpost
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("files from tsconfig: " + (useTsconfig ? "ON" : "OFF"));
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
if (tsconfigFile) {
console.log("specified tsconfig.json: " + tsconfigFile);
}
console.log("tslint: " + (tslint ? "ON" : "OFF"));
if (tslintFile) {
console.log("specified tslint.json: " + tslintFile);
}
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
console.log("vscode: " + (vscode ? "ON" : "OFF"));
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
if (tsfmtFile) {
console.log("specified tsfmt.json: " + tsfmtFile);
}
}

if (stdin) {
Expand All @@ -103,10 +122,13 @@ let root = commandpost
verify: verify,
baseDir: baseDir,
tsconfig: tsconfig,
tsconfigFile: tsconfigFile,
tslint: tslint,
tslintFile: tslintFile,
editorconfig: editorconfig,
vscode: vscode,
tsfmt: tsfmt,
tsfmtFile: tsfmtFile,
verbose: verbose,
})
.then(result => {
Expand All @@ -123,10 +145,13 @@ let root = commandpost
verify: verify,
baseDir: baseDir,
tsconfig: tsconfig,
tsconfigFile: tsconfigFile,
tslint: tslint,
tslintFile: tslintFile,
editorconfig: editorconfig,
vscode: vscode,
tsfmt: tsfmt,
tsfmtFile: tsfmtFile,
verbose: verbose,
})
.then(showResultHandler)
Expand Down
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ export interface Options {
replace: boolean;
verify: boolean;
tsconfig: boolean;
tsconfigFile: string | null;
tslint: boolean;
tslintFile: string | null;
editorconfig: boolean;
vscode: boolean;
tsfmt: boolean;
tsfmtFile: string | null;
}

export interface OptionModifier {
Expand Down
7 changes: 6 additions & 1 deletion lib/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ interface TsfmtSettings {

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tsfmt.json");
let configFileName: string | null;
if (opts.tsfmtFile && path.isAbsolute(opts.tsfmtFile)) {
configFileName = opts.tsfmtFile;
} else {
configFileName = getConfigFileName(baseDir, opts.tsfmtFile || "tsfmt.json");
}
if (!configFileName) {
return formatSettings;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/provider/tsconfigjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { getConfigFileName, parseJSON } from "../utils";
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tsconfig.json");
let configFileName: string | null;
if (opts.tsconfigFile && path.isAbsolute(opts.tsconfigFile)) {
configFileName = opts.tsconfigFile;
} else {
configFileName = getConfigFileName(baseDir, opts.tsconfigFile || "tsconfig.json");
}
if (!configFileName) {
return formatSettings;
}
Expand Down
15 changes: 13 additions & 2 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ export interface AdditionalFormatSettings {
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tslint.json");
let configFileName: string | null;
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
configFileName = opts.tslintFile;
} else {
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
}
if (!configFileName) {
return formatSettings;
}

if (opts.verbose) {
console.log(`read ${configFileName} for ${fileName}`);
}
Expand Down Expand Up @@ -75,7 +81,12 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
export function postProcess(fileName: string, formattedCode: string, opts: Options, _formatSettings: ts.FormatCodeSettings): string {

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tslint.json");
let configFileName: string | null;
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
configFileName = opts.tslintFile;
} else {
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
}
if (!configFileName) {
return formattedCode;
}
Expand Down
17 changes: 17 additions & 0 deletions test/expected/specified-config/tsconfig/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/specified-config/tsconfig/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {
hello(word: string = "world"): string { return "Hello, " + word; }
}

var s: Sample = new Sample();
if (s === s) { console.log(s.hello()); }
17 changes: 17 additions & 0 deletions test/expected/specified-config/tsfmt/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
"placeOpenBraceOnNewLineForFunctions": true,
"placeOpenBraceOnNewLineForControlBlocks": true
}
7 changes: 7 additions & 0 deletions test/expected/specified-config/tsfmt/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Sample
{
hello( word: string = "world" ): string { return "Hello, " + word; }
}

var s: Sample = new Sample();
if ( s === s ) { console.log( s.hello() ); }
17 changes: 17 additions & 0 deletions test/expected/specified-config/tslint/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": false,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/specified-config/tslint/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {
hello(word: string = "world"): string { return "Hello, " + word; }
}

var s: Sample = new Sample();
if (s === s) { console.log(s.hello()); }
5 changes: 5 additions & 0 deletions test/fixture/specified-config/tsconfig/alt-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"newLine": "LF"
}
}
6 changes: 6 additions & 0 deletions test/fixture/specified-config/tsconfig/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {
hello(word:string="world"):string{return "Hello, " + word;}
}

var s:Sample=new Sample();
if(s===s){console.log(s.hello());}
5 changes: 5 additions & 0 deletions test/fixture/specified-config/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"newLine": "CRLF"
}
}
17 changes: 17 additions & 0 deletions test/fixture/specified-config/tsfmt/alt-tsfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
"placeOpenBraceOnNewLineForFunctions": true,
"placeOpenBraceOnNewLineForControlBlocks": true
}
6 changes: 6 additions & 0 deletions test/fixture/specified-config/tsfmt/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {
hello(word:string="world"):string{return "Hello, " + word;}
}

var s:Sample=new Sample();
if(s===s){console.log(s.hello());}
17 changes: 17 additions & 0 deletions test/fixture/specified-config/tsfmt/tsfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": false,
"insertSpaceAfterCommaDelimiter": false,
"insertSpaceAfterSemicolonInForStatements": false,
"insertSpaceBeforeAndAfterBinaryOperators": false,
"insertSpaceAfterKeywordsInControlFlowStatements": false,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
5 changes: 5 additions & 0 deletions test/fixture/specified-config/tslint/alt-tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"indent": [true, "tabs"]
}
}
6 changes: 6 additions & 0 deletions test/fixture/specified-config/tslint/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {
hello(word:string="world"):string{return "Hello, " + word;}
}

var s:Sample=new Sample();
if(s===s){console.log(s.hello());}
5 changes: 5 additions & 0 deletions test/fixture/specified-config/tslint/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"indent": [true, "spaces"]
}
}
Loading

0 comments on commit 025c543

Please sign in to comment.