-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenimg.js
80 lines (72 loc) · 2.23 KB
/
genimg.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
/* eslint-disable */
/**
* @flow
*/
const path = require('path')
const fs = require('fs')
const argv = require('yargs-parser')(process.argv.slice(2))
// $FlowFixMe
String.prototype.format = function () {
let a = this
for (const k in arguments) {
// $FlowFixMe
a = a.replace(`{${k}}`.toRegex('g'), arguments[k])
}
return a
}
// $FlowFixMe
String.prototype.toRegex = function (option = 'i') {
let regexStr = this.replace(/[\.\*\+\?\^\$\{\}\(\)\|\[\]\\]/g, '\\$&')
regexStr = regexStr.replace(/\s/g, '\\s?')
return new RegExp(regexStr, option)
}
const getFileName = file => {
const fileNameMatch = file.match(/^(.+)\.[^\.]+$/)
return (
fileNameMatch &&
fileNameMatch[1].toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase())
)
}
const getFileSubName = file => {
return file
}
const folder = argv.folder || argv.d || argv._[0]
const match = folder.match(/^(.+\/([^\/]+))\/?$/)
// $FlowFixMe
let output = match && '{0}/{1}.tsx'.format(match[1], match[2])
output = argv.output || argv.o || output
const outputMatch = output.match(/^(?:(.*)\/)?([^\/]+)$/)
const outputPath = outputMatch[1] || '.'
const author = argv.author || argv.a || 'Robot'
const template = `/**
* @flow
*/
const Images = {
{0}}
export {Images}\n`
const processFolder = (folderPath, prefix = '') => {
const files = fs.readdirSync(folderPath)
const strCodes = []
let subfolderCodes = ''
files.forEach(file => {
const filePath = path.join(folderPath, file)
if (fs.statSync(filePath).isDirectory()) {
const subfolderPath = path.join(folderPath, file)
const subfolderImageKeys = processFolder(subfolderPath, `${prefix}/${file}`)
if (subfolderImageKeys.length > 0) {
const folderName = getFileSubName(file)
subfolderCodes += ` ${folderName}: {\n${subfolderImageKeys.join('\n')}\n},\n`
}
} else {
const fileName = getFileName(file)
if (fileName) {
const fileRequirePath = path.relative(outputPath, filePath)
strCodes.push(` ${fileName}: require('./${fileRequirePath}'),`)
}
}
})
return strCodes.concat(subfolderCodes)
}
const imageKeys = processFolder(folder)
const code = imageKeys.join('\n')
fs.writeFileSync(output, template.format(code, author))