babel-plugin-svg-react
Version:
Transpile SVG documents into React component modules with only Babel.
82 lines (72 loc) • 1.77 kB
JavaScript
const path = require("path");
const yargs = require("yargs");
const { version } = require("../package.json");
const writeDeclarations = require("./writeDeclarations");
/**
* @param {Error} error
*/
function handleError(error) {
console.error(error);
process.exit(1);
}
/**
* @param {yargs.Arguments} argv
*/
async function handleRunCommand(argv) {
const { svgDir, outDir, dryRun } = argv;
const cwd = process.cwd();
try {
await writeDeclarations({
svgDir: path.resolve(cwd, svgDir),
outDir: path.resolve(cwd, outDir),
dryRun
});
} catch (error) {
handleError(error);
}
console.log("Done.");
}
function createDefaultCommand() {
return {
command: "$0",
handler() {
console.log(`SVG React v${version}. Run --help, for a list of commands.`);
}
};
}
function createRunCommand() {
return {
describe: "Generate TypeScript declarations for SVG components.",
command: "declare",
handler: handleRunCommand,
builder: {
svgDir: {
alias: "s",
describe: "The directory to search for SVG files.",
normalize: true,
default: ".",
defaultDescription: "The current working directory."
},
outDir: {
alias: "o",
describe: "The directory to output declarations",
normalize: true,
default: ".",
defaultDescription: "The current working directory."
},
dryRun: {
alias: "d",
describe: "Run without persisting changes.",
boolean: true
}
}
};
}
module.exports = function reactSvgCli() {
// eslint-disable-next-line no-unused-expressions
yargs
.command(createDefaultCommand())
.help()
.command(createRunCommand())
.help().argv;
};