From e0dcc32db69b380e62c2cd9b15471e8da4eed2ae Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 19 Oct 2017 14:57:24 -0400 Subject: [PATCH 1/2] Adds --useVscode flag that specifies an alternative path to .vscode/settings.json configuration --- lib/cli.ts | 8 +++++++ lib/index.ts | 1 + lib/provider/vscodesettings.ts | 7 +++++- .../specified-config/vscode/main.json | 23 +++++++++++++++++++ test/expected/specified-config/vscode/main.ts | 1 + .../vscode/.vscode/settings.json | 3 +++ .../vscode/alt-vscode-settings.json | 3 +++ test/fixture/specified-config/vscode/main.ts | 1 + test/indexSpec.ts | 11 +++++++++ 9 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/expected/specified-config/vscode/main.json create mode 100644 test/expected/specified-config/vscode/main.ts create mode 100644 test/fixture/specified-config/vscode/.vscode/settings.json create mode 100644 test/fixture/specified-config/vscode/alt-vscode-settings.json create mode 100644 test/fixture/specified-config/vscode/main.ts diff --git a/lib/cli.ts b/lib/cli.ts index f872832..6a95779 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -28,6 +28,7 @@ interface RootOptions { useTsconfig: string[]; useTslint: string[]; useTsfmt: string[]; + useVscode: string[]; verbose: boolean; version: boolean; } @@ -50,6 +51,7 @@ let root = commandpost .option("--useTsconfig ", "using specified config file instead of tsconfig.json") .option("--useTslint ", "using specified config file instead of tslint.json") .option("--useTsfmt ", "using specified config file instead of tsfmt.json") + .option("--useVscode ", "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) => { @@ -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; @@ -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); @@ -156,6 +162,7 @@ let root = commandpost tslintFile: tslintFile, editorconfig: editorconfig, vscode: vscode, + vscodeFile: vscodeFile, tsfmt: tsfmt, tsfmtFile: tsfmtFile, verbose: verbose, @@ -179,6 +186,7 @@ let root = commandpost tslintFile: tslintFile, editorconfig: editorconfig, vscode: vscode, + vscodeFile: vscodeFile, tsfmt: tsfmt, tsfmtFile: tsfmtFile, verbose: verbose, diff --git a/lib/index.ts b/lib/index.ts index bf45b30..8fadf16 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -28,6 +28,7 @@ export interface Options { tslintFile: string | null; editorconfig: boolean; vscode: boolean; + vscodeFile: string | null; tsfmt: boolean; tsfmtFile: string | null; } diff --git a/lib/provider/vscodesettings.ts b/lib/provider/vscodesettings.ts index 92f9b1f..1943ccd 100644 --- a/lib/provider/vscodesettings.ts +++ b/lib/provider/vscodesettings.ts @@ -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; } diff --git a/test/expected/specified-config/vscode/main.json b/test/expected/specified-config/vscode/main.json new file mode 100644 index 0000000..7ac629d --- /dev/null +++ b/test/expected/specified-config/vscode/main.json @@ -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 +} \ No newline at end of file diff --git a/test/expected/specified-config/vscode/main.ts b/test/expected/specified-config/vscode/main.ts new file mode 100644 index 0000000..ad68f58 --- /dev/null +++ b/test/expected/specified-config/vscode/main.ts @@ -0,0 +1 @@ +if(true === false) { console.log('Hello world'); } diff --git a/test/fixture/specified-config/vscode/.vscode/settings.json b/test/fixture/specified-config/vscode/.vscode/settings.json new file mode 100644 index 0000000..1ccabb9 --- /dev/null +++ b/test/fixture/specified-config/vscode/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true +} diff --git a/test/fixture/specified-config/vscode/alt-vscode-settings.json b/test/fixture/specified-config/vscode/alt-vscode-settings.json new file mode 100644 index 0000000..5729caf --- /dev/null +++ b/test/fixture/specified-config/vscode/alt-vscode-settings.json @@ -0,0 +1,3 @@ +{ + "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": false +} diff --git a/test/fixture/specified-config/vscode/main.ts b/test/fixture/specified-config/vscode/main.ts new file mode 100644 index 0000000..b085aed --- /dev/null +++ b/test/fixture/specified-config/vscode/main.ts @@ -0,0 +1 @@ +if(true===false){console.log('Hello world');} diff --git a/test/indexSpec.ts b/test/indexSpec.ts index f9ea283..1d18784 100644 --- a/test/indexSpec.ts +++ b/test/indexSpec.ts @@ -122,6 +122,7 @@ describe("tsfmt test", () => { tslintFile: null, editorconfig: true, vscode: true, + vscodeFile: null, tsfmt: true, tsfmtFile: null, }) @@ -180,6 +181,7 @@ describe("tsfmt test", () => { tslintFile: null, editorconfig: true, vscode: true, + vscodeFile: null, tsfmt: true, tsfmtFile: null, }) @@ -207,6 +209,7 @@ describe("tsfmt test", () => { tslintFile: null, editorconfig: true, vscode: true, + vscodeFile: null, tsfmt: true, tsfmtFile: null, }) @@ -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 => { @@ -261,6 +271,7 @@ describe("tsfmt test", () => { tslintFile: null, editorconfig: true, vscode: true, + vscodeFile: null, tsfmt: true, tsfmtFile: null, }, matrix.settings)) From 3c6a7739c2fb369812b2bf494f85d955e9f68af2 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 20 Oct 2017 13:44:36 -0400 Subject: [PATCH 2/2] Fix tests to run on Windows - use cross-spawn to invoke tslint, since it's a .cmd on Windows - manually invoke `node` when running tsfmt since Windows doesn't natively support hashbangs - normalize process.cwd() to unix path separators when comparing against other paths - add .gitattributes that prevents automatic eol changes since some test cases require CRLF, some require LF --- .gitattributes | 1 + package.json | 1 + test/indexSpec.ts | 28 +++++++++++++++++++++++----- 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7f0732b --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* -text diff --git a/package.json b/package.json index d5e6e58..c33d0c7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/indexSpec.ts b/test/indexSpec.ts index 1d18784..24fcd0f 100644 --- a/test/indexSpec.ts +++ b/test/indexSpec.ts @@ -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/"); @@ -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 { + 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 { let process = childProcess.spawn(cmd, args, options); @@ -51,7 +69,7 @@ function exec(cmd: string, args: string[], options: childProcess.SpawnOptions): } function checkByTslint(configFileName: string, tsfileName: string, errorExpected: boolean): Promise { - 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) => { @@ -305,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 { @@ -318,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 { @@ -330,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")); });