From 70a1b2f768d2fd058b68c941e214dbd4c7b8bcaf Mon Sep 17 00:00:00 2001 From: Jason Jafari Date: Sun, 10 Nov 2024 01:06:11 -0500 Subject: [PATCH] WIP --- bashUtils/ensureDependency.sh | 104 +++++++++++++++++++++++++++++++++ bashUtils/index.sh | 52 ++++++++++++++++- helpers/jjEnsure.ts | 78 +++++++++++++++++++++---- helpers/saveFileIfItChanged.ts | 21 +++++++ jj.sh | 31 ++++++++-- src/index.ts | 9 +-- 6 files changed, 274 insertions(+), 21 deletions(-) create mode 100755 bashUtils/ensureDependency.sh mode change 100644 => 100755 bashUtils/index.sh mode change 100644 => 100755 helpers/jjEnsure.ts create mode 100644 helpers/saveFileIfItChanged.ts diff --git a/bashUtils/ensureDependency.sh b/bashUtils/ensureDependency.sh new file mode 100755 index 0000000..ec9ceed --- /dev/null +++ b/bashUtils/ensureDependency.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env zsh + +# source "$JJ_SOURCE_DIR/bashUtils/index.sh" + +osDependencies=(${=OS_DEPENDENCIES}) + +for osDependency in $osDependencies ; do + if ! hash $osDependency; then + jjCliLog jjLogWarning "bashUtils/ensureDependency.sh Unable to find OS package '$osDependency'. Installing now..." + eval "$PACKAGE_MANAGER_INSTALL $osDependency" + jjCliLog "jjLogSuccess" 'bashUtils/ensureDependency.sh OS package '$osDependency' has been installed' + fi +done + + + + +if [[ -e $ASDF_DIR/asdf.sh ]]; then + source $ASDF_DIR/asdf.sh +fi + + +asdfInstalledVersionPrefix=$(asdf info 2> /dev/null | grep -A 1 'ASDF VERSION' | tail -1 | grep "^${ASDF_VERSION}") + +if [[ -z "${asdfInstalledVersionPrefix}" ]] || \ + ! hash asdf || \ + [[ "${asdfInstalledVersionPrefix}" != "$ASDF_VERSION" ]] || \ + [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/asdf-tools-installed ]] \ + || jjIsFileNewerFile "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$JJ_SOURCE_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 + + source $ASDF_DIR/asdf.sh + rm -rf $ASDF_DIR/.git + + jjCliLog jjLogSuccess $scriptFileName "OS package 'asdf' has been installed" + + set +e + while read tool_and_version; do + jjCliLog jjLog "... checking tool: $tool_and_version" + tool_and_version_split=(${=tool_and_version}) + # echo ${tool_and_version_split[1]} + # echo ${tool_and_version_split[2]} + asdf plugin add ${tool_and_version_split[1]} || true + asdf install ${tool_and_version_split[1]} ${tool_and_version_split[2]} + 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 + jjCliLog jjLogSuccess "Asdf and tools installed" +fi + +if [[ -e $ASDF_DIR/asdf.sh ]]; then + source $ASDF_DIR/asdf.sh +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" \ + ; then + + pushd $JJ_WORKSPACE_DIR > /dev/null + bun install --production + popd > /dev/null + touch $JJ_SOURCE_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" \ + ; 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 + 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" \ + ; 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 + jjCliLog jjLogSuccess "Global dependencies successfully installed" +fi + +if [[ ! -e $JJ_SOURCE_DIR/~~tmp/deps/libpq-installed ]]; then + if [[ ${UNAME} == "Linux" ]]; then + eval "$PACKAGE_MANAGER_INSTALL libpq-dev g++ make" + else + eval "$PACKAGE_MANAGER_INSTALL libpq" + eval "$PACKAGE_MANAGER_INSTALL postgresql@14" + fi + + touch $JJ_SOURCE_DIR/~~tmp/deps/libpq-installed +fi \ No newline at end of file diff --git a/bashUtils/index.sh b/bashUtils/index.sh old mode 100644 new mode 100755 index fa50f98..01e8648 --- a/bashUtils/index.sh +++ b/bashUtils/index.sh @@ -1,7 +1,53 @@ +#!/usr/bin/env zsh + +set - export UNAME="$(uname)" -setopt aliases complete_aliases -unsetopt multios +# setopt aliases complete_aliases +# unsetopt multios + + + +if [[ ${UNAME} == "Linux" ]]; then + A="A" +else + A="A" + export PATH="/opt/homebrew/bin:${PATH}" +fi + +function getPackageManager { + local SUPPORTED_PACKAGE_MANAGERS=("apt-get" "brew" "yum") + + for manager in "${SUPPORTED_PACKAGE_MANAGERS[@]}"; do + if command -v "$manager" &> /dev/null; then + echo "$manager" + return 0 + fi + done + + echo "No supported package manager found." >&2 + return 1 +} + +# Set up the package manager base +PACKAGE_MANAGER_BASE=$(getPackageManager) + +if [[ $? -ne 0 ]]; then + echo "Error: No supported package manager found." + exit 1 +fi + +# Define install and remove commands based on the package manager +if [[ "$PACKAGE_MANAGER_BASE" == "apt-get" || "$PACKAGE_MANAGER_BASE" == "yum" ]]; then + PACKAGE_MANAGER_INSTALL="sudo $PACKAGE_MANAGER_BASE -y install" +elif [[ "$PACKAGE_MANAGER_BASE" == "brew" ]]; then + PACKAGE_MANAGER_INSTALL="$PACKAGE_MANAGER_BASE install" +else + echo "Error: Unrecognized package manager $PACKAGE_MANAGER_BASE." + exit 1 +fi + + function jjOsPackageMamanger { @@ -35,5 +81,5 @@ function jjIsFileNewerFile { } function jjCliLog { - "$JJ_SOURCE_DIR/helpers/jjLogger.ts" $1 ${@:2} + $JJ_SOURCE_DIR/helpers/jjLogger.ts $1 "${@:2}" } diff --git a/helpers/jjEnsure.ts b/helpers/jjEnsure.ts old mode 100644 new mode 100755 index ce9e961..0dbac02 --- a/helpers/jjEnsure.ts +++ b/helpers/jjEnsure.ts @@ -1,40 +1,51 @@ -import { jjConfigs } from "@src/index"; +#!/usr/bin/env bun + import path from "path"; import fs from 'fs-extra'; import inquirer from "inquirer"; import * as YAML from "js-yaml" import { _ } from "lodash"; +import { jjLog, jjLogAction, jjLogError } from "./jjLogger"; +import { saveFileIfItChanged } from "./saveFileIfItChanged"; -export const srmFileName = 'package.json' - -export const srmFilePath = path.resolve(srmFileName); +export const jjConfigs: any = { + processEnvs: {} +} -export const jjEnsure = async (opts) => { + + +export const jjEnsure = async ({ + returnProcessEnvs = false +}) => { jjConfigs.modulePath = path.resolve(__dirname, "../") jjConfigs.tmpDir = `${jjConfigs.modulePath}/~~tmp` jjConfigs.configSaveFilePath = `${jjConfigs.tmpDir}/configSaveFilePath.txt` + if (!fs.existsSync(jjConfigs.tmpDir)) { + jjLog(`tmp folder ${jjConfigs.tmpDir} is not exist creating`) fs.mkdirSync(jjConfigs.tmpDir, { recursive: true }) } if (!fs.existsSync(jjConfigs.configSaveFilePath)) { + jjLogAction(`JJ cli require jj.yaml path please add it here in absolute way`) + const answer: any = await inquirer.prompt([ { type: 'input', name: 'jjYamlPath', - message: 'Please provide jj.yaml file absolute path', + message: '', }, ]) if (!fs.existsSync(answer.jjYamlPath)) { - opts.context.error(`can not find ${answer.jjYamlPath}`) + jjLogError(`can not find ${answer.jjYamlPath}`) } const jjYamlRawContent = await fs.readFile(answer.jjYamlPath, "utf8") const jjYamlObj: any = YAML.load(jjYamlRawContent) if (_.isEmpty(jjYamlObj?.environments || {})) { - opts.context.error(`Wrong ${answer.jjYamlPath} file`) + jjLogError(`Wrong ${answer.jjYamlPath} file`) } jjConfigs.jjWorkSpacePath = path.dirname(answer.jjYamlPath) @@ -48,11 +59,58 @@ export const jjEnsure = async (opts) => { const jjYamlRawContent = await fs.readFile(jjConfigs.jjYamlPath, "utf8") const jjYamlObj: any = YAML.load(jjYamlRawContent) if (_.isEmpty(jjYamlObj?.environments || {})) { - opts.context.error(`Wrong ${jjConfigs.jjYamlPath} file`) + jjLogError(`Wrong ${jjConfigs.jjYamlPath} file`) } jjConfigs.jjYamlObj = jjYamlObj } - jjConfigs.processEnvs.JJ_WORKSPACE_DIR = jjConfigs.jjWorkSpacePath + + jjConfigs.processEnvs.JJ_WORKSPACE_DIR = jjConfigs.jjWorkSpacePath + 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` + + const asdfToolsVersionFileContent = Object.keys(jjConfigs.jjYamlObj.asdf.tools).map(key => `${key} ${jjConfigs.jjYamlObj.asdf.tools[key].version}`).join('\n') + + saveFileIfItChanged(asdfToolsVersionFileContent, 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 + + jjConfigs.processEnvs.OS_DEPENDENCIES = jjConfigs.jjYamlObj.osDependencies.join(' ') + + const bunGlobalDependencies = jjConfigs.jjYamlObj.bun.globals.join(' ') + saveFileIfItChanged(bunGlobalDependencies, `${jjConfigs.tmpDir}/bunGlobalDependencies.txt`) + jjConfigs.processEnvs.BUN_GLOBAL_DEPENDENCIES = bunGlobalDependencies + + + + saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.tmpDir}/jjConfigs.json`) + + + if (returnProcessEnvs) { + + Object.keys(jjConfigs.processEnvs).forEach((envKey) => { + console.log(`export ${envKey.toUpperCase().split("-").join("_")}="${jjConfigs.processEnvs[envKey]}"`) + }) + } else { + console.log(jjConfigs.jjYamlObj) + + } + + return jjConfigs + + +} + +if ( + // + _.get(process.argv, "[1]", "")?.includes('helpers/jjEnsure.ts') + && _.get(process.argv, "[2]", "") == 'returnProcessEnvs' +) { + + const jj = await jjEnsure({ returnProcessEnvs: true }) + // console.log(jj) + // console.log(jjConfigs) } \ No newline at end of file diff --git a/helpers/saveFileIfItChanged.ts b/helpers/saveFileIfItChanged.ts new file mode 100644 index 0000000..6e6a86a --- /dev/null +++ b/helpers/saveFileIfItChanged.ts @@ -0,0 +1,21 @@ +import fs from 'fs-extra'; + +export const saveFileIfItChanged = (newContent, filePath) => { + + try { + // Check if file exists and read the current content + if (fs.existsSync(filePath)) { + const currentContent = fs.readFileSync(filePath, 'utf-8'); + + // Compare current content with new content + if (currentContent === newContent) { + return; // Exit function if content is the same + } + } + + // Write new content to the file if it is different or file doesn't exist + fs.writeFileSync(filePath, newContent, 'utf-8'); + } catch (error) { + console.error('Error handling file:', error); + } +} \ No newline at end of file diff --git a/jj.sh b/jj.sh index c12c7a8..4c06fd1 100755 --- a/jj.sh +++ b/jj.sh @@ -1,7 +1,16 @@ #!/usr/bin/env zsh +source ~/.bashrc + set - +trap '[[ "$?" == "1" ]] && echo -e "\033[1;31m[jj] ERROR !\033[0m" >&2' EXIT + + +autoload -U compinit +compinit + + jjCpmmandRunPoint=$(echo ${BASH_SOURCE[0]}) @@ -27,6 +36,10 @@ function findJJSource { export JJ_SOURCE_DIR=$(findJJSource) +if [[ "$1" == "--clean" ]]; then + rm -rf $JJ_SOURCE_DIR/~~tmp/deps +fi + if [[ -e ~/.bun/bin/bun ]]; then export PATH="~/.bun/bin:${PATH}" fi @@ -40,21 +53,31 @@ if [[ "$__currentBunVersion" != "${__bunVersionExpect}" ]]; then fi -mkdir -p "$JJ_SOURCE_DIR/~~tmp" +mkdir -p "$JJ_SOURCE_DIR/~~tmp/deps" alias jj="$JJ_SOURCE_DIR/jj.sh" source "$JJ_SOURCE_DIR/bashUtils/index.sh" + + +# $JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs +# exit 0 + +eval $($JJ_SOURCE_DIR/helpers/jjEnsure.ts returnProcessEnvs) +# echo $JJ_WORKSPACE_DIR + # jjCliLog jjLog testjjLog a b c s # jjCliLog jjLogInfo testjjLogInfo # jjCliLog jjLogHeading testjjLogHeading -export ASDF_DIR="$JJ_SOURCE_DIR/~~asdf_gc" -export ASDF_DATA_DIR="$JJ_SOURCE_DIR/~~asdf_gc" -export ASDF_VERSION=v0.14.1 + +source "$JJ_SOURCE_DIR/bashUtils/ensureDependency.sh" + + + # if [[ ! -e $JJ_SOURCE_DIR/~~tmp/pa ]]; then diff --git a/src/index.ts b/src/index.ts index d98d35e..ea3fabb 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ #!/usr/bin/env bun -export const jjConfigs: any = { - processEnvs: {} -} +import fs from 'fs-extra'; -export { run } from '@oclif/core' + + +// console.log('aaaa') +// export { run } from '@oclif/core'