faster-webpack-upload-plugin
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "faster-webpack-upload-plugin", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "WangTieZhu92", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
108
src/index.js
@@ -28,3 +28,5 @@ 'use strict' | ||
options.port = options.port || '22'; | ||
options.firstEmit = true; | ||
options.firstEmit = options.firstEmit === undefined ? true : options.firstEmit; | ||
this.upload = this.upload.bind(this); | ||
} | ||
@@ -35,5 +37,5 @@ | ||
if (compiler.hooks) { | ||
compiler.hooks.afterEmit.tap('after-emit', this.upload(this.options)); | ||
compiler.hooks.afterEmit.tap('after-emit', this.upload); | ||
} else { | ||
compiler.plugin('after-emit', this.upload(this.options)); | ||
compiler.plugin('after-emit', this.upload); | ||
} | ||
@@ -43,67 +45,67 @@ } | ||
upload(options) { | ||
return async function (compilation) { | ||
const {localPath, remotePath, log, clearFolder, ...others} = options; | ||
const folders = []; | ||
const files = []; | ||
const uploadedFiles = []; | ||
async upload(compilation) { | ||
const {localPath, remotePath, log, clearFolder, ...others} = this.options; | ||
const folders = []; | ||
const files = []; | ||
const uploadedFiles = []; | ||
const sftp = new Client(); | ||
await sftp.connect({...others}); | ||
const sftp = new Client(); | ||
await sftp.connect({...others}); | ||
if (options.firstEmit) { | ||
try { | ||
await sftp.exists(remotePath); | ||
if (clearFolder) { | ||
log && console.log(chalk.red('clearing remote folder...')); | ||
await sftp.rmdir(remotePath, true); | ||
await sftp.mkdir(remotePath, true); | ||
} | ||
} catch (e) { | ||
if (this.options.firstEmit) { | ||
try { | ||
await sftp.exists(remotePath); | ||
if (clearFolder) { | ||
log && console.log(chalk.red('Clearing remote folder...')); | ||
await sftp.rmdir(remotePath, true); | ||
await sftp.mkdir(remotePath, true); | ||
} | ||
} catch (e) { | ||
await sftp.mkdir(remotePath, true); | ||
} | ||
getFolderNFiles(folders, files, localPath, remotePath); | ||
getFolderNFiles(folders, files, localPath, remotePath); | ||
options.firstEmit = false; | ||
} else { | ||
const assets = compilation.assets; | ||
for (const file in assets) { | ||
if (assets[file].emitted) { | ||
files.push({ | ||
local: assets[file].existsAt, | ||
remote: formatRemotePath(remotePath, file), | ||
size: assets[file].size(), | ||
}) | ||
} | ||
this.options.firstEmit = false; | ||
} else { | ||
const assets = compilation.assets; | ||
for (const file in assets) { | ||
if (assets[file].emitted) { | ||
files.push({ | ||
local: assets[file].existsAt, | ||
remote: formatRemotePath(remotePath, file), | ||
size: assets[file].size(), | ||
}) | ||
} | ||
} | ||
} | ||
if (folders.length > 0) { | ||
log && console.log(chalk.green('creating remote folders...')); | ||
await Promise.all(folders.map(folder => sftp.mkdir(folder).catch(() => log && console.log(chalk.yellow('folder create failed,it might exists'))))); | ||
} | ||
if (folders.length > 0) { | ||
log && console.log(chalk.green('Creating remote folders...')); | ||
await Promise.all(folders.map(folder => sftp.mkdir(folder).catch(() => log && console.log(chalk.yellow('Folder create failed,it might exists'))))); | ||
} | ||
const pb = new ProgressBar('', 50); | ||
const pb = new ProgressBar('', 50); | ||
if (files.length > 0) { | ||
await Promise.all(files.map(file => | ||
sftp.fastPut(file.local, file.remote) | ||
.catch(log && console.log) | ||
.then(result => { | ||
if (result) { | ||
uploadedFiles.push(file); | ||
pb.render({ | ||
percent: (uploadedFiles.length / files.length).toFixed(4), | ||
completed: uploadedFiles.length, | ||
total: files.length, | ||
}) | ||
} | ||
}))); | ||
if (files.length > 0) { | ||
await Promise.all(files.map(file => | ||
sftp.fastPut(file.local, file.remote) | ||
.catch(log && console.log) | ||
.then(result => { | ||
if (result) { | ||
uploadedFiles.push(file); | ||
pb.render({ | ||
percent: (uploadedFiles.length / files.length).toFixed(4), | ||
completed: uploadedFiles.length, | ||
total: files.length, | ||
}) | ||
} | ||
}))); | ||
log && console.log('\n' + chalk.green('upload done! files size: ' + (uploadedFiles.reduce((pre, next) => ({size: pre.size + next.size}), {size: 0}).size / 1000).toFixed(2) + ' KB')); | ||
} | ||
log && console.log('\n' + chalk.green('Upload done! Files size: ' + (uploadedFiles.reduce((pre, next) => ({size: pre.size + next.size}), {size: 0}).size / 1000).toFixed(2) + ' KB')); | ||
} | ||
await sftp.end(); | ||
} | ||
@@ -110,0 +112,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7629
4
1
42