Created
January 31, 2020 11:22
-
-
Save peterbe/4ba3efb1693ac4c634cf605b9cf84a40 to your computer and use it in GitHub Desktop.
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"); | |
var childProcess = require("child_process"); | |
function run(cwd) { | |
const cmd = "git"; | |
const args = ["log", "--pretty=format:metadata:%aD,%an,%ae", "--name-only"]; | |
const perFile = {}; | |
const lastModified = {}; | |
// const lastModified = new Set(); | |
function add(files, metadata) { | |
if (!metadata) { | |
console.log({ files, metadata }); | |
throw new Error("!"); | |
} | |
for (let file of files) { | |
if (!(file in lastModified)) { | |
lastModified[file] = metadata.date; | |
} | |
// if (!(file in perFile)) { | |
// perFile[file] = []; | |
// } | |
// perFile[file].push(metadata); | |
} | |
// files.length = 0; // empty the array trick | |
} | |
var spawned = childProcess.spawnSync(cmd, args, { | |
cwd, | |
maxBuffer: 1024 * 1024 * 10 // 10 MB, good enough?? | |
}); | |
if (spawned.error) { | |
console.error("crap!!"); | |
console.log(spawned.error); | |
} else { | |
console.log("done!"); | |
let metadata = null; | |
const files = []; | |
spawned.stdout | |
.toString() | |
.split("\n") | |
.forEach(line => { | |
// console.log(line); | |
if (line.startsWith("metadata:")) { | |
if (files.length) { | |
add(files, metadata); | |
} | |
metadata = { | |
date: line.split(",")[1].trim() | |
// author: { | |
// name: line.split(",")[2], | |
// email: line.split(",")[3] | |
// } | |
}; | |
} else if (line) { | |
files.push(line); | |
} | |
}); | |
if (files.length) { | |
add(files, metadata); | |
} | |
//for (let line of spawned.stdout.toString().split('\n')) { | |
// console.log(i, line); | |
//} | |
} | |
// Object.entries(perFile).forEach(([file, metadatas]) => { | |
// lastModified[file] = metadatas.date; | |
// }); | |
// console.log(JSON.stringify(perFile, null, 2)); | |
// console.log(JSON.stringify(lastModified, null, 2)); | |
fs.writeFileSync( | |
"lastmodified.json", | |
JSON.stringify(lastModified, null, 2), | |
"utf8" | |
); | |
console.log(`${Object.keys(lastModified).length.toLocaleString()} keys`); | |
} | |
run("/Users/peterbe/dev/MOZILLA/MDN/kuma"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment