与nodejs的两个目录在两个方向上 我尝试了一切,无处不在,甚至尝试编写我自己的程序。我迷路了。 想法:具有相同文件的两个目录。将文件重命名为另一个文件。编辑...

问题描述 投票:0回答:1
/* Require Packages */ const $ = Object.assign.apply(0, ([ 'fs', 'path', 'readline' ]).map(a => ({ [a]: require(a) }))); /* Define Functions */ const _ = ({ sls: { index: {}, listen: function (a, b) { _.sls.index[a] = b; }, trigger: function (a, b) { _.sls.index[a](b); } }, inquire: function (a, b) { let x = $.readline.createInterface({ input: process.stdin, output: process.stdout }); x.question(a, c => _.sls.trigger('question', c)); _.sls.listen('question'); return _.sls.listen('question', b); }, walk: function (a) { return $.fs.statSync(a).isDirectory() ? $.fs.readdirSync(a).map(b => _.walk($.path.join(a, b))) : a; }, regex: function (a, b) { return new RegExp(a.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').split('*').join(b)); }, match: function (a, b, c) { let x = $.path.parse(a), y = null, z = false; for (let i = 0; i < b.length; ++b) { y = $.path.parse(c + b[i].context).dir.split('/'); for (let j = 0; j < x.dir.split('/').length; ++j) { if (_.regex(y[j], '.').test(x.dir.split('/')[j])) { for (let k = 0; k < b[i].match.length; ++k) { if (b.type == 'regex') { z = new RegExp(b[i].match[k]).test(x.base); } else { z = _.regex(b[i].match[k], '.*').test(x.base); if (b.type == 'exclude') z = !z; } } } } } return z; }, watch: function (a, b) { for (let i of b) $.fs.watch($.path.format({ dir: $.path.parse($.path.normalize(i) + $.path.normalize(a).split($.path.normalize(b[0])).join('')).dir, base: $.path.parse(a).base }), {}, function (a, b) { console.log(b); }); } }); /* Execute Script */ $.fs.readFile('sync.json', function (a, b) { if (a) { console.log('No configuration file was found. One is being generated now.'); $.fs.writeFile('sync.json', JSON.stringify({ targets: [ 'C:/sync-this-directory', 'C:/and-this-one', 'D:/and-this-one' ], patterns: [{ type: 'include', context: '/include-matches-only-in-this-directory', match: ['*.*', 'by default all files are excluded'] }, { type: 'exclude', context: '/exclude-matches-only-in-the-subdirectories-of-this-directory/*', match: ['*.exclude_this_extension', 'exclude_this_filename.*'] }, { type: 'include', context: '/*', match: ['(a,b,c,d)', 'would match any file with a, b, c, and d in the name'] }, { type: 'include', context: '/*', match: ['[1,2,3,4].*', 'would match any file with 1, 2, 3, or 4 in the name'] }, { type: 'include', context: '/*', match: ['{1,b,3,d}.*', 'would match any file with ONLY 1, b, 3, or d in the name'] }, { type: 'include', context: '/*', match: ['these-characters{([1,a],[2,b]),([3,c],[4,d])}', 'patterns are checked in entry order'] }, { type: 'regex', context: '/*', match: ['these matches are regex-enabled', 'all matches will be included, not excluded'] }] })); } else { _.inquire('Once you have ensured all configured target directories and their contents match, press enter.', function () { let x = null; try { x = JSON.parse(String(b).split('/').join('\/')); } catch (g) { console.log('Parsing Error: Your config is fucked. Fix it.'); throw new Error(); } console.log('Your files are now in sync. A change to one is a change to all.'); let y = Object.assign.apply(0, _.walk(x.targets[0]).map(function (a) { return a.constructor.name == 'Array' ? a : [a]; })); for (let i of y) if (_.match(i, x.patterns, x.targets[0])) _.watch(i, x.targets); }); } });

您可以使用node.js的

watch

API来做到这一点,但是它可能比使用非JS解决方案更麻烦。像

rsync

一样,可能会提供您需要的所有功能,同时也是最简单的解决方案之一。它很容易在大多数Linux发行版中获得。对于Windows,您可以使用

cygwin
之类的东西,以便使用posix shell(和命令比在其上运行)。
    
javascript node.js synchronization filesystems
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.