Skip to content

Read list of files to format from tsconfig.json #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $ tsfmt

## Usage

### Format or verify specific TypeScript files

```bash
$ cat sample.ts
class Sample {hello(word="world"){return "Hello, "+word;}}
Expand Down Expand Up @@ -61,6 +63,17 @@ $ echo $?
1
```

### Reformat all files in a TypeScript project

If no files are specified on the command line but
a TypeScript project file (tsconfig.json) exists,
the list of files will be read from the project file.

```bash
# reads list of files to format from tsconfig.json
tsfmt -r
```

## Note

now `indentSize` parameter is ignored. it is TypeScript compiler matters.
Expand Down
25 changes: 22 additions & 3 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require("es6-promise").polyfill();

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

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

Expand Down Expand Up @@ -42,7 +43,12 @@ var root = commandpost
var editorconfig = !!opts.editorconfig;
var tsfmt = !!opts.tsfmt;

if (args.files.length === 0 && !opts.stdin) {
var files = args.files;
if (files.length === 0 && fs.existsSync("tsconfig.json")) {
files = readFilesFromTsconfig("tsconfig.json");
}

if (files.length === 0 && !opts.stdin) {
process.stdout.write(root.helpText() + '\n');
return;
}
Expand All @@ -62,7 +68,7 @@ var root = commandpost
return;
}
lib
.processStream(args.files[0] || "temp.ts", process.stdin, {
.processStream(files[0] || "temp.ts", process.stdin, {
replace: replace,
verify: verify,
tslint: tslint,
Expand All @@ -78,7 +84,7 @@ var root = commandpost
.catch(errorHandler);
} else {
lib
.processFiles(args.files, {
.processFiles(files, {
replace: replace,
verify: verify,
tslint: tslint,
Expand Down Expand Up @@ -129,3 +135,16 @@ function errorHandler(err: any): Promise<any> {
return null;
});
}

function readFilesFromTsconfig(configPath: string) {
"use strict";

var tsconfigDir = path.dirname(configPath);
var tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
if (tsconfig.files) {
var files: string[] = tsconfig.files;
return files.map(filePath => path.resolve(tsconfigDir, filePath));
} else {
throw new Error(`No "files" section present in tsconfig.json`);
}
}
5 changes: 5 additions & 0 deletions test/cli/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TestCLI {
method () {

}
}
14 changes: 14 additions & 0 deletions test/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1.5.3",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false
},
"files": [
"./main.ts"
]
}
Loading