forked from jxnblk/repng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·145 lines (126 loc) · 3.01 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const meow = require('meow')
const ora = require('ora')
const render = require('./index')
const readPkg = require('read-pkg-up')
const absolute = (file = '') => !file || path.isAbsolute(file)
? file
: path.join(process.cwd(), file)
const getDateTime = () => {
const now = new Date()
const Y = now.getFullYear()
const M = now.getMonth()
const d = now.getDate()
const h = now.getHours()
const m = now.getMinutes()
const s = now.getSeconds()
return {
date: [ Y, M, d ].join('-'),
time: [ h, m, s ].join('.')
}
}
const cli = meow(`
Usage
$ repng <ReactComponent>
Options
-d --out-dir Directory to save file to
-f --filename Specify a custom output filename
-w --width Width of image
-h --height Height of image
-p --props Props in JSON format to pass to the React component
--css Path to CSS file to include
--webfont Path to custom webfont for rendering
`, {
flags: {
outDir: {
type: 'string',
alias: 'd'
},
filename: {
type: 'string',
alias: 'f'
},
width: {
type: 'string',
alias: 'w'
},
height: {
type: 'string',
alias: 'h'
},
scale: {
type: 'string',
alias: 's'
},
delay: {
type: 'string',
alias: 'D'
},
props: {
type: 'string',
alias: 'p'
},
css: {
type: 'string'
},
cssLibrary: {
type: 'string'
}
}
})
const [ file ] = cli.input
const spinner = ora(`Rendering ${file}`).start()
const name = path.basename(file, path.extname(file))
const filepath = absolute(file)
const { pkg } = readPkg.sync({ cwd: filepath })
const opts = Object.assign({
outDir: process.cwd(),
filepath,
width: 512,
height: 512,
}, cli.flags)
const Component = require(filepath).default || require(filepath)
opts.css = absolute(opts.css)
opts.outDir = absolute(opts.outDir)
if (opts.css) {
opts.css = fs.readFileSync(opts.css, 'utf8')
}
if (opts.props) {
opts.props = JSON.parse(opts.props)
}
if (pkg && pkg.dependencies) {
if (pkg.dependencies['styled-components']) {
opts.cssLibrary = 'styled-components'
} else if (pkg.dependencies['emotion']) {
opts.cssLibrary = 'emotion'
}
}
const run = async () => {
try {
const image = await render(Component, opts)
const { date, time } = getDateTime()
const outFile = opts.filename
? opts.filename
: `${name}-${date}-${time}-${opts.width}x${opts.height}.png`
const outPath = path.join(opts.outDir, outFile)
const file = fs.createWriteStream(outPath)
spinner.info('Creating stream')
file.on('finish', () => {
spinner.succeed(`File saved to ${outPath}`)
process.exit()
})
image.pipe(file)
image.on('readable', () => {
spinner.info(`Saving file`)
})
image.on('error', err => {
spinner.fail('Error: ' + err)
})
} catch (err) {
spinner.fail(`Error: ${err}`)
process.exit(1)
}
}
run()