-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi18next-resources-for-ts.js
executable file
·98 lines (83 loc) · 2.98 KB
/
i18next-resources-for-ts.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const getNamespaces = require('./getNamespaces.js')
const {
tocForResources,
mergeResources,
mergeResourcesAsInterface,
json2ts
} = require('../')
const cliArgs = process.argv.slice(2)
const subCommands = [
'toc',
'merge',
'interface'
]
if (cliArgs.length === 0 || subCommands.indexOf(cliArgs[0]) < 0) {
throw new Error(`Please define an appropriate subcommand: ${subCommands.join(', ')}!`)
}
const subCommand = cliArgs[0]
let inputPath = process.cwd()
let outputPath = inputPath
if (cliArgs.length > 0 && cliArgs[1].indexOf('-') !== 0) {
inputPath = cliArgs[1]
outputPath = inputPath
}
let comment
const inputArgIndex = cliArgs.indexOf('-i')
const outputArgIndex = cliArgs.indexOf('-o')
const commentArgIndex = cliArgs.indexOf('-c')
if (inputArgIndex > -1 && cliArgs[inputArgIndex + 1]) inputPath = cliArgs[inputArgIndex + 1]
if (outputArgIndex > -1 && cliArgs[outputArgIndex + 1]) outputPath = cliArgs[outputArgIndex + 1]
if (commentArgIndex > -1 && cliArgs[commentArgIndex + 1]) comment = cliArgs[commentArgIndex + 1]
inputPath = path.resolve(inputPath)
outputPath = path.resolve(outputPath)
const commentSection = comment ? `/**\n * ${comment}\n */\n` : ''
const namespaces = getNamespaces(inputPath)
if (subCommand === 'toc') {
let outputFile = outputPath
if (!outputFile.endsWith('.ts')) {
outputFile = path.join(outputFile, 'resources.ts')
}
let nsToUse = namespaces
const convertToTs = cliArgs.indexOf('-cts') > 0
const del = cliArgs.indexOf('-d') > 0
if (convertToTs) {
nsToUse = namespaces.map((n) => {
n.ts = json2ts(n.resources)
n.tsPath = n.path.replace('.json', '.ts')
return n
})
nsToUse.forEach((n) => {
fs.writeFileSync(n.tsPath, commentSection + n.ts, 'utf-8')
})
}
const toc = tocForResources(nsToUse, outputFile)
fs.writeFileSync(outputFile, commentSection + toc, 'utf-8')
if (convertToTs && del) {
nsToUse.forEach((n) => {
fs.unlinkSync(n.path)
})
}
console.log(`created toc resources file for ${nsToUse.length} ${convertToTs ? 'converted ts ' : ''}namespaces: ${outputFile}`)
}
if (subCommand === 'merge') {
if (comment) console.warn('Comment is ignored for json file output.')
const merged = mergeResources(namespaces)
let outputFile = outputPath
if (!outputFile.endsWith('.json')) {
outputFile = path.join(outputFile, 'resources.json')
}
fs.writeFileSync(outputFile, JSON.stringify(merged, null, 2), 'utf-8')
console.log(`created merged resources file for ${namespaces.length} namespaces: ${outputFile}`)
}
if (subCommand === 'interface') {
let outputFile = outputPath
if (!outputFile.endsWith('.d.ts')) {
outputFile = path.join(outputFile, 'resources.d.ts')
}
const typeDefinitionFile = mergeResourcesAsInterface(namespaces)
fs.writeFileSync(outputFile, commentSection + typeDefinitionFile, 'utf-8')
console.log(`created .d.ts resources file for ${namespaces.length} namespaces: ${outputFile}`)
}