1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const fs_1 = tslib_1.__importDefault(require("fs"));
|
5 | const make_dir_1 = tslib_1.__importDefault(require("make-dir"));
|
6 | const path_1 = require("path");
|
7 | const util_1 = require("util");
|
8 | const command_1 = require("../cli/command");
|
9 | const debug_1 = tslib_1.__importDefault(require("../cli/debug"));
|
10 | const error_1 = require("../cli/error");
|
11 | const debug = debug_1.default.extend("init");
|
12 | const readFile = util_1.promisify(fs_1.default.readFile);
|
13 | const writeFile = util_1.promisify(fs_1.default.writeFile);
|
14 | const DEFAULT_CONFIG = `components = ["*"]`;
|
15 | function exists(path) {
|
16 | return new Promise((res) => fs_1.default.exists(path, res));
|
17 | }
|
18 | function writeJSON(path, data) {
|
19 | return writeFile(path, JSON.stringify(data, null, " "));
|
20 | }
|
21 | exports.writeJSON = writeJSON;
|
22 | function sortKeys(data) {
|
23 | const result = {};
|
24 | for (const key of Object.keys(data).sort()) {
|
25 | result[key] = data[key];
|
26 | }
|
27 | return result;
|
28 | }
|
29 | function updatePkg(path, data) {
|
30 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
31 | let base = {};
|
32 | try {
|
33 | debug("Reading existing package.json from", path);
|
34 | base = JSON.parse(yield readFile(path, "utf8"));
|
35 | }
|
36 | catch (err) {
|
37 | if (err.code !== "ENOENT")
|
38 | throw err;
|
39 | }
|
40 | debug("Writing package.json at", path);
|
41 | yield writeJSON(path, Object.assign({}, base, data, { dependencies: sortKeys(Object.assign({}, base.dependencies, data.dependencies)) }));
|
42 | });
|
43 | }
|
44 | exports.initCmd = {
|
45 | command: "init [path]",
|
46 | describe: "Set up a new kosko directory",
|
47 | builder(argv) {
|
48 |
|
49 | return argv
|
50 | .option("force", {
|
51 | type: "boolean",
|
52 | describe: "Overwrite existing files",
|
53 | default: false,
|
54 | alias: "f"
|
55 | })
|
56 | .positional("path", { type: "string", describe: "Path to initialize" })
|
57 | .example("$0 init", "Initialize in current directory")
|
58 | .example("$0 init example", "Initialize in specified directory");
|
59 | },
|
60 | handler(args) {
|
61 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
62 | const logger = command_1.getLogger(args);
|
63 | const path = args.path ? path_1.resolve(args.cwd, args.path) : args.cwd;
|
64 | logger.info("Initialize in", path);
|
65 | const exist = yield exists(path);
|
66 | if (exist && !args.force) {
|
67 | throw new error_1.CLIError("Already exists", {
|
68 | output: `Already exists. Use "--force" to overwrite existing files.`
|
69 | });
|
70 | }
|
71 | for (const name of ["components", "environments", "templates"]) {
|
72 | const dir = path_1.join(path, name);
|
73 | debug("Creating directory", dir);
|
74 | yield make_dir_1.default(dir);
|
75 | }
|
76 | yield updatePkg(path_1.join(path, "package.json"), {
|
77 | dependencies: {
|
78 | "@kosko/env": "^0.4.1",
|
79 | "kubernetes-models": "^0.5.0"
|
80 | }
|
81 | });
|
82 | const configPath = path_1.join(path, "kosko.toml");
|
83 | debug("Writing config", configPath);
|
84 | yield writeFile(configPath, DEFAULT_CONFIG);
|
85 | logger.success(`We are almost there. Run "npm install" to finish the setup.`);
|
86 | });
|
87 | }
|
88 | };
|
89 |
|
\ | No newline at end of file |