This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
cli.js
executable file
·142 lines (124 loc) · 3.03 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
#!/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 (or path to JSON file) to pass to the React component
-t --type Type of ouptut (png default) (pdf, jpeg or png)
--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'
},
props: {
type: 'string',
alias: 'p'
},
css: {
type: 'string'
},
type: {
type: 'string',
alias: 't'
},
puppeteer: {
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,
}, 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) {
const stat = fs.statSync(opts.props)
if (stat.isFile()) {
const req = path.join(process.cwd(), opts.props)
try {
opts.props = require(req)
} catch (e) {
console.log(e)
opts.props = {}
}
} else {
opts.props = JSON.parse(opts.props)
}
}
if (opts.puppeteer) opts.puppeteer = JSON.parse(opts.puppeteer)
const run = async () => {
try {
const image = await render(Component, opts)
const { date, time } = getDateTime()
const outFile = opts.filename
? opts.filename
: `${name}-${date}-${time}.png`
const outPath = path.join(opts.outDir, outFile)
const file = fs.createWriteStream(outPath)
file.on('finish', () => {
spinner.succeed(`File saved to ${outPath}`)
process.exit()
})
file.on('error', err => {
spinner.fail('Error: ' + err)
})
spinner.info('Saving file')
file.write(image)
file.end()
} catch (err) {
spinner.fail(`Error: ${err}`)
process.exit(1)
}
}
run()