Skip to content

Commit 17a16ac

Browse files
committed
feat(api): print tsc version when exec --version
1 parent 64ac10f commit 17a16ac

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/cli.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ try {
44
console.error("typescript is required. please try 'npm install -g typescript'\n");
55
}
66

7+
import * as ts from "typescript";
8+
79
import * as fs from "fs";
810
import * as path from "path";
911
import * as commandpost from "commandpost";
1012

1113
import * as lib from "./";
1214
import { getConfigFileName, readFilesFromTsconfig } from "./utils";
1315

14-
let packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());
16+
const packageJson = JSON.parse(fs.readFileSync(path.join( __dirname, "../package.json")).toString());
1517

1618
interface RootOptions {
1719
replace: boolean;
@@ -27,6 +29,7 @@ interface RootOptions {
2729
useTslint: string[];
2830
useTsfmt: string[];
2931
verbose: boolean;
32+
version: boolean;
3033
}
3134

3235
interface RootArguments {
@@ -35,7 +38,6 @@ interface RootArguments {
3538

3639
let root = commandpost
3740
.create<RootOptions, RootArguments>("tsfmt [files...]")
38-
.version(packageJson.version, "-v, --version")
3941
.option("-r, --replace", "replace .ts file")
4042
.option("--verify", "checking file format")
4143
.option("--baseDir <path>", "config file lookup from <path>")
@@ -49,6 +51,7 @@ let root = commandpost
4951
.option("--useTslint <path>", "using specified config file insteaf of tslint.json")
5052
.option("--useTsfmt <path>", "using specified config file insteaf of tsfmt.json")
5153
.option("--verbose", "makes output more verbose")
54+
.option("-v, --version", "output the version number")
5255
.action((opts, args) => {
5356
let replace = !!opts.replace;
5457
let verify = !!opts.verify;
@@ -63,6 +66,13 @@ let root = commandpost
6366
let tslintFile = opts.useTslint[0] ? path.join(process.cwd(), opts.useTslint[0]) : null;
6467
let tsfmtFile = opts.useTsfmt[0] ? path.join(process.cwd(), opts.useTsfmt[0]) : null;
6568
let verbose = !!opts.verbose;
69+
let version = !!opts.version;
70+
71+
if (version) {
72+
console.log(`tsfmt : ${packageJson.version}`);
73+
console.log(`tsc : ${ts.version}`);
74+
return;
75+
}
6676

6777
let files = args.files;
6878
let useTsconfig = false;

lib/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import { createDefaultFormatCodeSettings, parseJSON } from "./utils";
55
export { parseJSON };
66

77
import * as fs from "fs";
8+
import * as path from "path";
89

910
import base from "./provider/base";
1011
import tsconfigjson from "./provider/tsconfigjson";
1112
import editorconfig, { postProcess as editorconfigPostProcess } from "./provider/editorconfig";
1213
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";
1314
import vscodesettings from "./provider/vscodesettings";
1415

16+
const packageJson = JSON.parse(fs.readFileSync(path.join( __dirname, "../package.json")).toString());
17+
export const version = packageJson.version;
18+
1519
export interface Options {
1620
dryRun?: boolean;
1721
verbose?: boolean;

0 commit comments

Comments
 (0)