Skip to content

Commit

Permalink
feat(remote-sync): Enable remote syncing based on an API response (#1506
Browse files Browse the repository at this point in the history
)

* feat(remote-sync): Enable remote syncing based on an API response

* feat(remote-sync): Added option for passing SSH key

---------

Co-authored-by: Tyler Gauntlett <tgauntlett@box.com>
  • Loading branch information
TylerGauntlett and TylerGauntlett authored Sep 21, 2023
1 parent 01fb40a commit 11cfaa7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions build/ApiRsyncPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { execSync } = require('child_process');

function ApiRsyncPlugin(source, destination) {
this.source = source;
this.destination = destination;
}

/* eslint-disable no-console */
ApiRsyncPlugin.prototype.apply = function rsync(compiler) {
compiler.plugin('done', () => {
console.log('');
console.log(`🔄 🔄 🔄 Rsync starting for ${this.source} 🔄 🔄 🔄`);
execSync(
`rsync -avz -e "ssh -p 8022 -o ConnectTimeout=3 ${process.env.DEVPOD_RSYNC_OPTIONS}" ${this.source} ${this.destination}`,
{
stdio: [0, 1, 2],
},
);
});
};

module.exports = ApiRsyncPlugin;
19 changes: 19 additions & 0 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ const isDev = NODE_ENV === 'dev';
const isProd = NODE_ENV === 'production';

const fs = require('fs');
const get = require('lodash/get');
const path = require('path');
const locales = require('@box/languages');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const { execSync } = require('child_process');
const commonConfig = require('./webpack.common.config');
const RsyncPlugin = require('./RsyncPlugin');
const ApiRsyncPlugin = require('./ApiRsyncPlugin');
const version = isProd ? require('../package.json').version : 'dev';

let rsyncLocation = '';
let rsyncApiLocation = null;
if (fs.existsSync('build/rsync.json')) {
/* eslint-disable */
const rsyncConf = require('./rsync.json');
rsyncLocation = rsyncConf.location;
rsyncApiLocation = rsyncConf.apiLocation;
/* eslint-enable */
}

Expand Down Expand Up @@ -78,6 +83,20 @@ function updateConfig(conf, language, index) {
if (rsyncLocation) {
config.plugins.push(new RsyncPlugin('dist/.', rsyncLocation));
}

if (rsyncApiLocation) {
const serverResponse = execSync(
`curl -sk -H "Content-Type: application/json" --connect-timeout 1 ${rsyncApiLocation.url}`,
)
.toString()
.replace('\n', '');

const json = JSON.parse(serverResponse);

const destination = `${rsyncApiLocation.user}@${get(json, rsyncApiLocation.ip)}:${rsyncApiLocation.path}`;

config.plugins.push(new ApiRsyncPlugin('dist/.', destination));
}
}

if (isProd) {
Expand Down

0 comments on commit 11cfaa7

Please sign in to comment.