@kosko/cli
Version:
Organize Kubernetes manifests in JavaScript.
84 lines • 3.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initCmd = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const make_dir_1 = tslib_1.__importDefault(require("make-dir"));
const path_1 = require("path");
const command_1 = require("../cli/command.js");
const debug_1 = tslib_1.__importDefault(require("../cli/debug.js"));
const error_1 = require("../cli/error.js");
const debug = debug_1.default.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_extra_1.default.readFile(path, "utf8"));
}
catch (err) {
if (err.code !== "ENOENT")
throw err;
}
debug("Writing package.json at", path);
await fs_extra_1.default.writeJSON(path, {
...base,
...data,
dependencies: sortKeys({
...base.dependencies,
...data.dependencies
})
}, { spaces: 2 });
}
exports.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 = command_1.getLogger(args);
const path = args.path ? path_1.resolve(args.cwd, args.path) : args.cwd;
logger.info("Initialize in", path);
const exist = await fs_extra_1.default.pathExists(path);
if (exist && !args.force) {
throw new error_1.CLIError("Already exists", {
output: `Already exists. Use "--force" to overwrite existing files.`
});
}
for (const name of ["components", "environments", "templates"]) {
const dir = path_1.join(path, name);
debug("Creating directory", dir);
await make_dir_1.default(dir);
}
await updatePkg(path_1.join(path, "package.json"), {
dependencies: {
"@kosko/env": "^1.0.1",
kosko: "^1.0.1",
"kubernetes-models": "^1.0.3"
}
});
const configPath = path_1.join(path, "kosko.toml");
debug("Writing config", configPath);
await fs_extra_1.default.writeFile(configPath, DEFAULT_CONFIG);
logger.success(`We are almost there. Run "npm install" to finish the setup.`);
}
};
//# sourceMappingURL=init.js.map