Skip to content

Commit

Permalink
feat(tsfmt): support --verify option closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jun 28, 2015
1 parent ac7f787 commit 8dd0f8e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
35 changes: 35 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toS

interface RootOptions {
replace:boolean;
verify:boolean;
stdin:boolean;
tslint:boolean;
editorconfig:boolean;
Expand All @@ -27,13 +28,15 @@ var root = commandpost
.create<RootOptions, RootArguments>("tsfmt [files...]")
.version(packageJson.version, "-v, --version")
.option("-r, --replace", "replace .ts file")
.option("--verify", "checking file format")
.option("--stdin", "get formatting content from stdin")
.option("--no-tslint", "don't read a tslint.json")
.option("--no-editorconfig", "don't read a .editorconfig")
.option("--no-tsfmt", "don't read a tsfmt.json")
.option("--verbose", "makes output more verbose")
.action((opts, args)=> {
var replace = !!opts.replace;
var verify = !!opts.verify;
var stdin = !!opts.stdin;
var tslint = !!opts.tslint;
var editorconfig = !!opts.editorconfig;
Expand All @@ -46,6 +49,7 @@ var root = commandpost

if (opts.verbose) {
console.log("replace: " + (replace ? "ON" : "OFF"));
console.log("verify: " + (verify ? "ON" : "OFF"));
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
Expand All @@ -60,19 +64,28 @@ var root = commandpost
lib
.processStream(args.files[0] || "temp.ts", process.stdin, {
replace: replace,
verify: verify,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt
})
.then(result => {
var resultMap: lib.ResultMap = {};
resultMap[result.fileName] = result;
return resultMap;
})
.then(showResultHandler)
.catch(errorHandler);
} else {
lib
.processFiles(args.files, {
replace: replace,
verify: verify,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt
})
.then(showResultHandler)
.catch(errorHandler);
}
});
Expand All @@ -81,6 +94,28 @@ commandpost
.exec(root, process.argv)
.catch(errorHandler);

function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
"use strict";

var hasError = Object.keys(resultMap).filter(fileName => resultMap[fileName].error).length !== 0;
if (hasError) {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.filter(result => result.error)
.forEach(result => console.error(result.message));
process.exit(1);
} else {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.forEach(result => {
if (result.message) {
console.log(result.message);
}
});
}
return null;
}

function errorHandler(err:any): Promise<any> {
"use strict";

Expand Down
30 changes: 24 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Options {
dryRun?: boolean;
verbose?: boolean;
replace: boolean;
verify: boolean;
tslint: boolean;
editorconfig: boolean;
tsfmt: boolean;
Expand All @@ -30,6 +31,8 @@ export interface ResultMap {
export interface Result {
fileName: string;
options: ts.FormatCodeOptions;
message: string;
error: boolean;
src: string;
dest: string;
}
Expand All @@ -40,9 +43,15 @@ export function processFiles(files:string[], opts:Options):Promise<ResultMap> {
var resultMap:ResultMap = {};
var promises = files.map(fileName => {
if (!fs.existsSync(fileName)) {
console.error(fileName + " is not exists. process abort.");
process.exit(1);
return;
var result:Result = {
fileName: fileName,
options: null,
message: `${fileName} is not exists. process abort.`,
error: true,
src: "",
dest: ""
};
return Promise.resolve(result);
}

var content = fs.readFileSync(fileName).toString();
Expand Down Expand Up @@ -97,18 +106,27 @@ export function processString(fileName:string, content:string, opts:Options):Pro
.then(()=> {
var formattedCode = formatter(content, options);
// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
if (opts && opts.replace) {
var message: string;
var error = false;
if (opts && opts.verify) {
if (content !== formattedCode) {
message = `${fileName} is not formatted`;
error = true;
}
} else if (opts && opts.replace) {
if (content !== formattedCode) {
fs.writeFileSync(fileName, formattedCode);
console.log("replaced " + fileName);
message = `replaced ${fileName}`;
}
} else if (opts && !opts.dryRun) {
console.log(formattedCode);
message = formattedCode;
}

var result:Result = {
fileName: fileName,
options: options,
message: message,
error: error,
src: content,
dest: formattedCode
};
Expand Down
22 changes: 21 additions & 1 deletion test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ describe("tsfmt test", () => {
.processFiles([fileName], {
dryRun: true,
replace: false,
verify: false,
tslint: true,
editorconfig: true,
tsfmt: true
})
.then(resultMap => {
var result = resultMap[fileName];
assert(result !== null);
assert(result.error === false);

var expectedTsFileName = fileName.replace(fixtureDir, expectedDir);
// console.log(fileName, expectedFileName);
Expand Down Expand Up @@ -131,8 +133,24 @@ describe("tsfmt test", () => {
});
});
});
});

it("verify unformatted file", () => {
var fileName = "./test/fixture/tsfmt/a/main.ts";
return lib
.processFiles([fileName], {
dryRun: true,
replace: false,
verify: true,
tslint: true,
editorconfig: true,
tsfmt: true
})
.then(resultMap => {
assert(resultMap[fileName].error);
assert(resultMap[fileName].message === "./test/fixture/tsfmt/a/main.ts is not formatted");
});
});
});

describe("processStream function", () => {
var fileName = "test/fixture/default/main.ts";
Expand All @@ -144,12 +162,14 @@ describe("tsfmt test", () => {
.processStream(fileName, input, {
dryRun: true,
replace: false,
verify: false,
tslint: true,
editorconfig: true,
tsfmt: true
})
.then(result=> {
assert(result !== null);
assert(result.error === false);
assert(result.dest === `class Sample { getString(): string { return "hi!"; } }`);
});
});
Expand Down
3 changes: 3 additions & 0 deletions typescript-formatter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module 'typescript-formatter' {
dryRun?: boolean;
verbose?: boolean;
replace: boolean;
verify: boolean;
tslint: boolean;
editorconfig: boolean;
tsfmt: boolean;
Expand All @@ -18,6 +19,8 @@ declare module 'typescript-formatter' {
export interface Result {
fileName: string;
options: ts.FormatCodeOptions;
message: string;
error: boolean;
src: string;
dest: string;
}
Expand Down

0 comments on commit 8dd0f8e

Please sign in to comment.