-
-
Save vdanchenkov/6fe439c1e35545572d8f64c8aea66595 to your computer and use it in GitHub Desktop.
Generate ReScript module for icons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chokidar = require('chokidar') | |
const fs = require('fs') | |
let emitComponent = (name, path) => { | |
return `module ${name} = { | |
@module("../svg/${path}") @react.component | |
external make: ( | |
~width: int=?, | |
~height: int=?, | |
~className: string=? | |
) => React.element = "default" | |
} | |
` | |
} | |
const rebuild = (watched) => { | |
let components = [] | |
Object.entries(watched).forEach(([key, files]) => { | |
let namePrefix = key == '.' ? '' : `${key}__` | |
let pathPrefix = key == '.' ? '' : `${key}/` | |
files.forEach((fileName) => { | |
if (fileName.includes('.svg')) { | |
let [name] = fileName.split('.') | |
components.push(emitComponent(namePrefix + name, pathPrefix + fileName)) | |
} | |
}) | |
}) | |
fs.writeFileSync('./components/Icon.res', components.sort().join('\n')) | |
} | |
const watcher = chokidar.watch('.', { ignoreInitial: true, cwd: './svg' }) | |
watcher | |
.on('add', () => rebuild(watcher.getWatched())) | |
.on('unlink', () => rebuild(watcher.getWatched())) | |
.on('ready', () => rebuild(watcher.getWatched())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
watches
./svg/**
Given there are an image with path
./svg/Foo/Bar.svg
, following content will be generated in./components/Icon.res