Skip to content

Commit

Permalink
feat(api): add baseDir options closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Sep 22, 2015
1 parent c425bac commit b69c4b6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toS
interface RootOptions {
replace: boolean;
verify: boolean;
baseDir: string[];
stdin: boolean;
tslint: boolean;
editorconfig: boolean;
Expand All @@ -28,6 +29,7 @@ let root = commandpost
.version(packageJson.version, "-v, --version")
.option("-r, --replace", "replace .ts file")
.option("--verify", "checking file format")
.option("--baseDir <path>", "config file lookup from <path>")
.option("--stdin", "get formatting content from stdin")
.option("--no-tslint", "don't read a tslint.json")
.option("--no-editorconfig", "don't read a .editorconfig")
Expand All @@ -36,6 +38,7 @@ let root = commandpost
.action((opts, args) => {
let replace = !!opts.replace;
let verify = !!opts.verify;
let baseDir = opts.baseDir ? opts.baseDir[0] : null;
let stdin = !!opts.stdin;
let tslint = !!opts.tslint;
let editorconfig = !!opts.editorconfig;
Expand All @@ -45,7 +48,7 @@ let root = commandpost
let files = args.files;
let useTsconfig = false;
if (files.length === 0) {
let configFileName = getConfigFileName(process.cwd(), "tsconfig.json");
let configFileName = getConfigFileName(baseDir || process.cwd(), "tsconfig.json");
if (configFileName) {
files = readFilesFromTsconfig(configFileName);
if (verbose) {
Expand All @@ -63,6 +66,7 @@ let root = commandpost
if (verbose) {
console.log("replace: " + (replace ? "ON" : "OFF"));
console.log("verify: " + (verify ? "ON" : "OFF"));
console.log("baseDir: " + (baseDir ? baseDir : process.cwd()));
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("tsconfig: " + (useTsconfig ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
Expand All @@ -79,6 +83,7 @@ let root = commandpost
.processStream(files[0] || "temp.ts", process.stdin, {
replace: replace,
verify: verify,
baseDir: baseDir,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt,
Expand All @@ -96,6 +101,7 @@ let root = commandpost
.processFiles(files, {
replace: replace,
verify: verify,
baseDir: baseDir,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt,
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import tslintjson from "./provider/tslintjson";
export interface Options {
dryRun?: boolean;
verbose?: boolean;
baseDir?: string;
replace: boolean;
verify: boolean;
tslint: boolean;
Expand Down
5 changes: 3 additions & 2 deletions lib/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ interface TsfmtSettings {
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

var configFileName = getConfigFileName(path.dirname(path.resolve(fileName)), "tsfmt.json");
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tsfmt.json");
if (!configFileName) {
return formatOptions;
}

if (opts.verbose) {
console.log(`read ${configFileName}`);
console.log(`read ${configFileName} for ${fileName}`);
}

var config: TsfmtSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
Expand Down
7 changes: 7 additions & 0 deletions lib/provider/editorconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import * as editorconfig from "editorconfig";

import {Options} from "../";

let emitBaseDirWarning = false;

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<ts.FormatCodeOptions> {
"use strict";

if (opts.verbose && opts.baseDir && !emitBaseDirWarning) {
console.log("editorconfig is not supported baseDir options");
emitBaseDirWarning = true;
}

return editorconfig
.parse(fileName)
.then(config => {
Expand Down
5 changes: 3 additions & 2 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ interface TslintSettings {
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

let configFileName = getConfigFileName(path.dirname(path.resolve(fileName)), "tslint.json");
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tslint.json");
if (!configFileName) {
return formatOptions;
}
if (opts.verbose) {
console.log(`read ${configFileName} for ${fileName}`);
console.log(`read ${configFileName} for ${fileName}`);
}

let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
Expand Down

0 comments on commit b69c4b6

Please sign in to comment.