diff --git a/assets/white-label-bin/appStyle.js b/assets/white-label-bin/appStyle.js new file mode 100644 index 0000000..2630cbb --- /dev/null +++ b/assets/white-label-bin/appStyle.js @@ -0,0 +1,56 @@ +#!/usr/bin/env node + +const program = require('commander'); +require('colors'); +const shippingConfigs = require('./lib/Configs'); +const serviceAgent = require('./lib/appStyle.js'); + +let commandValid = false; +const configs = shippingConfigs.getConfigList(); + +function checkCommandValid(config) { + commandValid = configs.indexOf(config) !== -1; + return commandValid; +} + +program + .version(require('../../../YourAwesomeProject/package').version) + .description('Save and restore appStyle.'); + +program + .command('save ') + .alias('s') + .description('Save the appStyle for a shipping config.') + .action(config => { + if (!checkCommandValid(config)) { + return; + } + serviceAgent.saveAppStyle(config); + }); + +program + .command('restore ') + .alias('r') + .description('Save the appStyle from a shipping config.') + .action(config => { + if (!checkCommandValid(config)) { + return; + } + serviceAgent.restoreAppStyle(config); + }); + +program.on('--help', () => { + console.log(''); + console.log(' Available shipping config : '); + console.log(''); + + configs.forEach(config => { + console.log(`\t - ${config}`); + }); +}); + +program.parse(process.argv); + +if (!commandValid) { + program.help(); +} diff --git a/assets/white-label-bin/lib/appStyle.js b/assets/white-label-bin/lib/appStyle.js new file mode 100644 index 0000000..82c2eb5 --- /dev/null +++ b/assets/white-label-bin/lib/appStyle.js @@ -0,0 +1,65 @@ +const path = require('path'); +const fs = require('fs'); +require('colors'); +const shippingConfigs = require('./configs'); + +const appStylePath = path.join(__dirname, '../../../../YourAweSomeProject/src/appStyle.js'); +const destDirName = shippingConfigs.configDir; + +function copyAppStyle(config, saving) { + const savePath = path.join(destDirName, config, 'appStyle/appStyle.js'); + const restorePath = appStylePath; + + const destFile = saving ? savePath : restorePath; + const initialFile = saving ? restorePath : savePath; + + return new Promise((resolve, reject) => { + try { + console.log('Copying the appStyle'); + fs.createReadStream(initialFile).pipe(fs.createWriteStream(destFile)); + } catch (e) { + console.log('Error copying the appStyle'.red); + reject(e); + } + + const verb = saving ? 'saved' : 'restored'; + console.log(`AppStyle successfully ${verb}`.green); + resolve(); + }); +} + +function saveAppStyle(config) { + console.log(''); + console.log(`Saving the appStyle for config ${config}`.yellow); + + const destDir = path.join(destDirName, config); + + if (!fs.existsSync(destDir)) { + try { + fs.mkdirSync(destDir); + } catch (e) { + console.log(e); + } + } + if (!fs.existsSync(path.join(destDir, 'appStyle'))) { + try { + fs.mkdirSync(path.join(destDir, 'appStyle')); + } catch (e) { + console.log(e); + } + } + + return copyAppStyle(config, true); +} + +function restoreAppStyle(config) { + console.log(''); + console.log(`Restoring the appStyle for config ${config}`.yellow); + + return copyAppStyle(config, false); +} + +module.exports = { + saveAppStyle, + restoreAppStyle +}; diff --git a/assets/white-label-bin/lib/configs.js b/assets/white-label-bin/lib/configs.js new file mode 100644 index 0000000..9e14fa2 --- /dev/null +++ b/assets/white-label-bin/lib/configs.js @@ -0,0 +1,14 @@ +const path = require('path'); +const fs = require('fs'); + +const configDir = path.join(__dirname, '../../config'); + +function getConfigList() { + const files = fs.readdirSync(configDir); + return files.map(file => file.replace('.js', '')); +} + +module.exports = { + configDir, + getConfigList +}; diff --git a/backend/node-js/white-label.mo.md b/backend/node-js/white-label.mo.md new file mode 100644 index 0000000..fba1d72 --- /dev/null +++ b/backend/node-js/white-label.mo.md @@ -0,0 +1,7 @@ +# [MO] How to implement white-label on a nodeJS server _(~