This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
cli.js
executable file
·116 lines (94 loc) · 2.04 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
#!/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')
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)
--metadata Include metadata from Figma API
`, {
flags: {
outDir: {
type: 'string',
alias: 'd'
},
metadata: {
type: 'boolean'
},
debug: {
type: 'boolean'
}
}
})
const token = process.env.FIGMA_TOKEN
const [ id ] = cli.input
const opts = Object.assign({
outDir: ''
}, config, cli.flags)
if (!token) {
log.error('FIGMA_TOKEN not found')
process.exit(1)
}
if (!id) {
cli.showHelp(0)
}
opts.outDir = path.resolve(opts.outDir)
if (!fs.existsSync(opts.outDir)) {
fs.mkdirSync(opts.outDir)
}
const outFile = path.join(
opts.outDir,
'theme.json'
)
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 = JSON.stringify(parse(data, opts), null, 2)
fs.writeFile(outFile, json, (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()