Skip to content

Commit

Permalink
feat(tsfmt): add tsconfig.json support. thanks @robertknight #22
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Sep 22, 2015
1 parent 984c447 commit cb52bd4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
11 changes: 9 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import commandpost = require("commandpost");
import path = require("path");

import lib = require("./index");
import utils = require("./utils");

var packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());

Expand Down Expand Up @@ -41,8 +42,13 @@ var root = commandpost
var tsfmt = !!opts.tsfmt;

var files = args.files;
if (files.length === 0 && fs.existsSync("tsconfig.json")) {
files = readFilesFromTsconfig("tsconfig.json");
var useTsconfig = false;
if (files.length === 0) {
var configFileName = utils.getConfigFileName(process.cwd(), "tsconfig.json");
if (configFileName) {
files = readFilesFromTsconfig(configFileName);
useTsconfig = true;
}
}

if (files.length === 0 && !opts.stdin) {
Expand All @@ -54,6 +60,7 @@ var root = commandpost
console.log("replace: " + (replace ? "ON" : "OFF"));
console.log("verify: " + (verify ? "ON" : "OFF"));
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("tsconfig: " + (useTsconfig ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
Expand Down
18 changes: 2 additions & 16 deletions lib/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@ import ts = require("typescript");
import path = require("path");
import fs = require("fs");

function getConfigFileName(baseFileName: string, configFileName: string): string {
"use strict";

var baseDir = path.dirname(baseFileName);

if (fs.existsSync(baseDir + path.sep + configFileName)) {
return baseDir + path.sep + configFileName;
}

if (baseDir.length === path.dirname(baseDir).length) {
return null;
}

return getConfigFileName(baseDir, configFileName);
}
import utils = require("../utils");

interface TsfmtSettings {
// from FormatCodeOptions
Expand Down Expand Up @@ -50,7 +36,7 @@ interface TsfmtSettings {
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

var configFileName = getConfigFileName(path.resolve(fileName), "tsfmt.json");
var configFileName = utils.getConfigFileName(path.dirname(path.resolve(fileName)), "tsfmt.json");
if (!configFileName) {
return options;
}
Expand Down
18 changes: 2 additions & 16 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@ import ts = require("typescript");
import path = require("path");
import fs = require("fs");

function getConfigFileName(baseFileName: string, configFileName: string): string {
"use strict";

var baseDir = path.dirname(baseFileName);

if (fs.existsSync(baseDir + path.sep + configFileName)) {
return baseDir + path.sep + configFileName;
}

if (baseDir.length === path.dirname(baseDir).length) {
return null;
}

return getConfigFileName(baseDir, configFileName);
}
import utils = require("../utils");

interface TslintSettings {
rules: {
Expand All @@ -42,7 +28,7 @@ interface TslintSettings {
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

var configFileName = getConfigFileName(path.resolve(fileName), "tslint.json");
var configFileName = utils.getConfigFileName(path.dirname(path.resolve(fileName)), "tslint.json");
if (!configFileName) {
return options;
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import ts = require("typescript");

import fs = require("fs");
import path = require("path");

export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
"use strict";

Expand All @@ -21,3 +24,18 @@ export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
PlaceOpenBraceOnNewLineForControlBlocks: false
};
}

export function getConfigFileName(baseDir: string, configFileName: string): string {
"use strict";

var configFilePath = path.resolve(baseDir, configFileName);
if (fs.existsSync(configFilePath)) {
return configFilePath;
}

if (baseDir.length === path.dirname(baseDir).length) {
return null;
}

return getConfigFileName(path.resolve(baseDir, "../"), configFileName);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
"./typings/power-assert/power-assert.d.ts",
"./node_modules/typescript/lib/lib.es6.d.ts"
]
}
}

0 comments on commit cb52bd4

Please sign in to comment.