thedesk/app/view/make/make.js

165 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-11-28 01:13:19 +11:00
let ver = '20.0.5 (Kawaii)'
2019-11-16 02:01:54 +11:00
if (process.argv.indexOf('--automatic') === -1) {
let input = require('readline-sync').question('version string [empty: ' + ver + ' (default)]? ')
if (input) {
ver = input
}
//var basefile="../../"
2019-10-20 21:36:44 +11:00
} else {
2019-11-16 02:01:54 +11:00
//var basefile="./"
2019-06-05 00:01:04 +10:00
}
2019-10-20 22:04:00 +11:00
//const { app } = require('electron');
2019-11-16 02:01:54 +11:00
const path = require('path')
const basefile = path.join(__dirname, '../../')
2019-10-20 21:36:44 +11:00
function main(ver, basefile) {
2019-11-16 02:01:54 +11:00
const fs = require('fs')
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()
fs.writeFileSync(basefile + 'git', gitHash)
console.log('Constructing view files ' + ver + ': make sure to update package.json')
const langs = ['ja', 'ja-KS', 'en', 'bg', 'cs', 'de', 'es-AR', 'ps']
const langsh = [
'日本語',
'日本語(関西)',
'English',
'български',
'Česky',
'Deutsch',
'Spanish, Argentina',
'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 +
'<a onclick="changelang(\'' +
lang +
'\')" class="pointer" style="margin:4px;border: 1px solid var(--color); padding: 3px">' +
langsh[n] +
'</a>'
let mainJson = JSON.parse(
fs.readFileSync(basefile + 'view/make/language/' + lang + '/main.json', 'utf8')
)
if (lang == 'ja-KS') {
Object.keys(jaJson).forEach(function(key) {
if (!mainJson[key]) {
mainJson[key] = jaJson[key]
}
})
} else if (lang != 'en') {
2019-10-20 22:04:00 +11:00
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
}
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') {
2019-10-20 21:36:44 +11:00
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') {
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) {
2019-11-16 02:01:54 +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)
fs.writeFileSync(basefile + 'view/' + lang + '/' + pages[i], source)
2019-10-20 21:36:44 +11:00
}
}
}
2019-11-16 02:01:54 +11:00
main(ver, basefile)
2019-10-20 21:36:44 +11:00
//if --watch, to yarn dev
2019-11-16 02:01:54 +11:00
if (process.argv.indexOf('--watch') !== -1) {
const chokidar = require('chokidar')
console.log(
'watch mode(not hot-watch): when construction files are changed, refresh view files but not reload. Please reload manually.'
)
const watcher = chokidar.watch(basefile + 'view/make', {
ignored: 'view/make/make.js',
2019-10-20 21:36:44 +11:00
persistent: true
2019-11-16 02:01:54 +11:00
})
watcher.on('ready', function() {
console.log('watching...')
watcher.on('change', function(path) {
console.log(path + ' changed.')
main(ver, basefile)
})
})
2019-05-07 01:42:06 +10:00
}