-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·60 lines (49 loc) · 1.6 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
#!/usr/bin/env node
const yargs = require('yargs');
const fs = require('fs').promises;
const path = require('path');
const { generateIndexFile, renameSvgFiles, updateSvgFile } = require('./utils');
const argv = yargs.option('p', {
alias: 'path',
describe: 'The path to the SVG folder',
demandOption: true,
type: 'string',
}).argv;
async function main() {
const svgFolderPath = argv.path;
if (!svgFolderPath) {
console.error(
'The -p parameter is required. Please provide the path to the SVG folder.'
);
process.exit(1);
}
try {
await fs.access(svgFolderPath, fs.constants.F_OK);
} catch (err) {
console.warn(`Warning: The folder ${svgFolderPath} does not exist.`);
return;
}
const files = await fs.readdir(svgFolderPath);
const [svgFiles, jsxFiles] = files.reduce(
([svgs, jsxs], file) => {
const ext = path.extname(file);
return ext === '.svg'
? [[...svgs, file], jsxs]
: ext === '.js'
? [svgs, [...jsxs, file]]
: [svgs, jsxs];
},
[[], []]
);
if (svgFiles.length === 0 && jsxFiles.length === 0) {
console.log('No .svg and .jsx files found in the folder');
return;
}
await Promise.all(
svgFiles.map((file) => updateSvgFile(path.join(svgFolderPath, file)))
);
const newJsxFiles = await renameSvgFiles(svgFolderPath, svgFiles);
generateIndexFile(svgFolderPath, [...jsxFiles, ...newJsxFiles]);
console.log('Updated!');
}
main().catch(console.error);