-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·172 lines (159 loc) · 5.57 KB
/
build.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
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const difference = require('ramda').difference
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
const readFilePromise = promisify(fs.readFile)
const readFile = fp =>
readFilePromise(fp)
.then(d => JSON.parse(d.toString()))
.catch(err => null)
const writeFile = promisify(fs.writeFile)
const Logger = require('./classes/BadgeLogger')
const JSBuilder = require('./classes/JSGenerator')
const { ReduceToObjects, CreateSpreadsheet, ReadSpreadsheet, RegexCreator, GetCliOptions } = require('./functions')
const cliOptions = GetCliOptions()
const isDirectory = p => fs.existsSync(p) && fs.lstatSync(p).isDirectory()
function GetAllKeysInDir(dir, rules, ignore = []) {
ignore = ignore.concat(['node_modules', '.git'])
dir = path.resolve(dir)
if (!fs.existsSync(dir)) {
Logger.warning('Directory', dir, 'does not exist, skipping...')
return []
}
if (!isDirectory(dir)) {
Logger.warning('Directory', dir, 'is not a directory, skipping...')
return []
}
let files = fs
.readdirSync(dir)
.filter(f => !ignore.includes(f))
.map(f => path.join(dir, f))
return Array.from(new Set(GetFileKeys(files, rules)))
}
function GetFileKeys(files = [], rules) {
let fileKeys = []
files = files.filter(fs.existsSync)
files.forEach(file => {
if (isDirectory(file)) {
let nestedKeys = GetAllKeysInDir(file, rules)
fileKeys = fileKeys.concat(nestedKeys)
}
if (!(path.extname(file) in rules)) return
let text = fs.readFileSync(file).toString()
if (text) {
rules[path.extname(file)].forEach(test => {
let matches = text.match(test)
fileKeys = matches ? fileKeys.concat(matches) : fileKeys
})
}
})
return fileKeys
}
async function UpdateUserConfig(CONFIG_PATH, content) {
if (!fs.existsSync(CONFIG_PATH)) {
await writeFile(CONFIG_PATH, '').then(() => Logger.info('Successfully created default config file.'))
}
await writeFile(CONFIG_PATH, JSON.stringify(content, null, '\t'))
Logger.info('Successfully updated user config file.')
return content
}
async function GetConfig(CONFIG_PATH) {
CONFIG_PATH = path.resolve(CONFIG_PATH)
return readFile(CONFIG_PATH)
.then(async userConfig => {
const defaultConfig = require('./i18n.default.js')
async function CreateDefaultConfig() {
return UpdateUserConfig(CONFIG_PATH, defaultConfig).catch(Logger.error)
}
if (cliOptions.setup || cliOptions.new) {
if (cliOptions.setup) {
Logger.info('Creating default config file...')
userConfig = await CreateDefaultConfig()
}
if (cliOptions.new) {
if (!userConfig) {
Logger.error('No user config, use flag --setup to create a default one')
Logger.error('Cancelling...')
process.exit(0)
}
Logger.info('Creating new spreadsheet...')
await CreateSpreadsheet('i18n Language Spreadsheet (title can be edited freely)').then(sheet => {
Logger.info('New spreadsheet created with ID ' + sheet.spreadsheetId)
userConfig.path = sheet.spreadsheetId
return UpdateUserConfig(CONFIG_PATH, userConfig)
})
}
process.exit(0)
}
if (!userConfig) {
Logger.error('No user config, use flag --setup to create a default one')
Logger.error('Cancelling...')
process.exit(0)
}
if (!userConfig.path) {
Logger.error(
'No path in config file, either add the ID of an existing spreadsheet, or create a new one by adding the --new flag.',
)
process.exit(0)
}
return Object.assign(defaultConfig, userConfig)
})
.catch(err => {
Logger.error('Something went wrong while trying create your config file.')
throw err
})
}
function SearchFiles({ check, rules, ignore }) {
rules = RegexCreator(rules)
let fileKeys = []
fileKeys = check.reduce((result, dir) => {
Logger.info('Searching in directory:', dir)
result = result.concat(GetAllKeysInDir(dir, rules, ignore))
return Array.from(new Set(result))
}, [])
return fileKeys
}
async function GetTranslations({ path: id } = {}) {
let keys = await ReadSpreadsheet(id)
return ReduceToObjects(keys)
}
async function Build() {
const GoogleSheetsAPI = await require('./auth/GoogleAPI')
if (!GoogleSheetsAPI.auth) return
let options = await GetConfig('i18n.config.json')
Logger.info('Searching files...')
let fileKeys = SearchFiles(options)
Logger.info('Found', fileKeys.length, 'language-key(s) in files.')
Logger.info('Fetching spreadsheet...')
let translations = await GetTranslations(options)
Logger.info('Found', translations.keys.length, 'language-key(s) in the spreadsheet.')
if (translations.keys.length) {
let missingKeys = difference(fileKeys, translations.keys)
if (missingKeys.length) {
Logger.warning('MISSING KEYS')
Logger.warning('Found', missingKeys.length, 'language-key(s) in files that are not in the spreadsheet.')
if (!cliOptions.showMissing) {
Logger.warning('If you wish to see a list of missing keys, add the --missing-keys flag.')
} else Logger.warning('Missing key(s) in spreadsheet:', missingKeys.join())
if (options.strict !== false) {
Logger.warning(
'Plugin will not build with missing language keys, to disable this behaviour add "strict":false to your config file',
)
process.exit(0)
}
} else {
Logger.info('Found no missing keys.')
}
if (!cliOptions.build) Logger.info('Add the --build flag to build language files.')
else {
Logger.info('Building files...')
let files = JSBuilder.PrepareFiles(translations.languages)
JSBuilder.Write(files, options.outputDir || path.join(__dirname, '/dist'))
Logger.info('Finished building files.')
}
} else {
Logger.info('No files to build, no keys in spreadsheet.')
}
}
Build()