WIP
This commit is contained in:
+28
-21
@@ -19,6 +19,7 @@ export const jjConfigs: any = {
|
||||
export const jjEnsure = async ({
|
||||
returnProcessEnvs = false
|
||||
}) => {
|
||||
// return
|
||||
jjConfigs.modulePath = path.resolve(__dirname, "../")
|
||||
jjConfigs.tmpDir = `${jjConfigs.modulePath}/~~tmp`
|
||||
jjConfigs.configSaveFilePath = `${jjConfigs.tmpDir}/configSaveFilePath.txt`
|
||||
@@ -39,20 +40,21 @@ export const jjEnsure = async ({
|
||||
message: '',
|
||||
},
|
||||
])
|
||||
if (!fs.existsSync(answer.jjYamlPath)) {
|
||||
jjLogError(`can not find ${answer.jjYamlPath}`)
|
||||
jjConfigs.jjYamlPath = answer.jjYamlPath
|
||||
if (!fs.existsSync(jjConfigs.jjYamlPath)) {
|
||||
jjLogError(`can not find ${jjConfigs.jjYamlPath}`)
|
||||
}
|
||||
|
||||
const jjYamlRawContent = await fs.readFile(answer.jjYamlPath, "utf8")
|
||||
const jjYamlRawContent = await fs.readFile(jjConfigs.jjYamlPath, "utf8")
|
||||
const jjYamlObj: any = YAML.load(jjYamlRawContent)
|
||||
if (_.isEmpty(jjYamlObj?.environments || {})) {
|
||||
jjLogError(`Wrong ${answer.jjYamlPath} file`)
|
||||
jjLogError(`Wrong ${jjConfigs.jjYamlPath} file`)
|
||||
}
|
||||
jjConfigs.jjWorkSpacePath = path.dirname(answer.jjYamlPath)
|
||||
jjConfigs.jjWorkSpacePath = path.dirname(jjConfigs.jjYamlPath)
|
||||
|
||||
jjConfigs.jjYamlObj = jjYamlObj
|
||||
|
||||
await fs.writeFile(jjConfigs.configSaveFilePath, answer.jjYamlPath, "utf8")
|
||||
await fs.writeFile(jjConfigs.configSaveFilePath, jjConfigs.jjYamlPath, "utf8")
|
||||
|
||||
} else {
|
||||
jjConfigs.jjYamlPath = await fs.readFile(jjConfigs.configSaveFilePath, "utf8")
|
||||
@@ -104,8 +106,8 @@ export const jjEnsure = async ({
|
||||
email: jjConfigs.processEnvs.JJ_GIT_USER_EMAIL,
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(
|
||||
jjConfigs.jjYamlPath,
|
||||
|
||||
saveFileIfItChanged(
|
||||
YAML.dump(
|
||||
jjConfigs.jjYamlObj,
|
||||
{
|
||||
@@ -115,36 +117,41 @@ export const jjEnsure = async ({
|
||||
// sortKeys: true
|
||||
}
|
||||
),
|
||||
"utf8"
|
||||
jjConfigs.jjYamlPath
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
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)
|
||||
let shEnvs = ''
|
||||
|
||||
Object.keys(jjConfigs.processEnvs).forEach((envKey) => {
|
||||
const command = `export ${envKey.toUpperCase().split("-").join("_")}="${jjConfigs.processEnvs[envKey]}"`
|
||||
shEnvs += `${command}\n`
|
||||
console.log(command)
|
||||
})
|
||||
|
||||
fs.writeFileSync(`${jjConfigs.tmpDir}/shEnvs.sh`, shEnvs, 'utf-8');
|
||||
}
|
||||
|
||||
|
||||
return jjConfigs
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
//
|
||||
_.get(process.argv, "[1]", "")?.includes('helpers/jjEnsure.ts')
|
||||
&& _.get(process.argv, "[2]", "") == 'returnProcessEnvs'
|
||||
) {
|
||||
|
||||
const jj = await jjEnsure({ returnProcessEnvs: true })
|
||||
if (_.get(process.argv, "[2]", "") == 'returnProcessEnvs') {
|
||||
const jj = await jjEnsure({ returnProcessEnvs: true })
|
||||
process.exit(0)
|
||||
}
|
||||
if (_.get(process.argv, "[2]", "") == 'init') {
|
||||
const jj = await jjEnsure({ returnProcessEnvs: false })
|
||||
process.exit(0)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
Reference in New Issue
Block a user