-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from srowhani/feat/config-resolution
Feat/config resolution
- Loading branch information
Showing
10 changed files
with
120 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,54 @@ | ||
import { Nullable, SlfParserOptions } from '@src/types'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
const merge = require('merge'); | ||
const yaml = require('js-yaml'); | ||
|
||
type OptionParser = (filename: string) => Nullable<SlfParserOptions>; | ||
interface MappedParserOptions { | ||
[key: string]: OptionParser; | ||
import merge from 'merge'; | ||
|
||
import { ConfigOpts } from '@src/types'; | ||
import { cosmiconfigSync as configSync } from 'cosmiconfig'; | ||
import { readFileSync } from 'fs'; | ||
import { safeLoad } from 'js-yaml'; | ||
import { LintOpts } from 'sass-lint'; | ||
|
||
const defaultSearchPlaces = (moduleName: string) => [ | ||
'package.json', | ||
`.${moduleName}rc`, | ||
`.${moduleName}.json`, | ||
`.${moduleName}.yaml`, | ||
`.${moduleName}.yml`, | ||
`${moduleName}.config.js`, | ||
]; | ||
|
||
export function loadDefaults(): ConfigOpts { | ||
return safeLoad( | ||
readFileSync(require.resolve('../config/default.yml'), { | ||
encoding: 'utf8', | ||
}), | ||
); | ||
} | ||
|
||
const _configurationProxy = new Proxy<MappedParserOptions>( | ||
{ | ||
yml: parseYaml, | ||
yaml: parseYaml, | ||
json: parseJSON, | ||
}, | ||
{ | ||
get(target: MappedParserOptions, filename: string) { | ||
const resolvedParserKey = Object.keys(target).find(targetExtension => | ||
filename.endsWith(`.${targetExtension}`), | ||
); | ||
|
||
const resolvedParser = | ||
(resolvedParserKey && target[resolvedParserKey]) || parseModule; | ||
|
||
return resolvedParser(filename); | ||
}, | ||
}, | ||
); | ||
|
||
function parseYaml(filename: string): Nullable<SlfParserOptions> { | ||
return yaml.safeLoad(fs.readFileSync(filename).toString()); | ||
export enum CONFIG_TYPE { | ||
SASS_LINT = 'sass_lint', | ||
SASS_LINT_AUTO_FIX = 'sass_lint_auto_fix', | ||
} | ||
|
||
function parseJSON(filename: string): Nullable<SlfParserOptions> { | ||
const file = fs.readFileSync(filename).toString(); | ||
return JSON.parse(file); | ||
export type ConfigType<T> = T extends CONFIG_TYPE.SASS_LINT | ||
? LintOpts | ||
: T extends CONFIG_TYPE.SASS_LINT_AUTO_FIX | ||
? ConfigOpts | ||
: any; | ||
|
||
export function getConfig<T extends CONFIG_TYPE>( | ||
moduleName: T, | ||
filepath?: string, | ||
): ConfigType<T> { | ||
const explorer = configSync(moduleName, { | ||
searchPlaces: defaultSearchPlaces(moduleName), | ||
}); | ||
|
||
const resolvedConfig = filepath ? explorer.load(filepath) : explorer.search(); | ||
|
||
if (resolvedConfig) { | ||
return resolvedConfig.config; | ||
} | ||
return {} as any; | ||
} | ||
|
||
function parseModule(filename: string): Nullable<SlfParserOptions> { | ||
return require(path.resolve(filename)); | ||
} | ||
|
||
export const getConfig = (filename: string): Record<string, any> => | ||
_configurationProxy[filename]; | ||
|
||
export const mergeConfig = ( | ||
baseConfig: Record<string, any>, | ||
extendedConfig: Record<string, any>, | ||
) => merge.recursive(true, baseConfig, extendedConfig); | ||
export const mergeConfig = <A, B>(baseConfig: A, extendedConfig: B) => | ||
merge.recursive(true, baseConfig, extendedConfig); |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'merge' { | ||
export function recursive<A, B>(clone: boolean, a: A, b: B): A & B; | ||
} |
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,4 +1,4 @@ | ||
module.exports = { | ||
{ | ||
resolvers: { | ||
'property-sort-order': 1 | ||
} | ||
|
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,5 @@ | ||
{ | ||
resolvers: { | ||
'property-sort-order': 1 | ||
} | ||
} |
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
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