Don't recreate translation meta in watch mode (#9909)

This commit is contained in:
Bram Kragten 2021-08-30 22:13:47 +02:00 committed by GitHub
parent 43a585187c
commit 2bddd151eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 125 deletions

View File

@ -5,32 +5,32 @@ require("./translations");
gulp.task( gulp.task(
"clean", "clean",
gulp.parallel("clean-translations", function cleanOutputAndBuildDir() { gulp.parallel("clean-translations", () =>
return del([paths.app_output_root, paths.build_dir]); del([paths.app_output_root, paths.build_dir])
}) )
); );
gulp.task( gulp.task(
"clean-demo", "clean-demo",
gulp.parallel("clean-translations", function cleanOutputAndBuildDir() { gulp.parallel("clean-translations", () =>
return del([paths.demo_output_root, paths.build_dir]); del([paths.demo_output_root, paths.build_dir])
}) )
); );
gulp.task( gulp.task(
"clean-cast", "clean-cast",
gulp.parallel("clean-translations", function cleanOutputAndBuildDir() { gulp.parallel("clean-translations", () =>
return del([paths.cast_output_root, paths.build_dir]); del([paths.cast_output_root, paths.build_dir])
}) )
); );
gulp.task("clean-hassio", function cleanOutputAndBuildDir() { gulp.task("clean-hassio", () =>
return del([paths.hassio_output_root, paths.build_dir]); del([paths.hassio_output_root, paths.build_dir])
}); );
gulp.task( gulp.task(
"clean-gallery", "clean-gallery",
gulp.parallel("clean-translations", function cleanOutputAndBuildDir() { gulp.parallel("clean-translations", () =>
return del([paths.gallery_output_root, paths.build_dir]); del([paths.gallery_output_root, paths.build_dir])
}) )
); );

View File

@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const crypto = require("crypto"); const crypto = require("crypto");
const del = require("del"); const del = require("del");
const path = require("path"); const path = require("path");
@ -26,13 +28,6 @@ gulp.task("translations-enable-merge-backend", (done) => {
done(); done();
}); });
String.prototype.rsplit = function (sep, maxsplit) {
var split = this.split(sep);
return maxsplit
? [split.slice(0, -maxsplit).join(sep)].concat(split.slice(-maxsplit))
: split;
};
// Panel translations which should be split from the core translations. // Panel translations which should be split from the core translations.
const TRANSLATION_FRAGMENTS = Object.keys( const TRANSLATION_FRAGMENTS = Object.keys(
require("../../src/translations/en.json").ui.panel require("../../src/translations/en.json").ui.panel
@ -40,7 +35,7 @@ const TRANSLATION_FRAGMENTS = Object.keys(
function recursiveFlatten(prefix, data) { function recursiveFlatten(prefix, data) {
let output = {}; let output = {};
Object.keys(data).forEach(function (key) { Object.keys(data).forEach((key) => {
if (typeof data[key] === "object") { if (typeof data[key] === "object") {
output = { output = {
...output, ...output,
@ -101,15 +96,19 @@ function lokaliseTransform(data, original, file) {
if (value instanceof Object) { if (value instanceof Object) {
output[key] = lokaliseTransform(value, original, file); output[key] = lokaliseTransform(value, original, file);
} else { } else {
output[key] = value.replace(re_key_reference, (match, key) => { output[key] = value.replace(re_key_reference, (_match, lokalise_key) => {
const replace = key.split("::").reduce((tr, k) => { const replace = lokalise_key.split("::").reduce((tr, k) => {
if (!tr) { if (!tr) {
throw Error(`Invalid key placeholder ${key} in ${file.path}`); throw Error(
`Invalid key placeholder ${lokalise_key} in ${file.path}`
);
} }
return tr[k]; return tr[k];
}, original); }, original);
if (typeof replace !== "string") { if (typeof replace !== "string") {
throw Error(`Invalid key placeholder ${key} in ${file.path}`); throw Error(
`Invalid key placeholder ${lokalise_key} in ${file.path}`
);
} }
return replace; return replace;
}); });
@ -118,9 +117,7 @@ function lokaliseTransform(data, original, file) {
return output; return output;
} }
gulp.task("clean-translations", function () { gulp.task("clean-translations", () => del([workDir]));
return del([workDir]);
});
gulp.task("ensure-translations-build-dir", (done) => { gulp.task("ensure-translations-build-dir", (done) => {
if (!fs.existsSync(workDir)) { if (!fs.existsSync(workDir)) {
@ -129,7 +126,7 @@ gulp.task("ensure-translations-build-dir", (done) => {
done(); done();
}); });
gulp.task("create-test-metadata", function (cb) { gulp.task("create-test-metadata", (cb) => {
fs.writeFile( fs.writeFile(
workDir + "/testMetadata.json", workDir + "/testMetadata.json",
JSON.stringify({ JSON.stringify({
@ -143,17 +140,13 @@ gulp.task("create-test-metadata", function (cb) {
gulp.task( gulp.task(
"create-test-translation", "create-test-translation",
gulp.series("create-test-metadata", function createTestTranslation() { gulp.series("create-test-metadata", () =>
return gulp gulp
.src(path.join(paths.translations_src, "en.json")) .src(path.join(paths.translations_src, "en.json"))
.pipe( .pipe(transform((data, _file) => recursiveEmpty(data)))
transform(function (data, file) {
return recursiveEmpty(data);
})
)
.pipe(rename("test.json")) .pipe(rename("test.json"))
.pipe(gulp.dest(workDir)); .pipe(gulp.dest(workDir))
}) )
); );
/** /**
@ -165,7 +158,7 @@ gulp.task(
* project is buildable immediately after merging new translation keys, since * project is buildable immediately after merging new translation keys, since
* the Lokalise update to translations/en.json will not happen immediately. * the Lokalise update to translations/en.json will not happen immediately.
*/ */
gulp.task("build-master-translation", function () { gulp.task("build-master-translation", () => {
const src = [path.join(paths.translations_src, "en.json")]; const src = [path.join(paths.translations_src, "en.json")];
if (mergeBackend) { if (mergeBackend) {
@ -174,11 +167,7 @@ gulp.task("build-master-translation", function () {
return gulp return gulp
.src(src) .src(src)
.pipe( .pipe(transform((data, file) => lokaliseTransform(data, data, file)))
transform(function (data, file) {
return lokaliseTransform(data, data, file);
})
)
.pipe( .pipe(
merge({ merge({
fileName: "translationMaster.json", fileName: "translationMaster.json",
@ -187,18 +176,14 @@ gulp.task("build-master-translation", function () {
.pipe(gulp.dest(workDir)); .pipe(gulp.dest(workDir));
}); });
gulp.task("build-merged-translations", function () { gulp.task("build-merged-translations", () =>
return gulp gulp
.src([inFrontendDir + "/*.json", workDir + "/test.json"], { .src([inFrontendDir + "/*.json", workDir + "/test.json"], {
allowEmpty: true, allowEmpty: true,
}) })
.pipe(transform((data, file) => lokaliseTransform(data, data, file)))
.pipe( .pipe(
transform(function (data, file) { foreach((stream, file) => {
return lokaliseTransform(data, data, file);
})
)
.pipe(
foreach(function (stream, file) {
// For each language generate a merged json file. It begins with the master // For each language generate a merged json file. It begins with the master
// translation as a failsafe for untranslated strings, and merges all parent // translation as a failsafe for untranslated strings, and merges all parent
// tags into one file for each specific subtag // tags into one file for each specific subtag
@ -230,17 +215,17 @@ gulp.task("build-merged-translations", function () {
) )
.pipe(gulp.dest(fullDir)); .pipe(gulp.dest(fullDir));
}) })
); )
}); );
var taskName; let taskName;
const splitTasks = []; const splitTasks = [];
TRANSLATION_FRAGMENTS.forEach((fragment) => { TRANSLATION_FRAGMENTS.forEach((fragment) => {
taskName = "build-translation-fragment-" + fragment; taskName = "build-translation-fragment-" + fragment;
gulp.task(taskName, function () { gulp.task(taskName, () =>
// Return only the translations for this fragment. // Return only the translations for this fragment.
return gulp gulp
.src(fullDir + "/*.json") .src(fullDir + "/*.json")
.pipe( .pipe(
transform((data) => ({ transform((data) => ({
@ -251,18 +236,18 @@ TRANSLATION_FRAGMENTS.forEach((fragment) => {
}, },
})) }))
) )
.pipe(gulp.dest(workDir + "/" + fragment)); .pipe(gulp.dest(workDir + "/" + fragment))
}); );
splitTasks.push(taskName); splitTasks.push(taskName);
}); });
taskName = "build-translation-core"; taskName = "build-translation-core";
gulp.task(taskName, function () { gulp.task(taskName, () =>
// Remove the fragment translations from the core translation. // Remove the fragment translations from the core translation.
return gulp gulp
.src(fullDir + "/*.json") .src(fullDir + "/*.json")
.pipe( .pipe(
transform((data, file) => { transform((data, _file) => {
TRANSLATION_FRAGMENTS.forEach((fragment) => { TRANSLATION_FRAGMENTS.forEach((fragment) => {
delete data.ui.panel[fragment]; delete data.ui.panel[fragment];
}); });
@ -270,14 +255,14 @@ gulp.task(taskName, function () {
return data; return data;
}) })
) )
.pipe(gulp.dest(coreDir)); .pipe(gulp.dest(coreDir))
}); );
splitTasks.push(taskName); splitTasks.push(taskName);
gulp.task("build-flattened-translations", function () { gulp.task("build-flattened-translations", () =>
// Flatten the split versions of our translations, and move them into outDir // Flatten the split versions of our translations, and move them into outDir
return gulp gulp
.src( .src(
TRANSLATION_FRAGMENTS.map( TRANSLATION_FRAGMENTS.map(
(fragment) => workDir + "/" + fragment + "/*.json" (fragment) => workDir + "/" + fragment + "/*.json"
@ -285,26 +270,28 @@ gulp.task("build-flattened-translations", function () {
{ base: workDir } { base: workDir }
) )
.pipe( .pipe(
transform(function (data) { transform((data) =>
// Polymer.AppLocalizeBehavior requires flattened json // Polymer.AppLocalizeBehavior requires flattened json
return flatten(data); flatten(data)
}) )
) )
.pipe( .pipe(
rename((filePath) => { rename((filePath) => {
if (filePath.dirname === "core") { if (filePath.dirname === "core") {
filePath.dirname = ""; filePath.dirname = "";
} }
// In dev we create the file with the fake hash in the filename
if (!env.isProdBuild()) {
filePath.basename += "-dev";
}
}) })
) )
.pipe(gulp.dest(outDir)); .pipe(gulp.dest(outDir))
}); );
const fingerprints = {}; const fingerprints = {};
gulp.task( gulp.task("build-translation-fingerprints", () => {
"build-translation-fingerprints",
function fingerprintTranslationFiles() {
// Fingerprint full file of each language // Fingerprint full file of each language
const files = fs.readdirSync(fullDir); const files = fs.readdirSync(fullDir);
@ -320,6 +307,8 @@ gulp.task(
}; };
} }
// In dev we create the file with the fake hash in the filename
if (env.isProdBuild()) {
mapFiles(outDir, ".json", (filename) => { mapFiles(outDir, ".json", (filename) => {
const parsed = path.parse(filename); const parsed = path.parse(filename);
@ -335,35 +324,35 @@ gulp.task(
}` }`
); );
}); });
}
const stream = source("translationFingerprints.json"); const stream = source("translationFingerprints.json");
stream.write(JSON.stringify(fingerprints)); stream.write(JSON.stringify(fingerprints));
process.nextTick(() => stream.end()); process.nextTick(() => stream.end());
return stream.pipe(vinylBuffer()).pipe(gulp.dest(workDir)); return stream.pipe(vinylBuffer()).pipe(gulp.dest(workDir));
} });
);
gulp.task("build-translation-fragment-supervisor", function () { gulp.task("build-translation-fragment-supervisor", () =>
return gulp gulp
.src(fullDir + "/*.json") .src(fullDir + "/*.json")
.pipe(transform((data) => data.supervisor)) .pipe(transform((data) => data.supervisor))
.pipe(gulp.dest(workDir + "/supervisor")); .pipe(gulp.dest(workDir + "/supervisor"))
}); );
gulp.task("build-translation-flatten-supervisor", function () { gulp.task("build-translation-flatten-supervisor", () =>
return gulp gulp
.src(workDir + "/supervisor/*.json") .src(workDir + "/supervisor/*.json")
.pipe( .pipe(
transform(function (data) { transform((data) =>
// Polymer.AppLocalizeBehavior requires flattened json // Polymer.AppLocalizeBehavior requires flattened json
return flatten(data); flatten(data)
})
) )
.pipe(gulp.dest(outDir)); )
}); .pipe(gulp.dest(outDir))
);
gulp.task("build-translation-write-metadata", function writeMetadata() { gulp.task("build-translation-write-metadata", () =>
return gulp gulp
.src( .src(
[ [
path.join(paths.translations_src, "translationMetadata.json"), path.join(paths.translations_src, "translationMetadata.json"),
@ -374,13 +363,14 @@ gulp.task("build-translation-write-metadata", function writeMetadata() {
) )
.pipe(merge({})) .pipe(merge({}))
.pipe( .pipe(
transform(function (data) { transform((data) => {
const newData = {}; const newData = {};
Object.entries(data).forEach(([key, value]) => { Object.entries(data).forEach(([key, value]) => {
// Filter out translations without native name. // Filter out translations without native name.
if (value.nativeName) { if (value.nativeName) {
newData[key] = value; newData[key] = value;
} else { } else {
// eslint-disable-next-line no-console
console.warn( console.warn(
`Skipping language ${key}. Native name was not translated.` `Skipping language ${key}. Native name was not translated.`
); );
@ -396,19 +386,26 @@ gulp.task("build-translation-write-metadata", function writeMetadata() {
})) }))
) )
.pipe(rename("translationMetadata.json")) .pipe(rename("translationMetadata.json"))
.pipe(gulp.dest(workDir)); .pipe(gulp.dest(workDir))
}); );
gulp.task(
"create-translations",
gulp.series(
env.isProdBuild() ? (done) => done() : "create-test-translation",
"build-master-translation",
"build-merged-translations",
gulp.parallel(...splitTasks),
"build-flattened-translations"
)
);
gulp.task( gulp.task(
"build-translations", "build-translations",
gulp.series( gulp.series(
"clean-translations", "clean-translations",
"ensure-translations-build-dir", "ensure-translations-build-dir",
env.isProdBuild() ? (done) => done() : "create-test-translation", "create-translations",
"build-master-translation",
"build-merged-translations",
gulp.parallel(...splitTasks),
"build-flattened-translations",
"build-translation-fingerprints", "build-translation-fingerprints",
"build-translation-write-metadata" "build-translation-write-metadata"
) )

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
// Tasks to run webpack. // Tasks to run webpack.
const fs = require("fs"); const fs = require("fs");
const gulp = require("gulp"); const gulp = require("gulp");
@ -44,7 +45,7 @@ const runDevServer = ({
open: true, open: true,
watchContentBase: true, watchContentBase: true,
contentBase, contentBase,
}).listen(port, listenHost, function (err) { }).listen(port, listenHost, (err) => {
if (err) { if (err) {
throw err; throw err;
} }
@ -65,6 +66,7 @@ const doneHandler = (done) => (err, stats) => {
} }
if (stats.hasErrors() || stats.hasWarnings()) { if (stats.hasErrors() || stats.hasWarnings()) {
// eslint-disable-next-line no-console
console.log(stats.toString("minimal")); console.log(stats.toString("minimal"));
} }
@ -90,16 +92,10 @@ gulp.task("webpack-watch-app", () => {
process.env.ES5 process.env.ES5
? bothBuilds(createAppConfig, { isProdBuild: false }) ? bothBuilds(createAppConfig, { isProdBuild: false })
: createAppConfig({ isProdBuild: false, latestBuild: true }) : createAppConfig({ isProdBuild: false, latestBuild: true })
).watch( ).watch({ poll: isWsl }, doneHandler());
{
ignored: /build-translations/,
poll: isWsl,
},
doneHandler()
);
gulp.watch( gulp.watch(
path.join(paths.translations_src, "en.json"), path.join(paths.translations_src, "en.json"),
gulp.series("build-translations", "copy-translations-app") gulp.series("create-translations", "copy-translations-app")
); );
}); });