diff options
-rw-r--r-- | .idea/vcs.xml | 6 | ||||
-rwxr-xr-x | autopush | 102 |
2 files changed, 106 insertions, 2 deletions
diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project>
\ No newline at end of file @@ -1,5 +1,9 @@ #!/usr/bin/env node +function capitalizeFirstLetter(string) { + return string.charAt(0).toUpperCase() + string.slice(1); +} + (async () => { const version = "1.0"; @@ -13,6 +17,9 @@ console.log("(c) Equestria.dev Developers"); console.log(""); + let archives = false; + if (fs.existsSync("./._")) archives = true; + let list = fs.readdirSync(".").filter(i => { try { let isDotFile = i.startsWith(".") @@ -33,7 +40,80 @@ let root = fs.realpathSync("./" + file); if (fs.existsSync(root + "/.git")) { - spinner.succeed(chalk.gray("[" + file + "] ") + "Completed"); + spinner.start(chalk.gray("[" + file + "] ") + "Adding files..."); + child_process.execFileSync("git", ["add", "-A"], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Generating commit message..."; + let changes = child_process.execSync("git status --porcelain", {cwd: root}).toString().trim(); + + if (changes === "") { + spinner.succeed(chalk.gray("[" + file + "] ") + "No changes since last commit"); + } else { + let changed = changes.split("\n").map(i => i.trim().replace(/ +/g, " ").split(" ")[0].substring(0, 1)); + let amounts = {}; + + for (let change of changed) { + if (!amounts[change]) amounts[change] = 0; + amounts[change] += 1; + } + + let message = ""; + let parts = []; + let firstPart = true; + + for (let action of [ + { code: "M", name: "updated" }, + { code: "T", name: "changed type for" }, + { code: "A", name: "added" }, + { code: "D", name: "deleted" }, + { code: "R", name: "renamed" }, + { code: "C", name: "copied" }, + ]) { + if (amounts[action.code]) { + if (amounts[action.code] > 1) { + if (firstPart) { + firstPart = false; + parts.push(capitalizeFirstLetter(action.name) + " " + amounts[action.code] + " files"); + } else { + parts.push(action.name + " " + amounts[action.code] + " files"); + } + } else { + let fileName = changes.split("\n").filter(i => i.startsWith(action.code))[0].trim().replace(/ +/g, " ").split(" ")[1]; + + if (firstPart) { + firstPart = false; + parts.push(capitalizeFirstLetter(action.name) + " " + fileName); + } else { + parts.push(action.name + " " + fileName); + } + } + } + } + + let index = 0; + + for (let part of parts) { + if (index === 0) { + message += part; + } else if (index >= parts.length - 1) { + message += " and " + part; + } else { + message += ", " + part; + } + + index++; + } + + message += " (automated)"; + + spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; + child_process.execFileSync("git", ["commit", "-m", message], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; + child_process.execFileSync("git", ["push", "--all", "origin"], { cwd: root }); + + spinner.succeed(chalk.gray("[" + file + "] ") + "Completed"); + } } else { spinner.stop(); @@ -51,9 +131,10 @@ choices: [ { title: "Ignore this project", value: "ignore" }, { title: "Create a new repository", value: "create" }, + archives ? { title: "Move project to archive", value: "archive" } : null, { title: "Delete this project", value: "delete" }, { title: "Quit Autopush", value: "quit" }, - ], + ].filter(i => i), initial: 0 }); } @@ -66,6 +147,12 @@ spinner.fail(chalk.gray("[" + file + "] ") + "No Git repository"); break; + case "archive": + fs.renameSync("./" + file, "./._/" + file); + completed = true; + spinner.warn(chalk.gray("[" + file + "] ") + "Moved to archive"); + break; + case "create": while (!response2 || Object.keys(response2).length === 0) { response2 = await prompts({ @@ -76,10 +163,19 @@ } try { + spinner.start(chalk.gray("[" + file + "] ") + "Initialising repository..."); child_process.execFileSync("git", ["init"], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Adding files..."; child_process.execFileSync("git", ["add", "-A"], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; child_process.execFileSync("git", ["commit", "-m", "Initial commit"], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Adding remote..."; child_process.execFileSync("git", ["remote", "add", "origin", response2.value], { cwd: root }); + + spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; child_process.execFileSync("git", ["push", "--all", "origin"], { cwd: root }); completed = true; } catch (e) { @@ -115,4 +211,6 @@ } } } + + console.log(""); })();
\ No newline at end of file |