diff --git a/bashUtils/ensureDependency.sh b/bashUtils/ensureDependency.sh index 90bcd0c..48ea71c 100755 --- a/bashUtils/ensureDependency.sh +++ b/bashUtils/ensureDependency.sh @@ -25,7 +25,7 @@ if [[ -e $ASDF_DIR/asdf.sh ]]; then source $ASDF_DIR/asdf.sh fi - +# rm -rf $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed if ! hash asdf || \ [[ ! -e $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed ]] \ || jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed" ; then diff --git a/bun.lockb b/bun.lockb index 4cd211b..585753c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/cli/secrets.ts b/cli/secrets.ts index de73ef9..532b60d 100644 --- a/cli/secrets.ts +++ b/cli/secrets.ts @@ -9,25 +9,40 @@ import { saveFileIfItChanged } from "@jjHelpers/saveFileIfItChanged"; import * as YAML from "js-yaml"; import { subRepositoryClean, subRepositoryPull, subRepositoryPushChanges } from "@jjHelpers/gitSubRepoUtils"; import moment from "moment"; +import inquirer from "inquirer"; import { bashRunAndReturnSync } from "@jjHelpers/bashUtils"; const logger = makeJJlogger({ filename: "jj/cli/secrets.ts", }); -export const getSecretFolder = () => { - let secretsFolder = getJJConfigByKeyChain(`secretsFolder`); - secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false); - return secretsFolder; -}; +// export const getSecretFolder = () => { +// let secretsFolder = getJJConfigByKeyChain(`secretsFolder`); +// secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false); +// return secretsFolder; +// }; -export const getEncryptionKeyForEnv = (envKey) => { - const secretsFolder = getSecretFolder(); - if (!fs.existsSync(`${secretsFolder}/~~${envKey}`)) { - logger.error(`Encryption key for ${envKey} env not fond and nt exist `); - process.exit(1); +export const getEncryptionKeyForEnv = async (envKey) => { + // const secretsFolder = getSecretFolder(); + const secretsFolder = process.env['JJ_SECRET_TMP_DIR']; + if (!secretsFolder) { + 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; }; @@ -56,7 +71,9 @@ export const decryptFile = async (absFilePath) => { } const absDirPath = path.dirname(absFilePath); 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) ) { const isOlderThanOriginal = await isFileOlder(decryptFileJsonPath, absFilePath) if(isOlderThanOriginal) { @@ -70,10 +87,10 @@ export const decryptFile = async (absFilePath) => { const secretFileObjRaw = await fs.readFile(absFilePath, "utf8"); const secretObj = JSON.parse(secretFileObjRaw); - const envKey = path.basename(path.join(absFilePath, "..")); + const decryptedSecretObj = {}; - const encryptionKey = getEncryptionKeyForEnv(envKey); + const encryptionKey = await getEncryptionKeyForEnv(envKey); Object.keys(secretObj).forEach((key) => { const plainValue = secretObj[key]; if (plainValue.includes(`${envKey}--`)) { @@ -91,13 +108,13 @@ export const decryptFile = async (absFilePath) => { - if (!fs.existsSync(`${absDirPath}/~~decrypt`)) { - fs.mkdirSync(`${absDirPath}/~~decrypt`, { recursive: true }); + if (!fs.existsSync(`${decryptDir}`)) { + 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"); - const decryptedShFormat = `${absDirPath}/~~decrypt/${fileName}.sh`; + const decryptedShFormat = `${decryptDir}/${fileName}.sh`; fs.writeFileSync(decryptedShFormat, shFormatDecrypt, "utf-8"); logger.success(`decrypt file-secret for ${absFilePath} done`); return decryptedSecretObj; @@ -124,7 +141,7 @@ export const setSecretFile = async ({ absFilePath, secretKey, secretValue }) => const envKey = path.basename(path.join(absFilePath, "..")); const secretFileObjRaw = await fs.readFile(absFilePath, "utf8"); const secretObj = JSON.parse(secretFileObjRaw); - const encryptionKey = getEncryptionKeyForEnv(envKey); + const encryptionKey = await getEncryptionKeyForEnv(envKey); const encryptVal = jjEncrypt(secretValue, encryptionKey); secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`; diff --git a/helpers/jjEnsure.ts b/helpers/jjEnsure.ts index b8ba300..39dd14d 100755 --- a/helpers/jjEnsure.ts +++ b/helpers/jjEnsure.ts @@ -28,6 +28,11 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => { 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)) { await fs.writeFile(jjConfigs.workspacesFilePath, JSON.stringify([], null, 4), "utf8"); } @@ -56,16 +61,22 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => { } if (!jjConfigs.jjYamlPath) { - console.log(chalk.magenta.bold(`please add it here in absolute path`)); - const answer: any = await inquirer.prompt([ - { - type: "input", - name: "jjYamlPath", - message: "", - }, - ]); - jjConfigs.jjYamlPath = answer.jjYamlPath; + if (process.env['JJ_WORKSPACE_DIR_TMP']) { + jjConfigs.jjYamlPath = `${process.env['JJ_WORKSPACE_DIR_TMP']}/jj.yaml`; + } else { + + console.log(chalk.magenta.bold(`please add it here in absolute path`)); + + const answer: any = await inquirer.prompt([ + { + type: "input", + name: "jjYamlPath", + message: "", + }, + ]); + jjConfigs.jjYamlPath = answer.jjYamlPath; + } } if (!fs.existsSync(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.JJ_WORKSPACE_TMP_DIR = `${jjConfigs.JJ_WORKSPACE_DIR}/~~tmp`; 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.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}`; @@ -113,7 +126,7 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => { .map((key) => `${key} ${jjConfigs.jjYamlObj.asdf.tools[key].version}`) .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" jjConfigs.processEnvs.ASDF_VERSION = jjConfigs.jjYamlObj.asdf.version; diff --git a/jj.sh b/jj.sh index daff676..4d127c2 100755 --- a/jj.sh +++ b/jj.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash - MILLISECONDS_START=$(echo "$EPOCHREALTIME * 1000" | bc) 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" -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) - - -if [[ -e ~/.bun/bin/bun ]]; then - export PATH="~/.bun/bin:${PATH}" +if ! JJ_DATA_DIR=$(bun -e ' + const fs = require("fs"); + const YAML = require(`${process.env.JJ_SOURCE_DIR}/node_modules/js-yaml`); + 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 +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" -__currentBunVersion=$(bun --version || true) -if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then - rm -rf ~/.bun/bin/bun - curl -fsSL https://bun.sh/install | bash -s "bun-v$__bunVersionExpect" - export PATH="~/.bun/bin:${PATH}" -fi +# __bunVersionExpect="1.4.3" +# __currentBunVersion=$(bun --version || true) +# if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then +# rm -rf ~/.bun/bin/bun +# curl -fsSL https://bun.sh/install | bash -s "bun-v$__bunVersionExpect" +# export PATH="~/.bun/bin:${PATH}" +# fi 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_VERSION=$(cat $JJ_WORKSPACE_DIR/package.json | jq -r '.version') 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}') @@ -146,6 +171,11 @@ cat << EOF > "$JJ_WORKSPACE_DIR/active.sh" #!/bin/bash if [[ -e "$ASDF_DIR/asdf.sh" ]]; then 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 EOF chmod +x "$JJ_WORKSPACE_DIR/active.sh" @@ -185,4 +215,4 @@ ${JJ_SOURCE_DIR}/helpers/jjCli.ts $@ -# $JJ_SOURCE_DIR/bin/run.ts $@ \ No newline at end of file +# $JJ_SOURCE_DIR/bin/run.ts $@ diff --git a/package.json b/package.json index 6c88e24..da8a935 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "jj-command-line", "description": "jj cli", - "version": "1.0.29", + "version": "2.1.1", "author": "Jason Jafari", "bin": { "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": { "chalk": "^5.3.0", "crypto-js": "^4.2.0",