Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #871 from palantir/init
Browse files Browse the repository at this point in the history
Automatic tslint.json generation with --init command
  • Loading branch information
adidahiya committed Dec 16, 2015
2 parents 409aa6e + eab535a commit b7206e2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
34 changes: 25 additions & 9 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,36 @@ import * as fs from "fs";
import * as path from "path";
import * as findup from "findup-sync";

const CONFIG_FILENAME = "tslint.json";
const DEFAULT_CONFIG = {
export const CONFIG_FILENAME = "tslint.json";
export const DEFAULT_CONFIG = {
"rules": {
"curly": true,
"indent": [true, 4],
"no-duplicate-key": true,
"class-name": true,
"comment-format": [true, "check-space"],
"indent": [true, "spaces"],
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unreachable": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"one-line": [true, "check-open-brace", "check-whitespace"],
"quotemark": [true, "double"],
"semicolon": true
"semicolon": true,
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"variable-name": [true, "ban-keywords"],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
],
}
};
const moduleDirectory = path.dirname(module.filename);
Expand Down
22 changes: 20 additions & 2 deletions src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import * as fs from "fs";
import * as glob from "glob";
import * as optimist from "optimist";
import * as Linter from "./tslint";
import {getRulesDirectories} from "./configuration";
import {CONFIG_FILENAME, DEFAULT_CONFIG, getRulesDirectories} from "./configuration";

let processed = optimist
.usage("Usage: $0 [options] [file ...]")
.check((argv: any) => {
// at least one of file, help, version or unqualified argument must be present
if (!(argv.h || argv.v || argv._.length > 0)) {
if (!(argv.h || argv.i || argv.v || argv._.length > 0)) {
throw "Missing files";
}

Expand All @@ -42,6 +42,10 @@ let processed = optimist
alias: "help",
describe: "display detailed help"
},
"i": {
alias: "init",
describe: "generate a tslint.json config file in the current working directory"
},
"o": {
alias: "out",
describe: "output file"
Expand Down Expand Up @@ -81,6 +85,17 @@ if (argv.v != null) {
process.exit(0);
}

if (argv.i != null) {
if (fs.existsSync(CONFIG_FILENAME)) {
console.error(`Cannot generate ${CONFIG_FILENAME}: file already exists`);
process.exit(1);
}

const tslintJSON = JSON.stringify(DEFAULT_CONFIG, undefined, " ");
fs.writeFileSync(CONFIG_FILENAME, tslintJSON);
process.exit(0);
}

if ("help" in argv) {
outputStream.write(processed.help());
const outputString = `
Expand All @@ -101,6 +116,9 @@ tslint accepts the following commandline options:
of characters for the max-line-length rule, or what functions to ban
for the ban rule).
-i, --init:
Generates a tslint.json config file in the current working directory.
-o, --out:
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.
Expand Down
26 changes: 22 additions & 4 deletions test/check-bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ expectOut () {
expect=$2
msg=$3

if [ $expect != $actual ]
then
if [ $expect != $actual ]; then
echo "$msg: expected $expect got $actual"
num_failures=$(expr $num_failures + 1)
fi
Expand All @@ -43,8 +42,27 @@ expectOut $? 0 "tslint with valid arguments did not exit correctly"
./bin/tslint src/configuration.ts -f src/formatterLoader.ts
expectOut $? 1 "tslint with -f flag did not exit correctly"

if [ $num_failures != 0 ]
then
# make sure tslint --init generates a file
cd ./bin
if [ -f tslint.json ]; then
rm tslint.json
fi

./tslint --init
if [ ! -f tslint.json ]; then
echo "--init failed, tslint.json not created"
num_failures=$(expr $num_failures + 1)
fi
expectOut $? 0 "tslint with --init flag did not exit correctly"

# should fail since tslint.json already exists
./tslint --init
expectOut $? 1 "tslint with --init flag did not exit correctly when tslint.json already exists"

rm tslint.json
cd ..

if [ $num_failures != 0 ]; then
echo "Failed $num_failures tests"
exit 1
else
Expand Down
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
"radix": true,
"semicolon": true,
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"variable-name": false,
"whitespace": [true,
"check-branch",
Expand Down

0 comments on commit b7206e2

Please sign in to comment.