-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·61 lines (51 loc) · 1.25 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
const clipboardy = require("clipboardy");
const prompts = require("prompts");
const fs = require("fs");
const svgr = require("@svgr/core").default;
const { join } = require("path");
const createSpinner = require("ora");
if (require.main === module) {
main();
}
async function main() {
const clipboard = clipboardy.readSync();
const isXML = clipboard.trim()[0] === "<";
if (!isXML) {
console.error("🙅 You need an SVG in the clipbord. 🙅");
return;
}
const { componentName } = await prompts({
type: "text",
name: "componentName",
message: "What should the component be called?",
});
await importSvg({
svg: clipboard,
componentName,
});
}
exports.importSvg = importSvg;
async function importSvg({ svg, componentName }) {
const svgSpinner = createSpinner("Importing SVG").start();
const code = await svgr(
svg,
{
native: true,
template,
},
{ componentName }
);
clipboardy.writeSync(code);
svgSpinner.stop();
console.log("📋 SVG copied to clipboard");
}
function template(
{ template },
opts,
{ imports, componentName, props, jsx, exports }
) {
return template.ast`${imports}
export default function ${componentName} (props) { return (${jsx}) }
`;
}