Skip to content

Commit

Permalink
Add a rootDir option (#12)
Browse files Browse the repository at this point in the history
This adds a `rootDir` option (`--rootDir` or `-r` on the command line) to specify the root directory of the project.  (It defaults to the current working directory.)  This also refactors options a bit: the options object is now stored so that it can be accessed globally via `getOptions()`, and when storing it paths are normalized and defaults filled in for missing values.
  • Loading branch information
smikula authored Nov 5, 2017
1 parent e33dd3f commit 0d33406
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const packageVersion = require('../package').version;
const options = commander
.version(packageVersion)
.option('-p, --project <string>', 'tsconfig.json file')
.option('-r, --rootDir <string>', 'root directory of the project')
.parse(process.argv) as Options;

// Run good-fences
Expand Down
4 changes: 3 additions & 1 deletion src/getAllConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import * as fs from 'fs';
import * as glob from 'glob';
import * as path from 'path';
import ConfigSet from './types/ConfigSet';
import getOptions from './getOptions';

let configSet: ConfigSet = null;

export default function getAllConfigs(): ConfigSet {
if (!configSet) {
configSet = {};

let files = glob.sync('**/fence.json');
// Glob for configs under the project root directory
let files = glob.sync(path.resolve(getOptions().rootDir, '**/fence.json'));
files.forEach(file => {
let absolutePath = path.resolve(path.dirname(file));
configSet[absolutePath] = JSON.parse(fs.readFileSync(file).toString());
Expand Down
18 changes: 18 additions & 0 deletions src/getOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as path from 'path';
import Options from './types/Options';

let options: Options;

export default function getOptions() {
return options;
}

export function setOptions(providedOptions: Options) {
options = providedOptions;

// Normalize and apply defaults
options.rootDir = options.rootDir ? path.resolve(options.rootDir) : path.resolve();
options.project = options.project
? path.resolve(options.project)
: path.resolve(options.rootDir, 'tsconfig.json');
}
8 changes: 2 additions & 6 deletions src/reportError.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
let errorReporter = logToConsole;
import getOptions from './getOptions';

export default function reportError(message: string) {
errorReporter(message);
}

export function setErrorReporter(onError: (message: string) => void) {
errorReporter = onError;
(getOptions().onError || logToConsole)(message);
}

function logToConsole(message: string) {
Expand Down
11 changes: 4 additions & 7 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Options from './types/Options';
import { setErrorReporter } from './reportError';
import getOptions, { setOptions } from './getOptions';
import validateFile from './validateFile';
import TypeScriptProgram from './TypeScriptProgram';

export function run(options: Options) {
// Apply options
const project = options.project || 'tsconfig.json';
if (options.onError) {
setErrorReporter(options.onError);
}
// Store options so they can be globally available
setOptions(options);

// Run validation
let tsProgram = new TypeScriptProgram(project);
let tsProgram = new TypeScriptProgram(getOptions().project);
let files = tsProgram.getSourceFiles();
files.forEach(file => {
validateFile(file, tsProgram);
Expand Down
1 change: 1 addition & 0 deletions src/types/Options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface Options {
project?: string;
rootDir?: string;
onError?: (message: string) => void;
};

0 comments on commit 0d33406

Please sign in to comment.