-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support fix and check commands (#3)
- Loading branch information
Showing
24 changed files
with
2,155 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [["env", { "targets": { "node": "4.5" } }]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
node_modules | ||
.esm-cache | ||
.vscode | ||
dist | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"env": { | ||
"jest": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import path from "path"; | ||
|
||
const actual = require.requireActual("fs"); | ||
|
||
let writes = {}; | ||
|
||
export const statSync = jest.fn(actual.statSync); | ||
export const existsSync = jest.fn(actual.existsSync); | ||
|
||
export const writeFileSync = jest.fn((filePath, content) => { | ||
const relative = path.relative(process.cwd(), filePath); | ||
writes[relative] = content; | ||
}); | ||
|
||
export const readFileSync = jest.fn(filePath => { | ||
const relative = path.relative(process.cwd(), filePath); | ||
if (writes[relative]) { | ||
return writes[relative]; | ||
} | ||
return actual.readFileSync(filePath, "utf8"); | ||
}); | ||
|
||
export const __clear = () => { | ||
writes = {}; | ||
existsSync.mockClear(); | ||
statSync.mockClear(); | ||
writeFileSync.mockClear(); | ||
readFileSync.mockClear(); | ||
}; | ||
|
||
export default { | ||
__clear, | ||
existsSync, | ||
statSync, | ||
readFileSync, | ||
writeFileSync, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import runTsLint from "./run-tslint"; | ||
import runPrettier from "./run-prettier"; | ||
|
||
const check = filePath => { | ||
return runPrettier(filePath, false) && runTsLint(filePath, false); | ||
}; | ||
|
||
export default check; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint no-console: 0 */ | ||
|
||
import chalk from "chalk"; | ||
import yargs from "yargs"; | ||
|
||
import fix from "./fix"; | ||
import check from "./check"; | ||
import expandGlob from "./expand-glob"; | ||
import filterIgnored from "./filter-ignored"; | ||
|
||
const cli = argv => { | ||
const yargsInstance = yargs | ||
// Fix | ||
.command("fix", "Fix one or more files") | ||
.example("prettier-tslint fix file1.ts file2.ts", "Fix provided files") | ||
.example("prettier-tslint fix '**/*.ts'", "Fix all .ts files") | ||
// Check | ||
.command("check", "List files that aren't formatted") | ||
.example("prettier-tslint check '**/*.ts'", "List unformatted .ts files") | ||
// Meta | ||
.demandCommand(1, "Command not provided.") | ||
.help(); | ||
|
||
const args = yargsInstance.parse(argv); | ||
const command = args._[0]; | ||
const patterns = args._.slice(1); | ||
|
||
switch (command) { | ||
case "fix": | ||
return patterns.forEach(fixFiles); | ||
case "check": | ||
return patterns.forEach(checkFiles); | ||
default: | ||
yargs.showHelp(); | ||
console.error(`Unknown command: ${command}`); | ||
} | ||
}; | ||
|
||
const fixFiles = filePattern => { | ||
const files = filterIgnored(expandGlob(filePattern)); | ||
files.forEach(file => { | ||
const changed = !fix(file); | ||
console.log(changed ? file : chalk.gray(file)); | ||
}); | ||
}; | ||
|
||
const checkFiles = filePattern => { | ||
const files = filterIgnored(expandGlob(filePattern)); | ||
const invalid = files.filter(file => !check(file)); | ||
if (invalid.length) { | ||
process.exitCode = 1; | ||
} | ||
invalid.forEach(file => console.error(chalk.red.bold(file))); | ||
}; | ||
|
||
export default cli; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import globby from "globby"; | ||
|
||
const expandGlob = glob => { | ||
return globby.sync(glob, { | ||
dot: true, | ||
}); | ||
return globby.sync( | ||
[glob].concat(["!**/node_modules/**", "!./node_modules/**"]), | ||
{ | ||
dot: true, | ||
} | ||
); | ||
}; | ||
|
||
export default expandGlob; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import runTsLint from "./run-tslint"; | ||
import runPrettier from "./run-prettier"; | ||
|
||
const fix = filePath => { | ||
const prettierCheck = runPrettier(filePath, true); | ||
const tslintCheck = runTsLint(filePath, true); | ||
return prettierCheck && tslintCheck; | ||
}; | ||
|
||
export default fix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,5 @@ | ||
import fs from "fs"; | ||
import minimist from "minimist"; | ||
|
||
import expandGlob from "./expand-glob"; | ||
import filterIgnored from "./filter-ignored"; | ||
import runTsLint from "./run-tslint"; | ||
import runPrettier from "./run-prettier"; | ||
|
||
const format = filePath => { | ||
runPrettier(filePath); | ||
runTsLint(filePath); | ||
}; | ||
|
||
const formatFiles = filePattern => { | ||
const files = filterIgnored(expandGlob(filePattern)); | ||
files.forEach(format); | ||
}; | ||
|
||
const cliOpts = {}; | ||
|
||
export const cli = argv => { | ||
const args = minimist(argv, cliOpts); | ||
args._.forEach(formatFiles); | ||
}; | ||
|
||
export default format; | ||
export { default as cli } from "./cli"; | ||
export { default as fix } from "./fix"; | ||
export { default as check } from "./check"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import fs from "fs"; | ||
import prettier from "prettier"; | ||
|
||
const runPrettier = filepath => { | ||
/** | ||
* @returns true iff output === input | ||
*/ | ||
const runPrettier = (filepath, fix) => { | ||
const config = prettier.resolveConfig.sync(filepath); | ||
const code = fs.readFileSync(filepath, "utf8"); | ||
const output = prettier.format(code, Object.assign({ filepath }, config)); | ||
fs.writeFileSync(filepath, output); | ||
const options = Object.assign({ filepath }, config); | ||
|
||
if (fix) { | ||
const output = prettier.format(code, options); | ||
if (output !== code) { | ||
fs.writeFileSync(filepath, output); | ||
return false; | ||
} | ||
return true; | ||
} else { | ||
return prettier.check(code, options); | ||
} | ||
}; | ||
|
||
export default runPrettier; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
import fs from "fs"; | ||
import tslint from "tslint"; | ||
import * as tslint from "tslint"; | ||
import createProgram from "./create-program"; | ||
|
||
const runTsLint = filepath => { | ||
/** | ||
* @returns true iff output === input | ||
*/ | ||
const runTsLint = (filepath, fix) => { | ||
const code = fs.readFileSync(filepath, "utf8"); | ||
const config = tslint.Configuration.findConfiguration(null, filepath).results; | ||
|
||
const program = createProgram(filepath); | ||
|
||
// TODO(azz): This actually writes over the file, we don't really want that... | ||
const linter = new tslint.Linter({ fix: true }, program); | ||
const linter = new tslint.Linter({ fix }, program); | ||
|
||
linter.lint(filepath, code, config); | ||
const result = linter.getResult(); | ||
return result; | ||
if (fix) { | ||
// There were no fixes applied | ||
return result.fixes.length === 0; | ||
} else { | ||
// There were no auto-fixable problems | ||
return !result.failures.find(failure => failure.fix); | ||
} | ||
}; | ||
|
||
export default runTsLint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"env": { | ||
"jest": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`fix() bad format, bad lint writes fix to disk 1`] = ` | ||
Array [ | ||
"test/fixture/bad-format-bad-lint.ts", | ||
"throw new Error(\\"no-string-throw\\"); | ||
", | ||
] | ||
`; | ||
|
||
exports[`fix() bad format, good lint writes fix to disk 1`] = ` | ||
Array [ | ||
"test/fixture/bad-format-good-lint.ts", | ||
"throw new Error(\\"no-string-throw\\"); | ||
", | ||
] | ||
`; | ||
|
||
exports[`fix() good format, bad lint writes fix to disk 1`] = ` | ||
Array [ | ||
"test/fixture/good-format-bad-lint.ts", | ||
"throw new Error(\\"no-string-throw\\"); | ||
", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import check from "../src/check"; | ||
|
||
describe("check()", () => { | ||
test("good format, bad lint fails check", () => { | ||
expect(check("test/fixture/good-format-bad-lint.ts")).toEqual(false); | ||
}); | ||
test("bad format, good lint fails check", () => { | ||
expect(check("test/fixture/bad-format-good-lint.ts")).toEqual(false); | ||
}); | ||
test("bad format, bad lint fails check", () => { | ||
expect(check("test/fixture/bad-format-bad-lint.ts")).toEqual(false); | ||
}); | ||
test("good format, good lint passes check", () => { | ||
expect(check("test/fixture/good-format-good-lint.ts")).toEqual(true); | ||
}); | ||
}); |
Oops, something went wrong.