folder-upload-webpack-plugin
Version:
Upload your folder to ssh sftp server after build, add support to 'webpack --watch'
42 lines (38 loc) • 1.29 kB
text/typescript
import archiver from "archiver";
import chalk, {Chalk} from "chalk";
import fs from "fs-extra";
import path from "path";
export default class Compressor {
private logging: boolean;
constructor(log = false) {
this.logging = log;
}
compress(from: string, to: string, compress: number) {
const output = fs.createWriteStream(path.resolve(to));
const arch = archiver('zip', {
zlib: { level: compress } // Sets the compression level.
});
arch.pipe(output);
arch.append(fs.createReadStream(from), { name: path.parse(from).base });
return new Promise((resolve, reject) => {
// handle success
output.on('warning', (err) => {
if (err.code !== 'ENOENT') {
throw new Error(err)
}
});
output.on(`error`, reject);
output.on('close', () => {
this.log(arch.pointer() + ' total bytes', chalk.bold.blue);
resolve(to);
});
arch.finalize();
});
}
log(text: string, formatter:Chalk = chalk) {
if(!this.logging) {
return;
}
console.log(formatter(text));
}
};