forked from jxnblk/figma-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·164 lines (140 loc) · 3.32 KB
/
cli.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
require('dotenv').config()
const fs = require('fs')
const path = require('path')
const meow = require('meow')
const chalk = require('chalk')
const Figma = require('figma-js')
const parse = require('./index')
JSON.sortify = require('json.sortify');
const config = require('pkg-conf').sync('figma-theme')
const log = (...args) => {
console.log(
chalk.cyan('[figma-theme]'),
...args
)
}
log.error = (...args) => {
console.log(
chalk.red('[error]'),
...args
)
}
const cli = meow(`
${chalk.gray('Usage')}
$ figma-theme <file-id>
${chalk.gray('Options')}
-d --out-dir Output directory (default cwd)
-n --out-name Output filename without extension (default theme)
-t --out-type Output type (default json)
-f --filter Comma separated list for filtering outputs
--metadata Include metadata from Figma API
--opacity-as-alpha Use fill opacity as alpha channel
--rgb Use rgb(a) format in all color values
--rgba Use rgba format in color values with an alpha channel
--sort Sort results
`, {
flags: {
outDir: {
type: 'string',
alias: 'd'
},
outName: {
type: 'string',
alias: 'n'
},
outType: {
type: 'string',
alias: 't'
},
filter: {
type: 'string'
},
metadata: {
type: 'boolean'
},
opacityAsAlpha: {
type: 'boolean'
},
rgb: {
type: 'boolean'
},
rgba: {
type: 'boolean'
},
sort: {
type: 'boolean'
},
debug: {
type: 'boolean'
}
}
})
const token = process.env.FIGMA_TOKEN
const [ id ] = cli.input
const opts = Object.assign({
outDir: '',
outName: 'theme',
outType: 'json'
}, config, cli.flags)
opts.filter = (opts.filter !== undefined) ? opts.filter.split(',') : []
if (!token) {
log.error('FIGMA_TOKEN not found')
process.exit(1)
}
if (!id) {
cli.showHelp(0)
}
const allowedTypes = [
'json'
]
opts.outDir = path.resolve(opts.outDir)
opts.outName = opts.outName.toLowerCase().replace(/[^a-z0-9\-]/gi, '-')
opts.outType = (allowedTypes.indexOf(opts.outType) >= 0) ? opts.outType : 'json'
if (!fs.existsSync(opts.outDir)) {
fs.mkdirSync(opts.outDir)
}
const outFile = path.join(
opts.outDir,
opts.outName + '.' + opts.outType
)
const figma = Figma.Client({
personalAccessToken: token
})
log('fetching data for:', chalk.gray(id))
figma.file(id)
.then(res => {
if (res.status !== 200) {
log.error(res.status, res.statusText)
process.exit(1)
return
}
const { data } = res
log('parsing data...')
const json = parse(data, opts)
let outContent = '';
switch (opts.outType) {
default:
case 'json':
outContent = JSON[opts.sort ? 'sortify' : 'stringify'](json, null, 2)
break;
}
fs.writeFile(outFile, outContent, (err) => {
if (err) {
log.error(err)
process.exit(1)
}
log('file saved', chalk.gray(outFile))
})
if (opts.debug) {
fs.writeFile(path.join(opts.outDir, 'data.json'), JSON.stringify(data, null, 2), err => {})
}
})
.catch(err => {
const { response } = err
log.error(response.status, response.statusText)
process.exit(1)
})
require('update-notifier')({
pkg: cli.pkg
}).notify()