1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| const path = require('path'); const fs = require('fs'); const yargs = require('yargs'); const webpack = require('webpack'); const webpackConfig = require('./webpack.prod.config'); const sftp = require('./sftp');
const user = yargs.argv.user || '';
console.log(user);
const staticFilesPath = { js: { local: path.resolve(__dirname, '../dist/js'), remote: `/upload_code/${user}/static/mobile/js/dist`, }, css: { local: path.resolve(__dirname, '../dist/css'), remote: `/upload_code/${user}/static/mobile/css/`, }, img: { local: path.resolve(__dirname, '../dist/images'), remote: `/upload_code/${user}/static/mobile/images/`, }, };
let isFirstBuild = true;
const compiler = webpack(webpackConfig); const watching = compiler.watch( { ignored: /node_modules/, aggregateTimeout: 100, poll: 1000, }, (err, stats) => { if (err || stats.hasErrors()) { console.log(err); } console.log('编译成功!'); if (isFirstBuild) { isFirstBuild = false; return; } console.log('正在上传...'); uploadFile() .then(() => { console.log('------所有文件上传完成!-------\n'); }) .catch(() => { console.log('------上传失败,请检查!-------\n'); }); } );
function handleFilePath(obj, type) { const { local, remote } = obj; const files = fs.readdirSync(local); return files.map(file => { const _lp = `${local}/${file}`; return { type: type, file: file, localPath: type !== 'img' ? _lp : fs.readFileSync(_lp), remotePath: `${remote}/${file}`, }; }); }
function uploadFile() { let files = [];
Object.keys(staticFilesPath).forEach(key => { files = files.concat(handleFilePath(staticFilesPath[key], key)); });
const tasks = files.map(item => { return new Promise((resolve, reject) => { sftp .put(item.localPath, item.remotePath) .then(() => { console.log(`${item.file}上传完成`); resolve(); }) .catch(err => { console.log(`${item.file}上传失败`); reject(); }); }); });
return Promise.all(tasks); }
|