This commit is contained in:
2024-11-10 02:24:30 -05:00
parent 8ad4dd7182
commit 0fd330eaab
6 changed files with 137 additions and 1 deletions
+94
View File
@@ -0,0 +1,94 @@
import util from 'util';
import { exec, execSync } from 'child_process';
import shelljs from 'shelljs';
const newExec = util.promisify(exec);
export async function bashRunAndReturn({
command,
noError = false,
showOutput = false
}) {
try {
const { stdout, stderr } = await newExec(command);
if (stderr && !noError) {
throw new Error(stderr);
}
if (showOutput) {
console.log(stdout);
}
// remove last new line from output
return stdout.replace(/\n$/, '');;
}
catch (err) {
throw err;
};
}
// Synchronous version of bashRunAndReturn
export function bashRunAndReturnSync({ command }) {
try {
const result = execSync(command, { encoding: 'utf-8' }).trim();
return result.replace(/\n$/, '');;
} catch (error) {
console.error(`Error executing command: ${command}`, error);
return null;
}
}
export async function bashRunAndShowLogsPromise({
command,
noError = false,
prefix = ''
}) {
await new Promise((resolve, reject) => {
const child = shelljs.exec(
command,
{
async: false,
silent: true
},
(code, stdout, stderr) => { }
);
child.stdout.on('data', function (data) {
if (data.includes(' SUCCESS: ')) {
if (prefix) {
console.log(prefix)
}
console.log(`Success ${data}`);
} else {
if (prefix) {
console.log(prefix)
}
console.log(data);
}
});
child.stderr.on('data', function (data) {
if (data.includes(' WARNING: ')) {
if (prefix) {
console.log(prefix)
}
console.warn(data);
} else {
if (prefix) {
console.log(prefix)
}
console.error(data);
if (!noError) {
reject()
}
}
});
child.on('close', function (data) {
resolve(data);
});
});
}
+27
View File
@@ -7,6 +7,7 @@ import * as YAML from "js-yaml"
import { _ } from "lodash";
import { jjLog, jjLogAction, jjLogError } from "./jjLogger";
import { saveFileIfItChanged } from "./saveFileIfItChanged";
import { gitUserEmail, gitUserName, jjUserName } from "./userUtils";
export const jjConfigs: any = {
processEnvs: {}
@@ -93,6 +94,32 @@ export const jjEnsure = async ({
}
jjConfigs.processEnvs.JJ_GIT_USER_EMAIL = gitUserEmail
jjConfigs.processEnvs.JJ_GIT_USER_NAME = gitUserName
jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME = jjUserName(jjConfigs.processEnvs.JJ_GIT_USER_EMAIL)
if (jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME && !jjConfigs.jjYamlObj.users[jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME]) {
jjConfigs.jjYamlObj.users[jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME] = {
name: jjConfigs.processEnvs.JJ_GIT_USER_NAME,
email: jjConfigs.processEnvs.JJ_GIT_USER_EMAIL,
}
}
fs.writeFileSync(
jjConfigs.jjYamlPath,
YAML.dump(
jjConfigs.jjYamlObj,
{
indent: 4,
lineWidth: 3,
forceQuotes: true,
// sortKeys: true
}
),
"utf8"
)
saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.tmpDir}/jjConfigs.json`)
+14
View File
@@ -0,0 +1,14 @@
import { bashRunAndReturnSync } from "./bashUtils";
export const gitUserEmail = bashRunAndReturnSync({
command: 'git config user.email'
})
export const gitUserName = bashRunAndReturnSync({
command: 'git config user.name'
})
export const jjUserName = (userName = gitUserEmail) => {
const result = userName.replace(/@/g, '__').replace(/\./g, '-')
return result
}