Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Port more build scripts to ES6, merge i18n management for client and …
Browse files Browse the repository at this point in the history
…plugins
  • Loading branch information
elisee committed Jan 27, 2016
1 parent 2b71b05 commit e132f4e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 203 deletions.
134 changes: 38 additions & 96 deletions client/gulpfile.js
@@ -1,91 +1,49 @@
var gulp = require("gulp");
var tasks = [];
var fs = require("fs");
"use strict";

// Jade
var jade = require("gulp-jade");
var rename = require("gulp-rename");
var localesFolder = "../public/locales/";
var locales = fs.readdirSync(localesFolder);

function loadLocales(locale) {
var localsByContext = {};
var files = fs.readdirSync(localesFolder + locale);
files.forEach(function(fileName) {
var file = fs.readFileSync(localesFolder + locale + "/" + fileName, { encoding: "utf8" } );
localsByContext[fileName.slice(0, fileName.lastIndexOf("."))] = JSON.parse(file);
});

if (defaultLocals != null) {
function checkRecursively(defaultRoot, root, key, path) {
if (root[key] == undefined) {
console.log("Missing key in " + locale + " translation: " + path)
root[key] = defaultRoot[key];

} else if (typeof defaultRoot[key] === "object") {
var keys = Object.keys(defaultRoot[key]);
for (var i = 0; i < keys.length; i++) {
checkRecursively(defaultRoot[key], root[key], keys[i], path + "." + keys[i]);
}
}
}
var keys = Object.keys(defaultLocals);
for (var i = 0; i < keys.length; i++)
checkRecursively(defaultLocals, localsByContext, keys[i], keys[i]);
}
return localsByContext;
}

var defaultLocals = loadLocales("en");
locales.forEach(function(locale) {
var localsByContext = loadLocales(locale);

gulp.task("jade-" + locale, function() {
var result = gulp.src("./src/**/index.jade")
.pipe(jade({ locals: { t: function(path) {
var parts = path.split(":");
var local = localsByContext[parts[0]];
if (local == null) return path;

var keys = parts[1].split(".");
for (var i = 0; i < keys.length; i++) {
local = local[keys[i]];
if (local == null) return path;
}
return local;
}}
}));
const gulp = require("gulp");
const tasks = [];

if (locale !== "en") result.pipe(rename({ extname: "." + locale + ".html" }))
// Jade
const jade = require("gulp-jade");
const rename = require("gulp-rename");
const fs = require("fs");

const i18n = require("../scripts/i18n.js");
const languageCodes = fs.readdirSync(i18n.rootLocalesPath);

for (const languageCode of languageCodes) {
const locale = i18n.loadLocale(languageCode);
gulp.task(`jade-${languageCode}`, () => {
const result = gulp.src("./src/**/index.jade").pipe(jade({ locals: { t: i18n.makeT(locale) } }));
if (languageCode !== "en") result.pipe(rename({ extname: `.${languageCode}.html` }));
return result.pipe(gulp.dest("../public"));
});
tasks.push("jade-" + locale);
})
tasks.push(`jade-${languageCode}`);
}

gulp.task("jade-none", function() {
return gulp.src("./src/**/index.jade")
.pipe(jade({ locals: { t: function(path) { return path; } } }))
.pipe(rename({ extname: ".none.html" }))
.pipe(gulp.dest("../public"));
});
gulp.task("jade-none", () => gulp.src("./src/**/index.jade")
.pipe(jade({ locals: { t: (path) => path } }))
.pipe(rename({ extname: ".none.html" }))
.pipe(gulp.dest("../public"))
);
tasks.push("jade-none");

gulp.task("jade-build", function() { return gulp.src("./src/build.jade").pipe(jade()).pipe(gulp.dest("../public")); });
gulp.task("jade-build", () => gulp.src("./src/build.jade").pipe(jade()).pipe(gulp.dest("../public")));
tasks.push("jade-build");

// Stylus
var stylus = require("gulp-stylus");
gulp.task("stylus", function() { return gulp.src("./src/**/index.styl").pipe(stylus({ errors: true, compress: true })).pipe(gulp.dest("../public")); });
const stylus = require("gulp-stylus");
gulp.task("stylus", () => gulp.src("./src/**/index.styl").pipe(stylus({ errors: true, compress: true })).pipe(gulp.dest("../public")));
tasks.push("stylus");

// TypeScript
var ts = require("gulp-typescript");
var tsProject = ts.createProject("./tsconfig.json");
var tslint = require("gulp-tslint");
const ts = require("gulp-typescript");
const tsProject = ts.createProject("./tsconfig.json");
const tslint = require("gulp-tslint");

gulp.task("typescript", function() {
var failed = false;
var tsResult = tsProject.src()
gulp.task("typescript", () => {
let failed = false;
const tsResult = tsProject.src()
.pipe(tslint({ tslint: require("tslint") }))
.pipe(tslint.report("prose", { emitError: false }))
.pipe(ts(tsProject))
Expand All @@ -96,29 +54,13 @@ gulp.task("typescript", function() {
tasks.push("typescript");

// Browserify
var browserify = require("browserify");
var source = require("vinyl-source-stream");

gulp.task("browserify-login", [ "typescript" ], function() {
var bundler = browserify("./src/login/index.js");
function bundle() { return bundler.bundle().pipe(source("index.js")).pipe(gulp.dest("../public/login")); }
return bundle();
});
tasks.push("browserify-login");

gulp.task("browserify-hub", [ "typescript" ], function() {
var bundler = browserify("./src/hub/index.js");
function bundle() { return bundler.bundle().pipe(source("index.js")).pipe(gulp.dest("../public/hub")); }
return bundle();
});
tasks.push("browserify-hub");
const browserify = require("browserify");
const source = require("vinyl-source-stream");

gulp.task("browserify-project", [ "typescript" ], function() {
var bundler = browserify("./src/project/index.js");
function bundle() { return bundler.bundle().pipe(source("index.js")).pipe(gulp.dest("../public/project")); }
return bundle();
});
tasks.push("browserify-project");
gulp.task("browserify-login", [ "typescript" ], () => browserify("./src/login/index.js").bundle().pipe(source("index.js")).pipe(gulp.dest("../public/login")));
gulp.task("browserify-hub", [ "typescript" ], () => browserify("./src/hub/index.js").bundle().pipe(source("index.js")).pipe(gulp.dest("../public/hub")));
gulp.task("browserify-project", [ "typescript" ], () => browserify("./src/project/index.js").bundle().pipe(source("index.js")).pipe(gulp.dest("../public/project")));
tasks.push("browserify-login", "browserify-hub", "browserify-project");

// All
gulp.task("default", tasks);
60 changes: 60 additions & 0 deletions scripts/i18n.js
@@ -0,0 +1,60 @@
"use strict";

const fs = require("fs");
exports.rootLocalesPath = `${__dirname}/../public/locales`;
exports.relativeLocalesPath = "./public/locales";

let fallbackLocale = null;

exports.loadLocale = (languageCode, relative) => {
if (fallbackLocale == null && languageCode !== "en") fallbackLocale = exports.loadLocale("en", relative);

const localePath = relative ? exports.relativeLocalesPath : exports.rootLocalesPath;

const namespaces = {};
if (relative) namespaces["common"] = JSON.parse(fs.readFileSync(`${exports.rootLocalesPath}/${languageCode}/common.json`, { encoding: "utf8" }));

let filenames = [];
try { filenames = fs.readdirSync(`${localePath}/${languageCode}`); } catch (err) { /* Ignore */ }
for (const filename of filenames) {
const file = fs.readFileSync(`${localePath}/${languageCode}/${filename}`, { encoding: "utf8" });
namespaces[filename.slice(0, filename.lastIndexOf("."))] = JSON.parse(file);
}

if (languageCode !== "en") reportMissingKeys(languageCode, namespaces);
return namespaces;
};

exports.makeT = (locale) => {
return function t(path) {
const parts = path.split(":");
let value = locale[parts[0]];
if (value == null) return path;

const keys = parts[1].split(".");
for (const key of keys) {
value = value[key];
if (value == null) return path;
}
return value;
}
};

function reportMissingKeys(languageCode, locale) {
const missingKeys = [];

function checkRecursively(fallbackRoot, root, key, path) {
if (root[key] == null) {
missingKeys.push(path);
root[key] = fallbackRoot[key];
} else if (typeof fallbackRoot[key] === "object") {
const childKeys = Object.keys(fallbackRoot[key]);
for (const childKey of childKeys) checkRecursively(fallbackRoot[key], root[key], childKey, `${path}.${childKey}`);
}
}

const rootKeys = Object.keys(fallbackLocale);
for (const rootKey of rootKeys) checkRecursively(fallbackLocale, locale, rootKey, rootKey);

if (missingKeys.length > 0) console.log(`Missing keys in ${languageCode} locale: ${missingKeys.join(", ")}`);
}
104 changes: 52 additions & 52 deletions scripts/pluginGulpfile.js
@@ -1,51 +1,51 @@
var gulp = require("gulp");
var tasks = [];
var fs = require("fs");
"use strict";

var editors = [];
const gulp = require("gulp");
const tasks = [];
const fs = require("fs");

let editors = [];
console.log(process.cwd());
try { editors = fs.readdirSync("./editors"); } catch (err) { /* Ignore */ }

if (editors.length > 0) {
// Jade
var jade = require("gulp-jade");
var rename = require("gulp-rename");
var pluginI18n = require("./pluginI18n");
var locales = [ "en" ];
try { locales = fs.readdirSync(pluginI18n.rootLocalesPath); } catch (err) { /* Ignore */ }

locales.forEach(function(locale) {
var contexts = pluginI18n.loadLocale(locale);
gulp.task("jade-" + locale, function() {
var result = gulp.src("./editors/**/index.jade").pipe(jade({ locals: { t: pluginI18n.makeT(contexts) } }));
if (locale !== "en") result.pipe(rename({ extname: "." + locale + ".html" }));
const jade = require("gulp-jade");
const rename = require("gulp-rename");

const i18n = require("./i18n");
const languageCodes = fs.readdirSync(i18n.rootLocalesPath);

for (const languageCode of languageCodes) {
const locale = i18n.loadLocale(languageCode, true);
gulp.task(`jade-${languageCode}`, () => {
const result = gulp.src("./editors/**/index.jade").pipe(jade({ locals: { t: i18n.makeT(locale) } }));
if (languageCode !== "en") result.pipe(rename({ extname: `.${languageCode}.html` }));
return result.pipe(gulp.dest("./public/editors"));
});
tasks.push("jade-" + locale);
});
tasks.push(`jade-${languageCode}`);
}

gulp.task("jade-none", function() {
return gulp.src("./editors/**/index.jade")
.pipe(jade({ locals: { t: function(path) { return path; } } }))
.pipe(rename({ extname: ".none.html" }))
.pipe(gulp.dest("./public/editors"));
});
gulp.task("jade-none", () => gulp.src("./editors/**/index.jade")
.pipe(jade({ locals: { t: (path) => path } }))
.pipe(rename({ extname: ".none.html" }))
.pipe(gulp.dest("./public/editors"))
);
tasks.push("jade-none");

// Stylus
var stylus = require("gulp-stylus");
gulp.task("stylus", function() {
return gulp.src("./editors/**/index.styl").pipe(stylus({ errors: true, compress: true })).pipe(gulp.dest("./public/editors"));
});
const stylus = require("gulp-stylus");
gulp.task("stylus", () => gulp.src("./editors/**/index.styl").pipe(stylus({ errors: true, compress: true })).pipe(gulp.dest("./public/editors")));
tasks.push("stylus");
}

// TypeScript
var ts = require("gulp-typescript");
var tsProject = ts.createProject("./tsconfig.json");
const ts = require("gulp-typescript");
const tsProject = ts.createProject("./tsconfig.json");

gulp.task("typescript", function() {
var failed = false;
var tsResult = tsProject.src()
let failed = false;
const tsResult = tsProject.src()
.pipe(ts(tsProject))
.on("error", () => { failed = true; })
.on("end", () => { if (failed) throw new Error("There were TypeScript errors."); });
Expand All @@ -54,34 +54,34 @@ gulp.task("typescript", function() {
tasks.push("typescript");

// Browserify
var browserify = require("browserify");
var vinylSourceStream = require("vinyl-source-stream");
function makeBrowserify(source, destination, output) {
gulp.task(output + "-browserify", [ "typescript" ], function() {
if (!fs.existsSync(source)) return;

var bundler = browserify(source);
bundler.transform("brfs");
function bundle() { return bundler.bundle().pipe(vinylSourceStream(output + ".js")).pipe(gulp.dest(destination)); };
return bundle();
const browserify = require("browserify");
const source = require("vinyl-source-stream");

function makeBrowserify(src, dest, output) {
gulp.task(`${output}-browserify`, [ "typescript" ], () => {
if (!fs.existsSync(src)) return;

return browserify(src)
.transform("brfs").bundle()
.pipe(source(`${output}.js`))
.pipe(gulp.dest(dest));
});
tasks.push(output + "-browserify");
tasks.push(`${output}-browserify`);
}

if (fs.existsSync("./public/bundles")) {
var bundles = fs.readdirSync("./public/bundles");
bundles.forEach(function(bundle) { fs.unlinkSync("./public/bundles/" + bundle); });
for (const bundle of fs.readdirSync("./public/bundles")) fs.unlinkSync(`./public/bundles/${bundle}`);
}

var folders = fs.readdirSync("./");
folders.forEach(function(folder) {
if (folder === "public" || folder === "editors" || folder === "node_modules" || folder === "typings") return;
if (fs.existsSync("./" + folder + "/index.ts") || fs.existsSync("./" + folder + "/index.js"))
makeBrowserify("./" + folder + "/index.js", "./public/bundles", folder);
})
const nonBundledFolders = [ "public", "editors", "node_modules", "typings" ];
for (const folder of fs.readdirSync("./")) {
if (nonBundledFolders.indexOf(folder) !== -1) continue;

if (fs.existsSync(`./${folder}/index.ts`) || fs.existsSync(`./${folder}/index.js`))
makeBrowserify(`./${folder}/index.js`, "./public/bundles", folder);
}

editors.forEach(function(editor) { makeBrowserify("./editors/" + editor + "/index.js", "./public/editors", editor + "/index"); });
for (const editor of editors) makeBrowserify(`./editors/${editor}/index.js`, "./public/editors", `${editor}/index`);

// All
gulp.task("default", tasks);

0 comments on commit e132f4e

Please sign in to comment.