Skip to content

Commit

Permalink
Changes ConfigLoader to be a class.
Browse files Browse the repository at this point in the history
Fixes casing in comments.

Issue : JoshuaKGoldberg#17
  • Loading branch information
Arnaud Dufranne committed Apr 18, 2016
1 parent fe7994b commit 3b2570b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
49 changes: 26 additions & 23 deletions src/ConfigLoader.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
/// <reference path="../typings/main/ambient/node/index.d.ts" />

namespace TSLint.MSBuild.ConfigLoader {
namespace TSLint.MSBuild {
"use strict";

const fs = require("fs"),
path = require("path");

/**
* Reads and deserializes a tslint.json file.
* @param path The path of the file.
* @returns A promise with the configuration object.
*/
export function readJSONConfig(path: string) {
return new Promise((resolve, reject) => {
fs.readFile(path, (error, result) => {
if (error) {
reject(error);
} else {
resolve(JSON.parse(stripBomIfNeeded(result.toString())));
}
export class ConfigLoader {

/**
* Reads and deserializes a tslint.json file.
* @param path The path of the file.
* @returns A promise with the configuration object.
*/
public readJSONConfig(path: string) {
return new Promise((resolve, reject) => {
fs.readFile(path, (error, result) => {
if (error) {
reject(error);
} else {
resolve(JSON.parse(ConfigLoader.stripBomIfNeeded(result.toString())));
}
});
});
});
}
}

/**
* Strips BOM if any.
* @param content the raw content of a file.
* @returns The content with the BOM stripped.
*/
function stripBomIfNeeded(content: string) {
return content.replace(/^\uFEFF/, "");
/**
* Strips BOM if any.
* @param content the raw content of a file.
* @returns The content with the BOM stripped.
*/
static stripBomIfNeeded(content: string) {
return content.replace(/^\uFEFF/, "");
}
}
}
5 changes: 3 additions & 2 deletions src/Folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ namespace TSLint.MSBuild {
* Initializes a new instance of the Folder class.
*
* @param path The path to this folder.
* @param configLoader The configuration loader.
*/
constructor(path: string) {
constructor(path: string, private configLoader: ConfigLoader) {
this.path = path;
}

Expand Down Expand Up @@ -87,7 +88,7 @@ namespace TSLint.MSBuild {
*/
public loadTSLintConfig(): Promise<boolean> {
this.loadWaiter.markActionStart();
return ConfigLoader
return this.configLoader
.readJSONConfig(path.join(this.path, "tslint.json"))
.then((config) => {
this.setTSLintConfig({
Expand Down
5 changes: 3 additions & 2 deletions src/FolderCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace TSLint.MSBuild {
* Initializes a new instance of the LintRunner class.
*
* @param rootDirectory The root directory to look at files under.
* @param ConfigLoader The configuration loader.
*/
constructor(rootDirectory: string) {
constructor(rootDirectory: string, private configLoader: ConfigLoader) {
this.rootDirectory = rootDirectory;
}

Expand Down Expand Up @@ -74,7 +75,7 @@ namespace TSLint.MSBuild {
return new Promise(resolve => resolve(folder));
}

folder = this.folders[folderPath] = new Folder(folderPath);
folder = this.folders[folderPath] = new Folder(folderPath, this.configLoader);

return folder
.loadTSLintConfig()
Expand Down
5 changes: 3 additions & 2 deletions src/LintRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ namespace TSLint.MSBuild {
* Initializes a new instance of the LintRunner class.
*
* @param rootDirectory The root directory to look at files under.
* @param configLoader The configuration loader.
*/
constructor(rootDirectory: string) {
constructor(rootDirectory: string, private configLoader: ConfigLoader) {
this.rootDirectory = rootDirectory;
this.folders = new FolderCollection(rootDirectory);
this.folders = new FolderCollection(rootDirectory, this.configLoader);
this.tsLintSearcher = new TSLintSearcher();
this.tsLint = require(this.tsLintSearcher.resolve());
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace TSLint.MSBuild {
let rootDirectory: string = process.argv[2],
summaryFilePath: string = process.argv[3],
filePaths: string[] = getInputFilesList(summaryFilePath),
runner = new LintRunner(rootDirectory);
runner = new LintRunner(rootDirectory, new ConfigLoader());

console.log(`Running TSLint on ${filePaths.length} files.`);
console.log(`Root directory is '${rootDirectory}'.`);
Expand Down

0 comments on commit 3b2570b

Please sign in to comment.