-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·135 lines (126 loc) · 3.78 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
const inquirer = require('inquirer');
const icons = require('@mdi/svg/meta.json');
const Fuse = require('fuse.js');
const chalk = require('chalk');
const ora = require('ora');
const meow = require('meow');
const mdi = require('mdi-core');
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'));
const fuse = new Fuse(icons, {
shouldSort: true,
threshold: 0,
keys: ['name', 'aliases']
});
const spinner = ora('Generating icons');
const cli = meow(`
${chalk.cyan.underline('Usage:')}
${chalk.gray('$')} ${chalk.green('mdi')} ${chalk.yellow('[options]')} ${chalk.magenta('<icon-name> <icon-name> ...')}
${chalk.cyan.underline('Options:')}
${chalk.yellow('--size')}, ${chalk.yellow('-s')} ${chalk.gray('Set the icon size. Defaults to 24px.')}
${chalk.yellow('--padding')}, ${chalk.yellow('-p')} ${chalk.gray('Set the icon padding. Defaults to 0px.')}
${chalk.yellow('--radius')}, ${chalk.yellow('-r')} ${chalk.gray('Set the icon border radius. Defaults to 0px.')}
${chalk.yellow('--foreground')}, ${chalk.yellow('-f')} ${chalk.gray('Set the icon foreground color. Defaults to #333.')}
${chalk.yellow('--background')}, ${chalk.yellow('-b')} ${chalk.gray('Set the icon background color. Defaults to transparent.')}
${chalk.yellow('--output')}, ${chalk.yellow('-o')} ${chalk.gray('Write icons to this directory. Defaults to the current directory.')}
${chalk.cyan.underline('Examples:')}
${chalk.gray('$')} ${chalk.green('mdi')} ${chalk.yellow('-s 32 -p 4 -r 5 -f #aaa -b #fff')} ${chalk.magenta('google youtube twitter')}
`, {
flags: {
size: {
type: 'string',
alias: 's',
default: '24'
},
padding: {
type: 'string',
alias: 'p',
default: '0'
},
radius: {
type: 'string',
alias: 'r',
default: '0'
},
foreground: {
type: 'string',
alias: 'f',
default: '#333'
},
background: {
type: 'string',
alias: 'b',
default: 'transparent'
},
output: {
type: 'string',
alias: 'o',
default: '.'
}
},
inferType: true
});
const promptOptions = [{
type: 'checkbox-plus',
name: 'names',
message: 'Icon names:',
source(answers, input) {
return input
? Promise.resolve(fuse.search(input))
: Promise.resolve(icons.map(icon => icon.name));
},
searchable: true
}, {
type: 'input',
name: 'size',
message: 'Icon size (px):',
default: 24,
filter: Number
}, {
type: 'input',
name: 'padding',
message: 'Icon padding (px):',
default: 0,
filter: Number
}, {
type: 'input',
name: 'radius',
message: 'Border radius (px):',
default: 0,
filter: Number
}, {
type: 'input',
name: 'foreground',
message: 'Foreground color:',
default: '#333'
}, {
type: 'input',
name: 'background',
message: 'Background color:',
default: 'transparent'
}, {
type: 'input',
name: 'output',
message: 'Output path:',
default: 'current directory',
filter(input) {
return input === 'current directory' ? '.' : input;
}
}];
(async function() {
const useBuilder = process.argv.length === 2;
const config = useBuilder
? await inquirer.prompt(promptOptions)
: cli.flags;
if (!useBuilder) {
config.names = cli.input;
}
spinner.start();
const iconPaths = await mdi(config);
spinner.stop();
// Add an extra separator line
console.log();
iconPaths.forEach(iconPath => {
console.log(` Generated ${chalk.green(iconPath)}`);
});
})();