|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const crypto = require('crypto'); |
| 4 | +const ora = require('ora'); |
| 5 | + |
| 6 | +const colors = { |
| 7 | + red: s => `\x1b[31m${s}\x1b[39m`, |
| 8 | + gray: s => `\x1b[90m${s}\x1b[39m`, |
| 9 | + green: s => `\x1b[32m${s}\x1b[39m` |
| 10 | +}; |
| 11 | + |
| 12 | +function loadOptions (args) { |
| 13 | + args.cwd = args.cwd || process.cwd(); |
| 14 | + const name = args.config || 'up-config.js'; |
| 15 | + const file = path.join(args.cwd, name); |
| 16 | + try { |
| 17 | + const options = require(file); |
| 18 | + if (typeof options === 'function') { |
| 19 | + return options(args); |
| 20 | + } else { |
| 21 | + return Object.assign({}, args, options); |
| 22 | + } |
| 23 | + } catch (e) { |
| 24 | + return args; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function checksum (algorithm, file) { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + const hash = crypto.createHash(algorithm); |
| 31 | + const stream = fs.createReadStream(file); |
| 32 | + stream.on('error', err => reject(err)); |
| 33 | + stream.on('data', chunk => hash.update(chunk)); |
| 34 | + stream.on('end', () => resolve(hash.digest('hex'))); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +exports.runTask = async (list, tasks) => { |
| 39 | + if (!list || !list.length) return; |
| 40 | + for (const item of list) { |
| 41 | + const name = item.task; |
| 42 | + const fn = tasks[name]; |
| 43 | + if (!fn) { |
| 44 | + throw new Error(`Task '${item.task}' not found.`); |
| 45 | + } |
| 46 | + const spin = ora(`Task → ${name}`).start(); |
| 47 | + const actions = item.actions || []; |
| 48 | + const len = actions.length; |
| 49 | + for (let i = 0; i < len; i++) { |
| 50 | + spin.text = `Task → ${name} (${i + 1}/${len})`; |
| 51 | + await fn(actions[i]); |
| 52 | + } |
| 53 | + spin.succeed(); |
| 54 | + } |
| 55 | + console.log(); |
| 56 | +}; |
| 57 | + |
| 58 | +exports.getEndPoint = (region) => { |
| 59 | + const map = { |
| 60 | + // 华东 |
| 61 | + 'qn:cn-east-1': { |
| 62 | + port: 443, |
| 63 | + region: 'cn-east-1', |
| 64 | + endPoint: 's3-cn-east-1.qiniucs.com' |
| 65 | + }, |
| 66 | + // 华北 |
| 67 | + 'qn:cn-north-1': { |
| 68 | + port: 443, |
| 69 | + region: 'cn-north-1', |
| 70 | + endPoint: 's3-cn-north-1.qiniucs.com' |
| 71 | + }, |
| 72 | + // 华南 |
| 73 | + 'qn:cn-south-1': { |
| 74 | + port: 443, |
| 75 | + region: 'cn-south-1', |
| 76 | + endPoint: 's3-cn-south-1.qiniucs.com' |
| 77 | + }, |
| 78 | + // 北美 |
| 79 | + 'qn:us-north-1': { |
| 80 | + port: 443, |
| 81 | + region: 'us-north-1', |
| 82 | + endPoint: 's3-us-north-1.qiniucs.com' |
| 83 | + }, |
| 84 | + // 东南亚 |
| 85 | + 'qn:ap-southeast-1': { |
| 86 | + port: 443, |
| 87 | + region: 'ap-southeast-1', |
| 88 | + endPoint: 's3-ap-southeast-1.qiniucs.com' |
| 89 | + } |
| 90 | + }; |
| 91 | + if (map[region]) { |
| 92 | + return map[region]; |
| 93 | + } |
| 94 | + return { region }; |
| 95 | +}; |
| 96 | + |
| 97 | +exports.colors = colors; |
| 98 | +exports.checksum = checksum; |
| 99 | +exports.loadOptions = loadOptions; |
| 100 | + |
| 101 | +exports.defaultPrefix = (cwd) => { |
| 102 | + const pkgPath = path.join(cwd, 'package.json'); |
| 103 | + if (fs.existsSync(pkgPath)) { |
| 104 | + const pkg = require(pkgPath); |
| 105 | + if (pkg.name && pkg.version) { |
| 106 | + return `${pkg.name}/${pkg.version}`; |
| 107 | + } |
| 108 | + return ''; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +exports.exists = async (client, bucket, object, file) => { |
| 113 | + try { |
| 114 | + const stat = await client.statObject(bucket, object); |
| 115 | + const hash = await checksum('md5', file); |
| 116 | + return stat.etag === hash; |
| 117 | + } catch (e) { |
| 118 | + return false; |
| 119 | + } |
| 120 | +}; |
0 commit comments