Socket
Socket
Sign inDemoInstall

webpack-aliyun-oss

Package Overview
Dependencies
214
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.12 to 0.3.13

274

index.js

@@ -5,3 +5,3 @@ const fs = require('fs');

const globby = require("globby");
const Listr = require('listr');
const slash = require("slash");
require('colors');

@@ -15,21 +15,20 @@

accessKeySecret,
bucket,
ossOptions = {}
bucket
} = options;
this.config = Object.assign({
test: false, // 测试
dist: '', // oss目录
buildRoot: '.', // 构建目录名
deleteOrigin: false, // 是否删除源文件
timeout: 30 * 1000, // 超时时间
parallel: 5, // 并发数
setOssPath: null, // 手动设置每个文件的上传路径
setHeaders: null, // 设置头部
overwrite: false, // 覆盖oss同名文件
bail: false, // 出错中断上传
logToLocal: false // 出错信息写入本地文件
test: false,
verbose: true,
dist: '',
buildRoot: '.',
deleteOrigin: false,
deleteEmptyDir: false,
timeout: 30 * 1000,
setOssPath: null,
setHeaders: null,
overwrite: true
}, options);
this.configErrStr = this.checkOptions(options);
this.client = new OSS({

@@ -39,5 +38,4 @@ region,

accessKeySecret,
bucket,
...ossOptions
});
bucket
})

@@ -50,3 +48,3 @@ this.filesUploaded = []

if (compiler) {
return this.doWithWebpack(compiler);
this.doWithWebpack(compiler);
} else {

@@ -60,26 +58,19 @@ return this.doWidthoutWebpack();

if (this.configErrStr) {
compilation.errors.push(this.configErrStr);
compilation.errors.push(new Error(this.configErrStr));
return Promise.resolve();
}
const outputPath = path.resolve(this.slash(compiler.options.output.path));
const outputPath = path.resolve(slash(compiler.options.output.path));
const {
from = outputPath + '/**'
from = outputPath + '/' + '**',
verbose
} = this.config;
const files = await globby(from, {dot: true});
const files = await globby(from);
if (files.length) {
try {
await this.upload(files, true, outputPath);
console.log('');
console.log(' All files uploaded successfully '.bgGreen.bold.white);
} catch (err) {
compilation.errors.push(err);
return Promise.reject(err);
}
} else {
console.log('no files to be uploaded');
return Promise.resolve('no files to be uploaded');
if (files.length) return this.upload(files, true, outputPath);
else {
verbose && console.log('no files to be uploaded');
return Promise.resolve();
}

@@ -90,18 +81,10 @@ });

async doWidthoutWebpack() {
if (this.configErrStr) return Promise.reject(this.configErrStr);
if (this.configErrStr) return Promise.reject(new Error(this.configErrStr));
const { from } = this.config;
const { from, verbose } = this.config;
const files = await globby(from);
if (files.length) {
try {
await this.upload(files);
console.log('');
console.log(' All files uploaded successfully '.bgGreen.bold.white);
} catch (err) {
return Promise.reject(err);
}
}
if (files.length) return await this.upload(files);
else {
console.log('no files to be uploaded');
verbose && console.log('no files to be uploaded');
return Promise.resolve('no files to be uploaded');

@@ -114,158 +97,71 @@ }

dist,
buildRoot,
setHeaders,
deleteOrigin,
deleteEmptyDir,
setOssPath,
timeout,
verbose,
test,
overwrite,
bail,
parallel,
logToLocal
overwrite
} = this.config;
if (test) {
console.log('');
console.log('Currently running in test mode. your files won\'t realy be uploaded.'.green.underline);
console.log('');
} else {
console.log('');
console.log('Your files will be uploaded very soon.'.green.underline);
console.log('');
}
files = files.map(file => path.resolve(file))
files = files.map(file => ({
path: file,
fullPath: path.resolve(file)
}))
this.filesUploaded = []
this.filesIgnored = []
this.filesErrors = []
const basePath = this.getBasePath(inWebpack, outputPath)
const splitToken = inWebpack ?
'/' + outputPath.split('/').slice(-2).join('/') + '/' :
'/' + path.resolve(buildRoot).split('/').slice(-2).join('/') + '/';
const _upload = async file => {
const { fullPath: filePath, path: fPath } = file
try {
for (let filePath of files) {
let ossFilePath = slash(path.join(dist, (setOssPath && setOssPath(filePath) || (splitToken && filePath.split(splitToken)[1] || ''))));
let ossFilePath = this.slash(
path.join(
dist,
(
setOssPath && setOssPath(filePath)
|| basePath && filePath.split(basePath)[1]
|| ''
)
)
);
const fileExists = await this.fileExists(ossFilePath)
if (test) {
return Promise.resolve(fPath.blue.underline + ' is ready to upload to ' + ossFilePath.green.underline);
}
if (!overwrite) {
const fileExists = await this.fileExists(ossFilePath)
if (fileExists) {
if (fileExists && !overwrite) {
this.filesIgnored.push(filePath)
return Promise.resolve(fPath.blue.underline + ' ready exists in oss, ignored');
continue
}
}
const headers = setHeaders && setHeaders(filePath) || {}
let result
try {
result = await this.client.put(ossFilePath, filePath, {
if (test) {
console.log(filePath.blue, 'is ready to upload to ' + ossFilePath.green);
continue;
}
const headers = setHeaders && setHeaders(filePath) || {}
let result = await this.client.put(ossFilePath, filePath, {
timeout,
// headers: !overwrite ? Object.assign(headers, { 'x-oss-forbid-overwrite': true }) : headers
headers
headers: !overwrite ? Object.assign(headers, { 'x-oss-forbid-overwrite': true }) : headers
})
} catch (err) {
// if (err.name === 'FileAlreadyExistsError') {
// this.filesIgnored.push(filePath)
// return Promise.resolve(fPath.blue.underline + ' ready exists in oss, ignored');
// }
this.filesErrors.push({
file: fPath,
err: { code: err.code, message: err.message, name: err.name }
});
result.url = this.normalize(result.url);
this.filesUploaded.push(filePath)
const errorMsg = `Failed to upload ${fPath.underline}: ` + `${err.name}-${err.code}: ${err.message}`.red;
return Promise.reject(new Error(errorMsg))
}
verbose && console.log(filePath.blue, '\nupload to ' + ossFilePath + ' success,'.green, 'cdn url =>', result.url.green);
result.url = this.normalize(result.url);
this.filesUploaded.push(fPath)
if (deleteOrigin) {
fs.unlinkSync(filePath);
this.deleteEmptyDir(filePath);
if (deleteOrigin) {
fs.unlinkSync(filePath);
if (deleteEmptyDir && files.every(f => f.indexOf(path.dirname(filePath)) === -1))
this.deleteEmptyDir(filePath);
}
}
return Promise.resolve(fPath.blue.underline + ' successfully uploaded, oss url => ' + result.url.green)
} catch (err) {
console.log(`failed to upload to ali oss: ${err.name}-${err.code}: ${err.message}`.red)
}
let len = parallel
const addTask = () => {
if (len < files.length) {
tasks.add(createTask(files[len]))
len++
}
}
const createTask = file => ({
title: `uploading ${file.path.underline}`,
task(_, task) {
return _upload(file)
.then(msg => {
task.title = msg;
addTask()
})
.catch(e => {
if (!bail) addTask()
return Promise.reject(e)
})
}
});
const tasks = new Listr(
files.slice(0, len).map(createTask),
{
exitOnError: bail,
concurrent: parallel
})
await tasks.run().catch(() => { });
// this.filesIgnored.length && console.log('files ignored due to not overwrite'.blue, this.filesIgnored);
if (this.filesErrors.length) {
console.log(' UPLOAD ENDED WITH ERRORS '.bgRed.white, '\n');
logToLocal && fs.writeFileSync(path.resolve('upload.error.log'), JSON.stringify(this.filesErrors, null, 2))
return Promise.reject(' UPLOAD ENDED WITH ERRORS ')
}
verbose && console.log('files ignored'.blue, this.filesIgnored);
}
getBasePath(inWebpack, outputPath) {
if (this.config.setOssPath) return '';
let basePath = ''
if (inWebpack) {
if (path.isAbsolute(outputPath)) basePath = outputPath
else basePath = path.resolve(outputPath)
} else {
const { buildRoot } = this.config
if (path.isAbsolute(buildRoot)) basePath = buildRoot
else basePath = path.resolve(buildRoot)
}
return this.slash(basePath)
}
fileExists(filepath) {
// return this.client.get(filepath)
return this.client.head(filepath)
return this.client.get(filepath)
.then((result) => {
return result.res.status == 200
}).catch((e) => {
if (e.code == 'NoSuchKey') return false
if (e.code == 'NoSuchKey') {
// console.log(filepath, 'not exist', e);
return false
}
})

@@ -276,3 +172,3 @@ }

const tmpArr = url.split(/\/{2,}/);
if (tmpArr.length >= 2) {
if (tmpArr.length > 2) {
const [protocol, ...rest] = tmpArr;

@@ -284,13 +180,2 @@ url = protocol + '//' + rest.join('/');

slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
// const hasNonAscii = /[^\u0000-\u0080]+/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, '/');
}
deleteEmptyDir(filePath) {

@@ -302,3 +187,11 @@ let dirname = path.dirname(filePath);

else {
if (!files.length) fs.rmdir(dirname, () => { })
if (!files.length) {
fs.rmdir(dirname, (err) => {
if (err) {
console.log(err.red);
} else {
this.config.verbose && console.log('empty directory deleted'.green, dirname)
}
})
}
}

@@ -310,3 +203,3 @@ })

checkOptions(options = {}) {
let {
const {
from,

@@ -316,4 +209,3 @@ region,

accessKeySecret,
bucket,
ossOptions = {}
bucket
} = options;

@@ -323,6 +215,6 @@

if (!region && !ossOptions.region) errStr += '\nregion not specified';
if (!accessKeyId && !ossOptions.accessKeyId) errStr += '\naccessKeyId not specified';
if (!accessKeySecret && !ossOptions.accessKeySecret) errStr += '\naccessKeySecret not specified';
if (!bucket && !ossOptions.bucket) errStr += '\nbucket not specified';
if (!region) errStr += '\nregion not specified';
if (!accessKeyId) errStr += '\naccessKeyId not specified';
if (!accessKeySecret) errStr += '\naccessKeySecret not specified';
if (!bucket) errStr += '\nbucket not specified';

@@ -329,0 +221,0 @@ if (Array.isArray(from)) {

{
"name": "webpack-aliyun-oss",
"version": "0.3.12",
"version": "0.3.13",
"main": "index.js",

@@ -5,0 +5,0 @@ "author": "paul",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc