278 lines
11 KiB
TypeScript
Executable File
278 lines
11 KiB
TypeScript
Executable File
#!/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";
|
|
import { gitUserEmail, gitUserName, jjUserName } from "./userUtils";
|
|
import chalk from "chalk";
|
|
|
|
export const jjConfigs: any = {
|
|
processEnvs: {},
|
|
};
|
|
|
|
export const jjEnsure = async ({ returnProcessEnvs = false }) => {
|
|
// return
|
|
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.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)) {
|
|
console.log(chalk.yellow.bold(`JJ cli require jj.yaml absolute`));
|
|
|
|
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.JJ_WORKSPACE_DIR = path.dirname(jjConfigs.jjYamlPath);
|
|
|
|
jjConfigs.jjYamlObj = jjYamlObj;
|
|
|
|
await fs.writeFile(jjConfigs.configSaveFilePath, jjConfigs.jjYamlPath, "utf8");
|
|
} else {
|
|
jjConfigs.jjYamlPath = await fs.readFile(jjConfigs.configSaveFilePath, "utf8");
|
|
jjConfigs.JJ_WORKSPACE_DIR = path.dirname(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.jjYamlObj = jjYamlObj;
|
|
}
|
|
|
|
jjConfigs.processEnvs.JJ_WORKSPACE_DIR = jjConfigs.JJ_WORKSPACE_DIR;
|
|
jjConfigs.processEnvs.JJ_TMP_DIR = jjConfigs.JJ_TMP_DIR;
|
|
|
|
jjConfigs.processEnvs.JJ_SOURCE_DIR = jjConfigs.JJ_SOURCE_DIR;
|
|
jjConfigs.processEnvs.JJ_DATA_DIR = jjConfigs.JJ_DATA_DIR;
|
|
jjConfigs.JJ_WORKSPACE_TMP_DIR = `${jjConfigs.JJ_WORKSPACE_DIR}/~~tmp`;
|
|
jjConfigs.processEnvs.JJ_WORKSPACE_TMP_DIR = jjConfigs.JJ_WORKSPACE_TMP_DIR;
|
|
jjConfigs.processEnvs.JJ_WORKSPACE_PROJECT_NAME = jjConfigs.jjYamlObj.name;
|
|
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");
|
|
|
|
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.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}`;
|
|
jjConfigs.processEnvs.JJ_GIT_METADATA_BRANCH = jjConfigs.jjYamlObj.metadataRepo.branch;
|
|
jjConfigs.processEnvs.JJ_GIT_METADATA_MAIN_ORIGIN = jjConfigs.jjYamlObj.metadataRepo.remotes.origin;
|
|
if (jjConfigs.jjYamlObj.metadataRepo.remotes.github) {
|
|
jjConfigs.processEnvs.JJ_GIT_METADATA_GITHUB_ORIGIN = jjConfigs.jjYamlObj.metadataRepo.remotes.github;
|
|
jjConfigs.processEnvs.JJ_GIT_METADATA_GITHUB_ORIGIN_NAME = "github";
|
|
}
|
|
|
|
jjConfigs.processEnvs.JJ_GIT_USER_EMAIL = gitUserEmail;
|
|
jjConfigs.processEnvs.JJ_GIT_USER_NAME = gitUserName;
|
|
jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME = jjUserName(jjConfigs.processEnvs.JJ_GIT_USER_EMAIL);
|
|
|
|
if (jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME && !jjConfigs.jjYamlObj.users[jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME]) {
|
|
jjConfigs.jjYamlObj.users[jjConfigs.processEnvs.JJ_GIT_USER_KEY_NAME] = {
|
|
name: jjConfigs.processEnvs.JJ_GIT_USER_NAME,
|
|
email: jjConfigs.processEnvs.JJ_GIT_USER_EMAIL,
|
|
};
|
|
}
|
|
|
|
saveFileIfItChanged(
|
|
YAML.dump(jjConfigs.jjYamlObj, {
|
|
indent: 4,
|
|
lineWidth: 3,
|
|
forceQuotes: true,
|
|
// sortKeys: true
|
|
}),
|
|
jjConfigs.jjYamlPath
|
|
);
|
|
|
|
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: [],
|
|
},
|
|
{
|
|
key: "listOfEnvs",
|
|
desc: "listOfEnvs",
|
|
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) {
|
|
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.JJ_TMP_DIR}/shEnvs.sh`, shEnvs, "utf-8");
|
|
}
|
|
|
|
return jjConfigs;
|
|
};
|
|
|
|
if (_.get(process.argv, "[1]", "")?.includes("helpers/jjEnsure.ts")) {
|
|
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);
|
|
}
|