├── .npmignore ├── .gitignore ├── bin ├── assert.js ├── log.js └── index.js ├── package.json ├── index.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | test.js 2 | _test 3 | .aliossrc 4 | .idea 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.* 3 | .idea 4 | coverage 5 | _test 6 | .aliossrc 7 | test.js 8 | -------------------------------------------------------------------------------- /bin/assert.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const figures = require('figures'); 3 | 4 | module.exports = function assert(condition, errorMsg) { 5 | if (!condition) { 6 | console.log(chalk.bold.red(figures.cross), chalk.bold.red(errorMsg)); 7 | process.exit(0); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aliyun-oss-deploy", 3 | "version": "0.1.5", 4 | "description": "An aliyun oss deploy tool, putObject and putStream are all supported.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "bin": { 10 | "aliyun-oss-deploy": "bin/index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/hustcc/aliyun-oss-deploy.git" 15 | }, 16 | "keywords": [ 17 | "aliyun", 18 | "oss", 19 | "aliyun-oss", 20 | "deploy", 21 | "cd" 22 | ], 23 | "author": "hustcc", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/hustcc/aliyun-oss-deploy/issues" 27 | }, 28 | "homepage": "https://github.com/hustcc/aliyun-oss-deploy#readme", 29 | "dependencies": { 30 | "ali-oss": "^5.2.0", 31 | "chalk": "^2.4.2", 32 | "co": "^4.6.0", 33 | "figures": "^2.0.0", 34 | "globby": "^10.0.0", 35 | "terminal-link": "^1.3.0", 36 | "yargs": "^13.2.4" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bin/log.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const figures = require('figures'); 3 | const terminalLink = require('terminal-link'); 4 | 5 | function successFile(url) { 6 | console.log(chalk.green(figures.tick), url); 7 | } 8 | 9 | function failFile(url) { 10 | console.log(chalk.green(figures.cross), url); 11 | } 12 | 13 | function count(success, fail) { 14 | console.log(chalk.green(`${success} success.`), chalk.green(`${fail} fail.`)); 15 | } 16 | 17 | function powerBy() { 18 | console.log('\nPowered by', terminalLink('aliyun-oss-deploy', 'https://github.com/hustcc/aliyun-oss-deploy')) 19 | } 20 | 21 | module.exports = async function log(dg) { 22 | let sc = 0, fc = 0; 23 | 24 | // 1. log deploy generator 25 | for (const r of dg) { 26 | const result = await r; 27 | 28 | const isSuccess = result.res && result.res.status === 200; 29 | const url = result.url; 30 | 31 | if (isSuccess) { 32 | sc ++; 33 | successFile(url); 34 | } else { 35 | fc ++; 36 | failFile(url); 37 | } 38 | } 39 | 40 | // 2. log result count 41 | count(sc, fc); 42 | 43 | // 3. log power by 44 | powerBy(); 45 | } 46 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/5/10. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | const fs = require('fs'); 7 | const path = require('path'); 8 | 9 | const co = require('co'); 10 | const OSS = require('ali-oss'); 11 | const globby = require('globby'); 12 | 13 | const fileList = filePath => 14 | fs.statSync(filePath).isFile() ? [ filePath ] : globby.sync([ '**/*.*' ], { cwd: filePath }); 15 | 16 | /** 17 | * 上传文件的 generator 18 | * @param filePath 19 | * @param config 20 | * @param prefix 21 | * @param byStream 22 | */ 23 | const deployGenerator = function* (filePath, config, prefix, byStream) { 24 | filePath = filePath && path.resolve(filePath); 25 | prefix = prefix || ''; 26 | 27 | const client = new OSS(config); 28 | 29 | // 获得文件列表,并添加相对目录 30 | const files = fileList(filePath); 31 | // const func = byStream ? uploadByPutStream : uploadByPutObject; 32 | 33 | const r = []; 34 | 35 | for (const file of files) { 36 | const ossTarget = path.join(prefix, file); 37 | // stream 38 | const stream = fs.createReadStream(path.join(filePath, file)); 39 | 40 | // 使用 oss 上传文件 file 到 targetFile 41 | const result = yield byStream ? 42 | co(client.putStream(ossTarget, stream)) : 43 | co(client.put(ossTarget, stream)); 44 | 45 | r.push(result); 46 | } 47 | return r; 48 | }; 49 | 50 | /** 51 | * 入口方法 52 | * @param filePath 上传的文件目录 53 | * @param config aliyun oss 配置内容 54 | * @param prefix 上传到 oss 的批量文件前缀,可以用于做版本号 55 | * @param byStream 默认是 putObject 56 | */ 57 | module.exports = async (filePath, config, prefix, byStream) => { 58 | return await co(deployGenerator(filePath, config, prefix, byStream)); 59 | }; 60 | 61 | module.exports.deployGenerator = deployGenerator; 62 | -------------------------------------------------------------------------------- /bin/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | const assert = require('./assert'); 7 | const log = require('./log.js'); 8 | const deploy = require('../index.js'); 9 | 10 | const deployGenerator = deploy.deployGenerator; 11 | 12 | /** 13 | * 命令行参数配置 14 | */ 15 | const argv = require('yargs') 16 | .option('p', { 17 | describe: 'Set your upload files path', 18 | type: 'string', 19 | alias: 'filePath', 20 | }) 21 | .option('d', { 22 | describe: 'Set the target dir of upload', 23 | type: 'string', 24 | alias: 'prefix', 25 | }) 26 | .option('c', { 27 | describe: 'Set your .aliossrc file path or JSON string', 28 | type: 'string', 29 | alias: 'aliossrc', 30 | }) 31 | .option('s', { 32 | describe: 'Upload file by putStream', 33 | type: 'boolean', 34 | alias: 'useStream', 35 | }) 36 | .help() 37 | .argv; 38 | 39 | // 校验路径 40 | assert(argv.filePath, 'argv -p should be required.'); 41 | 42 | // oss 配置文件 43 | const aliossrc = argv.aliossrc ? path.resolve(argv.aliossrc) : path.join(process.cwd(), '.aliossrc'); 44 | 45 | let ossConfig; 46 | // 文件存在则直接读取 47 | try { 48 | if (fs.existsSync(aliossrc)) { 49 | ossConfig = JSON.parse(fs.readFileSync(aliossrc, { 50 | encoding: 'utf8', 51 | })); 52 | } else { 53 | ossConfig = JSON.parse(argv.aliossrc); 54 | } 55 | } catch (e) { 56 | assert(false, `ossConfig '${aliossrc}' is not a valid JSON file.`) 57 | } 58 | 59 | 60 | // 校验配置 61 | assert(ossConfig.region, `region is required in ${aliossrc}.`); 62 | assert(ossConfig.accessKeyId, `accessKeyId is required in ${aliossrc}.`); 63 | assert(ossConfig.accessKeySecret, `accessKeySecret is required in ${aliossrc}.`); 64 | assert(ossConfig.bucket, `bucket is required in ${aliossrc}.`); 65 | 66 | const dg = deployGenerator(argv.filePath, ossConfig, argv.prefix, argv.useStream); 67 | 68 | log(dg); 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aliyun-oss-deploy 2 | 3 | > 一个用于部署静态资源到 aliyun oss 的模块,支持 putObject 和 putStream。可以代码方式或者 cli 方式调用! 4 | 5 | [![npm](https://img.shields.io/npm/v/aliyun-oss-deploy.svg)](https://www.npmjs.com/package/aliyun-oss-deploy) 6 | [![npm](https://img.shields.io/npm/dm/aliyun-oss-deploy.svg)](https://www.npmjs.com/package/aliyun-oss-deploy) 7 | 8 | ![image](https://user-images.githubusercontent.com/7856674/40217123-aa5df822-5a9d-11e8-8b2d-5488967e3fcc.png) 9 | 10 | ## Install 11 | 12 | ```bash 13 | npm i --save-dev aliyun-oss-deploy 14 | ``` 15 | 16 | 17 | ## Usage 18 | 19 | - 代码方式 20 | 21 | ```js 22 | const deploy = require('aliyun-oss-deploy'); 23 | 24 | deploy(path, ossConfig[, prefix, byStream]); 25 | ``` 26 | 27 | **注意**:prefix 用来配置资源版本号比较合适,默认为空;byStream 默认为 false,表示使用 putObject 方法! 28 | 29 | 30 | - CLI 方式 31 | 32 | ```bash 33 | aliyun-oss-deploy -p ./dist -c .aliossrc -d static 34 | ``` 35 | 36 | 帮助文档: 37 | 38 | ```bash 39 | aliyun-oss-deploy --help 40 | Options: 41 | --version Show version number [boolean] 42 | -p, --filePath Set your upload files path [string] 43 | -d, --prefix Set the target dir of upload [string] 44 | -c, --aliossrc Set your .aliossrc file path [string] 45 | -s, --useStream Upload file by putStream [boolean] 46 | --help Show help [boolean] 47 | ``` 48 | 49 | 50 | 可以在 package.json 中直接使用 51 | 52 | ```json 53 | { 54 | "script": { 55 | "deploy": "aliyun-oss-deploy -p ./dist" 56 | } 57 | } 58 | ``` 59 | 60 | 61 | ## Config 62 | 63 | > 无论是代码方式还是 cli 方式,aliyun oss 配置文件都是下面的数据结构! 64 | 65 | **需要注意的是**:对于 CLI 方法,配置文件必须是 JSON 格式(双引号)! 66 | 67 | 68 | ```js 69 | { 70 | "accessKeyId": "your accessKeyId", 71 | "accessKeySecret": "your accessKeySecret", 72 | "region": "your region", 73 | "bucket": "your bucket" 74 | } 75 | ``` 76 | 77 | **注意**:`region` 是区分 endpoint 的区域分类。 78 | 79 | 80 | ## License 81 | 82 | ISC@[hustcc](https://github.com/hustcc). 83 | --------------------------------------------------------------------------------