Skip to content

Commit

Permalink
Merge branch 'master' into requireFenceJson
Browse files Browse the repository at this point in the history
  • Loading branch information
smikula authored Nov 5, 2018
2 parents 55dc7da + bb0b238 commit 3b509db
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion sample/src/componentA/fence.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"tags": [ "tagA" ],
"exports": {
"componentA": "*"
}
},
"imports":[]
}
4 changes: 2 additions & 2 deletions src/core/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let hadError = false;
// Run good-fences
run({
...options,
onError(message) {
console.error(`Error: ${message}`);
onError(error) {
console.error(error.detailedMessage);
hadError = true;
},
});
Expand Down
24 changes: 22 additions & 2 deletions src/core/reportError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import * as path from 'path';
import getOptions from '../utils/getOptions';
import Config from '../types/Config';

export default function reportError(
message: string,
sourceFile: string,
rawImport: string,
config: Config
) {
let fencePath = config.path + path.sep + 'fence.json';

let detailedMessage =
`Good-fences violation in ${sourceFile}:\n` +
` ${message}: ${rawImport}\n` +
` Fence: ${fencePath}`;

export default function reportError(message: string) {
if (getOptions().onError) {
getOptions().onError(message);
getOptions().onError({
message,
sourceFile,
rawImport,
fencePath,
detailedMessage,
});
}
}
4 changes: 3 additions & 1 deletion src/types/NormalizedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import NormalizedPath from './NormalizedPath';
import ValidationError from './ValidationError';

export default interface NormalizedOptions {
project: NormalizedPath;
rootDir: NormalizedPath;
onError?: (message: string) => void;
ignoreExternalFences: boolean;
onError?: (error: ValidationError) => void;
};
5 changes: 4 additions & 1 deletion src/types/Options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import ValidationError from './ValidationError';

export default interface Options {
project?: string;
rootDir?: string;
onError?: (message: string) => void;
ignoreExternalFences?: boolean;
onError?: (error: ValidationError) => void;
};
7 changes: 7 additions & 0 deletions src/types/ValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default interface ValidationError {
message: string;
sourceFile: string;
rawImport: string;
fencePath: string;
detailedMessage: string;
};
5 changes: 5 additions & 0 deletions src/utils/getAllConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default function getAllConfigs(): ConfigSet {

// Glob for configs under the project root directory
let files = glob.sync(normalizePath(getOptions().rootDir, '**/fence.json'));

if (getOptions().ignoreExternalFences) {
files = files.filter(f => f.split(path.sep).indexOf('node_modules') > -1);
}

files.forEach(file => {
let configPath = normalizePath(path.dirname(file));
configSet[configPath] = JSON.parse(fs.readFileSync(file).toString());
Expand Down
1 change: 1 addition & 0 deletions src/utils/getOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function setOptions(providedOptions: Options) {
options = {
project,
rootDir,
ignoreExternalFences: providedOptions.ignoreExternalFences,
onError: providedOptions.onError,
};
}
2 changes: 1 addition & 1 deletion src/validation/validateDependencyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function validateConfig(config: Config, sourceFile: NormalizedPath, importRecord
}

// If we made it here, we didn't find a rule that allows the dependency
reportError(`${sourceFile} is not allowed to import '${importRecord.rawImport}'`);
reportError('Dependency is not allowed', sourceFile, importRecord.rawImport, config);
}

function getFullDependencyRule(dependency: DependencyRule): FullDependencyRule {
Expand Down
2 changes: 1 addition & 1 deletion src/validation/validateExportRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function validateConfig(config: Config, sourceFile: NormalizedPath, importFile:
}

// If we made it here, the import is invalid
reportError(`${sourceFile} is importing inaccessible module ${importFile}`);
reportError('Module is not exported', sourceFile, importFile, config);
}

function hasMatchingExport(config: Config, sourceFile: NormalizedPath, importFile: NormalizedPath) {
Expand Down
2 changes: 1 addition & 1 deletion src/validation/validateImportRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ function validateConfig(config: Config, sourceFile: NormalizedPath, importRecord
}

// If we made it here, the import is invalid
reportError(`${sourceFile} is not allowed to import '${importRecord.rawImport}'`);
reportError('Import not allowed', sourceFile, importRecord.rawImport, config);
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"sourceMap": false,
"outDir": "lib",
"noImplicitAny": true,
"noUnusedLocals": true
"noUnusedLocals": true,
"noImplicitThis": true
},
"exclude": [
"node_modules",
Expand Down

0 comments on commit 3b509db

Please sign in to comment.