forked from andrew-paterson/svg-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-from-svg.js
37 lines (33 loc) · 1.45 KB
/
make-from-svg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const path = require('path');
const fs = require('fs');
const sourcePath = process.argv[2];
const subDir = process.argv[3] || 'icons'
const subDirSingular = subDir.slice(0, -1);
const chalk = require('chalk');
if (!fs.existsSync(sourcePath)) {
console.log(chalk.red(`${sourcePath} does not exist.`))
}
const componentName = `${subDirSingular}-${path.parse(sourcePath).name}`;
const templateFileAdditions = ` style={{style}} class="{{class}} single-colour ${componentName}" data-test-icon="${componentName}"`;
const sourceFileContents = fs.readFileSync(sourcePath, 'utf8');
const sourceAttrs = sourceFileContents.match(/<svg(.*?)>(.*)/);
const templateFileContents = `<svg ${sourceAttrs[1]} ${templateFileAdditions}>${sourceAttrs[2]}`.replace(/\s+/g, ' ');
const files = [{
outPath: `./app/components/svg-repo/${subDir}/${componentName}.js`,
contents: `export { default } from 'svg-repo/components/svg-repo/${subDir}/${componentName}';`
}, {
outPath: `./addon/components/svg-repo/${subDir}/${componentName}.js`,
contents:
`import SvgBase from 'svg-repo/components/svg-base';
import layout from '../../../templates/components/svg-repo/${subDir}/${componentName}';
export default SvgBase.extend({
layout,
});`
}, {
outPath: `./addon/templates/components/svg-repo/${subDir}/${componentName}.hbs`,
contents: templateFileContents
}];
files.forEach(file => {
fs.writeFileSync(file.outPath, file.contents);
console.log(chalk.green(`${file.outPath} was created.`))
});