Skip to content

Commit e322fc7

Browse files
committed
feat(tsfmt): support --stdin option refs #9
1 parent 1921fdd commit e322fc7

File tree

4 files changed

+119
-44
lines changed

4 files changed

+119
-44
lines changed

lib/cli.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toS
1313

1414
interface RootOptions {
1515
replace:boolean;
16+
stdin:boolean;
1617
tslint:boolean;
1718
editorconfig:boolean;
1819
tsfmt:boolean;
@@ -27,26 +28,41 @@ var root = commandpost
2728
.create<RootOptions, RootArguments>("tsfmt [files...]")
2829
.version(packageJson.version, "-v, --version")
2930
.option("-r, --replace", "replace .ts file")
31+
.option("--stdin", "get formatting content from stdin")
3032
.option("--no-tslint", "don't read a tslint.json")
3133
.option("--no-editorconfig", "don't read a .editorconfig")
3234
.option("--no-tsfmt", "don't read a tsfmt.json")
3335
.option("--verbose", "makes output more verbose")
3436
.action((opts, args)=> {
3537
var replace = !!opts.replace;
38+
var stdin = !!opts.stdin;
3639
var tslint = !!opts.tslint;
3740
var editorconfig = !!opts.editorconfig;
3841
var tsfmt = !!opts.tsfmt;
3942

4043
if (args.files.length === 0) {
4144
process.stdout.write(root.helpText() + '\n');
42-
} else {
43-
if (opts.verbose) {
44-
console.log("replace: " + (replace ? "ON" : "OFF"));
45-
console.log("tslint: " + (tslint ? "ON" : "OFF"));
46-
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
47-
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
48-
}
45+
return;
46+
}
47+
48+
if (opts.verbose) {
49+
console.log("replace: " + (replace ? "ON" : "OFF"));
50+
console.log("stdin: " + (stdin ? "ON" : "OFF"));
51+
console.log("tslint: " + (tslint ? "ON" : "OFF"));
52+
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
53+
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
54+
}
4955

56+
if (opts.stdin) {
57+
lib
58+
.processStream(args.files[0], process.stdin, {
59+
replace: replace,
60+
tslint: tslint,
61+
editorconfig: editorconfig,
62+
tsfmt: tsfmt
63+
})
64+
.catch(errorHandler);
65+
} else {
5066
lib
5167
.processFiles(args.files, {
5268
replace: replace,

lib/index.ts

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,81 @@ export interface Result {
3636
export function processFiles(files:string[], opts:Options):Promise<ResultMap> {
3737
"use strict";
3838

39-
var result:ResultMap = {};
39+
var resultMap:ResultMap = {};
4040
var promises = files.map(fileName => {
4141
if (!fs.existsSync(fileName)) {
4242
console.error(fileName + " is not exists. process abort.");
4343
process.exit(1);
4444
return;
4545
}
46+
4647
var content = fs.readFileSync(fileName).toString();
48+
return processString(fileName, content, opts);
49+
});
50+
return Promise.all(promises).then(resultList=> {
51+
resultList.forEach(result => {
52+
resultMap[result.fileName] = result;
53+
});
54+
return resultMap;
55+
});
56+
}
4757

48-
var options = utils.createDefaultFormatCodeOptions();
49-
var optGenPromises:any[] = [];
50-
if (opts.tsfmt) {
51-
optGenPromises.push(base.makeFormatCodeOptions(fileName, options));
52-
}
53-
if (opts.editorconfig) {
54-
optGenPromises.push(editorconfig.makeFormatCodeOptions(fileName, options));
55-
}
56-
if (opts.tslint) {
57-
optGenPromises.push(tslintjson.makeFormatCodeOptions(fileName, options));
58-
}
59-
return Promise
60-
.all(optGenPromises)
61-
.then(()=> {
62-
var formattedCode = formatter(content, options);
63-
// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
64-
if (opts && opts.replace) {
65-
if (content !== formattedCode) {
66-
fs.writeFileSync(fileName, formattedCode);
67-
console.log("replaced " + fileName);
68-
}
69-
} else if (opts && !opts.dryRun) {
70-
console.log(formattedCode);
71-
}
72-
result[fileName] = {
73-
fileName: fileName,
74-
options: options,
75-
src: content,
76-
dest: formattedCode
77-
};
78-
});
58+
export function processStream(fileName:string, input:NodeJS.ReadableStream, opts:Options):Promise<Result> {
59+
"use strict";
60+
61+
input.setEncoding("utf8");
62+
63+
var promise = new Promise<string>((resolve, reject) => {
64+
var fragment = "";
65+
input.on("data", (chunk:string) => {
66+
if (chunk === "") {
67+
return;
68+
}
69+
fragment += chunk;
70+
});
71+
72+
input.on("end", () => {
73+
resolve(fragment);
74+
});
7975
});
80-
return Promise.all(promises).then(()=> result);
76+
return promise.then(content => processString(fileName, content, opts));
77+
}
78+
79+
export function processString(fileName:string, content:string, opts:Options):Promise<Result> {
80+
"use strict";
81+
82+
var options = utils.createDefaultFormatCodeOptions();
83+
var optGenPromises:(ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
84+
if (opts.tsfmt) {
85+
optGenPromises.push(base.makeFormatCodeOptions(fileName, options));
86+
}
87+
if (opts.editorconfig) {
88+
optGenPromises.push(editorconfig.makeFormatCodeOptions(fileName, options));
89+
}
90+
if (opts.tslint) {
91+
optGenPromises.push(tslintjson.makeFormatCodeOptions(fileName, options));
92+
}
93+
94+
return Promise
95+
.all(optGenPromises)
96+
.then(()=> {
97+
var formattedCode = formatter(content, options);
98+
// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
99+
if (opts && opts.replace) {
100+
if (content !== formattedCode) {
101+
fs.writeFileSync(fileName, formattedCode);
102+
console.log("replaced " + fileName);
103+
}
104+
} else if (opts && !opts.dryRun) {
105+
console.log(formattedCode);
106+
}
107+
108+
var result:Result = {
109+
fileName: fileName,
110+
options: options,
111+
src: content,
112+
dest: formattedCode
113+
};
114+
return Promise.resolve(result);
115+
});
81116
}

test/indexSpec.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ require("es6-promise").polyfill();
66

77
import assert = require("power-assert");
88

9-
// collision between node.d.ts to typescriptServices
10-
var fs = require("fs");
11-
var path = require("path");
12-
var childProcess = require("child_process");
9+
import fs = require("fs");
10+
import path = require("path");
11+
import childProcess = require("child_process");
12+
import stream = require("stream");
1313

1414
import lib = require("../lib/index");
1515

@@ -133,4 +133,26 @@ describe("tsfmt test", () => {
133133
});
134134
});
135135
});
136+
137+
138+
describe("processStream function", () => {
139+
var fileName = "test/fixture/default/main.ts";
140+
it(fileName, () => {
141+
var input = new stream.Readable();
142+
input.push(`class Sample{getString():string{return "hi!";}}`);
143+
input.push(null);
144+
return lib
145+
.processStream(fileName, input, {
146+
dryRun: true,
147+
replace: false,
148+
tslint: true,
149+
editorconfig: true,
150+
tsfmt: true
151+
})
152+
.then(result=> {
153+
assert(result !== null);
154+
assert(result.dest === `class Sample { getString(): string { return "hi!"; } }`);
155+
});
156+
});
157+
});
136158
});

typescript-formatter.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ declare module 'typescript-formatter' {
2222
dest: string;
2323
}
2424
export function processFiles(files: string[], opts: Options): Promise<ResultMap>;
25+
export function processStream(fileName: string, input: NodeJS.ReadableStream, opts: Options): Promise<Result>;
26+
export function processString(fileName: string, content: string, opts: Options): Promise<Result>;
2527
}
2628

0 commit comments

Comments
 (0)