From d9eb0bf7d6d1a7289ef574b04516dd35323b13f8 Mon Sep 17 00:00:00 2001 From: Maxime Cesson Date: Mon, 25 Jul 2022 11:28:29 +0200 Subject: [PATCH] Refine last details, handle some edge cases --- build.js | 117 ---------------------------------------- package.json | 4 +- scripts/build.js | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 119 deletions(-) delete mode 100644 build.js create mode 100644 scripts/build.js diff --git a/build.js b/build.js deleted file mode 100644 index c91c44f3e..000000000 --- a/build.js +++ /dev/null @@ -1,117 +0,0 @@ -var Fs = require("fs"); -var Fse = require("fs-extra"); -var Path = require("path"); - -if (!Fs.existsSync('./config/config.js')) { - console.log(`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) { - return s - .replace(/\{\{([^}]+?)\}\}/g, function (all, token) { - var content = o[token]; - - if (typeof(content) === 'number') { - content = String(content); - } - - if (typeof(content) !== 'string') { - console.error("expected {{%s}}", token); - throw new Error("invalid input"); - } - - if (!content) { - console.error("expected {{%s}}", token); - throw new Error("insufficient input"); - } - return content; - }); -}; - -const ogData = ` - - - - `; - -var previewExists = function (name) { - if (Fs.existsSync('customize/images/opengraph_preview/')) { - return Fs.existsSync(`customize/images/opengraph_preview/${name}`); - } - return Fs.existsSync(`customize.dist/images/opengraph_preview/${name}`); -}; - -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 insert = function (src, template) { - var matchs = src.match(/()|(.*<\/title>)/g); - - if (!matchs || !matchs.length) { - return src; - } - return src.replace(matchs.at(-1), `$& ${template}`); -}; - -var buildPath = './customize/www'; -var tmpPath = 'CRYPTPAD_TEMP_BUILD'; - -var write = function (content, dest) { - console.log(`Creating ${dest}`); - - var path = Path.join(tmpPath, dest); - var dirPath = Path.dirname(path); - Fse.mkdirpSync(dirPath); - Fs.writeFileSync(path, content); -}; - -console.log("Creating target directories"); -// remove tmp path so we start fresh -Fse.removeSync(tmpPath); -Fse.mkdirpSync(tmpPath); - -var srcAppTypes = Fs.readFileSync('./www/common/translations/messages.json', 'utf8'); -var types = JSON.parse(srcAppTypes).type; - -var appIndexesToBuild = [ - 'sheet', - 'doc', - 'presentation', - 'pad', - 'kanban', - 'code', - 'form', - 'poll', - 'whiteboard', - 'slide', - 'file', - 'drive' -]; - -appIndexesToBuild.forEach(function (app) { - console.log(`Parsing ./www/${app}/index.html`); - var src = Fs.readFileSync(`./www/${app}/index.html`, 'utf8'); - - write( - insert(src, templateOG(app, types[app])), - `${app}/index.html` - ); -}); - -Fse.removeSync(buildPath); -Fse.moveSync(tmpPath, buildPath); -/* Fse.renameSync(tmpPath, buildPath); // is rename doing more than move here? -// cos not working with buildPath like 'pre-path/main-dir'. If so, - var dirPath = Path.dirname(path); - Fse.mkdirpSync(dirPath); // may work */ diff --git a/package.json b/package.json index 3d197d029..2c953f20c 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "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", - "build": "node build.js", - "clean-built": "rm -rf customize/www/" + "make-opengraph": "node scripts/build.js", + "clean-opengraph": "rm -rf customize/www/" } } diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 000000000..3d9f70a06 --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,136 @@ +var Fs = require("fs"); +var Fse = require("fs-extra"); +var Path = require("path"); + +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) { + return s + .replace(/\{\{([^}]+?)\}\}/g, function (all, token) { + var content = o[token]; + + if (typeof(content) === 'number') { + content = String(content); + } + + if (typeof(content) !== 'string') { + console.error("expected {{%s}}", token); + throw new Error("invalid input"); + } + + if (!content) { + console.error("expected {{%s}}", token); + throw new Error("insufficient input"); + } + return content; + }); +}; + +const ogData = ` + + + + + `; + +var previewExists = function (name) { + if (Fs.existsSync(__dirname + '/../customize/images/opengraph_preview/')) { + return Fs.existsSync(__dirname + `/../customize/images/opengraph_preview/${name}`); + } + return Fs.existsSync(__dirname + `/../customize.dist/images/opengraph_preview/${name}`); +}; + +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 insert = function (src, template) { + var matchs = src.match(/()|(.*<\/title>)/g); + + if (!matchs || !matchs.length) { + return src; + } + return src.replace(matchs.at(-1), `$& ${template}`); +}; + +var buildPath = __dirname + '/../customize/www'; +var tmpPath = __dirname + '/../CRYPTPAD_TEMP_BUILD'; + +var write = function (content, dest) { + console.log(`Creating ${dest}`); + + var path = Path.join(tmpPath, dest); + var dirPath = Path.dirname(path); + Fse.mkdirpSync(dirPath); + Fs.writeFileSync(path, content); +}; + +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', + 'doc', + 'presentation', + 'pad', + 'kanban', + 'code', + 'form', + 'poll', + 'whiteboard', + 'slide', + 'file', + 'drive', + 'teams' +]; + +appIndexesToBuild.forEach(function (app) { + console.log(`Parsing www/${app}/index.html`); + var src = Fs.readFileSync(__dirname + `/../www/${app}/index.html`, 'utf8'); + + // rename types for shared documents (ones in place can sound weird) + if (app === 'drive') { types[app] = 'Drive'; } + if (app === 'teams') { types[app] = 'Team drive'; } + + write( + insert(src, templateOG(app, types[app])), + `${app}/index.html` + ); +}); + +Fse.removeSync(buildPath); +var dirPath = Path.dirname(buildPath); +Fse.mkdirpSync(dirPath); +Fse.renameSync(tmpPath, buildPath);