diff options
author | Minteck <contact@minteck.org> | 2023-01-22 09:12:46 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2023-01-22 09:12:46 +0100 |
commit | 8a7bba848fd392e5afcc85d285dac613e393a6e2 (patch) | |
tree | 6b56a63388568b10fa08199b0085e34f4727a741 | |
parent | 2a6a2b3a7c906eba1964f9ef714089fb69c32515 (diff) | |
download | autopush-8a7bba848fd392e5afcc85d285dac613e393a6e2.tar.gz autopush-8a7bba848fd392e5afcc85d285dac613e393a6e2.tar.bz2 autopush-8a7bba848fd392e5afcc85d285dac613e393a6e2.zip |
Updated autopush (automated)
-rwxr-xr-x | autopush | 315 |
1 files changed, 169 insertions, 146 deletions
@@ -1,17 +1,35 @@ #!/usr/bin/env node +const child_process = require('node:child_process'); + function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } +function exec(name, args, options) { + return new Promise((res, rej) => { + try { + child_process.execFile(name, args, options ?? {}, (error, stdout, stderr) => { + if (error) { + rej(error); + return; + } + + res(stdout, stderr); + }) + } catch (e) { + rej(e); + } + }) +} + (async () => { - const version = "1.0"; + const version = "1.1"; const ora = (await import("ora")).default; const chalk = require('chalk'); const prompts = require('prompts'); const fs = require('node:fs'); - const child_process = require('node:child_process'); console.log("autopush v" + version); console.log("(c) Equestria.dev Developers"); @@ -39,176 +57,181 @@ function capitalizeFirstLetter(string) { let spinner = ora(chalk.gray("[" + file + "] ") + "Initialising...").start(); let root = fs.realpathSync("./" + file); - if (fs.existsSync(root + "/.git")) { - 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]; + try { + if (fs.existsSync(root + "/.git")) { + spinner.start(chalk.gray("[" + file + "] ") + "Adding files..."); + await exec("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; + } - if (firstPart) { - firstPart = false; - parts.push(capitalizeFirstLetter(action.name) + " " + fileName); + 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 { - parts.push(action.name + " " + fileName); + 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; + 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; - } + for (let part of parts) { + if (index === 0) { + message += part; + } else if (index >= parts.length - 1) { + message += " and " + part; + } else { + message += ", " + part; + } - index++; - } + index++; + } - message += " (automated)"; + message += " (automated)"; - spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; - child_process.execFileSync("git", ["commit", "-m", message], { cwd: root }); + spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; + await exec("git", ["commit", "-m", message], { cwd: root }); - spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; - child_process.execFileSync("git", ["push", "--all", "--force", "origin"], { cwd: root }); + spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; + await exec("git", ["push", "--all", "--force", "origin"], { cwd: root }); - spinner.succeed(chalk.gray("[" + file + "] ") + "Completed"); - } - } else { - spinner.stop(); - - let completed = false; - - while (!completed) { - let response = null; - - while (!response || Object.keys(response).length === 0) { - response = await prompts({ - type: 'select', - name: 'value', - hint: 'What should be done?', - message: chalk.bold(file) + " does not contain a Git repository", - 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 - }); + spinner.succeed(chalk.gray("[" + file + "] ") + "Completed"); } + } else { + spinner.stop(); + + let completed = false; + + while (!completed) { + let response = null; + + while (!response || Object.keys(response).length === 0) { + response = await prompts({ + type: 'select', + name: 'value', + hint: 'What should be done?', + message: chalk.bold(file) + " does not contain a Git repository", + 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 + }); + } - let response2 = null; - - switch (response.value) { - case "ignore": - completed = true; - 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({ - type: 'text', - name: 'value', - message: 'Enter the URL to the Git repository' - }); - } + let response2 = null; - try { - spinner.start(chalk.gray("[" + file + "] ") + "Initialising repository..."); - child_process.execFileSync("git", ["init"], { cwd: root }); + switch (response.value) { + case "ignore": + completed = true; + spinner.fail(chalk.gray("[" + file + "] ") + "No Git repository"); + break; - spinner.text = chalk.gray("[" + file + "] ") + "Adding files..."; - child_process.execFileSync("git", ["add", "-A"], { cwd: root }); + 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({ + type: 'text', + name: 'value', + message: 'Enter the URL to the Git repository' + }); + } - spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; - child_process.execFileSync("git", ["commit", "-m", "Initial commit"], { cwd: root }); + try { + spinner.start(chalk.gray("[" + file + "] ") + "Initialising repository..."); + await exec("git", ["init"], { 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 + "] ") + "Adding files..."; + await exec("git", ["add", "-A"], { cwd: root }); - spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; - child_process.execFileSync("git", ["push", "--all", "origin"], { cwd: root }); - completed = true; - } catch (e) { - spinner.fail(chalk.gray("[" + file + "] ") + e.message); - completed = true; - } + spinner.text = chalk.gray("[" + file + "] ") + "Making commit..."; + await exec("git", ["commit", "-m", "Initial commit"], { cwd: root }); - break; + spinner.text = chalk.gray("[" + file + "] ") + "Adding remote..."; + await exec("git", ["remote", "add", "origin", response2.value], { cwd: root }); - case "delete": - while (!response2 || Object.keys(response2).length === 0) { - response2 = await prompts({ - type: 'confirm', - name: 'value', - message: "Do you really want to delete this project permanently? This cannot be undone.", - initial: false - }); - } + spinner.text = chalk.gray("[" + file + "] ") + "Pushing to remote..."; + await exec("git", ["push", "--all", "origin"], { cwd: root }); + completed = true; + } catch (e) { + spinner.fail(chalk.gray("[" + file + "] ") + e.message); + completed = true; + } - if (response2.value) { - completed = true; - fs.rmSync(root, { recursive: true }); - spinner.warn(chalk.gray("[" + file + "] ") + "Deleted project"); - } + break; - break; + case "delete": + while (!response2 || Object.keys(response2).length === 0) { + response2 = await prompts({ + type: 'confirm', + name: 'value', + message: "Do you really want to delete this project permanently? This cannot be undone.", + initial: false + }); + } - case "quit": - completed = true; - spinner.fail(chalk.gray("[" + file + "] ") + "No Git repository"); - process.exit(1); + if (response2.value) { + completed = true; + fs.rmSync(root, { recursive: true }); + spinner.warn(chalk.gray("[" + file + "] ") + "Deleted project"); + } + + break; + + case "quit": + completed = true; + spinner.fail(chalk.gray("[" + file + "] ") + "No Git repository"); + process.exit(1); + } } } + } catch (e) { + spinner.fail(chalk.gray("[" + file + "] ") + e.message.split("\n")[0]); + process.exit(1); } } |