2019-01-27 12:47:43 +08:00
|
|
|
import * as gulp from 'gulp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as zip from 'gulp-zip';
|
|
|
|
import * as ts from 'gulp-typescript';
|
|
|
|
// @ts-ignore
|
2019-12-22 01:14:28 +08:00
|
|
|
import * as jeditor from 'gulp-json-editor';
|
2019-01-27 12:47:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
|
|
|
|
const translationFolder = 'translate';
|
|
|
|
const tsBackendProject = ts.createProject('tsconfig.json');
|
2019-12-13 02:11:49 +08:00
|
|
|
declare var process: NodeJS.Process;
|
2019-01-27 12:47:43 +08:00
|
|
|
|
2019-12-22 07:04:52 +08:00
|
|
|
const getSwitch = (name: string, def: string = null): string => {
|
2019-12-22 01:14:28 +08:00
|
|
|
name = '--' + name;
|
|
|
|
for (let i = 0; i < process.argv.length; i++) {
|
|
|
|
if (process.argv[i].startsWith(name + '=')) {
|
|
|
|
return process.argv[i].replace(name + '=', '').trim();
|
|
|
|
}
|
2019-12-22 07:04:52 +08:00
|
|
|
if (process.argv[i].startsWith(name)) {
|
|
|
|
if (i + 1 < process.argv.length) {
|
|
|
|
return process.argv[i + 1].trim();
|
|
|
|
}
|
|
|
|
return 'true';
|
2019-12-22 01:14:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
};
|
|
|
|
|
2019-01-27 12:47:43 +08:00
|
|
|
gulp.task('build-backend', function () {
|
|
|
|
return gulp.src([
|
2019-12-10 17:44:35 +08:00
|
|
|
'src/common/**/*.ts',
|
|
|
|
'src/backend/**/*.ts'], {base: '.'})
|
2019-01-27 12:47:43 +08:00
|
|
|
.pipe(tsBackendProject())
|
|
|
|
.js
|
|
|
|
.pipe(gulp.dest('./release'));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleError = (cb: (err: any) => void) => {
|
|
|
|
return (err: any, stdout: string, stderr: string) => {
|
|
|
|
console.log(stdout);
|
|
|
|
console.log(stderr);
|
|
|
|
cb(err);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createFrontendTask = (type: string, script: string) => {
|
|
|
|
gulp.task(type, (cb) => {
|
|
|
|
exec(script, handleError(cb));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-22 01:14:28 +08:00
|
|
|
|
2019-01-27 12:47:43 +08:00
|
|
|
const getLanguages = () => {
|
2019-12-10 17:44:35 +08:00
|
|
|
if (!fs.existsSync('./src/frontend/' + translationFolder)) {
|
2019-01-27 12:47:43 +08:00
|
|
|
return [];
|
|
|
|
}
|
2019-12-10 17:44:35 +08:00
|
|
|
const dirCont = fs.readdirSync('./src/frontend/' + translationFolder);
|
2019-01-27 12:47:43 +08:00
|
|
|
const files: string[] = dirCont.filter((elm) => {
|
|
|
|
return elm.match(/.*\.[a-zA-Z]+\.(xlf)/ig);
|
|
|
|
});
|
2019-12-08 07:33:50 +08:00
|
|
|
|
|
|
|
// get languages to filter
|
|
|
|
let languageFilter: string[] = null;
|
2019-12-22 01:14:28 +08:00
|
|
|
if (getSwitch('languages')) {
|
|
|
|
languageFilter = getSwitch('languages').split(',');
|
2019-12-08 07:33:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let languages = files.map((f: string) => {
|
2019-01-27 12:47:43 +08:00
|
|
|
return f.split('.')[1];
|
|
|
|
});
|
2019-12-08 07:33:50 +08:00
|
|
|
|
|
|
|
if (languageFilter !== null) {
|
|
|
|
languages = languages.filter((l) => {
|
|
|
|
return languageFilter.indexOf(l) !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return languages;
|
2019-01-27 12:47:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
gulp.task('build-frontend', (() => {
|
|
|
|
const languages = getLanguages().filter((l) => {
|
|
|
|
return l !== 'en';
|
|
|
|
});
|
|
|
|
const tasks = [];
|
|
|
|
createFrontendTask('build-frontend-release default',
|
|
|
|
'ng build --aot --prod --output-path=./release/dist --no-progress --i18n-locale=en' +
|
2019-12-10 17:44:35 +08:00
|
|
|
' --i18n-format xlf --i18n-file src/frontend/' + translationFolder + '/messages.en.xlf' +
|
2019-01-27 12:47:43 +08:00
|
|
|
' --i18n-missing-translation warning');
|
|
|
|
tasks.push('build-frontend-release default');
|
|
|
|
for (let i = 0; i < languages.length; i++) {
|
|
|
|
createFrontendTask('build-frontend-release ' + languages[i],
|
|
|
|
'ng build --aot --prod --output-path=./release/dist/' + languages[i] +
|
|
|
|
' --no-progress --i18n-locale=' + languages[i] +
|
2019-12-10 17:44:35 +08:00
|
|
|
' --i18n-format xlf --i18n-file src/frontend/' + translationFolder + '/messages.' + languages[i] + '.xlf' +
|
2019-01-27 12:47:43 +08:00
|
|
|
' --i18n-missing-translation warning');
|
|
|
|
tasks.push('build-frontend-release ' + languages[i]);
|
|
|
|
}
|
|
|
|
return gulp.series(...tasks);
|
|
|
|
})());
|
|
|
|
|
|
|
|
gulp.task('copy-static', function () {
|
|
|
|
return gulp.src([
|
2019-12-10 17:44:35 +08:00
|
|
|
'src/backend/model/diagnostics/blank.jpg',
|
2019-01-27 12:47:43 +08:00
|
|
|
'README.md',
|
2019-12-22 01:14:28 +08:00
|
|
|
'package-lock.json',
|
2019-01-27 12:47:43 +08:00
|
|
|
'LICENSE'], {base: '.'})
|
|
|
|
.pipe(gulp.dest('./release'));
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('copy-package', function () {
|
|
|
|
return gulp.src([
|
|
|
|
'package.json'], {base: '.'})
|
2019-12-22 01:14:28 +08:00
|
|
|
.pipe(jeditor((json: {
|
|
|
|
devDependencies: { [key: string]: string },
|
|
|
|
scripts: { [key: string]: string },
|
|
|
|
dependencies: { [key: string]: string },
|
|
|
|
optionalDependencies: { [key: string]: string }
|
|
|
|
}) => {
|
|
|
|
delete json.devDependencies;
|
|
|
|
json.scripts = {start: 'node ./src/backend/index.js'};
|
|
|
|
|
|
|
|
if (getSwitch('skip-opt-packages')) {
|
|
|
|
const skipPackages = getSwitch('skip-opt-packages').split(',');
|
|
|
|
for (const pkg of skipPackages) {
|
|
|
|
for (const key of Object.keys(json.optionalDependencies)) {
|
|
|
|
if (key.indexOf(pkg) !== -1) {
|
|
|
|
delete json.optionalDependencies[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 07:04:52 +08:00
|
|
|
|
|
|
|
if (!!getSwitch('force-opt-packages')) {
|
|
|
|
for (const key of Object.keys(json.optionalDependencies)) {
|
|
|
|
json.dependencies[key] = json.optionalDependencies[key];
|
|
|
|
}
|
|
|
|
delete json.optionalDependencies;
|
|
|
|
}
|
2019-12-22 01:14:28 +08:00
|
|
|
return json;
|
2019-01-27 12:47:43 +08:00
|
|
|
}))
|
|
|
|
.pipe(gulp.dest('./release'));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
gulp.task('zip-release', function () {
|
|
|
|
return gulp.src(['release/**/*'], {base: './release'})
|
|
|
|
.pipe(zip('pigallery2.zip'))
|
|
|
|
.pipe(gulp.dest('.'));
|
|
|
|
});
|
|
|
|
|
2019-12-08 07:33:50 +08:00
|
|
|
gulp.task('create-release', gulp.series('build-frontend', 'build-backend', 'copy-static', 'copy-package', 'zip-release'));
|
2019-01-27 12:47:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
const simpleBuild = (isProd: boolean) => {
|
2019-12-08 07:33:50 +08:00
|
|
|
const languages = getLanguages().filter((l) => {
|
2019-01-27 12:47:43 +08:00
|
|
|
return l !== 'en';
|
|
|
|
});
|
|
|
|
const tasks = [];
|
|
|
|
let cmd = 'ng build --aot ';
|
|
|
|
if (isProd) {
|
|
|
|
cmd += ' --prod --no-extract-licenses ';
|
|
|
|
}
|
|
|
|
createFrontendTask('build-frontend default', cmd + '--output-path=./dist --no-progress --no-progress --i18n-locale en' +
|
2019-12-10 17:44:35 +08:00
|
|
|
' --i18n-format=xlf --i18n-file=src/frontend/' + translationFolder + '/messages.en.xlf' + ' --i18n-missing-translation warning');
|
2019-01-27 12:47:43 +08:00
|
|
|
tasks.push('build-frontend default');
|
2019-01-28 03:53:46 +08:00
|
|
|
if (!process.env.CI) { // don't build languages if running in CI
|
|
|
|
for (let i = 0; i < languages.length; i++) {
|
|
|
|
createFrontendTask('build-frontend ' + languages[i], cmd +
|
|
|
|
'--output-path=./dist/' + languages[i] +
|
|
|
|
' --no-progress --i18n-locale ' + languages[i] +
|
2019-12-10 17:44:35 +08:00
|
|
|
' --i18n-format=xlf --i18n-file=src/frontend/' + translationFolder +
|
2019-01-28 03:53:46 +08:00
|
|
|
'/messages.' + languages[i] + '.xlf' + ' --i18n-missing-translation warning');
|
|
|
|
tasks.push('build-frontend ' + languages[i]);
|
|
|
|
}
|
2019-01-27 12:47:43 +08:00
|
|
|
}
|
|
|
|
return gulp.series(...tasks);
|
|
|
|
};
|
|
|
|
|
|
|
|
gulp.task('extract-locale', (cb) => {
|
|
|
|
console.log('creating source translation file: locale.source.xlf');
|
2019-12-10 18:11:28 +08:00
|
|
|
exec('ng xi18n --out-file=./../../locale.source.xlf --i18n-format=xlf --i18n-locale=en',
|
2019-01-27 12:47:43 +08:00
|
|
|
{maxBuffer: 1024 * 1024}, (error: any, stdOut: string, stdErr: string) => {
|
|
|
|
console.log(stdOut);
|
|
|
|
console.log(stdErr);
|
|
|
|
if (error) {
|
|
|
|
return cb(error);
|
|
|
|
}
|
2019-12-10 17:44:35 +08:00
|
|
|
exec('ngx-extractor -i src/frontend/**/*.ts -f xlf --out-file locale.source.xlf',
|
2019-01-27 12:47:43 +08:00
|
|
|
handleError(cb));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const translate = (list: any[], cb: (err: any) => void) => {
|
|
|
|
const localsStr = '"[\\"' + list.join('\\",\\"') + '\\"]"';
|
2019-12-10 17:44:35 +08:00
|
|
|
exec('xlf-google-translate --source-lang="en" --source-file="./locale.source.xlf" --destination-folder="./src/frontend/"' +
|
2019-01-27 12:47:43 +08:00
|
|
|
translationFolder + ' --destination-languages=' + localsStr,
|
|
|
|
handleError(cb));
|
|
|
|
};
|
2019-07-21 21:14:12 +08:00
|
|
|
const merge = (list: any[], cb: (err: any) => void) => {
|
|
|
|
const localsStr = '"[\\"' + list.join('\\",\\"') + '\\"]"';
|
2019-12-10 17:44:35 +08:00
|
|
|
exec('xlf-google-translate --method="extend-only" --source-lang="en" --source-file="./locale.source.xlf" --destination-folder="./src/frontend/"' +
|
2019-07-21 21:14:12 +08:00
|
|
|
translationFolder + ' --destination-languages=' + localsStr,
|
|
|
|
handleError(cb));
|
|
|
|
};
|
2019-01-27 12:47:43 +08:00
|
|
|
|
|
|
|
gulp.task('update-translation-only', function (cb) {
|
|
|
|
translate(getLanguages(), cb);
|
|
|
|
});
|
2019-07-21 21:14:12 +08:00
|
|
|
gulp.task('merge-translation-only', function (cb) {
|
|
|
|
merge(getLanguages(), cb);
|
|
|
|
});
|
2019-01-27 12:47:43 +08:00
|
|
|
|
|
|
|
gulp.task('update-translation', gulp.series('extract-locale', 'update-translation-only'));
|
|
|
|
|
2019-07-21 21:14:12 +08:00
|
|
|
gulp.task('merge-new-translation', gulp.series('extract-locale', 'merge-translation-only'));
|
|
|
|
|
2019-01-27 12:47:43 +08:00
|
|
|
|
|
|
|
gulp.task('add-translation-only', (cb) => {
|
|
|
|
const languages = getLanguages();
|
|
|
|
let lng = null;
|
|
|
|
for (let i = 0; i < process.argv.length - 1; i++) {
|
|
|
|
if (process.argv[i] === 'add-translation') {
|
|
|
|
lng = process.argv[i + 1].replace('--', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lng == null) {
|
|
|
|
console.error('Error: set language with \'--\' e.g: npm run add-translation -- --en');
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
if (languages.indexOf(lng) !== -1) {
|
|
|
|
console.error('Error: language already exists, can\'t add. These language(s) already exist(s): ' + languages);
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
translate([lng], cb);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
gulp.task('add-translation', gulp.series('extract-locale', 'add-translation-only'));
|
|
|
|
|
|
|
|
|
|
|
|
gulp.task('build-dev', simpleBuild(false));
|
|
|
|
gulp.task('build-prod', simpleBuild(true));
|