Skip to content

Commit

Permalink
feat: 🔥 support typescript configuration file
Browse files Browse the repository at this point in the history
support typescript configuration file
  • Loading branch information
Tal Rofe committed Apr 25, 2022
1 parent 9987534 commit a3e5b1f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
},
"files.associations": {
"commit-msg": "shellscript"
},
"material-icon-theme.activeIconPack": "nest"
}
}
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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<string, RuleValue>;
aliases: Record<string, AliasValue>;
ignorePath: string;
ignore: boolean;
ignorePatterns: string[];
quiet: boolean;
maxWarnings: number;
bail: number;
outputFile: string;
}>;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Bootstrap from './modules/app';

Bootstrap();

export * from './config';
2 changes: 1 addition & 1 deletion src/modules/cli/functions/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cli/interfaces/init.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/modules/cli/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions src/modules/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';

import TypeScriptLoader from 'cosmiconfig-typescript-loader';

import { cosmiconfig } from 'cosmiconfig';
import { CosmiconfigResult } from 'cosmiconfig/dist/types';

Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/configuration/models/cosmiconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
];
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"declaration": true
},
"include": ["src/**/*.ts"]
}
4 changes: 4 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit a3e5b1f

Please sign in to comment.