thedesk/app/view/make/make.js

153 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2020-04-08 18:58:37 +10:00
const fs = require('fs')
2020-04-08 19:12:58 +10:00
function construct(ver, basefile, pwa, store) {
2019-11-16 02:01:54 +11:00
const execSync = require('child_process').execSync
let gitHash = execSync('git rev-parse HEAD')
2019-10-20 21:36:44 +11:00
.toString()
2019-11-16 02:01:54 +11:00
.trim()
2020-04-08 18:58:37 +10:00
console.log('Constructing view files ' + ver)
const langs = ['ja', 'ja-KS', 'en', 'bg', 'cs', 'de',
2021-04-09 17:35:40 +10:00
'es-AR', 'it-IT', 'zh-CN', 'fr-FR', 'zh-TW', 'no-NO', 'pt-BR', 'ru-RU', 'es-ES','pl-PL', 'ps']
2019-11-16 02:01:54 +11:00
const langsh = [
'日本語',
'日本語(関西)',
'English',
'български',
'Česky',
'Deutsch',
'Español, argentina',
2020-09-26 22:18:28 +10:00
'italiano',
'简体中文',
2021-04-02 12:56:01 +11:00
'français',
'繁體中文(β)',
'norsk(β)',
'Português, brasileiro(β)',
'русский(β)',
'Español(β)',
2021-04-02 12:56:01 +11:00
'Polskie(β)',
2019-11-16 02:01:54 +11:00
'Crowdin translate system(beta)'
]
const simples = ['acct', 'index', 'setting', 'update', 'setting']
const samples = [
'acct.sample.html',
'index.sample.html',
'setting.sample.html',
'update.sample.html',
'setting.sample.js'
]
const pages = ['acct.html', 'index.html', 'setting.html', 'update.html', 'setting.vue.js']
let langstr = ''
let refKey = []
const enJson = JSON.parse(fs.readFileSync(basefile + 'view/make/language/en/main.json', 'utf8'))
const jaJson = JSON.parse(fs.readFileSync(basefile + 'view/make/language/ja/main.json', 'utf8'))
2019-10-20 21:36:44 +11:00
for (let n = 0; n < langs.length; n++) {
2019-11-16 02:01:54 +11:00
let lang = langs[n]
let targetDir = basefile + 'view/' + lang
2019-10-20 21:36:44 +11:00
if (!fs.existsSync(targetDir)) {
2019-11-16 02:01:54 +11:00
fs.mkdirSync(targetDir)
2019-10-20 21:36:44 +11:00
}
2019-11-16 02:01:54 +11:00
langstr =
langstr + `<option value="${lang}">${langsh[n]}</option>`
2019-11-16 02:01:54 +11:00
let mainJson = JSON.parse(
fs.readFileSync(basefile + 'view/make/language/' + lang + '/main.json', 'utf8')
)
if (lang == 'ja-KS') {
Object.keys(jaJson).forEach(function (key) {
2019-11-16 02:01:54 +11:00
if (!mainJson[key]) {
mainJson[key] = jaJson[key]
}
})
} else if (lang != 'en') {
Object.keys(enJson).forEach(function (key) {
2019-11-16 02:01:54 +11:00
if (!mainJson[key]) {
mainJson[key] = enJson[key]
2019-10-20 22:04:00 +11:00
}
2019-11-16 02:01:54 +11:00
})
2019-10-20 22:04:00 +11:00
}
mainJson.language = lang
2019-11-16 02:01:54 +11:00
fs.writeFileSync(
basefile + 'view/' + lang + '/main.js',
JSON.stringify(mainJson).replace(/^{/, 'var lang = {')
)
2019-10-20 21:36:44 +11:00
}
for (let i = 0; i < samples.length; i++) {
2019-11-16 02:01:54 +11:00
let sample = samples[i]
let sourceParent = fs.readFileSync(basefile + 'view/make/' + sample, 'utf8')
let englishRefer = JSON.parse(
fs.readFileSync(basefile + 'view/make/language/en/' + simples[i] + '.json', 'utf8')
)
let jaRefer = JSON.parse(
fs.readFileSync(basefile + 'view/make/language/ja/' + simples[i] + '.json', 'utf8')
)
2019-10-20 21:36:44 +11:00
for (let j = 0; j < langs.length; j++) {
2019-11-16 02:01:54 +11:00
let source = sourceParent
let lang = langs[j]
let target = JSON.parse(
fs.readFileSync(
basefile + 'view/make/language/' + lang + '/' + simples[i] + '.json',
'utf8'
)
)
if (lang == 'ja') {
Object.keys(target).forEach(function (key) {
2019-11-16 02:01:54 +11:00
refKey.push(key)
let str = target[key]
if (pages[i] == 'setting.vue.js') {
2021-04-01 19:20:56 +11:00
str = str.replace(/'/g, "\\'")
2019-10-20 21:36:44 +11:00
}
2019-11-16 02:01:54 +11:00
var regExp = new RegExp('@@' + key + '@@', 'g')
source = source.replace(regExp, str)
})
2019-10-20 21:36:44 +11:00
} else {
for (let k = 0; k < refKey.length; k++) {
2019-11-16 02:01:54 +11:00
let tarKey = refKey[k]
2019-10-20 21:36:44 +11:00
if (target[tarKey]) {
2019-11-16 02:01:54 +11:00
var str = target[tarKey]
2019-10-20 21:36:44 +11:00
} else {
2019-11-16 02:01:54 +11:00
if (lang == 'ja-KS') {
var str = jaRefer[tarKey]
} else {
var str = englishRefer[tarKey]
}
2019-10-20 21:36:44 +11:00
}
2019-11-16 02:01:54 +11:00
if (pages[i] == 'setting.vue.js') {
2019-10-20 21:36:44 +11:00
if (str) {
2021-04-01 19:37:39 +11:00
str = str.replace(/'/g, "\\'")
2019-10-20 21:36:44 +11:00
}
}
2019-11-16 02:01:54 +11:00
var regExp = new RegExp('@@' + tarKey + '@@', 'g')
source = source.replace(regExp, str)
2019-10-20 21:36:44 +11:00
}
}
2019-11-16 02:01:54 +11:00
if (lang == 'ps') {
source = source.replace(/@@comment-start@@/g, '')
source = source.replace(/@@comment-end@@/g, '')
2019-10-20 21:36:44 +11:00
} else {
2019-11-16 02:01:54 +11:00
source = source.replace(/@@comment-start@@/g, '<!--')
source = source.replace(/@@comment-end@@/g, '-->')
2019-10-20 21:36:44 +11:00
}
2019-11-16 02:01:54 +11:00
source = source.replace(/@@versionLetter@@/g, ver)
source = source.replace(/@@gitHash@@/g, gitHash)
source = source.replace(/@@gitHashShort@@/g, gitHash.slice(0, 7))
source = source.replace(/@@lang@@/g, lang)
source = source.replace(/@@langlist@@/g, langstr)
if (pwa) {
2020-09-09 17:59:18 +10:00
source = source.replace(/@@pwa@@/g, `<link rel="manifest" href="../../manifest.json" />
2020-02-08 03:34:07 +11:00
<script>var pwa = true;"serviceWorker"in navigator&&navigator.serviceWorker.register("/sw.pwa.js").then(e=>{});</script>`)
2020-02-08 02:25:31 +11:00
source = source.replace(/@@node_base@@/g, 'dependencies')
2020-05-06 19:02:14 +10:00
source = source.replace(/@@pwaClass@@/g, 'pwaView')
2020-02-08 02:01:11 +11:00
} else {
2020-02-08 02:31:06 +11:00
source = source.replace(/@@pwa@@/g, '<script>var pwa = false;</script>')
2020-02-08 02:25:31 +11:00
source = source.replace(/@@node_base@@/g, 'node_modules')
2020-05-06 19:02:14 +10:00
source = source.replace(/@@pwaClass@@/g, '')
2020-02-08 02:01:11 +11:00
}
2020-09-09 17:59:18 +10:00
if (store) {
source = source.replace(/@@store@@/g, '<script>var store = true;</script>')
} else {
source = source.replace(/@@store@@/g, '<script>var store = false;</script>')
}
2019-11-16 02:01:54 +11:00
fs.writeFileSync(basefile + 'view/' + lang + '/' + pages[i], source)
2019-10-20 21:36:44 +11:00
}
}
}
module.exports = construct