Skip to content

Adds --useVscode flag to override location of .vscode/settings.json #112

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -text
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/node": "^8.0.4",
"@types/power-assert": "^1.4.29",
"conventional-changelog-cli": "^1.3.2",
"cross-spawn": "^5.1.0",
"intelli-espower-loader": "^1.0.1",
"mkdirp": "^0.5.1",
"mocha": "^3.0.2",
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');}
39 changes: 34 additions & 5 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path = require("path");
import childProcess = require("child_process");
import stream = require("stream");
import mkdirp = require("mkdirp");
const crossSpawn = require("cross-spawn");

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

Expand All @@ -30,6 +31,23 @@ interface ExecResult {
stderr: string;
}

function normalizePathSeparators(p: string): string {
if (path.sep === "\\") {
return p.replace(/\\/g, "/");
} else {
return p;
}
}

function execTsfmt(args: string[], options: childProcess.SpawnOptions): Promise<ExecResult> {
const tsfmtBin = path.resolve("./bin/tsfmt");
if (process.platform === "win32") {
return exec("node", [tsfmtBin, ...args], options);
} else {
return exec(tsfmtBin, args, options);
}
}

function exec(cmd: string, args: string[], options: childProcess.SpawnOptions): Promise<ExecResult> {
let process = childProcess.spawn(cmd, args, options);

Expand All @@ -51,7 +69,7 @@ function exec(cmd: string, args: string[], options: childProcess.SpawnOptions):
}

function checkByTslint(configFileName: string, tsfileName: string, errorExpected: boolean): Promise<boolean> {
let process = childProcess.spawn("./node_modules/.bin/tslint", ["-c", configFileName, tsfileName]);
let process = crossSpawn.spawn(`./node_modules/.bin/tslint`, ["-c", configFileName, tsfileName]);

let stdout = "";
process.stdout.on("data", (data: any) => {
Expand Down Expand Up @@ -122,6 +140,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -180,6 +199,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -207,6 +227,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
})
Expand Down Expand Up @@ -246,6 +267,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 +289,7 @@ describe("tsfmt test", () => {
tslintFile: null,
editorconfig: true,
vscode: true,
vscodeFile: null,
tsfmt: true,
tsfmtFile: null,
}, matrix.settings))
Expand Down Expand Up @@ -294,7 +323,7 @@ describe("tsfmt test", () => {

describe("CLI test", () => {
it("should reformat files specified at files in tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve("./test/cli/files") }).then(result => {
return execTsfmt([], { cwd: path.resolve("./test/cli/files") }).then(result => {
assert.equal(result.status, 0);
assert.equal(result.stdout.trim(), `
class TestCLI {
Expand All @@ -307,7 +336,7 @@ class TestCLI {
});

it("should reformat files specified at include, exclude in tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve("./test/cli/includeExclude") }).then(result => {
return execTsfmt([], { cwd: path.resolve("./test/cli/includeExclude") }).then(result => {
assert.equal(result.status, 0);
assert.equal(result.stdout.trim(), `
export class TestCLI {
Expand All @@ -319,10 +348,10 @@ export class TestCLI {
});

it("should pickup files from --useTsconfig specified tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), ["--verify", "--useTsconfig", "./tsconfig.main.json"], { cwd: path.resolve("./test/cli/useTsconfig") }).then(result => {
return execTsfmt(["--verify", "--useTsconfig", "./tsconfig.main.json"], { cwd: path.resolve("./test/cli/useTsconfig") }).then(result => {
assert.equal(result.status, 1);
assert.equal(result.stdout.trim(), "");
assert.equal(result.stderr.replace(process.cwd(), "**").trim(), `
assert.equal(result.stderr.replace(normalizePathSeparators(process.cwd()), "**").trim(), `
**/test/cli/useTsconfig/include.ts is not formatted
`.trim().replace(/\n/g, "\r\n"));
});
Expand Down