From 1af25d6c619e41b8a0c36ceb78d37d5d9ed2ee8e Mon Sep 17 00:00:00 2001 From: Jason Jafari Date: Thu, 14 Nov 2024 16:11:00 -0500 Subject: [PATCH] WIP --- bashUtils/auto-completion.sh | 4 +- bashUtils/ensureDependency.sh | 34 +++---- helpers/jjConfigUtils.ts | 2 +- helpers/jjEnsure.ts | 174 ++++++++++++++++++++++++++++++---- helpers/jjLogger.ts | 2 +- jj.sh | 43 +++++---- package.json | 2 +- 7 files changed, 199 insertions(+), 62 deletions(-) diff --git a/bashUtils/auto-completion.sh b/bashUtils/auto-completion.sh index b73df89..ea31ecd 100644 --- a/bashUtils/auto-completion.sh +++ b/bashUtils/auto-completion.sh @@ -2,8 +2,8 @@ autoload -U compinit compinit -if [[ -e "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" ]]; then - source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh +if [[ -e "$JJ_DATA_DIR/~~tmp/shEnvs.sh" ]]; then + source $JJ_DATA_DIR/~~tmp/shEnvs.sh fi diff --git a/bashUtils/ensureDependency.sh b/bashUtils/ensureDependency.sh index d10f490..eee8d2c 100755 --- a/bashUtils/ensureDependency.sh +++ b/bashUtils/ensureDependency.sh @@ -17,8 +17,8 @@ fi if ! hash asdf || \ - [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/asdf-tools-installed ]] \ - || jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_SOURCE_DIR/~~tmp/deps/asdf-tools-installed" ; then + [[ ! -e $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed ]] \ + || jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed" ; then rm -rf $ASDF_DIR git -c advice.detachedHead=false clone -q https://github.com/asdf-vm/asdf.git $ASDF_DIR --branch $ASDF_VERSION @@ -39,9 +39,9 @@ if ! hash asdf || \ done <${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME} asdf install set -e - rm -rf $JJ_SOURCE_DIR/~~tmp/deps - mkdir -p "$JJ_SOURCE_DIR/~~tmp/deps" - touch $JJ_SOURCE_DIR/~~tmp/deps/asdf-tools-installed + rm -rf $JJ_DATA_DIR/~~tmp/deps + mkdir -p "$JJ_DATA_DIR/~~tmp/deps" + touch $JJ_DATA_DIR/~~tmp/deps/asdf-tools-installed jjCliLog jjLogSuccess "Asdf and tools installed" fi @@ -50,41 +50,41 @@ if [[ -e $ASDF_DIR/asdf.sh ]]; then fi -if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/bun-packages-installed ]] \ - || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/package.json" "$JJ_SOURCE_DIR/~~tmp/deps/bun-packages-installed" \ - || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/bun.lockb" "$JJ_SOURCE_DIR/~~tmp/deps/bun-packages-installed" \ +if [[ ! -e $JJ_DATA_DIR/~~tmp/deps/bun-packages-installed ]] \ + || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/package.json" "$JJ_DATA_DIR/~~tmp/deps/bun-packages-installed" \ + || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/bun.lockb" "$JJ_DATA_DIR/~~tmp/deps/bun-packages-installed" \ ; then pushd $JJ_WORKSPACE_DIR > /dev/null bun install popd > /dev/null - touch $JJ_SOURCE_DIR/~~tmp/deps/bun-packages-installed + touch $JJ_DATA_DIR/~~tmp/deps/bun-packages-installed jjCliLog jjLogSuccess "Bun dependencies successfully installed" fi -if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/python-packages-installed ]] \ - || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/requirements.txt" "$JJ_SOURCE_DIR/~~tmp/deps/python-packages-installed" \ +if [[ ! -e $JJ_DATA_DIR/~~tmp/deps/python-packages-installed ]] \ + || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/requirements.txt" "$JJ_DATA_DIR/~~tmp/deps/python-packages-installed" \ ; then pushd $JJ_WORKSPACE_DIR > /dev/null pip install -r requirements.txt || pip3 install -r requirements.txt popd > /dev/null - touch $JJ_SOURCE_DIR/~~tmp/deps/python-packages-installed + touch $JJ_DATA_DIR/~~tmp/deps/python-packages-installed jjCliLog jjLogSuccess "Python dependencies successfully installed" fi -if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/npmGlobalDependencies-installed ]] \ - || jjIsFileNewerFile "$JJ_SOURCE_DIR/~~tmp/npmGlobalDependencies.txt" "$JJ_SOURCE_DIR/~~tmp/deps/npmGlobalDependencies-installed" \ +if [[ ! -e $JJ_DATA_DIR/~~tmp/deps/npmGlobalDependencies-installed ]] \ + || jjIsFileNewerFile "$JJ_DATA_DIR/~~tmp/npmGlobalDependencies.txt" "$JJ_DATA_DIR/~~tmp/deps/npmGlobalDependencies-installed" \ ; then pushd $JJ_WORKSPACE_DIR > /dev/null eval "npm i -g $BUN_GLOBAL_DEPENDENCIES" popd > /dev/null - touch $JJ_SOURCE_DIR/~~tmp/deps/npmGlobalDependencies-installed + touch $JJ_DATA_DIR/~~tmp/deps/npmGlobalDependencies-installed jjCliLog jjLogSuccess "Global dependencies successfully installed" fi -if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/libpq-installed ]]; then +if [[ ! -e $JJ_DATA_DIR/~~tmp/deps/libpq-installed ]]; then if [[ ${UNAME} == "Linux" ]]; then eval "$PACKAGE_MANAGER_INSTALL libpq-dev g++ make" else @@ -92,7 +92,7 @@ if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/libpq-installed ]]; then eval "$PACKAGE_MANAGER_INSTALL postgresql@14" fi - touch $JJ_SOURCE_DIR/~~tmp/deps/libpq-installed + touch $JJ_DATA_DIR/~~tmp/deps/libpq-installed fi if [[ ! -d "$JJ_GIT_METADATA_DIR" ]]; then diff --git a/helpers/jjConfigUtils.ts b/helpers/jjConfigUtils.ts index 6aafabc..2094e3c 100644 --- a/helpers/jjConfigUtils.ts +++ b/helpers/jjConfigUtils.ts @@ -2,7 +2,7 @@ import fs from 'fs-extra'; import _ from 'lodash'; -const rawConfigData = fs.readFileSync(`${process.env.JJ_SOURCE_DIR}/~~tmp/jjConfigs.json`, 'utf8'); +const rawConfigData = fs.readFileSync(`${process.env.JJ_DATA_DIR}/~~tmp/jjConfigs.json`, 'utf8'); export const jjConfigsExp = JSON.parse(rawConfigData) diff --git a/helpers/jjEnsure.ts b/helpers/jjEnsure.ts index 342a9e6..e8f79da 100755 --- a/helpers/jjEnsure.ts +++ b/helpers/jjEnsure.ts @@ -8,6 +8,7 @@ import { _ } from "lodash"; import { jjLog, jjLogAction, jjLogError } from "./jjLogger"; import { saveFileIfItChanged } from "./saveFileIfItChanged"; import { gitUserEmail, gitUserName, jjUserName } from "./userUtils"; +import chalk from "chalk"; export const jjConfigs: any = { processEnvs: {} @@ -20,36 +21,74 @@ export const jjEnsure = async ({ returnProcessEnvs = false }) => { // return - jjConfigs.modulePath = path.resolve(__dirname, "../") - jjConfigs.tmpDir = `${jjConfigs.modulePath}/~~tmp` - jjConfigs.configSaveFilePath = `${jjConfigs.tmpDir}/configSaveFilePath.txt` + jjConfigs.JJ_SOURCE_DIR = process.env.JJ_SOURCE_DIR + jjConfigs.JJ_DATA_DIR = process.env.JJ_DATA_DIR + jjConfigs.JJ_TMP_DIR = `${jjConfigs.JJ_DATA_DIR}/~~tmp` + jjConfigs.JJ_DEPS_DIR = `${jjConfigs.JJ_DATA_DIR}/~~tmp/deps` + jjConfigs.configSaveFilePath = `${jjConfigs.JJ_TMP_DIR}/configSaveFilePath.txt` + jjConfigs.workspacesFilePath = `${jjConfigs.JJ_TMP_DIR}/workspacesFilePath.json` - if (!fs.existsSync(jjConfigs.tmpDir)) { - jjLog(`tmp folder ${jjConfigs.tmpDir} is not exist creating`) - fs.mkdirSync(jjConfigs.tmpDir, { recursive: true }) + if (!fs.existsSync(jjConfigs.JJ_TMP_DIR)) { + jjLog(`tmp folder ${jjConfigs.JJ_TMP_DIR} is not exist creating`) + fs.mkdirSync(jjConfigs.JJ_TMP_DIR, { recursive: true }) } + if (!fs.existsSync(jjConfigs.workspacesFilePath)) { + await fs.writeFile(jjConfigs.workspacesFilePath, JSON.stringify([], null, 4), "utf8") + } + + jjConfigs.workspaces = JSON.parse((await fs.readFile(jjConfigs.workspacesFilePath, "utf8"))) + if (!fs.existsSync(jjConfigs.configSaveFilePath)) { - jjLogAction(`JJ cli require jj.yaml path please add it here in absolute way`) + console.log(chalk.yellow.bold(`JJ cli require jj.yaml absolute`)) - const answer: any = await inquirer.prompt([ - { - type: 'input', - name: 'jjYamlPath', - message: '', - }, - ]) - jjConfigs.jjYamlPath = answer.jjYamlPath + if (jjConfigs.workspaces.length > 0) { + console.log(chalk.magenta.bold(`please select following options:`)) + jjConfigs.workspaces.forEach((jjYamlPath, index) => { + console.log(chalk.magenta.bold(`${index + 1} ${jjYamlPath}`)) + }) + console.log(chalk.magenta.bold(`${jjConfigs.workspaces.length + 1} new workspace`)) + const optionSelect: any = await inquirer.prompt([ + { + type: 'input', + name: 'optionSelect', + message: '', + }, + ]) + if (optionSelect?.optionSelect - 1 != jjConfigs.workspaces.length) { + jjConfigs.jjYamlPath = jjConfigs.workspaces[optionSelect?.optionSelect - 1] + } + } + + 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 (!fs.existsSync(jjConfigs.jjYamlPath)) { jjLogError(`can not find ${jjConfigs.jjYamlPath}`) } + const jjYamlRawContent = await fs.readFile(jjConfigs.jjYamlPath, "utf8") const jjYamlObj: any = YAML.load(jjYamlRawContent) if (_.isEmpty(jjYamlObj?.environments || {})) { jjLogError(`Wrong ${jjConfigs.jjYamlPath} file`) } + + jjConfigs.workspaces = [... new Set([...jjConfigs.workspaces, jjConfigs.jjYamlPath])] + + await fs.writeFile(jjConfigs.workspacesFilePath, JSON.stringify(jjConfigs.workspaces, null, 4), "utf8") + jjConfigs.jjWorkSpacePath = path.dirname(jjConfigs.jjYamlPath) jjConfigs.jjYamlObj = jjYamlObj @@ -70,10 +109,13 @@ export const jjEnsure = async ({ jjConfigs.processEnvs.JJ_WORKSPACE_DIR = jjConfigs.jjWorkSpacePath + jjConfigs.processEnvs.JJ_SOURCE_DIR = jjConfigs.JJ_SOURCE_DIR + jjConfigs.processEnvs.JJ_DATA_DIR = jjConfigs.JJ_DATA_DIR + jjConfigs.JJ_WORKSPACE_DIR = jjConfigs.JJ_WORKSPACE_DIR jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME = jjConfigs.jjYamlObj.name - jjConfigs.processEnvs.ASDF_DIR = `${process.env.JJ_SOURCE_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}` - jjConfigs.processEnvs.ASDF_DATA_DIR = `${process.env.JJ_SOURCE_DIR}/~~asdf_${jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME}` - jjConfigs.processEnvs.ASDF_DEFAULT_TOOL_VERSIONS_FILENAME = `${jjConfigs.tmpDir}/.tool-versions` + 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_DEFAULT_TOOL_VERSIONS_FILENAME = `${jjConfigs.JJ_TMP_DIR}/.tool-versions` const asdfToolsVersionFileContent = Object.keys(jjConfigs.jjYamlObj.asdf.tools).map(key => `${key} ${jjConfigs.jjYamlObj.asdf.tools[key].version}`).join('\n') @@ -84,7 +126,7 @@ export const jjEnsure = async ({ jjConfigs.processEnvs.OS_DEPENDENCIES = jjConfigs.jjYamlObj.osDependencies.join(' ') const bunGlobalDependencies = jjConfigs.jjYamlObj.bun.globals.join(' ') - saveFileIfItChanged(bunGlobalDependencies, `${jjConfigs.tmpDir}/bunGlobalDependencies.txt`) + saveFileIfItChanged(bunGlobalDependencies, `${jjConfigs.JJ_TMP_DIR}/bunGlobalDependencies.txt`) jjConfigs.processEnvs.BUN_GLOBAL_DEPENDENCIES = bunGlobalDependencies jjConfigs.processEnvs.JJ_GIT_METADATA_DIR = `${jjConfigs.processEnvs.JJ_WORKSPACE_DIR}/${jjConfigs.jjYamlObj.metadataRepo.path}` @@ -120,7 +162,97 @@ export const jjEnsure = async ({ jjConfigs.jjYamlPath ) - saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.tmpDir}/jjConfigs.json`) + jjConfigs.jjYamlObj.commands = { + "help": { + "desc": "This help info.", + "mainTitle": "Help", + "postCommand": "", + "maxLength": 12, + "keys": [ + { + "key": "auto-completion", + "desc": "auto-completion", + "keys": [] + }, + { + "key": "workDirs", + "desc": "workDirs", + "keys": [] + }, + { + "key": "version-source", + "desc": "version-source", + "keys": [] + }, + { + "key": "version-workspace", + "desc": "version-workspace", + "keys": [] + }, + { + "key": "change-workspace", + "desc": "change-workspace", + "keys": [] + }, + { + "key": "clean-workspace", + "desc": "clean-workspace", + "keys": [] + } + ], + "scriptToRun": { + "prePath": "__JJ_SOURCE_DIR__", + "path": "cli/help.ts" + } + }, + "secrets": { + "desc": "Secrets", + "mainTitle": "Secrets", + "postCommand": "", + "maxLength": 12, + "keys": [ + { + "key": "setSecret", + "desc": "Set a secret to the given file", + "postCommand": " file key val", + "maxLength": 50, + "keys": [], + "postCommandsForce": [] + }, + { + "key": "setSecretFromFile", + "desc": "Set a secret to the given file", + "postCommand": " file key absFile", + "maxLength": 50, + "keys": [], + "postCommandsForce": [] + }, + { + "key": "decryptFile", + "desc": "decrypt file", + "postCommand": " file", + "maxLength": 50, + "keys": [], + "postCommandsForce": [] + }, + { + "key": "commitSecretMeta", + "desc": "", + "postCommand": "", + "maxLength": 50, + "keys": [], + "postCommandsForce": [] + } + ], + "scriptToRun": { + "prePath": "__JJ_SOURCE_DIR__", + "path": "cli/secrets.ts" + } + }, + ...jjConfigs.jjYamlObj.commands + } + + saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.JJ_TMP_DIR}/jjConfigs.json`) if (returnProcessEnvs) { @@ -133,7 +265,7 @@ export const jjEnsure = async ({ console.log(command) }) - fs.writeFileSync(`${jjConfigs.tmpDir}/shEnvs.sh`, shEnvs, 'utf-8'); + fs.writeFileSync(`${jjConfigs.JJ_TMP_DIR}/shEnvs.sh`, shEnvs, 'utf-8'); } diff --git a/helpers/jjLogger.ts b/helpers/jjLogger.ts index 39edbb3..6ca5313 100755 --- a/helpers/jjLogger.ts +++ b/helpers/jjLogger.ts @@ -11,7 +11,7 @@ export const jjLoggerObj = {} const __filename = fileURLToPath(import.meta.url); -const logFilePath = `${process.env.JJ_SOURCE_DIR}/~~tmp/jj.log`; +const logFilePath = `${process.env.JJ_DATA_DIR}/~~tmp/jj.log`; jjLoggerObj['logFilePath'] = logFilePath diff --git a/jj.sh b/jj.sh index fa56244..f35eb2b 100755 --- a/jj.sh +++ b/jj.sh @@ -19,6 +19,10 @@ trap '[[ "$?" == "1" ]] && echo -e "\033[1;31m[jj] ERROR !\033[0m" >&2' EXIT # compinit +export JJ_DATA_DIR="$HOME/.jjDataDir" + +mkdir -p $JJ_DATA_DIR + jjCpmmandRunPoint=$(echo ${BASH_SOURCE[0]}) @@ -44,10 +48,15 @@ export JJ_SOURCE_DIR=$(findJJSource) -if [[ "$1" == "--clean" ]]; then - rm -rf $JJ_SOURCE_DIR/~~tmp/deps -fi +if [[ "$1" == "help" ]]; then + if [[ "$2" == "change-workspace" ]]; then + rm -rf $JJ_DATA_DIR/~~tmp/configSaveFilePath.txt + fi + if [[ "$2" == "clean-workspace" ]]; then + rm -rf $JJ_DATA_DIR/~~tmp/deps + fi +fi if [[ -e ~/.bun/bin/bun ]]; then @@ -62,7 +71,7 @@ if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then export PATH="~/.bun/bin:${PATH}" fi -mkdir -p "$JJ_SOURCE_DIR/~~tmp/deps" +mkdir -p "$JJ_DATA_DIR/~~tmp/deps" alias jj="$JJ_SOURCE_DIR/jj.sh" @@ -70,34 +79,30 @@ source "$JJ_SOURCE_DIR/bashUtils/index.sh" -if [[ ! -e "$JJ_SOURCE_DIR/~~tmp/configSaveFilePath.txt" ]]; then +if [[ ! -e "$JJ_DATA_DIR/~~tmp/configSaveFilePath.txt" ]]; then $JJ_SOURCE_DIR/helpers/jjEnsure.ts init fi -# $JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs -# exit 0 +$JJ_SOURCE_DIR/helpers/jjEnsure.ts init -if [[ ! -e "$JJ_SOURCE_DIR/~~tmp/configSaveFilePath.txt" ]]; then - $JJ_SOURCE_DIR/helpers/jjEnsure.ts init -fi -if [[ -e "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" ]]; then - source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh +if [[ -e "$JJ_DATA_DIR/~~tmp/shEnvs.sh" ]]; then + source $JJ_DATA_DIR/~~tmp/shEnvs.sh fi -if [[ ! -e "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" ]] \ +if [[ ! -e "$JJ_DATA_DIR/~~tmp/shEnvs.sh" ]] \ || [[ -z $JJ_WORKSPACE_DIR ]] \ - || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/jj.yaml" "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" \ - || jjIsFileNewerFile "$JJ_SOURCE_DIR/~~tmp/jjConfigs.json" "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" \ - || jjIsFileNewerFile "$JJ_SOURCE_DIR/package.json" "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" \ - || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/package.json" "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" \ + || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/jj.yaml" "$JJ_DATA_DIR/~~tmp/shEnvs.sh" \ + || jjIsFileNewerFile "$JJ_DATA_DIR/~~tmp/jjConfigs.json" "$JJ_DATA_DIR/~~tmp/shEnvs.sh" \ + || jjIsFileNewerFile "$JJ_SOURCE_DIR/package.json" "$JJ_DATA_DIR/~~tmp/shEnvs.sh" \ + || jjIsFileNewerFile "$JJ_WORKSPACE_DIR/package.json" "$JJ_DATA_DIR/~~tmp/shEnvs.sh" \ ; then MILLISECONDS=$(get_milliseconds) ; echo "returnProcessEnvs 01 $MILLISECONDS" eval $($JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs) MILLISECONDS=$(get_milliseconds) ; echo "returnProcessEnvs 02 $MILLISECONDS" fi -source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh +source $JJ_DATA_DIR/~~tmp/shEnvs.sh export JJ_WORKSPACE_CURRENT_COMMIT=$(cd $JJ_WORKSPACE_DIR ; git rev-parse HEAD) export JJ_WORKSPACE_CURRENT_BRANCH=$(cd $JJ_WORKSPACE_DIR ; git rev-parse --abbrev-ref HEAD) @@ -134,7 +139,7 @@ source "$JJ_SOURCE_DIR/bashUtils/ensureDependency.sh" -# if [[ ! -e $JJ_SOURCE_DIR/~~tmp/pa ]]; then +# if [[ ! -e $JJ_DATA_DIR/~~tmp/pa ]]; then # echo "asdadasd" # fi diff --git a/package.json b/package.json index 3aa87d1..d6dcb3e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jj-command-line", "description": "jj cli", - "version": "1.0.7", + "version": "1.0.8", "author": "Jason Jafari", "bin": { "jj": "./jj.sh"