Created
October 10, 2019 06:18
-
-
Save tohokuaiki/b29a8f0069cdc5ad6a49afc2a8cbfd18 to your computer and use it in GitHub Desktop.
2つのディレクトリから差分ファイルをdiffして見つける。 $node make_difffiles.js --old old_dir --new new_dir --dest difffiles_destdir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const glob = require('glob'); | |
const slash = require('slash'); | |
var minimist = require("minimist"); | |
var nowString = ()=> { | |
var d = new Date(), | |
month = '' + (d.getMonth() + 1), | |
day = '' + d.getDate(), | |
year = d.getFullYear(); | |
if (month.length < 2) | |
month = '0' + month; | |
if (day.length < 2) | |
day = '0' + day; | |
let h = '' + d.getHours(); | |
if (h.length < 2) h = '0' + h; | |
let i = '' + d.getMinutes(); | |
if (i.length < 2) i = '0' + i; | |
let s = '' + d.getSeconds(); | |
if (s.length < 2) s = '0' + s; | |
return [year, month, day].join('') + '_' + [h, i, s].join('');; | |
}; | |
var now = nowString(); | |
var options = minimist(process.argv.slice(2), { | |
string: ['dest', 'old', 'new'], | |
default: { dest: now, old: '', new: 'htdocs' } | |
}); | |
if (!options.dest){ | |
options.dest = now; | |
} | |
var newdir = slash(path.join(process.cwd(), options.new)), | |
olddir = slash(path.join(process.cwd(), options.old)); | |
// directory check | |
try { | |
[newdir, olddir].forEach((dir, i) => { | |
if (!fs.statSync(dir).isDirectory()){ | |
throw new Error(newdir + 'はディレクトリではありません。'); | |
} | |
}); | |
} | |
catch(e){ | |
console.log(e.message); | |
return; | |
} | |
var newfiles = glob.sync(path.join(newdir, '**/*')).map((path)=>{ | |
return path.replace(newdir, "").replace(/^\//,""); | |
}); | |
var oldfiles = glob.sync(path.join(olddir, '**/*')).map((path)=>{ | |
return path.replace(olddir, "").replace(/^\//,""); | |
}); | |
var files = { | |
deleted: [], updated: [] | |
}; | |
for (var i=0,j=oldfiles.length; i<j; i++){ | |
if (newfiles.indexOf(oldfiles[i]) < 0){ | |
files.deleted.push(oldfiles[i]); | |
console.log('deleted: ' + oldfiles[i]); | |
} | |
} | |
for (var i=0,j=newfiles.length; i<j; i++){ | |
var fullpath = path.join(newdir, newfiles[i]); | |
if (fs.statSync(fullpath).isDirectory() === false){ | |
if (oldfiles.indexOf(newfiles[i]) < 0){ | |
files.updated.push(newfiles[i]); | |
console.log('added: ' + newfiles[i]); | |
} | |
else { | |
if (fs.readFileSync(fullpath).toString() !== | |
fs.readFileSync(path.join(olddir, newfiles[i])).toString()){ | |
files.updated.push(newfiles[i]); | |
console.log('updated: ' + newfiles[i]); | |
} | |
} | |
} | |
} | |
// create files | |
var destdir = path.join(process.cwd(), "diff", options.dest), | |
delfile = path.join(destdir, 'deleted_files.txt'); | |
fs.mkdirSync(destdir, { recursive: true }); | |
if (files.deleted.length > 0){ | |
fs.writeFile(delfile, files.deleted.join("\n"), (err)=>{ | |
}); | |
} | |
for (var i=0,j=files.updated.length; i<j; i++){ | |
var copyFrom = path.join(newdir,files.updated[i]); | |
var copyTo = path.join(destdir,files.updated[i]); | |
var copyToDir = path.dirname(copyTo); | |
try { | |
fs.accessSync(copyToDir); | |
} catch(e){ | |
console.log("mkdir: "+copyToDir ); | |
fs.mkdirSync(copyToDir, { recursive: true }); | |
} | |
console.log("copy: " + copyFrom + " => " + copyTo); | |
fs.copyFileSync(copyFrom, copyTo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment