Skip to content

Commit afc8a6d

Browse files
committed
fix(types): add type file for each icons
1 parent b265ff9 commit afc8a6d

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/generate.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import fetch from 'node-fetch'
22
import { JSDOM } from 'jsdom'
33
import fs from 'fs-extra'
4+
import path from 'path'
45
import { transform } from '@babel/core'
56
import {
67
moduleBabelConfig, allModulesBabelConfig, replaceAll,
7-
toHumpName, toComponentName,
8+
toHumpName, toComponentName, makeBasicDefinition,
89
} from './utils'
910

10-
const sourceFile = `${__dirname}/../.source`
11+
const outputDir = path.join(__dirname, '../', 'dist')
12+
const sourceFile = path.join(__dirname, '../', '.source')
1113

1214
export default (async () => {
13-
await fs.remove('./dist')
15+
await fs.remove(outputDir)
1416
let html = ''
1517
try {
1618
html = await fs.readFile(sourceFile, 'utf8')
@@ -22,12 +24,7 @@ export default (async () => {
2224

2325
const document = new JSDOM(html).window.document
2426
let exports = ''
25-
let definition = `import React from 'react';
26-
interface Props extends React.SVGProps {
27-
color?: string;
28-
size?: number;
29-
}
30-
type Icon = React.FunctionComponent<Props>;\n`
27+
let definition = makeBasicDefinition()
3128

3229
const icons = document.querySelectorAll('.geist-list .icon')
3330
const promises = Array.from(icons).map((icon: Element) => {
@@ -48,18 +45,24 @@ export default ${componentName};`
4845

4946
exports += `export { default as ${componentName} } from './${fileName}';\n`
5047
definition += `export const ${componentName}: Icon;\n`
51-
48+
49+
const singleDefinition = `${makeBasicDefinition()}declare const _default: Icon;\nexport default _default\n`
50+
51+
fs.outputFile(
52+
path.join(outputDir, `${fileName}.d.ts`),
53+
singleDefinition,
54+
)
5255
return fs.outputFile(
53-
`${__dirname}/../dist/${fileName}.js`,
54-
transform(component, moduleBabelConfig).code
56+
path.join(outputDir, `${fileName}.js`),
57+
transform(component, moduleBabelConfig).code,
5558
)
5659
})
5760

5861
await Promise.all(promises)
59-
await fs.outputFile(`${__dirname}/../dist/index.d.ts`, definition)
62+
await fs.outputFile(path.join(outputDir, 'index.d.ts'), definition)
6063
await fs.outputFile(
61-
`${__dirname}/../dist/index.js`,
62-
transform(exports, allModulesBabelConfig).code
64+
path.join(outputDir, 'index.js'),
65+
transform(exports, allModulesBabelConfig).code,
6366
)
6467
})()
6568

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ export const toComponentName = (name: string): string => {
2727
return `${first}${last}`
2828
}
2929

30+
export const makeBasicDefinition = (): string => {
31+
return `import React from 'react';
32+
interface Props extends React.SVGProps {
33+
color?: string;
34+
size?: number;
35+
}
36+
type Icon = React.FunctionComponent<Props>;\n`
37+
}
3038

0 commit comments

Comments
 (0)