Skip to content

Instantly share code, notes, and snippets.

@sebald
Created July 31, 2017 07:05
Show Gist options
  • Save sebald/6405bce76912a7e504b8569afdb9ca6c to your computer and use it in GitHub Desktop.
Save sebald/6405bce76912a7e504b8569afdb9ca6c to your computer and use it in GitHub Desktop.
/**
* Generate type definitions for `material-ui-icons`.
* The generated contens will be written to
* `src/typings/material-ui-icons.d.ts`.
*/
const { parse, resolve } = require('path');
const { EOL } = require('os');
const chalk = require('chalk');
const { writeFile } = require('fs-extra');
const globby = require('globby');
const { map, pipe } = require('ramda');
const { error } = require('./utils');
const log = console.log;
const MODULE_DIR = resolve(__dirname, '../node_modules/material-ui-icons');
const DTS_FILE = resolve(__dirname, '../src/typings/material-ui-icons.d.ts');
// Helpers
// ---------------
const normalizeFileName = name => parse(name).name;
const createTypeDefinition = name =>
`declare module 'material-ui-icons/${name}' {
import SvgIcon from 'material-ui/SvgIcon';
export default class ${name} extends SvgIcon {}
}`;
const convertFilesToTypings = map(
pipe(normalizeFileName, createTypeDefinition)
);
log(`\u{1f52c} Searching for modules inside "${chalk.dim(MODULE_DIR)}".`);
globby(['*.js', '!index*'], { cwd: MODULE_DIR })
.then(convertFilesToTypings)
.then(typings => typings.join(`${EOL}${EOL}`))
.then(contents => writeFile(DTS_FILE, contents, { encoding: 'utf8' }))
.then(() => log(`\u{1F5C4} Written typings to ${chalk.dim(DTS_FILE)}.`))
.catch(err => {
if (err) {
error(err);
}
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment