Skip to content

Commit

Permalink
feat: major update with spinners and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Jan 10, 2021
1 parent 35175c9 commit f0af1f1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gource generator for github organization.
## Requirements
- [Node.js](https://nodejs.org/en/) v12 or higher
- [Gource](https://gource.io/)
- [Sed](https://fr.wikipedia.org/wiki/Stream_Editor) version 4 or higher on your local system.

> ⚠️ gource.cmd must be available in the Windows path
Expand Down
78 changes: 52 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const cp = require("child_process");
const { fetch } = require("fetch-github-repositories");
const http = require("isomorphic-git/http/node");
const git = require("isomorphic-git");
const ms = require("ms");
const kleur = require("kleur");
const Locker = require("@slimio/lock");
const Spinner = require("@slimio/async-cli-spinner");

// Require Internal Dependencies
const { traverseProjectJSON } = require("./src/utils");
Expand All @@ -23,8 +27,10 @@ const GITHUB_ORGA = typeof process.env.GITHUB_ORGA === "string" ? process.env.GI
const GITHUB_KIND = typeof process.env.GITHUB_KIND === "string" ? process.env.GITHUB_KIND : "orgs";
const EXCLUDED = new Set(["blog"]);
const HISTORY_DIR = join(__dirname, "history");
const kCloneLocker = new Locker({ maxConcurrent: 10 });

// Global
// Spinner.DEFAULT_SPINNER = "dots";
const token = process.env.GIT_TOKEN;
const mapper = new Map();
const exec = promisify(cp.exec);
Expand All @@ -38,32 +44,45 @@ const exec = promisify(cp.exec);
* @returns {Promise<void>}
*/
async function cloneRep(orgaName, repName) {
const dir = join(__dirname, "clones", repName);
const url = `https://github.com/${orgaName}/${repName}`;

console.log(`Cloning: ${url}`);
await git.clone({
fs, http, dir, url,
onAuth() {
return { username: process.env.GIT_TOKEN, password: "x-oauth-basic" };
},
singleBranch: true,
noCheckout: true,
corsProxy: "https://cors.isomorphic-git.org"
});

const prefix = mapper.has(repName) ? mapper.get(repName) : repName;
await exec(`gource --output-custom-log history/${repName}.txt clones/${repName}`);
await exec(`sed -i -r "s#(.+)\\|#\\1|/${prefix}#" history/${repName}.txt`);
}
const free = await kCloneLocker.acquireOne();
const spin = new Spinner({
prefixText: kleur.cyan().bold(`${repName}`)
}).start();

/**
* @function cloneError
* @param {!any} error error
* @returns {void}
*/
function cloneError(error) {
console.error(error);
try {
const dir = join(__dirname, "clones", repName);
const url = `https://github.com/${orgaName}/${repName}`;

spin.text = kleur.white().bold(`Cloning git repo ${kleur.yellow().bold(url)}`);
await git.clone({
fs, http, dir, url,
onAuth() {
return { username: process.env.GIT_TOKEN, password: "x-oauth-basic" };
},
singleBranch: true,
noCheckout: true,
corsProxy: "https://cors.isomorphic-git.org"
});

const prefix = mapper.has(repName) ? mapper.get(repName) : repName;

spin.text = kleur.white().bold("Execute gource command");
await exec(`gource --output-custom-log history/${repName}.txt clones/${repName}`);

spin.text = kleur.white().bold("Execute sed command");
await exec(`sed -i -r "s#(.+)\\|#\\1|/${prefix}#" history/${repName}.txt`);

const executionTime = kleur.cyan().bold(ms(Number(spin.elapsedTime.toFixed(2))));
spin.succeed(kleur.green().bold(`Successfully managed in ${executionTime}`));
}
catch (err) {
spin.failed(kleur.bold().red(err.message));

throw err;
}
finally {
free();
}
}

/**
Expand All @@ -85,7 +104,10 @@ async function getAllRepo(orgaName, kind = "orgs") {
.map((repo) => cloneRep(orgaName, repo.name))
);

cloneResults.filter((result) => result.status === "rejected").forEach(cloneError);
console.log(" > Show clone errors: ");
cloneResults
.filter((result) => result.status === "rejected")
.forEach((error) => console.log(error?.message));
}

/**
Expand All @@ -94,6 +116,7 @@ async function getAllRepo(orgaName, kind = "orgs") {
* @returns {Promise<void>}
*/
async function main() {
console.log(" > Loading configuration.");
try {
const buffer = await readFile(join(__dirname, "src", "config", `${GITHUB_ORGA}.json`));
const mappingJSON = JSON.parse(buffer.toString());
Expand All @@ -108,9 +131,11 @@ async function main() {
}

// Create history/combined
console.log(" > Creating history and combined directory!");
await mkdir(join(HISTORY_DIR, "combined"), { recursive: true });

// Cleanup history dir
console.log(" > Cleaning /history dir from previous run");
try {
const files = await readdir(HISTORY_DIR);
await Promise.allSettled(
Expand All @@ -125,6 +150,7 @@ async function main() {
try {
await getAllRepo(GITHUB_ORGA, GITHUB_KIND);

console.log(" > Processing combined history before executing gource!");
await exec("cat history/*.txt | sort > history/combined/fullLog.txt");
const child = cp.spawn("gource.cmd", ["history/combined/fullLog.txt", "-s", "0.2"]);
await new Promise((resolve, reject) => {
Expand Down
64 changes: 47 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
},
"homepage": "https://github.com/SlimIO/Gource-view#readme",
"dependencies": {
"@slimio/async-cli-spinner": "^0.5.2",
"@slimio/is": "^1.5.1",
"@slimio/lock": "^0.5.0",
"dotenv": "^8.2.0",
"fetch-github-repositories": "^2.0.0",
"isomorphic-git": "^1.8.0",
"make-promises-safe": "^5.1.0"
"kleur": "^4.1.3",
"make-promises-safe": "^5.1.0",
"ms": "^2.1.3"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down

0 comments on commit f0af1f1

Please sign in to comment.