Skip to content

Commit

Permalink
Merge branch 'PKief:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-labs committed May 20, 2024
2 parents 43d5e10 + 94e9ce8 commit 1862362
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 145 deletions.
13 changes: 6 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,23 @@ It is also possible to use patterns for file names and extensions. This is usefu

```ts
{
name: 'sample',
name: 'graphql',
patterns: {
sample: ['ecmascript'],
}
graphql: FileNamePattern.Ecmascript,
},
}
```

In case of this example the generated file names are "sample.js", "sample.mjs", "sample.cjs",
sample.ts", "sample.mts" and "sample.cts". The pattern is defined in the [patterns.ts](src/icons/patterns/utils.ts) file.
In case of this example the generated file names are "graphql.js", "graphql.mjs", "graphql.cjs", "graphql.ts", "graphql.mts" and "graphql.cts". The pattern is defined in the [patterns.ts](src/icons/patterns/patterns.ts) file.

Allowed patterns are right now:
Available patterns are right now:

| Pattern | File extensions |
| ------------- | ---------------------------------------------------------------------------------------------------------- |
| ecmascript | `js`, `mjs`, `cjs`, `ts`, `mts`, `cts` |
| configuration | `json`, `jsonc`, `json5`, `yaml`, `yml`, `toml` |
| nodeEcosystem | Combination of ecmascript and configuration patterns |
| cosmiconfig | Similar to nodeEcosystem but in form of `.${fileName}rc`, `.config/${fileName}rc` and `${fileName}.config` |
| cosmiconfig | `.${fileName}rc`, `.config/${fileName}rc` and `${fileName}.config` with file extensions of `nodeEcosystem` |

#### Folder icons

Expand Down
105 changes: 52 additions & 53 deletions src/icons/fileIcons.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FileIcons, IconPack } from '../models/index';
import { parseByPattern } from './patterns/libs';
import { cosmiconfig, ecmascript } from './patterns/utils';
import { FileIcons, FileNamePattern, IconPack } from '../models';
import { parseByPattern } from './patterns';

/**
* Defines file icons
Expand Down Expand Up @@ -673,7 +672,7 @@ export const fileIcons: FileIcons = {
fileExtensions: ['graphql', 'gql'],
fileNames: ['.graphqlconfig'],
patterns: {
graphql: cosmiconfig,
graphql: FileNamePattern.Ecmascript,
},
},
{ name: 'rust', fileExtensions: ['rs', 'ron'] },
Expand Down Expand Up @@ -973,14 +972,14 @@ export const fileIcons: FileIcons = {
fileExtensions: ['pcss', 'sss'],
fileNames: [],
patterns: {
postcss: cosmiconfig,
postcss: FileNamePattern.Cosmiconfig,
},
},
{
name: 'posthtml',
fileNames: [],
patterns: {
posthtml: cosmiconfig,
posthtml: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -1013,41 +1012,41 @@ export const fileIcons: FileIcons = {
name: 'webpack',
fileNames: ['webpack.config.coffee'],
patterns: {
'webpack.base': ecmascript,
'webpack.client': ecmascript,
'webpack.common': ecmascript,
'webpack.config.babel': ecmascript,
'webpack.config.base.babel': ecmascript,
'webpack.config.base': ecmascript,
'webpack.config.client': ecmascript,
'webpack.config.common.babel': ecmascript,
'webpack.config.common': ecmascript,
'webpack.config.dev.babel': ecmascript,
'webpack.config.dev': ecmascript,
'webpack.config.main': ecmascript,
'webpack.config.prod.babel': ecmascript,
'webpack.config.prod': ecmascript,
'webpack.config.production.babel': ecmascript,
'webpack.config.production': ecmascript,
'webpack.config.renderer': ecmascript,
'webpack.config.server': ecmascript,
'webpack.config.staging.babel': ecmascript,
'webpack.config.staging': ecmascript,
'webpack.config.test': ecmascript,
'webpack.config.vendor.production': ecmascript,
'webpack.config.vendor': ecmascript,
'webpack.config': ecmascript,
'webpack.dev': ecmascript,
'webpack.development': ecmascript,
'webpack.dist': ecmascript,
'webpack.mix': ecmascript,
'webpack.prod.config': ecmascript,
'webpack.prod': ecmascript,
'webpack.production': ecmascript,
'webpack.server': ecmascript,
'webpack.test': ecmascript,
webpack: ecmascript,
webpackfile: ecmascript,
'webpack.base': FileNamePattern.Ecmascript,
'webpack.client': FileNamePattern.Ecmascript,
'webpack.common': FileNamePattern.Ecmascript,
'webpack.config.babel': FileNamePattern.Ecmascript,
'webpack.config.base.babel': FileNamePattern.Ecmascript,
'webpack.config.base': FileNamePattern.Ecmascript,
'webpack.config.client': FileNamePattern.Ecmascript,
'webpack.config.common.babel': FileNamePattern.Ecmascript,
'webpack.config.common': FileNamePattern.Ecmascript,
'webpack.config.dev.babel': FileNamePattern.Ecmascript,
'webpack.config.dev': FileNamePattern.Ecmascript,
'webpack.config.main': FileNamePattern.Ecmascript,
'webpack.config.prod.babel': FileNamePattern.Ecmascript,
'webpack.config.prod': FileNamePattern.Ecmascript,
'webpack.config.production.babel': FileNamePattern.Ecmascript,
'webpack.config.production': FileNamePattern.Ecmascript,
'webpack.config.renderer': FileNamePattern.Ecmascript,
'webpack.config.server': FileNamePattern.Ecmascript,
'webpack.config.staging.babel': FileNamePattern.Ecmascript,
'webpack.config.staging': FileNamePattern.Ecmascript,
'webpack.config.test': FileNamePattern.Ecmascript,
'webpack.config.vendor.production': FileNamePattern.Ecmascript,
'webpack.config.vendor': FileNamePattern.Ecmascript,
'webpack.config': FileNamePattern.Ecmascript,
'webpack.dev': FileNamePattern.Ecmascript,
'webpack.development': FileNamePattern.Ecmascript,
'webpack.dist': FileNamePattern.Ecmascript,
'webpack.mix': FileNamePattern.Ecmascript,
'webpack.prod.config': FileNamePattern.Ecmascript,
'webpack.prod': FileNamePattern.Ecmascript,
'webpack.production': FileNamePattern.Ecmascript,
'webpack.server': FileNamePattern.Ecmascript,
'webpack.test': FileNamePattern.Ecmascript,
webpack: FileNamePattern.Ecmascript,
webpackfile: FileNamePattern.Ecmascript,
},
},
{ name: 'ionic', fileNames: ['ionic.config.json', '.io-config.json'] },
Expand Down Expand Up @@ -1130,8 +1129,8 @@ export const fileIcons: FileIcons = {
name: 'babel',
fileNames: ['babel-transform.js'],
patterns: {
babel: cosmiconfig,
'babel-plugin-macros': cosmiconfig,
babel: FileNamePattern.Cosmiconfig,
'babel-plugin-macros': FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -1249,7 +1248,7 @@ export const fileIcons: FileIcons = {
'.eslintrc.base.json',
],
patterns: {
eslint: cosmiconfig,
eslint: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -1339,15 +1338,15 @@ export const fileIcons: FileIcons = {
light: true,
fileNames: ['.stylelintignore', '.stylelintcache'],
patterns: {
stylelint: cosmiconfig,
stylelint: FileNamePattern.Cosmiconfig,
},
},
{ name: 'code-climate', fileNames: ['.codeclimate.yml'], light: true },
{
name: 'prettier',
fileNames: ['.prettierignore'],
patterns: {
prettier: cosmiconfig,
prettier: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -1535,7 +1534,7 @@ export const fileIcons: FileIcons = {
light: true,
fileNames: [],
patterns: {
release: cosmiconfig,
release: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -1788,7 +1787,7 @@ export const fileIcons: FileIcons = {
name: 'husky',
fileNames: [],
patterns: {
husky: cosmiconfig,
husky: FileNamePattern.Cosmiconfig,
},
},
{ name: 'coconut', fileExtensions: ['coco'] },
Expand All @@ -1815,7 +1814,7 @@ export const fileIcons: FileIcons = {
name: 'commitlint',
fileNames: ['.commitlint.yaml', '.commitlint.yml'],
patterns: {
commitlint: cosmiconfig,
commitlint: FileNamePattern.Cosmiconfig,
},
},
{ name: 'buck', fileNames: ['.buckconfig'] },
Expand Down Expand Up @@ -2177,7 +2176,7 @@ export const fileIcons: FileIcons = {
name: 'svgr',
fileNames: [],
patterns: {
svgr: cosmiconfig,
svgr: FileNamePattern.Cosmiconfig,
},
},
{ name: 'rome', fileNames: ['rome.json'] },
Expand Down Expand Up @@ -2284,7 +2283,7 @@ export const fileIcons: FileIcons = {
name: 'craco',
fileNames: [],
patterns: {
craco: cosmiconfig,
craco: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -2340,7 +2339,7 @@ export const fileIcons: FileIcons = {
name: 'syncpack',
fileNames: [],
patterns: {
syncpack: cosmiconfig,
syncpack: FileNamePattern.Cosmiconfig,
},
},
{
Expand Down Expand Up @@ -2394,7 +2393,7 @@ export const fileIcons: FileIcons = {
name: 'puppeteer',
fileNames: [],
patterns: {
puppeteer: cosmiconfig,
puppeteer: FileNamePattern.Cosmiconfig,
},
},
{ name: 'apps-script', fileExtensions: ['gs'] },
Expand Down
File renamed without changes.
28 changes: 0 additions & 28 deletions src/icons/patterns/libs.ts

This file was deleted.

95 changes: 95 additions & 0 deletions src/icons/patterns/patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
FileIconWithPatterns,
FileIcon,
Patterns,
FileNamePattern,
} from '../../models';

/**
* Maps the patterns to an array of strings.
* Each pattern is a function that generates file names based on a key.
*
* @param patterns The patterns to map.
* @returns An array of strings generated by applying the patterns.
*/
const mapPatterns = (patterns: Patterns): string[] => {
return Object.entries(patterns).flatMap(([fileName, pattern]) => {
switch (pattern) {
case FileNamePattern.Ecmascript:
return [
`${fileName}.js`,
`${fileName}.mjs`,
`${fileName}.cjs`,
`${fileName}.ts`,
`${fileName}.mts`,
`${fileName}.cts`,
];

case FileNamePattern.Configuration:
return [
`${fileName}.json`,
`${fileName}.jsonc`,
`${fileName}.json5`,
`${fileName}.yaml`,
`${fileName}.yml`,
`${fileName}.toml`,
];

case FileNamePattern.NodeEcosystem:
return [
`${fileName}.js`,
`${fileName}.mjs`,
`${fileName}.cjs`,
`${fileName}.ts`,
`${fileName}.mts`,
`${fileName}.cts`,
`${fileName}.json`,
`${fileName}.jsonc`,
`${fileName}.json5`,
`${fileName}.yaml`,
`${fileName}.yml`,
`${fileName}.toml`,
];

case FileNamePattern.Cosmiconfig:
return [
`.${fileName}rc`,
`.config/${fileName}rc`,
`${fileName}.config`,
`${fileName}.js`,
`${fileName}.mjs`,
`${fileName}.cjs`,
`${fileName}.ts`,
`${fileName}.mts`,
`${fileName}.cts`,
`${fileName}.json`,
`${fileName}.jsonc`,
`${fileName}.json5`,
`${fileName}.yaml`,
`${fileName}.yml`,
`${fileName}.toml`,
];

default:
// Check if all potential pattern cases are handled
const exhaustiveCheck: never = pattern;
throw new Error(`Unhandled pattern: ${exhaustiveCheck}`);
}
});
};

/**
* Parses the raw file icons by applying the patterns.
* A pattern helps to generate file names based on a key.
*
* @param rawFileIcons - The list of file icons without applied patterns.
* @returns The list of file icons with applied patterns.
*/
export const parseByPattern = (
rawFileIcons: FileIconWithPatterns
): FileIcon[] => {
return rawFileIcons.map(({ patterns, fileNames = [], ...rest }) => ({
...rest,
fileNames: patterns ? [...mapPatterns(patterns), ...fileNames] : fileNames,
}));
};
Loading

0 comments on commit 1862362

Please sign in to comment.