@kosko/cli
Version:
Organize Kubernetes manifests in JavaScript.
80 lines • 2.71 kB
JavaScript
import fs from "fs-extra";
import makeDir from "make-dir";
import { join, resolve } from "path";
import { getLogger } from "../cli/command.mjs";
import Debug from "../cli/debug.mjs";
import { CLIError } from "../cli/error.mjs";
const debug = Debug.extend("init");
const DEFAULT_CONFIG = `components = ["*"]`;
function sortKeys(data) {
const result = {};
for (const key of Object.keys(data).sort()) {
result[key] = data[key];
}
return result;
}
async function updatePkg(path, data) {
let base = {};
try {
debug("Reading existing package.json from", path);
base = JSON.parse(await fs.readFile(path, "utf8"));
}
catch (err) {
if (err.code !== "ENOENT")
throw err;
}
debug("Writing package.json at", path);
await fs.writeJSON(path, {
...base,
...data,
dependencies: sortKeys({
...base.dependencies,
...data.dependencies
})
}, { spaces: 2 });
}
export const initCmd = {
command: "init [path]",
describe: "Set up a new kosko directory",
builder(argv) {
/* istanbul ignore next */
return argv
.option("force", {
type: "boolean",
describe: "Overwrite existing files",
default: false,
alias: "f"
})
.positional("path", { type: "string", describe: "Path to initialize" })
.example("$0 init", "Initialize in current directory")
.example("$0 init example", "Initialize in specified directory");
},
async handler(args) {
const logger = getLogger(args);
const path = args.path ? resolve(args.cwd, args.path) : args.cwd;
logger.info("Initialize in", path);
const exist = await fs.pathExists(path);
if (exist && !args.force) {
throw new CLIError("Already exists", {
output: `Already exists. Use "--force" to overwrite existing files.`
});
}
for (const name of ["components", "environments", "templates"]) {
const dir = join(path, name);
debug("Creating directory", dir);
await makeDir(dir);
}
await updatePkg(join(path, "package.json"), {
dependencies: {
"@kosko/env": "^1.0.1",
kosko: "^1.0.1",
"kubernetes-models": "^1.0.3"
}
});
const configPath = join(path, "kosko.toml");
debug("Writing config", configPath);
await fs.writeFile(configPath, DEFAULT_CONFIG);
logger.success(`We are almost there. Run "npm install" to finish the setup.`);
}
};
//# sourceMappingURL=init.mjs.map