-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
put requiredFences in fence.json instead of config
- Loading branch information
Showing
6 changed files
with
59 additions
and
18 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
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,6 +1,5 @@ | ||
export default interface Options { | ||
project?: string; | ||
rootDir?: string; | ||
requiredFences?: string[]; | ||
onError?: (message: string) => void; | ||
}; |
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 @@ | ||
export default function dedupe<T>(...arrays: T[][]) { | ||
return Array.from(new Set([].concat(...arrays))); | ||
} |
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,29 +1,72 @@ | ||
import reportError from '../core/reportError'; | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
import getOptions from '../utils/getOptions'; | ||
import normalizePath from '../utils/normalizePath'; | ||
import getAllConfigs from '../utils/getAllConfigs'; | ||
import Config from '../types/Config'; | ||
import ConfigSet from '../types/ConfigSet'; | ||
import dedupe from '../utils/dedupe'; | ||
|
||
interface ConfigPathPair { | ||
path: string; | ||
config: Config; | ||
} | ||
|
||
export default function validateFencesExistence() { | ||
if (!getOptions().requiredFences) { | ||
return true; | ||
} | ||
const configPaths = getAllConfigs(); | ||
const configPathMap = getAllConfigs(); | ||
const requiredFenceGlobs = getRequiredFenceGlobs(configPathMap); | ||
|
||
// get a deduped list of required paths | ||
const requiredPaths = Array.from( | ||
new Set([].concat(...getOptions().requiredFences.map(f => glob.sync(f)))) | ||
); | ||
const requiredPaths = dedupe(requiredFenceGlobs.map(f => glob.sync(f))); | ||
|
||
// get the required paths that aren't in our map of all configs | ||
const missingRequiredPaths = requiredPaths.filter( | ||
p => configPaths[normalizePath(path.dirname(p))] | ||
); | ||
const missingRequiredPaths = requiredPaths.filter(p => !configPathMap[p]); | ||
|
||
if (missingRequiredPaths.length > 0) { | ||
missingRequiredPaths.forEach(p => reportError(`Missing fence.json at ${p}`)); | ||
} | ||
|
||
return missingRequiredPaths.length === 0; | ||
} | ||
|
||
/** | ||
* @summary Create a set of globs that define directories that must have fences | ||
* @param configSet The set of configs to build our globs from | ||
*/ | ||
function getRequiredFenceGlobs(configSet: ConfigSet) { | ||
const configPathPairs = Object.keys(configSet).map(key => ({ | ||
path: key, | ||
config: configSet[key], | ||
})); | ||
|
||
const configPathPairsWithFences = configPathPairs.filter(pair => pair.config.requiredFences); | ||
|
||
const requiredFenceGlobs = configPathPairsWithFences | ||
.map(absolutifyGlobs) | ||
.map(directorizeGlobs) | ||
.reduce((acc, e) => acc.concat(e), []); | ||
|
||
return requiredFenceGlobs; | ||
} | ||
|
||
/** | ||
* @summary Create globs with absolute paths | ||
* @param pair A pair of base path and config | ||
*/ | ||
function absolutifyGlobs(pair: ConfigPathPair) { | ||
const pathParts = pair.path.split(/[\///]/); | ||
const requiredFences = pair.config.requiredFences; | ||
const absoluteGlobs = requiredFences.map(rf => | ||
pathParts.concat(rf.split(/[\///]/)).join(path.sep) | ||
); | ||
|
||
return absoluteGlobs; | ||
} | ||
|
||
/** | ||
* @summary ensure we only match directories by appending a path separator (e.g. '/') | ||
* @param globs The globs to directorize | ||
*/ | ||
function directorizeGlobs(globs: string[]) { | ||
const dirPaths = globs.map(g => g.replace(/[\///]*$/, path.sep)); | ||
return dirPaths; | ||
} |