diff --git a/package.json b/package.json index 2c953f20c..f7043104b 100644 --- a/package.json +++ b/package.json @@ -49,9 +49,7 @@ "unused-translations": "node ./scripts/translations/unused-translations.js", "test": "node scripts/TestSelenium.js", "test-rpc": "cd scripts/tests && node test-rpc", - "template": "cd customize.dist/src && for page in ../index.html ../contact.html ../features.html ../../www/login/index.html ../../www/register/index.html ../../www/user/index.html;do echo $page; cp template.html $page; done;", "evict-inactive": "node scripts/evict-inactive.js", - "make-opengraph": "node scripts/build.js", - "clean-opengraph": "rm -rf customize/www/" + "build": "node scripts/build.js" } } diff --git a/scripts/build.js b/scripts/build.js index 865ba2964..ccaf4b2b3 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -3,29 +3,8 @@ var Fs = require("fs"); var Fse = require("fs-extra"); var Path = require("path"); +var OS = require("os"); -if (process.env.CRYPTPAD_CONFIG) { - /* using Fs.existsSync() function and __dirname variable here won't filter - environment variables like CRYPTPAD_CONFIG='/../docs/config2.js', while require() - inside load-config.js will, outputing some non intellegible error messages. - (plus, it won't handle missing '.js' in file names neither) */ - try { - require.resolve(process.env.CRYPTPAD_CONFIG); - } catch (e) { - console.error(`The configuration file ${process.env.CRYPTPAD_CONFIG} can not - be loaded. Please review your CRYPTPAD_CONFIG environment variable.` - .replace(/\s{2,}/g, ' ')); - process.exit(1); - } -} else { - if (!Fs.existsSync(__dirname + '/../config/config.js')) { - console.error(`This script needs the file config/config.js to work properly. - You can make one by copying config/config.example.js. Check the - value of httpUnsafeOrigin for this script to behave as expected.` - .replace(/\s{2,}/g, ' ')); - process.exit(1); - } -} var config = require("../lib/load-config"); var swap = function (s, o) { @@ -50,41 +29,88 @@ var swap = function (s, o) { }); }; -const ogData = ` - - - - - - `; +var Messages = require("../www/common/translations/messages.json"); -var previewExists = function (name) { - if (Fs.existsSync(__dirname + '/../customize/images/opengraph_preview/')) { - return Fs.existsSync(__dirname + `/../customize/images/opengraph_preview/${name}`); +var types = Messages.type; +[ 'calendar', 'notifications', ].forEach(k => { types[k] = Messages[k]; }); + +// FIXME it would be better if these were just included in the translated list of types +types.settings = Messages.settings_title; +types.support = Messages.supportPage; +types.profile = Messages.profilePage; + +var translations; +try { + translations = Fs.readdirSync('./www/common/translations/').filter(name => { + return /messages\..*\.json$/.test(name); + }); +} catch (err) { + console.error(err); +} + +var preferredLanguage = config.preferredLanguage; +var Preferred; + +var noScriptContent = [ Messages.ui_jsRequired ]; +translations.forEach(name => { + var path = `./www/common/translations/${name}`; + var content; + try { + content = JSON.parse(Fs.readFileSync(path, 'utf-8')); + } catch (err) { + return void console.error(`Failed to parse ${path}`); } - return Fs.existsSync(__dirname + `/../customize.dist/images/opengraph_preview/${name}`); + + if (name === `messages.${preferredLanguage}.json`) { Preferred = content; } + + if (typeof(content.ui_jsRequired) !== 'string') { return; } + noScriptContent.push(content.ui_jsRequired); +}); + +var makeNoscript = (indent) => { + var lines = noScriptContent.map(s => { + return `${indent + indent}

${s}

`; + }).join('\n'); + return `${indent}`; }; -var templateOG = function (a, type) { - return swap(ogData, { - rootUrl: config.httpUnsafeOrigin, - app: a, - title: type && `Encrypted ${type}` || 'CryptPad', - image: previewExists(`og-${a}.png`) && `og-${a}.png` || `og-default.png` +var getKey = function (key, args) { + var source; + + if (Preferred && Preferred[key]) { + source = Preferred[key]; + } else if (Messages && Messages[key]) { + source = Messages[key]; + } else { + return '?'; + } + + if (typeof(source) !== 'string') { return '?'; } + if (!Array.isArray(args)) { return source; } + + return source.replace(/\{(\d+)\}/g, (str, p1) => { + if (['string', 'number'].includes(typeof(p1))) { return args[p1]; } + console.error("Only strings and numbers can be used in _getKey params.\nAborting..."); + process.exit(1); }); }; -var insert = function (src, template) { - var matchs = src.match(/()|(.*<\/title>)/g); - - if (!matchs || !matchs.length) { - return src; +var previewExists = function (name) { + if (Fs.existsSync('./customize/images/opengraph_preview/')) { + return Fs.existsSync(`./customize/images/opengraph_preview/${name}`); } - return src.replace(matchs.at(-1), `$& ${template}`); + return Fs.existsSync(`./customize.dist/images/opengraph_preview/${name}`); }; -var buildPath = __dirname + '/../customize/www'; -var tmpPath = __dirname + '/../CRYPTPAD_TEMP_BUILD'; +var imagePath = `/customize/images/opengraph_preview/`; + +var appImagePath = a => { + var partial = previewExists(`og-${a}.png`) && `og-${a}.png` || `og-default.png`; + return new URL(imagePath + partial, config.httpUnsafeOrigin).href; +}; + +var buildPath = Path.resolve('./customize'); +var tmpPath = Path.join(OS.tmpdir(), '/CRYPTPAD_TEMP_BUILD/'); var write = function (content, dest) { console.log(`Creating ${dest}`); @@ -93,15 +119,12 @@ var write = function (content, dest) { var dirPath = Path.dirname(path); Fse.mkdirpSync(dirPath); Fs.writeFileSync(path, content); + console.log(); }; console.log("Creating target directories"); // remove tmp path so we start fresh Fse.removeSync(tmpPath); -Fse.mkdirpSync(tmpPath); - -var srcAppTypes = Fs.readFileSync(__dirname + '/../www/common/translations/messages.json', 'utf8'); -var types = JSON.parse(srcAppTypes).type; var appIndexesToBuild = [ 'sheet', @@ -117,24 +140,180 @@ var appIndexesToBuild = [ 'file', 'calendar', 'drive', - 'teams' + 'teams', + 'contacts', + + 'notifications', + 'checkup', + 'file', + 'profile', + 'settings', + 'support', + // bounce ?? ]; +var baseAppPath = './www/'; +const ogData = ` + + + + + + `; + + +var versionString = String(+new Date()); + +var processPage = (src) => { + return src + .replace(/(\s*)