diff --git a/bun.lockb b/bun.lockb index 87cadbe..91b7960 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/helpers/bashUtils.ts b/helpers/bashUtils.ts new file mode 100644 index 0000000..2c22528 --- /dev/null +++ b/helpers/bashUtils.ts @@ -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); + }); + }); + +} \ No newline at end of file diff --git a/helpers/jjEnsure.ts b/helpers/jjEnsure.ts index a77621d..ee5dc31 100755 --- a/helpers/jjEnsure.ts +++ b/helpers/jjEnsure.ts @@ -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`) diff --git a/helpers/userUtils.ts b/helpers/userUtils.ts new file mode 100644 index 0000000..b2432b7 --- /dev/null +++ b/helpers/userUtils.ts @@ -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 +} \ No newline at end of file diff --git a/jj.sh b/jj.sh index 4c06fd1..019268c 100755 --- a/jj.sh +++ b/jj.sh @@ -76,7 +76,7 @@ eval $($JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs) source "$JJ_SOURCE_DIR/bashUtils/ensureDependency.sh" - +echo $JJ_GIT_USER_KEY_NAME diff --git a/package.json b/package.json index 8d362f8..8c1adc9 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "moment": "^2.30.1", "pino": "^9.5.0", "pino-pretty": "^12.1.0", + "shelljs": "^0.8.5", "winston": "^3.16.0" }, "devDependencies": {