Skip to content

Instantly share code, notes, and snippets.

@bengsiswantoh
Created July 17, 2020 03:24
Show Gist options
  • Save bengsiswantoh/c8a4b338d5d6777996c398d0c889c841 to your computer and use it in GitHub Desktop.
Save bengsiswantoh/c8a4b338d5d6777996c398d0c889c841 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const archiver = require('archiver');
const { exec } = require('child_process');
require('dotenv').config();
const SMB_USER = process.env.SMB_USER;
const SMB_PASSWORD = process.env.SMB_PASSWORD;
const SMB_SERVER = process.env.SMB_SERVER;
const SMB_DIR = process.env.SMB_DIR;
const replacePath = (fileName) => {
const content = fs.readFileSync(fileName, 'utf8');
const result = content.replace(/="\//g, '="./');
fs.writeFileSync(fileName, result, 'utf8');
console.log('replace path done');
};
const createZip = (sourceDir, archiveFilename, archiveType) => {
const output = fs.createWriteStream(archiveFilename);
const archive = archiver(archiveType);
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log(`archive ${archiveType} done`);
copyToServer(archiveFilename);
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
archive.directory(sourceDir, false);
archive.finalize();
};
const copyToServer = (archiveFilename) => {
const command = `curl --upload-file ${archiveFilename} --user "${SMB_USER}:${SMB_PASSWORD}" smb://${SMB_SERVER}${SMB_DIR}`;
exec(command, (err, stdout, stderr) => {
// delete file
fs.unlinkSync(archiveFilename);
if (err) {
//some err occurred
console.error(err);
} else {
console.log(`copy to ${SMB_SERVER}${SMB_DIR}${archiveFilename} done`);
// the *entire* stdout and stderr (buffered)
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
}
});
};
const archiveType = 'zip';
const paths = __dirname.split('/');
const fileName = `${paths[paths.length - 1]}.${archiveType}`;
try {
replacePath('out/index.html');
} catch (err) {
console.log(err);
}
createZip('out', fileName, archiveType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment