代码拉取完成,页面将自动刷新
同步操作将从 xiaozhuai/ossync 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const fs = require('fs');
const OSS = require('ali-oss');
const ignore = require('ignore');
const xxHash = require('xxhashjs');
const chalk = require('chalk');
const progress = require('cli-progress');
const Readable = require('stream').Readable;
const cwd = process.cwd();
module.exports = async (opts) => {
if(!opts.hasOwnProperty('config')) throw 'No config';
if (!fs.existsSync(opts.config)) throw `config file ${opts.config} not found`;
const config = JSON.parse(fs.readFileSync(opts.config).toString());
if(!opts.hasOwnProperty('ignore')) throw 'No ignore';
const ig = ignore();
ig.add(opts.config);
ig.add(opts.ignore);
if (fs.existsSync(opts.ignore)) ig.add(fs.readFileSync(opts.ignore).toString());
if(!opts.hasOwnProperty('ignore')) throw 'No force';
const forceReSyncAll = opts.force;
const client = new OSS(config);
await main();
async function main() {
console.log(`Sync ${cwd} ---> oss://${config.bucket}`);
console.log(`Calculating changes...`);
let ossFiles = forceReSyncAll ? {} : await getOssFiles(ig);
// console.log(ossFiles);
let localFiles = await getLocalFiles(ig);
// console.log(localFiles);
let createList = [];
let modifyList = [];
let removeList = [];
for (let file in localFiles) {
if (!localFiles.hasOwnProperty(file)) continue;
if (ossFiles.hasOwnProperty(file)) {
if (localFiles[file] !== ossFiles[file]) {
modifyList.push(file);
}
} else {
createList.push(file);
}
}
for (let file in ossFiles) {
if (!ossFiles.hasOwnProperty(file)) continue;
if (!localFiles.hasOwnProperty(file)) {
removeList.push(file);
}
}
let totalChanges = createList.length + modifyList.length + removeList.length;
if (totalChanges === 0) {
console.log(`Your workspace is already up to date!`);
process.exit(0);
}
console.log('');
for (let file of createList) console.log(chalk.green(` Create ${file}`));
for (let file of modifyList) console.log(chalk.blue(` Modify ${file}`));
for (let file of removeList) console.log(chalk.red(` Remove ${file}`));
console.log(`\nTotal: ${totalChanges} changes`);
if(opts.hasOwnProperty('dry') && opts.dry) return;
let bar = new progress.Bar();
bar.start(totalChanges, 0);
let finishedCount = 0;
for (let file of createList) {
await client.put(file, file);
finishedCount++;
bar.update(finishedCount);
}
for (let file of modifyList) {
await client.put(file, file);
finishedCount++;
bar.update(finishedCount);
}
for (let file of removeList) {
await client.delete(file);
finishedCount++;
bar.update(finishedCount);
}
bar.stop();
let fileList = JSON.stringify(localFiles, null, 4);
let s = new Readable();
s.push(fileList);
s.push(null);
console.log(`Commit oss://${config.bucket}/.ossyncfiles`);
await client.putStream('.ossyncfiles', s);
console.log(`Your workspace is up to date!`);
}
async function getOssFiles(ig) {
let map = {};
try {
let tmpMap = await client.get('.ossyncfiles');
tmpMap = JSON.parse(tmpMap.res.data.toString());
for (let file in tmpMap) {
if (tmpMap.hasOwnProperty(file)) {
if (!ig.ignores(file)) {
map[file] = tmpMap[file];
}
}
}
} catch (e) {
}
return map;
}
async function getLocalFiles(ig) {
let map = {};
let files = readDirRecursively(cwd, ig);
files = files.map(file => file.substr(cwd.length + 1));
for (let file of files) {
map[file] = (await fileHash(file)).toUpperCase();
}
return map;
}
function readDirRecursively(dir, ig) {
let files = [];
fs.readdirSync(dir).forEach(file => {
let fullPath = dir + '/' + file;
if (!ig.ignores(fullPath.substr(cwd.length + 1))) {
if (fs.lstatSync(fullPath).isDirectory()) {
files = files.concat(readDirRecursively(fullPath, ig));
} else {
files.push(fullPath);
}
}
});
return files;
}
async function fileHash(file) {
return new Promise((resolve, reject) => {
let rs = fs.createReadStream(file);
let hash = xxHash.h32('ossync');
rs.on('error', e => {
reject(e);
});
rs.on('data', data => {
hash.update(data)
});
rs.on('end', () => {
resolve(hash.digest().toString(16));
});
});
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。