Skip to content

Commit b05a167

Browse files
committed
refactor: use @carbon/[email protected]
Remove devDeps: npm-run-all, prettier-plugin-svelte, svelte.
1 parent 15443ac commit b05a167

12 files changed

+232
-375
lines changed

package.json

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
"svelte": "lib/index.js",
88
"main": "lib/index.js",
99
"scripts": {
10-
"build": "run-s build:*",
11-
"build:task": "tsc",
12-
"build:lib": "node dist",
13-
"build:format-lib": "prettier --write 'lib/**/*.{js,svelte}'",
10+
"build": "tsc && node dist",
1411
"test": "jest --coverage",
1512
"test:tdd": "jest --watch",
1613
"prepublishOnly": "yarn build"
1714
},
1815
"devDependencies": {
1916
"@carbon/icon-helpers": "10.4.0",
20-
"@carbon/icons": "10.6.1",
17+
"@carbon/icons": "10.8.0-rc.0",
2118
"@commitlint/cli": "^8.2.0",
2219
"@commitlint/config-conventional": "^8.2.0",
2320
"@types/fs-extra": "^8.0.1",
@@ -26,11 +23,8 @@
2623
"husky": "^3.1.0",
2724
"jest": "^24.9.0",
2825
"lint-staged": "^9.4.3",
29-
"npm-run-all": "^4.1.5",
3026
"prettier": "^1.19.1",
31-
"prettier-plugin-svelte": "^0.7.0",
3227
"pretty-quick": "^2.0.1",
33-
"svelte": "^3.16.4",
3428
"ts-jest": "^24.2.0",
3529
"tslint": "^5.20.1",
3630
"tslint-config-prettier": "^1.18.0",
@@ -49,6 +43,7 @@
4943
]
5044
},
5145
"prettier": {
46+
"printWidth": 100,
5247
"tabWidth": 2,
5348
"semi": true,
5449
"singleQuote": true

src/buildIcons.ts

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { ensureDir, existsSync, readFile, remove, writeFile } from 'fs-extra';
2+
import { template } from './template';
3+
4+
type IconSize = 16 | 20 | 24 | 32;
5+
6+
interface IPath {
7+
elem: 'path';
8+
attrs: { d: string };
9+
}
10+
11+
interface ICircle {
12+
elem: 'circle';
13+
attrs: { cx: string; cy: string; r: string };
14+
}
15+
16+
interface IRect {
17+
elem: 'rect';
18+
attrs: { width: string; height: string; x: string; y: string; rx: string };
19+
}
20+
21+
export type IconContent = ReadonlyArray<IPath | ICircle | IRect>;
22+
23+
export interface IIconAttrs {
24+
xmlns: 'http://www.w3.org/2000/svg';
25+
viewBox: string;
26+
width: IconSize;
27+
height: IconSize;
28+
}
29+
30+
export interface IBuildIcon {
31+
filename: string;
32+
basename: string;
33+
size: IconSize;
34+
prefix: string[];
35+
descriptor: {
36+
elem: 'svg';
37+
attrs: IIconAttrs;
38+
content: IconContent;
39+
name: string;
40+
size: IconSize;
41+
};
42+
moduleName: string;
43+
original: 32;
44+
outputOptions: {
45+
file: string;
46+
};
47+
}
48+
49+
async function buildIcons({ path, dist }: { path: string; dist: string }) {
50+
if (!existsSync(path)) {
51+
throw Error(`${path} does not exist.`);
52+
}
53+
54+
const buffer = await readFile(path);
55+
const iconMetadata: IBuildIcon[] = JSON.parse(buffer.toString());
56+
57+
await remove(dist);
58+
await ensureDir(dist);
59+
60+
const baseImports: string[] = [];
61+
const baseExports: string[] = [];
62+
63+
iconMetadata.forEach(async ({ descriptor: { attrs, content }, moduleName }) => {
64+
const component = template({ attrs, content });
65+
const componentName = `${moduleName}.svelte`;
66+
const componentFolder = `${dist}/${moduleName}`;
67+
const componentPath = `${componentFolder}/${componentName}`;
68+
const exportPath = `${dist}/${moduleName}/index.js`;
69+
const exportFile = `import ${moduleName} from './${componentName}';
70+
export default ${moduleName};`;
71+
72+
baseImports.push(`import ${moduleName} from './${moduleName}';\n`);
73+
baseExports.push(moduleName);
74+
75+
await ensureDir(componentFolder);
76+
await writeFile(componentPath, component);
77+
await writeFile(exportPath, exportFile);
78+
});
79+
80+
const baseFile = `${baseImports.join('')}
81+
export {
82+
${baseExports.join(',\n ')}
83+
};`;
84+
85+
await writeFile(`${dist}/index.js`, baseFile);
86+
}
87+
88+
export { buildIcons };

src/createComponent.ts

-98
This file was deleted.

src/format.ts

-79
This file was deleted.

src/index.ts

+9-48
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
1-
import * as icons from '@carbon/icons';
2-
import * as fs from 'fs-extra';
3-
import { createComponent } from './createComponent';
4-
import { formatName } from './format';
1+
import { buildIcons } from './buildIcons';
52

63
async function build() {
7-
await fs.remove('lib');
8-
await fs.ensureDir('lib');
9-
10-
const baseImports: string[] = [];
11-
const baseExports: { [componentName: string]: string } = {};
12-
13-
// TODO: use build-info.json (#5)
14-
Object.keys(icons).forEach(async iconName => {
15-
const { name, size, markup } = createComponent(icons[iconName]);
16-
17-
// TODO: use `moduleName` from build-info.json (#5)
18-
const componentName = formatName({ name, size });
19-
20-
if (componentName != null && !(componentName in baseExports)) {
21-
baseImports.push(`import ${componentName} from './${componentName}';`);
22-
baseExports[componentName] = componentName;
23-
24-
await fs.ensureDir(`lib/${componentName}`);
25-
await fs.writeFile(
26-
`lib/${componentName}/index.js`,
27-
`
28-
import ${componentName} from './${componentName}.svelte';
29-
export default ${componentName};
30-
`
31-
);
32-
await fs.writeFile(
33-
`lib/${componentName}/${componentName}.svelte`,
34-
markup
35-
);
36-
}
37-
});
38-
39-
const indexFile = [
40-
...baseImports,
41-
`
42-
export {
43-
${Object.keys(baseExports)
44-
.map(item => item)
45-
.join(',')}
46-
}
47-
`
48-
];
49-
50-
await fs.writeFile(`lib/index.js`, indexFile.join(''));
4+
const path = 'node_modules/@carbon/icons/build-info.json';
5+
const dist = 'lib';
6+
7+
try {
8+
await buildIcons({ path, dist });
9+
} catch (error) {
10+
process.stderr.write(`${error}\n`);
11+
}
5112
}
5213

5314
build();

0 commit comments

Comments
 (0)