Skip to content

Commit 5c0072a

Browse files
cspotcodevvakame
authored andcommitted
Adds --useVscode flag that specifies an alternative path to .vscode/settings.json configuration
1 parent 844955b commit 5c0072a

File tree

9 files changed

+57
-1
lines changed

9 files changed

+57
-1
lines changed

lib/cli.ts

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface RootOptions {
2828
useTsconfig: string[];
2929
useTslint: string[];
3030
useTsfmt: string[];
31+
useVscode: string[];
3132
verbose: boolean;
3233
version: boolean;
3334
}
@@ -50,6 +51,7 @@ let root = commandpost
5051
.option("--useTsconfig <path>", "using specified config file instead of tsconfig.json")
5152
.option("--useTslint <path>", "using specified config file instead of tslint.json")
5253
.option("--useTsfmt <path>", "using specified config file instead of tsfmt.json")
54+
.option("--useVscode <path>", "using specified config file instead of .vscode/settings.json")
5355
.option("--verbose", "makes output more verbose")
5456
.option("-v, --version", "output the version number")
5557
.action((opts, args) => {
@@ -64,6 +66,7 @@ let root = commandpost
6466
let tsfmt = !!opts.tsfmt;
6567
let tsconfigFile = opts.useTsconfig[0] ? path.join(process.cwd(), opts.useTsconfig[0]) : null;
6668
let tslintFile = opts.useTslint[0] ? path.join(process.cwd(), opts.useTslint[0]) : null;
69+
let vscodeFile = opts.useVscode[0] ? path.join(process.cwd(), opts.useVscode[0]) : null;
6770
let tsfmtFile = opts.useTsfmt[0] ? path.join(process.cwd(), opts.useTsfmt[0]) : null;
6871
let verbose = !!opts.verbose;
6972
let version = !!opts.version;
@@ -132,6 +135,9 @@ let root = commandpost
132135
}
133136
printSetting("editorconfig", editorconfig);
134137
printSetting("vscode", vscode);
138+
if (vscodeFile) {
139+
printSetting("specified vscode settings.json", vscodeFile);
140+
}
135141
printSetting("tsfmt", tsfmt);
136142
if (tsfmtFile) {
137143
printSetting("specified tsfmt.json", tsfmtFile);
@@ -156,6 +162,7 @@ let root = commandpost
156162
tslintFile: tslintFile,
157163
editorconfig: editorconfig,
158164
vscode: vscode,
165+
vscodeFile: vscodeFile,
159166
tsfmt: tsfmt,
160167
tsfmtFile: tsfmtFile,
161168
verbose: verbose,
@@ -179,6 +186,7 @@ let root = commandpost
179186
tslintFile: tslintFile,
180187
editorconfig: editorconfig,
181188
vscode: vscode,
189+
vscodeFile: vscodeFile,
182190
tsfmt: tsfmt,
183191
tsfmtFile: tsfmtFile,
184192
verbose: verbose,

lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface Options {
2828
tslintFile: string | null;
2929
editorconfig: boolean;
3030
vscode: boolean;
31+
vscodeFile: string | null;
3132
tsfmt: boolean;
3233
tsfmtFile: string | null;
3334
}

lib/provider/vscodesettings.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ interface VSCodeSettings {
3030
export function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
3131

3232
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
33-
let configFileName = getConfigFileName(baseDir, "./.vscode/settings.json");
33+
let configFileName: string | null;
34+
if (opts.vscodeFile && path.isAbsolute(opts.vscodeFile)) {
35+
configFileName = opts.vscodeFile;
36+
} else {
37+
configFileName = getConfigFileName(baseDir, opts.vscodeFile || ".vscode/settings.json");
38+
}
3439
if (!configFileName) {
3540
return formatSettings;
3641
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"baseIndentSize": 0,
3+
"indentSize": 4,
4+
"tabSize": 4,
5+
"indentStyle": 2,
6+
"newLineCharacter": "\r\n",
7+
"convertTabsToSpaces": true,
8+
"insertSpaceAfterCommaDelimiter": true,
9+
"insertSpaceAfterSemicolonInForStatements": true,
10+
"insertSpaceBeforeAndAfterBinaryOperators": true,
11+
"insertSpaceAfterConstructor": false,
12+
"insertSpaceAfterKeywordsInControlFlowStatements": false,
13+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
15+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
16+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
17+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
18+
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
19+
"insertSpaceAfterTypeAssertion": false,
20+
"insertSpaceBeforeFunctionParenthesis": false,
21+
"placeOpenBraceOnNewLineForFunctions": false,
22+
"placeOpenBraceOnNewLineForControlBlocks": false
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
if(true === false) { console.log('Hello world'); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": false
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
if(true===false){console.log('Hello world');}

test/indexSpec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ describe("tsfmt test", () => {
122122
tslintFile: null,
123123
editorconfig: true,
124124
vscode: true,
125+
vscodeFile: null,
125126
tsfmt: true,
126127
tsfmtFile: null,
127128
})
@@ -180,6 +181,7 @@ describe("tsfmt test", () => {
180181
tslintFile: null,
181182
editorconfig: true,
182183
vscode: true,
184+
vscodeFile: null,
183185
tsfmt: true,
184186
tsfmtFile: null,
185187
})
@@ -207,6 +209,7 @@ describe("tsfmt test", () => {
207209
tslintFile: null,
208210
editorconfig: true,
209211
vscode: true,
212+
vscodeFile: null,
210213
tsfmt: true,
211214
tsfmtFile: null,
212215
})
@@ -246,6 +249,13 @@ describe("tsfmt test", () => {
246249
},
247250
targetFile: "./test/fixture/specified-config/tsfmt/main.ts",
248251
},
252+
{
253+
name: "vscode settings.json",
254+
settings: {
255+
vscodeFile: "alt-vscode-settings.json",
256+
},
257+
targetFile: "./test/fixture/specified-config/vscode/main.ts",
258+
},
249259
];
250260

251261
list.forEach(matrix => {
@@ -261,6 +271,7 @@ describe("tsfmt test", () => {
261271
tslintFile: null,
262272
editorconfig: true,
263273
vscode: true,
274+
vscodeFile: null,
264275
tsfmt: true,
265276
tsfmtFile: null,
266277
}, matrix.settings))

0 commit comments

Comments
 (0)