-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·52 lines (44 loc) · 957 Bytes
/
cli.js
File metadata and controls
executable file
·52 lines (44 loc) · 957 Bytes
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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const DEFAULT_BASE = '16px';
const cleanString = val => {
const cleaned = val.match(/\d+(\.\d+)?/g);
return cleaned && Array.isArray(cleaned) ? cleaned.map(Number) : '0';
};
const emify = (val, args) => {
const {base, unit, rounding} = args;
const computed = cleanString(val) / cleanString(base);
const formatted = computed % 1 ? computed.toFixed(rounding) : computed;
return `${formatted}${unit}`;
};
const cli = meow(
`
Usage
$ emify [input]
Options
--base 10px [Default: 16px]
--unit em [Default: rem]
--rounding 2 [Default: 3]
Examples
$ emify 12px --base 14px --unit rem --rounding 3
1.17rem
`,
{
flags: {
base: {
type: 'string',
default: '16px'
},
unit: {
type: 'string',
default: 'rem'
},
rounding: {
type: 'number',
default: 3
}
}
}
);
console.log(emify(cli.input[0] || DEFAULT_BASE, cli.flags));