nxkit
Version:
This is a collection of tools, independent of any other libraries
92 lines (91 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("./fs");
const path = require("path");
const arguments_1 = require("./arguments");
const syscall_1 = require("./syscall");
arguments_1.defOpts('help', 0, '--help, --help print help info');
arguments_1.defOpts('u', 'louis', '-u username [{0}]');
arguments_1.defOpts('h', '192.168.0.115', '-h host [{0}]');
arguments_1.defOpts('t', '~/ngui', '-t target directory [{0}]');
arguments_1.defOpts('i', '', '-i ignore directory or file');
arguments_1.defOpts('d', 0, '-d delay time [{0}] watch');
if (arguments_1.options.help) {
console.log(' ', arguments_1.helpInfo.join('\n '));
process.exit(0);
}
// console.log('-----------------------------', options.i)
var root = process.cwd();
var target = `${arguments_1.options.u}@${arguments_1.options.h}:${arguments_1.options.t}`;
console.log(`watch ${root} ${target}`);
var ignore = ['.git', '.svn', 'out', 'node/deps', 'tools/android-toolchain', 'node_modules', '.o', '.a', '.d'];
var count = 1;
if (arguments_1.options.i) {
if (Array.isArray(arguments_1.options.i)) {
ignore = ignore.concat(arguments_1.options.i);
}
else {
ignore.push(arguments_1.options.i);
}
}
function each_directory(root, dir, cb) {
fs.readdirSync(root + '/' + dir).forEach(name => {
var pathname = dir + (dir ? '/' : '') + name;
var stat;
try {
stat = fs.lstatSync(root + '/' + pathname);
}
catch (e) {
console.error(e.message);
return;
}
if (!stat.isSymbolicLink()) {
var ext = path.extname(pathname);
if (stat.isDirectory()) {
if (cb(pathname, name, ext, true)) {
each_directory(root, pathname, cb);
}
}
else {
var name = pathname.substring(0, pathname.length - ext.length);
cb(pathname, name, ext, false);
}
}
});
}
function sync(type, dir, filename) {
if (ignore.indexOf(filename) != -1)
return;
console.log('sync', type, dir, filename, '...');
var cmd = `scp ${root}/${dir}/${filename} ${target}/${dir}`;
// console.log(cmd);
var r = syscall_1.execSync(cmd);
console.log('sync', type, dir, filename, r.code == 0 ? 'ok' : 'fail');
}
function start() {
fs.watch(root, (type, filename) => sync(type, '.', filename));
each_directory(root, '', function (pathname, name, ext, is_dir) {
if (is_dir) {
if (ignore.indexOf(name) >= 0 ||
ignore.indexOf(pathname) >= 0 ||
ignore.indexOf(ext) >= 0) {
// console.log(pathname);
return false;
}
// console.log('-------------', root + '/' + pathname)
fs.watch(root + '/' + pathname, (type, filename) => sync(type, pathname, filename));
count++;
return true;
}
return false;
});
// sudo ulimit -HSn 12000
syscall_1.execSync(`cd ${root}; git status -s | awk '{print $2}'`).stdout.forEach(e => {
if (e) {
sync('init', path.dirname(e), path.basename(e));
}
});
console.log(`---------------- Start watch dir count ${count} ... ---------------- `);
}
setTimeout(start, Number(arguments_1.options.d) || 0);