Files
jj-command-line/helpers/jjEnsure.ts
T
2024-11-09 04:17:26 -05:00

54 lines
1.8 KiB
TypeScript

import { jjConfigs } from "@src/index";
import path from "path";
import fs from 'fs-extra';
import inquirer from "inquirer";
import * as YAML from "js-yaml"
import { _ } from "lodash";
export const srmFileName = 'package.json'
export const srmFilePath = path.resolve(srmFileName);
export const jjEnsure = async (opts) => {
jjConfigs.modulePath = path.resolve(__dirname, "../")
jjConfigs.tmpDir = `${jjConfigs.modulePath}/~~tmp`
jjConfigs.configSaveFilePath = `${jjConfigs.tmpDir}/configSaveFilePath.txt`
if (!fs.existsSync(jjConfigs.tmpDir)) {
fs.mkdirSync(jjConfigs.tmpDir, { recursive: true })
}
if (!fs.existsSync(jjConfigs.configSaveFilePath)) {
const answer: any = await inquirer.prompt([
{
type: 'input',
name: 'jjYamlPath',
message: 'Please provide jj.yaml file absolute path',
},
])
if (!fs.existsSync(answer.jjYamlPath)) {
opts.context.error(`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`)
}
jjConfigs.jjYamlObj = jjYamlObj
await fs.writeFile(jjConfigs.configSaveFilePath, answer.jjYamlPath, "utf8")
} else {
jjConfigs.jjYamlPath = await fs.readFile(jjConfigs.configSaveFilePath, "utf8")
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`)
}
jjConfigs.jjYamlObj = jjYamlObj
}
}