From a3e5b1fb18f233d1533b6ea55bbd9354de2ef1b6 Mon Sep 17 00:00:00 2001 From: Tal Rofe Date: Mon, 25 Apr 2022 19:55:08 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=94=A5=20support=20typescript=20c?= =?UTF-8?q?onfiguration=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit support typescript configuration file --- .vscode/settings.json | 3 +- README.md | 13 ++++++-- package.json | 5 +++- src/config.ts | 30 +++++++++++++++++++ src/index.ts | 2 ++ src/modules/cli/functions/init.ts | 2 +- src/modules/cli/interfaces/init.ts | 2 +- src/modules/cli/utils/init.ts | 9 ++++++ src/modules/configuration/index.ts | 5 ++++ .../configuration/models/cosmiconfig.ts | 2 ++ tsconfig.build.json | 3 +- webpack.config.ts | 4 +++ 12 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/config.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 6e0e46ab..51683ff2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,5 @@ }, "files.associations": { "commit-msg": "shellscript" - }, - "material-icon-theme.activeIconPack": "nest" + } } diff --git a/README.md b/README.md index 6fdcdf67..2eee6074 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,17 @@ Configuration can be set in the following files: - a `package.json` property: `"inflint": {...}` - a `.inflintrc` file in JSON or YAML format -- a `.inflintrc.json`, `.inflintrc.yaml`, `.inflintrc.yml`, `.inflintrc.js`, or `.inflintrc.cjs` file -- a `inflint.config.js` or `inflint.config.cjs` CommonJS module exporting an object +- a `.inflintrc.json`, `.inflintrc.yaml`, `.inflintrc.yml`, `.inflintrc.js`, `.inflintrc.ts`, or `.inflintrc.cjs` file +- a `inflint.config.js`, `inflint.config.ts` or `inflint.config.cjs` CommonJS module exporting an object + +If you set a TypeScript configuration file, you can use the configuration interface: +```ts +import { Config } from 'inflint'; +const inflintConfig: Config = { + ... +} +export default inflintConfig; +``` You can set a configuration file using our wizard: ```bash diff --git a/package.json b/package.json index 94187d47..df7b000a 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,11 @@ "url": "git+https://github.com/Vinyl-Depository/Inflint.git" }, "main": "dist/index.js", + "types": "dist/config.d.ts", "files": [ "dist/index.js", - "bin" + "bin", + "dist/config.d.ts" ], "bin": { "inflint": "./bin/inflint.js" @@ -53,6 +55,7 @@ "dependencies": { "@expo/spawn-async": "1.6.0", "cosmiconfig": "7.0.1", + "cosmiconfig-typescript-loader": "1.0.9", "fast-glob": "3.2.11", "inquirer": "8.2.2", "micromatch": "4.0.5", diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 00000000..6fb18223 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,30 @@ +export type RuleOptions = Partial<{ + onlyDirectories: boolean; + onlyFiles: boolean; + dot: boolean; + caseSensitiveMatch: boolean; +}>; + +export type RuleEnforcement = 1 | 2 | 'warn' | 'error'; + +export type RuleValue = + | RuleEnforcement + | [RuleEnforcement] + | [RuleEnforcement, string] + | [RuleEnforcement, RuleOptions] + | [RuleEnforcement, string, RuleOptions]; + +export type AliasValue = string | [string, string]; + +export type Config = Partial<{ + extends?: string; + rules: Record; + aliases: Record; + ignorePath: string; + ignore: boolean; + ignorePatterns: string[]; + quiet: boolean; + maxWarnings: number; + bail: number; + outputFile: string; +}>; diff --git a/src/index.ts b/src/index.ts index 2d4689cd..649d033a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ import Bootstrap from './modules/app'; Bootstrap(); + +export * from './config'; diff --git a/src/modules/cli/functions/init.ts b/src/modules/cli/functions/init.ts index 10dd8779..3a30529f 100644 --- a/src/modules/cli/functions/init.ts +++ b/src/modules/cli/functions/init.ts @@ -15,7 +15,7 @@ export const initConfiguration = async () => { name: 'file_format', message: 'Please choose the configuration file format:', default: 'Javascript', - choices: ['Javascript', 'JSON', 'YAML'], + choices: ['Javascript', 'Typescript', 'JSON', 'YAML'], }, { type: 'list', diff --git a/src/modules/cli/interfaces/init.ts b/src/modules/cli/interfaces/init.ts index f35fd41f..55316c94 100644 --- a/src/modules/cli/interfaces/init.ts +++ b/src/modules/cli/interfaces/init.ts @@ -1,6 +1,6 @@ export type IBooleanQuestion = 'Yes' | 'No'; -export type IFileFormat = 'Javascript' | 'JSON' | 'YAML'; +export type IFileFormat = 'Javascript' | 'Typescript' | 'JSON' | 'YAML'; export interface IAliasAnswer { readonly name: string; diff --git a/src/modules/cli/utils/init.ts b/src/modules/cli/utils/init.ts index 8258c7a7..d1204a50 100644 --- a/src/modules/cli/utils/init.ts +++ b/src/modules/cli/utils/init.ts @@ -32,6 +32,15 @@ export const exportFilesFromInit = async ( } else if (fileFormat === 'Javascript') { contentString = `module.exports = ${JSON.stringify(contentObject, null, 2)};`; filename = '.inflintrc.js'; + } else if (fileFormat === 'Typescript') { + contentString = [ + "import { Config } from 'inflint'", + '', + `const inflintConfig = ${JSON.stringify(contentObject, null, 2)};`, + '', + 'export default inflintConfig;', + ].join('\n'); + filename = '.inflintrc.ts'; } else { contentString = JSON.stringify(contentObject, null, 2); filename = '.inflintrc.json'; diff --git a/src/modules/configuration/index.ts b/src/modules/configuration/index.ts index 15f8a7c7..65a2b1ee 100644 --- a/src/modules/configuration/index.ts +++ b/src/modules/configuration/index.ts @@ -1,5 +1,7 @@ import path from 'path'; +import TypeScriptLoader from 'cosmiconfig-typescript-loader'; + import { cosmiconfig } from 'cosmiconfig'; import { CosmiconfigResult } from 'cosmiconfig/dist/types'; @@ -15,6 +17,9 @@ const StartConfiguration = async ( ): Promise<[ISourceConfiguration, string | undefined] | null> => { const explorer = cosmiconfig(CONFIGURATION_MODULE_NAME, { searchPlaces: configFilePath ? [configFilePath] : DEFAULT_SEARCH_PLACES, + loaders: { + '.ts': TypeScriptLoader(), + }, }); let result: CosmiconfigResult | null; diff --git a/src/modules/configuration/models/cosmiconfig.ts b/src/modules/configuration/models/cosmiconfig.ts index 9d9468a6..0faffb5a 100644 --- a/src/modules/configuration/models/cosmiconfig.ts +++ b/src/modules/configuration/models/cosmiconfig.ts @@ -7,7 +7,9 @@ export const DEFAULT_SEARCH_PLACES = [ `.${CONFIGURATION_MODULE_NAME}rc.yaml`, `.${CONFIGURATION_MODULE_NAME}rc.yml`, `.${CONFIGURATION_MODULE_NAME}rc.js`, + `.${CONFIGURATION_MODULE_NAME}rc.ts`, `.${CONFIGURATION_MODULE_NAME}rc.cjs`, `${CONFIGURATION_MODULE_NAME}.config.js`, + `${CONFIGURATION_MODULE_NAME}.config.ts`, `${CONFIGURATION_MODULE_NAME}.config.cjs`, ]; diff --git a/tsconfig.build.json b/tsconfig.build.json index 4eacd20a..00a9aaa5 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,7 +1,8 @@ { "extends": "./tsconfig.base.json", "compilerOptions": { - "outDir": "./dist" + "outDir": "./dist", + "declaration": true }, "include": ["src/**/*.ts"] } diff --git a/webpack.config.ts b/webpack.config.ts index 7a12bedc..f1a5a8e3 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -10,6 +10,10 @@ const configuration: webpack.Configuration = { mode: 'production', target: 'node', entry: './src/index.ts', + // * https://stackoverflow.com/questions/48673408/should-javascript-npm-packages-be-minified + optimization: { + minimize: false, + }, externals: [ nodeExternals({ modulesDir: path.resolve(__dirname, 'node_modules'),