-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement better config logic (#48)
- Loading branch information
Showing
20 changed files
with
1,326 additions
and
1,298 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
collectCoverage: true, | ||
}; |
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,84 @@ | ||
import yargs from 'yargs'; | ||
import fs from 'fs'; | ||
import { hideBin } from 'yargs/helpers'; | ||
import _ from 'lodash'; | ||
import { defaultConfig } from '../constants'; | ||
import { configSchema } from './schemas'; | ||
import { Config } from '../types'; | ||
|
||
/** | ||
* Gets the config from the CLI or the config file | ||
* Prioritizes the config file over the CLI | ||
* @param {string} configPath - The expected config path | ||
* @returns {Config} - The config | ||
*/ | ||
export function getConfig(configPath: string): Config { | ||
const fileConfig = getFileConfig(configPath); | ||
const argConfig = getArgsConfig(); | ||
// Merge default config with file config and arg config | ||
const inputConfig = _.merge(_.merge(_.cloneDeep(defaultConfig), fileConfig), argConfig); | ||
|
||
return configSchema.validateSync(inputConfig); | ||
} | ||
|
||
/** | ||
* Retrieves the configuration from a file. | ||
* If the file does not exist or is invalid, an empty object is returned. | ||
* @param {string} configPath - The path to the configuration file. | ||
* @returns {Partial<Config>} - The configuration object parsed from the file, or an empty object if the file is invalid or does not exist. | ||
* @throws {Error} - If the config file is invalid. | ||
*/ | ||
export function getFileConfig(configPath: string): Partial<Config> { | ||
try { | ||
if (!fs.existsSync(configPath)) { | ||
return {}; | ||
} | ||
const fileContent = fs.readFileSync(configPath, 'utf8'); | ||
return JSON.parse(fileContent) as Partial<Config>; | ||
} catch (e) { | ||
throw Error(`Invalid config file: ${e}`); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the configuration from the command-line arguments. | ||
* Parses the command-line arguments using yargs and returns a partial configuration object. | ||
* @returns {Partial<Config>} - The configuration object parsed from the command-line arguments. | ||
*/ | ||
export function getArgsConfig(): Partial<Config> { | ||
const argv = yargs(hideBin(process.argv)) | ||
.options({ | ||
include: { | ||
type: 'string', | ||
description: 'Glob pattern of files to process.', | ||
}, | ||
exclude: { | ||
type: 'string', | ||
description: 'Glob pattern of files to exclude.', | ||
}, | ||
root: { | ||
type: 'string', | ||
description: 'Root directory of the project.', | ||
}, | ||
inheritdoc: { | ||
type: 'boolean', | ||
description: 'If set to true, all external and public functions must have @inheritdoc.', | ||
}, | ||
constructorNatspec: { | ||
type: 'boolean', | ||
description: 'If set to true, all contracts must have a natspec for the constructor.', | ||
}, | ||
}) | ||
.parseSync(); | ||
|
||
const { $0, _, ...config } = argv; | ||
|
||
// Remove kebab case items from config | ||
Object.keys(config).forEach((key) => { | ||
if (key.includes('-')) { | ||
delete config[key]; | ||
} | ||
}); | ||
|
||
return config; | ||
} |
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,2 @@ | ||
export * from './schemas'; | ||
export * from './handlers'; |
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,38 @@ | ||
import { object, string, boolean, date, InferType } from 'yup'; | ||
|
||
export const tagSchema = object({ | ||
tags: object({ | ||
dev: boolean().required().strict(), | ||
notice: boolean().required().strict(), | ||
param: boolean().required().strict(), | ||
}), | ||
}); | ||
|
||
export const functionSchema = object({ | ||
tags: object({ | ||
dev: boolean().required().strict(), | ||
notice: boolean().required().strict(), | ||
param: boolean().required().strict(), | ||
return: boolean().required().strict(), | ||
}), | ||
}); | ||
|
||
export const functionConfigSchema = object({ | ||
internal: functionSchema, | ||
external: functionSchema, | ||
public: functionSchema, | ||
private: functionSchema, | ||
}); | ||
|
||
export const configSchema = object({ | ||
include: string().required().strict(), | ||
exclude: string().strict().optional(), | ||
root: string().required().strict(), | ||
functions: functionConfigSchema, | ||
events: tagSchema, | ||
errors: tagSchema, | ||
modifiers: tagSchema, | ||
structs: tagSchema, | ||
inheritdoc: boolean().required().strict(), | ||
constructorNatspec: boolean().required().strict(), | ||
}); |
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,16 +1,43 @@ | ||
import { Functions } from './types'; | ||
import { Config } from './types'; | ||
|
||
export const defaultFunctions: Functions = { | ||
internal: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
external: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
public: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
private: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
} as const; | ||
|
||
export const defaultTags = { | ||
tags: { | ||
dev: false, | ||
notice: true, | ||
param: true, | ||
export const defaultConfig: Readonly<Config> = { | ||
include: './**/*.sol', | ||
exclude: undefined, | ||
root: './', | ||
functions: { | ||
internal: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
external: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
public: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
private: { tags: { dev: false, notice: true, return: true, param: true } }, | ||
}, | ||
modifiers: { | ||
tags: { | ||
dev: false, | ||
notice: true, | ||
param: true, | ||
}, | ||
}, | ||
structs: { | ||
tags: { | ||
dev: false, | ||
notice: true, | ||
param: true, | ||
}, | ||
}, | ||
events: { | ||
tags: { | ||
dev: false, | ||
notice: true, | ||
param: true, | ||
}, | ||
}, | ||
errors: { | ||
tags: { | ||
dev: false, | ||
notice: true, | ||
param: true, | ||
}, | ||
}, | ||
inheritdoc: true, | ||
constructorNatspec: false, | ||
} as const; |
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
Oops, something went wrong.