Skip to content

Commit

Permalink
Adds --useVscode flag that specifies an alternative path to .vscode/s…
Browse files Browse the repository at this point in the history
…ettings.json configuration
  • Loading branch information
cspotcode authored and vvakame committed Oct 26, 2017
1 parent 844955b commit 5c0072a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface RootOptions {
useTsconfig: string[];
useTslint: string[];
useTsfmt: string[];
useVscode: string[];
verbose: boolean;
version: boolean;
}
Expand All @@ -50,6 +51,7 @@ let root = commandpost
.option("--useTsconfig <path>", "using specified config file instead of tsconfig.json")
.option("--useTslint <path>", "using specified config file instead of tslint.json")
.option("--useTsfmt <path>", "using specified config file instead of tsfmt.json")
.option("--useVscode <path>", "using specified config file instead of .vscode/settings.json")
.option("--verbose", "makes output more verbose")
.option("-v, --version", "output the version number")
.action((opts, args) => {
Expand All @@ -64,6 +66,7 @@ let root = commandpost
let tsfmt = !!opts.tsfmt;
let tsconfigFile = opts.useTsconfig[0] ? path.join(process.cwd(), opts.useTsconfig[0]) : null;
let tslintFile = opts.useTslint[0] ? path.join(process.cwd(), opts.useTslint[0]) : null;
let vscodeFile = opts.useVscode[0] ? path.join(process.cwd(), opts.useVscode[0]) : null;
let tsfmtFile = opts.useTsfmt[0] ? path.join(process.cwd(), opts.useTsfmt[0]) : null;
let verbose = !!opts.verbose;
let version = !!opts.version;
Expand Down Expand Up @@ -132,6 +135,9 @@ let root = commandpost
}
printSetting("editorconfig", editorconfig);
printSetting("vscode", vscode);
if (vscodeFile) {
printSetting("specified vscode settings.json", vscodeFile);
}
printSetting("tsfmt", tsfmt);
if (tsfmtFile) {
printSetting("specified tsfmt.json", tsfmtFile);
Expand All @@ -156,6 +162,7 @@ let root = commandpost
tslintFile: tslintFile,
editorconfig: editorconfig,
vscode: vscode,
vscodeFile: vscodeFile,
tsfmt: tsfmt,
tsfmtFile: tsfmtFile,
verbose: verbose,
Expand All @@ -179,6 +186,7 @@ let root = commandpost
tslintFile: tslintFile,
editorconfig: editorconfig,
vscode: vscode,
vscodeFile: vscodeFile,
tsfmt: tsfmt,
tsfmtFile: tsfmtFile,
verbose: verbose,
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Options {
tslintFile: string | null;
editorconfig: boolean;
vscode: boolean;
vscodeFile: string | null;
tsfmt: boolean;
tsfmtFile: string | null;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/provider/vscodesettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ interface VSCodeSettings {
export function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "./.vscode/settings.json");
let configFileName: string | null;
if (opts.vscodeFile && path.isAbsolute(opts.vscodeFile)) {
configFileName = opts.vscodeFile;
} else {
configFileName = getConfigFileName(baseDir, opts.vscodeFile || ".vscode/settings.json");
}
if (!configFileName) {
return formatSettings;
}
Expand Down
23 changes: 23 additions & 0 deletions test/expected/specified-config/vscode/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": false,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
1 change: 1 addition & 0 deletions test/expected/specified-config/vscode/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if(true === false) { console.log('Hello world'); }
3 changes: 3 additions & 0 deletions test/fixture/specified-config/vscode/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true
}
3 changes: 3 additions & 0 deletions test/fixture/specified-config/vscode/alt-vscode-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": false
}
1 change: 1 addition & 0 deletions test/fixture/specified-config/vscode/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if(true===false){console.log('Hello world');}
11 changes: 11 additions & 0 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -180,6 +181,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -207,6 +209,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -246,6 +249,13 @@ describe("tsfmt test", () => {
},
targetFile: "./test/fixture/specified-config/tsfmt/main.ts",
},
{
name: "vscode settings.json",
settings: {
vscodeFile: "alt-vscode-settings.json",
},
targetFile: "./test/fixture/specified-config/vscode/main.ts",
},
];

list.forEach(matrix => {
Expand All @@ -261,6 +271,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
}, matrix.settings))
Expand Down

0 comments on commit 5c0072a

Please sign in to comment.