joyent-portal/packages/logos/convert.js

51 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2018-02-05 17:12:47 +02:00
const HTMLtoJSX = require('htmltojsx');
const { default: forEach } = require('apr-for-each');
const main = require('apr-main');
const pascalCase = require('pascal-case');
const { readFile, writeFile } = require('mz/fs');
const execa = require('execa');
const globby = require('globby');
const path = require('path');
const ASSETS_ROOT = path.join(__dirname, 'assets');
const SRC_ROOT = path.join(__dirname, 'src');
const converter = new HTMLtoJSX({
createClass: false
});
main(async () => {
const files = await globby(`${ASSETS_ROOT}/*.svg`);
await forEach(files, async file => {
const svg = await readFile(file, 'utf-8');
const jsx = converter.convert(svg);
2018-02-28 00:40:51 +02:00
const header = jsx.match(/(^<svg)([^>]*)/gi)[0];
2018-02-05 17:12:47 +02:00
await writeFile(
file.replace(/logos\/assets\//, '/logos/src/').replace(/\.svg$/, '.js'),
`
2018-02-06 12:32:47 +02:00
import React from 'react';\n
2018-02-28 00:40:51 +02:00
export default (props) => (${jsx.replace(
header,
`${header} {...props}`
)});
2018-02-05 17:12:47 +02:00
`
);
});
await writeFile(
path.join(SRC_ROOT, 'index.js'),
files
.map(file => {
const name = path.basename(file).replace(/\.svg$/, '');
return `export { default as ${pascalCase(name)} } from './${name}'`;
})
.join('\n')
);
await execa('prettier', ['--write', '--single-quote', 'src/*.js'], {
cwd: __dirname
});
});