This commit is contained in:
2024-11-10 01:06:11 -05:00
parent 23e234eac0
commit 70a1b2f768
6 changed files with 274 additions and 21 deletions
Regular → Executable
+68 -10
View File
@@ -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)
}
+21
View File
@@ -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);
}
}