This commit is contained in:
2026-10-10 22:18:04 -04:00
parent cca893b58d
commit 1654e0f867
6 changed files with 110 additions and 50 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ if [[ -e $ASDF_DIR/asdf.sh ]]; then
source $ASDF_DIR/asdf.sh source $ASDF_DIR/asdf.sh
fi fi
# rm -rf $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed
if ! hash asdf || \ if ! hash asdf || \
[[ ! -e $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed ]] \ [[ ! -e $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed ]] \
|| jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed" ; then || jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed" ; then
BIN
View File
Binary file not shown.
+36 -19
View File
@@ -9,25 +9,40 @@ import { saveFileIfItChanged } from "@jjHelpers/saveFileIfItChanged";
import * as YAML from "js-yaml"; import * as YAML from "js-yaml";
import { subRepositoryClean, subRepositoryPull, subRepositoryPushChanges } from "@jjHelpers/gitSubRepoUtils"; import { subRepositoryClean, subRepositoryPull, subRepositoryPushChanges } from "@jjHelpers/gitSubRepoUtils";
import moment from "moment"; import moment from "moment";
import inquirer from "inquirer";
import { bashRunAndReturnSync } from "@jjHelpers/bashUtils"; import { bashRunAndReturnSync } from "@jjHelpers/bashUtils";
const logger = makeJJlogger({ const logger = makeJJlogger({
filename: "jj/cli/secrets.ts", filename: "jj/cli/secrets.ts",
}); });
export const getSecretFolder = () => { // export const getSecretFolder = () => {
let secretsFolder = getJJConfigByKeyChain(`secretsFolder`); // let secretsFolder = getJJConfigByKeyChain(`secretsFolder`);
secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false); // secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false);
return secretsFolder; // return secretsFolder;
}; // };
export const getEncryptionKeyForEnv = (envKey) => { export const getEncryptionKeyForEnv = async (envKey) => {
const secretsFolder = getSecretFolder(); // const secretsFolder = getSecretFolder();
if (!fs.existsSync(`${secretsFolder}/~~${envKey}`)) { const secretsFolder = process.env['JJ_SECRET_TMP_DIR'];
logger.error(`Encryption key for ${envKey} env not fond and nt exist `); if (!secretsFolder) {
process.exit(1); throw new Error("JJ_SECRET_TMP_DIR is not set");
} }
const encryptionKey = fs.readFileSync(`${secretsFolder}/~~${envKey}`, "utf8"); const encryptionKeyPath = `${secretsFolder}/~~${envKey}`;
if (!fs.existsSync(encryptionKeyPath)) {
const { encryptionKey } = await inquirer.prompt([
{
type: "password",
name: "encryptionKey",
message: `Enter encryption key for ${envKey}:`,
mask: "*",
validate: (value) => value.trim().length > 0 || "Encryption key cannot be empty",
},
]);
await fs.ensureDir(secretsFolder);
await fs.writeFile(encryptionKeyPath, encryptionKey, { encoding: "utf8", mode: 0o600, flag: "wx" });
}
const encryptionKey = fs.readFileSync(encryptionKeyPath, "utf8");
return encryptionKey; return encryptionKey;
}; };
@@ -56,7 +71,9 @@ export const decryptFile = async (absFilePath) => {
} }
const absDirPath = path.dirname(absFilePath); const absDirPath = path.dirname(absFilePath);
const fileName = path.basename(absFilePath).replace(".json", ""); const fileName = path.basename(absFilePath).replace(".json", "");
const decryptFileJsonPath = `${absDirPath}/~~decrypt/${fileName}.json` const envKey = path.basename(path.join(absFilePath, ".."));
const decryptDir = `${process.env['JJ_SECRET_TMP_DIR']}/${envKey}/~~decrypt`
const decryptFileJsonPath = `${decryptDir}/${fileName}.json`
if (fs.existsSync(decryptFileJsonPath) ) { if (fs.existsSync(decryptFileJsonPath) ) {
const isOlderThanOriginal = await isFileOlder(decryptFileJsonPath, absFilePath) const isOlderThanOriginal = await isFileOlder(decryptFileJsonPath, absFilePath)
if(isOlderThanOriginal) { if(isOlderThanOriginal) {
@@ -70,10 +87,10 @@ export const decryptFile = async (absFilePath) => {
const secretFileObjRaw = await fs.readFile(absFilePath, "utf8"); const secretFileObjRaw = await fs.readFile(absFilePath, "utf8");
const secretObj = JSON.parse(secretFileObjRaw); const secretObj = JSON.parse(secretFileObjRaw);
const envKey = path.basename(path.join(absFilePath, ".."));
const decryptedSecretObj = {}; const decryptedSecretObj = {};
const encryptionKey = getEncryptionKeyForEnv(envKey); const encryptionKey = await getEncryptionKeyForEnv(envKey);
Object.keys(secretObj).forEach((key) => { Object.keys(secretObj).forEach((key) => {
const plainValue = secretObj[key]; const plainValue = secretObj[key];
if (plainValue.includes(`${envKey}--`)) { if (plainValue.includes(`${envKey}--`)) {
@@ -91,13 +108,13 @@ export const decryptFile = async (absFilePath) => {
if (!fs.existsSync(`${absDirPath}/~~decrypt`)) { if (!fs.existsSync(`${decryptDir}`)) {
fs.mkdirSync(`${absDirPath}/~~decrypt`, { recursive: true }); fs.mkdirSync(`${decryptDir}`, { recursive: true });
} }
const decryptedJsonFormat = `${absDirPath}/~~decrypt/${fileName}.json`; const decryptedJsonFormat = `${decryptDir}/${fileName}.json`;
fs.writeFileSync(decryptedJsonFormat, JSON.stringify(decryptedSecretObj, null, 4), "utf-8"); fs.writeFileSync(decryptedJsonFormat, JSON.stringify(decryptedSecretObj, null, 4), "utf-8");
const decryptedShFormat = `${absDirPath}/~~decrypt/${fileName}.sh`; const decryptedShFormat = `${decryptDir}/${fileName}.sh`;
fs.writeFileSync(decryptedShFormat, shFormatDecrypt, "utf-8"); fs.writeFileSync(decryptedShFormat, shFormatDecrypt, "utf-8");
logger.success(`decrypt file-secret for ${absFilePath} done`); logger.success(`decrypt file-secret for ${absFilePath} done`);
return decryptedSecretObj; return decryptedSecretObj;
@@ -124,7 +141,7 @@ export const setSecretFile = async ({ absFilePath, secretKey, secretValue }) =>
const envKey = path.basename(path.join(absFilePath, "..")); const envKey = path.basename(path.join(absFilePath, ".."));
const secretFileObjRaw = await fs.readFile(absFilePath, "utf8"); const secretFileObjRaw = await fs.readFile(absFilePath, "utf8");
const secretObj = JSON.parse(secretFileObjRaw); const secretObj = JSON.parse(secretFileObjRaw);
const encryptionKey = getEncryptionKeyForEnv(envKey); const encryptionKey = await getEncryptionKeyForEnv(envKey);
const encryptVal = jjEncrypt(secretValue, encryptionKey); const encryptVal = jjEncrypt(secretValue, encryptionKey);
secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`; secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`;
+23 -10
View File
@@ -28,6 +28,11 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => {
fs.mkdirSync(jjConfigs.JJ_TMP_DIR, { recursive: true }); fs.mkdirSync(jjConfigs.JJ_TMP_DIR, { recursive: true });
} }
if (!fs.existsSync(`${jjConfigs.JJ_TMP_DIR}/~~secrets`)) {
jjLog(`tmp folder ${`${jjConfigs.JJ_TMP_DIR}/~~secrets`} is not exist creating`);
fs.mkdirSync(`${jjConfigs.JJ_TMP_DIR}/~~secrets`, { recursive: true });
}
if (!fs.existsSync(jjConfigs.workspacesFilePath)) { if (!fs.existsSync(jjConfigs.workspacesFilePath)) {
await fs.writeFile(jjConfigs.workspacesFilePath, JSON.stringify([], null, 4), "utf8"); await fs.writeFile(jjConfigs.workspacesFilePath, JSON.stringify([], null, 4), "utf8");
} }
@@ -56,16 +61,22 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => {
} }
if (!jjConfigs.jjYamlPath) { if (!jjConfigs.jjYamlPath) {
console.log(chalk.magenta.bold(`please add it here in absolute path`));
const answer: any = await inquirer.prompt([ if (process.env['JJ_WORKSPACE_DIR_TMP']) {
{ jjConfigs.jjYamlPath = `${process.env['JJ_WORKSPACE_DIR_TMP']}/jj.yaml`;
type: "input", } else {
name: "jjYamlPath",
message: "", console.log(chalk.magenta.bold(`please add it here in absolute path`));
},
]); const answer: any = await inquirer.prompt([
jjConfigs.jjYamlPath = answer.jjYamlPath; {
type: "input",
name: "jjYamlPath",
message: "",
},
]);
jjConfigs.jjYamlPath = answer.jjYamlPath;
}
} }
if (!fs.existsSync(jjConfigs.jjYamlPath)) { if (!fs.existsSync(jjConfigs.jjYamlPath)) {
jjLogError(`can not find ${jjConfigs.jjYamlPath}`); jjLogError(`can not find ${jjConfigs.jjYamlPath}`);
@@ -104,6 +115,8 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => {
jjConfigs.processEnvs.JJ_DATA_DIR = jjConfigs.JJ_DATA_DIR; jjConfigs.processEnvs.JJ_DATA_DIR = jjConfigs.JJ_DATA_DIR;
jjConfigs.JJ_WORKSPACE_TMP_DIR = `${jjConfigs.JJ_WORKSPACE_DIR}/~~tmp`; jjConfigs.JJ_WORKSPACE_TMP_DIR = `${jjConfigs.JJ_WORKSPACE_DIR}/~~tmp`;
jjConfigs.processEnvs.JJ_WORKSPACE_TMP_DIR = jjConfigs.JJ_WORKSPACE_TMP_DIR; jjConfigs.processEnvs.JJ_WORKSPACE_TMP_DIR = jjConfigs.JJ_WORKSPACE_TMP_DIR;
jjConfigs.JJ_SECRET_TMP_DIR = `${jjConfigs.JJ_TMP_DIR}/~~secrets`;
jjConfigs.processEnvs.JJ_SECRET_TMP_DIR = jjConfigs.JJ_SECRET_TMP_DIR;
jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME = jjConfigs.jjYamlObj.name; jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME = jjConfigs.jjYamlObj.name;
jjConfigs.processEnvs.ASDF_DIR = `${process.env.JJ_DATA_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}`; jjConfigs.processEnvs.ASDF_DIR = `${process.env.JJ_DATA_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}`;
jjConfigs.processEnvs.ASDF_DATA_DIR = `${process.env.JJ_DATA_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}`; jjConfigs.processEnvs.ASDF_DATA_DIR = `${process.env.JJ_DATA_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}`;
@@ -113,7 +126,7 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => {
.map((key) => `${key} ${jjConfigs.jjYamlObj.asdf.tools[key].version}`) .map((key) => `${key} ${jjConfigs.jjYamlObj.asdf.tools[key].version}`)
.join("\n"); .join("\n");
saveFileIfItChanged(asdfToolsVersionFileContent, jjConfigs.processEnvs.ASDF_DEFAULT_TOOL_VERSIONS_FILENAME); saveFileIfItChanged(`${asdfToolsVersionFileContent}\n`, jjConfigs.processEnvs.ASDF_DEFAULT_TOOL_VERSIONS_FILENAME);
// export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="$GC_HOME_DIR/.tool-versions" // export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="$GC_HOME_DIR/.tool-versions"
jjConfigs.processEnvs.ASDF_VERSION = jjConfigs.jjYamlObj.asdf.version; jjConfigs.processEnvs.ASDF_VERSION = jjConfigs.jjYamlObj.asdf.version;
+47 -17
View File
@@ -1,6 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
MILLISECONDS_START=$(echo "$EPOCHREALTIME * 1000" | bc) MILLISECONDS_START=$(echo "$EPOCHREALTIME * 1000" | bc)
get_milliseconds() { get_milliseconds() {
@@ -21,11 +20,6 @@ trap '[[ "$?" == "1" ]] && echo -e "\033[1;31m[jj] ERROR !\033[0m" >&2' EXIT
# export JJ_DATA_DIR="$HOME/.jjDataDir" # export JJ_DATA_DIR="$HOME/.jjDataDir"
mkdir -p $JJ_DATA_DIR
if [[ -e ~/.cargo/bin/rustup ]]; then
export PATH="~/.cargo/bin:${PATH}"
fi
@@ -50,11 +44,38 @@ function findJJSource {
export JJ_SOURCE_DIR=$(findJJSource) export JJ_SOURCE_DIR=$(findJJSource)
if ! JJ_DATA_DIR=$(bun -e '
const fs = require("fs");
if [[ -e ~/.bun/bin/bun ]]; then const YAML = require(`${process.env.JJ_SOURCE_DIR}/node_modules/js-yaml`);
export PATH="~/.bun/bin:${PATH}" try {
const config = YAML.load(fs.readFileSync("jj.yaml", "utf8"));
if (typeof config?.dataFolder !== "string" || !config.dataFolder.trim()) {
throw new Error("jj.yaml must contain a non-empty dataFolder string");
}
process.stdout.write(config.dataFolder);
} catch (error) {
console.error(`[jj] ${error.message}`);
process.exit(1);
}
'); then
exit 1
fi fi
if [[ "$JJ_DATA_DIR" == "~" ]]; then
JJ_DATA_DIR="$HOME"
elif [[ "$JJ_DATA_DIR" == "~/"* ]]; then
JJ_DATA_DIR="$HOME/${JJ_DATA_DIR:2}"
fi
export JJ_DATA_DIR
mkdir -p "$JJ_DATA_DIR"
if [[ -e "jj.yaml" ]];then
export JJ_WORKSPACE_DIR_TMP=$(pwd)
fi
@@ -77,13 +98,13 @@ fi
__bunVersionExpect="1.3.9" # __bunVersionExpect="1.4.3"
__currentBunVersion=$(bun --version || true) # __currentBunVersion=$(bun --version || true)
if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then # if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then
rm -rf ~/.bun/bin/bun # rm -rf ~/.bun/bin/bun
curl -fsSL https://bun.sh/install | bash -s "bun-v$__bunVersionExpect" # curl -fsSL https://bun.sh/install | bash -s "bun-v$__bunVersionExpect"
export PATH="~/.bun/bin:${PATH}" # export PATH="~/.bun/bin:${PATH}"
fi # fi
mkdir -p "$JJ_DATA_DIR/~~tmp/deps" mkdir -p "$JJ_DATA_DIR/~~tmp/deps"
@@ -128,6 +149,10 @@ export JJ_WORKSPACE_CURRENT_BRANCH=$(cd $JJ_WORKSPACE_DIR ; git rev-parse --abbr
export JJ_WORKSPACE_CURRENT_COMMIT_AUTHOR=$(cd $JJ_WORKSPACE_DIR ; git show -s --format='%an %ae' $JJ_WORKSPACE_CURRENT_COMMIT ) export JJ_WORKSPACE_CURRENT_COMMIT_AUTHOR=$(cd $JJ_WORKSPACE_DIR ; git show -s --format='%an %ae' $JJ_WORKSPACE_CURRENT_COMMIT )
export JJ_WORKSPACE_CURRENT_VERSION=$(cat $JJ_WORKSPACE_DIR/package.json | jq -r '.version') export JJ_WORKSPACE_CURRENT_VERSION=$(cat $JJ_WORKSPACE_DIR/package.json | jq -r '.version')
export JJ_WORKSPACE_CURRENT_ISO_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") export JJ_WORKSPACE_CURRENT_ISO_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if ! command -v python >/dev/null 2>&1; then
shopt -s expand_aliases
alias python=python3
fi
export JJ_PYTHON_VERSION=$(python --version | awk '{print $2}') export JJ_PYTHON_VERSION=$(python --version | awk '{print $2}')
@@ -146,6 +171,11 @@ cat << EOF > "$JJ_WORKSPACE_DIR/active.sh"
#!/bin/bash #!/bin/bash
if [[ -e "$ASDF_DIR/asdf.sh" ]]; then if [[ -e "$ASDF_DIR/asdf.sh" ]]; then
source "$ASDF_DIR/asdf.sh" source "$ASDF_DIR/asdf.sh"
source "$JJ_DATA_DIR/~~tmp/shEnvs.sh" || true
export JJ_SOURCE_DIR="${JJ_SOURCE_DIR}"
export JJ_DATA_DIR="${JJ_DATA_DIR}"
export JJ_SECRET_TMP_DIR="${JJ_SECRET_TMP_DIR}"
source ${JJ_SOURCE_DIR}/bashUtils/auto-completion.sh
fi fi
EOF EOF
chmod +x "$JJ_WORKSPACE_DIR/active.sh" chmod +x "$JJ_WORKSPACE_DIR/active.sh"
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "jj-command-line", "name": "jj-command-line",
"description": "jj cli", "description": "jj cli",
"version": "1.0.29", "version": "2.1.1",
"author": "Jason Jafari", "author": "Jason Jafari",
"bin": { "bin": {
"jj": "./jj.sh" "jj": "./jj.sh"
}, },
"bugs": "https://github.com/https://github.com/jafarijason/jj/jj/issues", "bugs": "https://gitea.root.sarava.dev/jason/jj-command-line/issues",
"dependencies": { "dependencies": {
"chalk": "^5.3.0", "chalk": "^5.3.0",
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",