Skip to content

Commit

Permalink
Add type declarations (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewey committed Nov 29, 2021
1 parent b9861b1 commit d336555
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
"bugs": {
"url": "https://github.com/jaynewey/charm-icons/issues"
},
"typings": "dist/charm-icons.d.ts",
"scripts": {
"start": "babel-watch --watch src",
"clean": "rm -rf dist && rm -rf build",
"build": "npm run clean && npm run build:move && npm run build:icons && npm run build:esm && npm run build:bundles",
"build": "npm run clean && npm run build:move && npm run build:icons && npm run build:esm && npm run build:bundles && npm run build:move-types",
"build:move": "cp -av src build",
"build:icons": "npx babel-node --presets @babel/env scripts/build/buildModules.js",
"build:esm": "babel build -d dist/esm",
"build:bundles": "rollup -c",
"build:move-types": "cp build/charm-icons.d.ts dist/charm-icons.d.ts",
"test": "jest --coverage",
"release": "release-it"
},
Expand Down
5 changes: 4 additions & 1 deletion scripts/build/buildModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import moduleTemplate from './moduleTemplate';
import buildIconFiles from './buildIconFiles';
import buildIndexFile from './buildIndexFile';
import { readSvgDir } from '../helpers';
import buildTypesFile from './buildTypesFile';

const ICON_DIR = path.resolve(__dirname, '../../icons');
const OUTPUT_DIR = path.resolve(__dirname, '../../build/icons');
Expand All @@ -18,4 +19,6 @@ if (!fs.existsSync(OUTPUT_DIR)) {

buildIconFiles(ICON_DIR, OUTPUT_DIR, svgFiles, moduleTemplate);
buildIndexFile(OUTPUT_DIR, svgFiles);
console.log('Successfully built export file.');
console.log('Successfully built export file.')
buildTypesFile(path.resolve(`${OUTPUT_DIR}/..`), svgFiles);
console.log('Successfully built types file.')
53 changes: 53 additions & 0 deletions scripts/build/buildTypesFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { emptyFile, writeToFile, filenameToModule } from '../helpers';

const TYPES_FILENAME = 'charm-icons.d.ts'

function typeTemplate(filenames) {
return `\
declare module 'charm-icons';
export declare class Icon {
name: string;
paths: string;
keywords: [string];
constructor(name: string, paths: string, keywords: [string]);
}
export type Icons = { [key: string]: Icon };
export declare function getAttrs(icon: Icon, attrs?: { [key: string]: string; }): { [key: string]: string; };
export declare function toSvg(icon: Icon, attrs?: { [key: string]: string; }): string;
export declare function toElement(icon: Icon, attrs?: { [key: string]: string; }): HTMLElement;
export declare function replaceElement(
element: HTMLElement,
icon: Icon,
attrs?: { [key: string]: string; },
replaceAttr?: string
): Node;
export interface PlaceIconsOptions {
icons?: object, attrs?: object, replaceAttr?: string
}
export declare function placeIcons(options?: PlaceIconsOptions);
export declare const icons: Icons;
// icons
${
filenames.map((filename) => {
const iconName = filenameToModule(filename);
return `export declare const ${iconName}: Icon;`
}).join('\n')
}
`;
}

export default function buildTypesFile(outputDir, filenames) {
emptyFile(outputDir, TYPES_FILENAME);
writeToFile(outputDir, TYPES_FILENAME, typeTemplate(filenames));
}

0 comments on commit d336555

Please sign in to comment.