Skip to content

Commit f4d26b0

Browse files
authored
fix(svg-generator): support special file name cases (#137)
1 parent 1314a49 commit f4d26b0

16 files changed

+6385
-3690
lines changed

.github/workflows/generator.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,22 @@ on:
77
pull_request:
88

99
jobs:
10-
build:
10+
ci:
1111
runs-on: ubuntu-latest
1212
defaults:
1313
run:
1414
working-directory: svg-generator
1515

1616
steps:
1717
- uses: actions/checkout@v3
18-
- name: Cache node modules
19-
uses: actions/cache@v3
20-
env:
21-
cache-name: cache-node-modules
22-
with:
23-
path: ~/.npm
24-
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
25-
restore-keys: |
26-
${{ runner.os }}-build-${{ env.cache-name }}-
27-
${{ runner.os }}-build-
28-
${{ runner.os }}-
18+
2919
- uses: actions/setup-node@v3
3020
with:
31-
node-version: '16'
21+
node-version: '18'
3222
cache: 'npm'
3323

3424
- name: Install dependencies
35-
run: npm i --force
25+
run: npm i
3626

3727
- name: Run Build
3828
run: npm run build

.github/workflows/lib.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: '@ngneat/svg-icon'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
ci:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- uses: actions/setup-node@v3
17+
with:
18+
node-version: '18'
19+
cache: 'npm'
20+
21+
- name: Install dependencies
22+
run: npm i
23+
24+
- name: Run Build
25+
run: npm run build:lib
26+
27+
- name: Run unit tests
28+
run: npm run test:lib:headless
29+
env:
30+
CI: true

.github/workflows/test.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

svg-generator/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,3 @@ testem.log
4545
.DS_Store
4646
Thumbs.db
4747
node_modules
48-
*.js

svg-generator/index.spec.ts renamed to svg-generator/__tests__/create-tree.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mock from 'mock-fs';
2-
import { createTypeFile } from './create-types';
3-
import { createTree } from './tree';
4-
import { defaults } from './types';
2+
import { createTypeFile } from '../create-types';
3+
import { createTree } from '../tree';
4+
import { defaultConfig } from '../config';
55

66
const srcPath = `src/assets/svg`;
77

@@ -20,7 +20,7 @@ describe('createTree', () => {
2020
},
2121
});
2222

23-
const result = createTree(srcPath, `src/app/svg`, defaults);
23+
const result = createTree(srcPath, `src/app/svg`, defaultConfig);
2424

2525
mock.restore();
2626

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {resolveIdentifierName} from "../tree";
2+
import {defaultConfig, GeneratorConfig} from "../config";
3+
4+
type InvalidName = string;
5+
type Expected = string;
6+
7+
describe('resolveIdentifierName', () => {
8+
const invalidNames: Array<[InvalidName, Expected]> = [
9+
['1inch', `$inch`],
10+
['sth.b', `sthB`],
11+
['st_b_', `stB`],
12+
['some-n#me', `someN$me`],
13+
['_some', `some`],
14+
];
15+
16+
it('should normalize the icon name', () => {
17+
for (const item of invalidNames) {
18+
assertName(item, defaultConfig);
19+
}
20+
});
21+
22+
it('should be configurable', () => {
23+
const customConfig = {
24+
...defaultConfig,
25+
invalidCharReplacer: () => '$$'
26+
}
27+
assertName(['1inch', `$$inch`], customConfig)
28+
});
29+
30+
});
31+
32+
function assertName([name, expected]: [InvalidName, Expected], config: Required<GeneratorConfig>) {
33+
expect(resolveIdentifierName(name, config)).toEqual(`${expected}${config.postfix}`);
34+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
export interface Config {
1+
export interface GeneratorConfig {
22
srcPath: string;
33
outputPath: string;
44
svgoConfig: { plugins: any[] };
55
prefix?: string;
66
postfix?: string;
77
rootBarrelFile?: boolean;
88
rootBarrelFileName?: string;
9+
invalidCharReplacer?: (invalidChar: string) => string;
910
}
1011

11-
export const defaults: Config = {
12+
export const defaultConfig: Required<GeneratorConfig> = {
1213
prefix: '',
1314
postfix: 'Icon',
1415
svgoConfig: { plugins: [] },
1516
srcPath: '',
1617
outputPath: '',
1718
rootBarrelFile: false,
1819
rootBarrelFileName: 'index',
20+
invalidCharReplacer: () => '$'
1921
};

svg-generator/generator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import glob from 'glob';
22
import { join, resolve } from 'path';
33
import { outputFileSync, unlinkSync } from 'fs-extra';
4-
import { Config, defaults } from './types';
4+
import { GeneratorConfig, defaultConfig } from './config';
55
import { createTree, INDEX } from './tree';
66
import { createTypeFile } from './create-types';
77

8-
export function generateSVGIcons(config: Config | null) {
8+
export function generateSVGIcons(config: GeneratorConfig | null) {
99
if (!config) {
1010
console.log(`Can't find a config object!`);
1111

1212
process.exit();
1313
}
1414

15-
const mergedConfig: Config = { ...defaults, ...config };
15+
const mergedConfig: Required<GeneratorConfig> = { ...defaultConfig, ...config };
1616

1717
removeOldIcons(resolve(mergedConfig.outputPath));
1818

@@ -36,9 +36,9 @@ export function generateSVGIcons(config: Config | null) {
3636
});
3737
}
3838

39-
const path = resolve(process.cwd(), 'node_modules', '@ngneat', 'svg-icon', 'lib', 'types.d.ts');
39+
const typeFilePath = resolve(process.cwd(), 'node_modules', '@ngneat', 'svg-icon', 'lib', 'types.d.ts');
4040

41-
outputFileSync(path, createTypeFile(names), {
41+
outputFileSync(typeFilePath, createTypeFile(names), {
4242
encoding: 'utf-8',
4343
});
4444

svg-generator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Command } from 'commander';
33
import { cosmiconfigSync } from 'cosmiconfig';
44
import { generateSVGIcons } from './generator';
5-
import { Config } from './types';
5+
import { GeneratorConfig } from './config';
66

77
const program = new Command();
88
program
@@ -19,6 +19,6 @@ program.parse();
1919
const opts = program.opts();
2020

2121
const explorerSync = cosmiconfigSync('svgGenerator');
22-
const config: Config | null = explorerSync.search(opts.configDir)?.config;
22+
const config: GeneratorConfig | null = explorerSync.search(opts.configDir)?.config;
2323

2424
generateSVGIcons(config);

0 commit comments

Comments
 (0)