WIP
This commit is contained in:
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import * as YAML from "js-yaml"
|
|||||||
import { _ } from "lodash";
|
import { _ } from "lodash";
|
||||||
import { jjLog, jjLogAction, jjLogError } from "./jjLogger";
|
import { jjLog, jjLogAction, jjLogError } from "./jjLogger";
|
||||||
import { saveFileIfItChanged } from "./saveFileIfItChanged";
|
import { saveFileIfItChanged } from "./saveFileIfItChanged";
|
||||||
|
import { gitUserEmail, gitUserName, jjUserName } from "./userUtils";
|
||||||
|
|
||||||
export const jjConfigs: any = {
|
export const jjConfigs: any = {
|
||||||
processEnvs: {}
|
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`)
|
saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.tmpDir}/jjConfigs.json`)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -76,7 +76,7 @@ eval $($JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs)
|
|||||||
|
|
||||||
source "$JJ_SOURCE_DIR/bashUtils/ensureDependency.sh"
|
source "$JJ_SOURCE_DIR/bashUtils/ensureDependency.sh"
|
||||||
|
|
||||||
|
echo $JJ_GIT_USER_KEY_NAME
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
"moment": "^2.30.1",
|
"moment": "^2.30.1",
|
||||||
"pino": "^9.5.0",
|
"pino": "^9.5.0",
|
||||||
"pino-pretty": "^12.1.0",
|
"pino-pretty": "^12.1.0",
|
||||||
|
"shelljs": "^0.8.5",
|
||||||
"winston": "^3.16.0"
|
"winston": "^3.16.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user