-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.ts
110 lines (91 loc) · 2.84 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from "fs";
import makeDir from "make-dir";
import { join, resolve } from "path";
import { promisify } from "util";
import { Command, getLogger, RootArguments } from "../cli/command";
import Debug from "../cli/debug";
import { CLIError } from "../cli/error";
const debug = Debug.extend("init");
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const DEFAULT_CONFIG = `components = ["*"]`;
function exists(path: string): Promise<boolean> {
return new Promise((res): void => fs.exists(path, res));
}
export function writeJSON(path: string, data: any): Promise<void> {
return writeFile(path, JSON.stringify(data, null, " "));
}
function sortKeys(data: any): any {
const result: any = {};
for (const key of Object.keys(data).sort()) {
result[key] = data[key];
}
return result;
}
async function updatePkg(path: string, data: any): Promise<void> {
let base: any = {};
try {
debug("Reading existing package.json from", path);
base = JSON.parse(await readFile(path, "utf8"));
} catch (err) {
if (err.code !== "ENOENT") throw err;
}
debug("Writing package.json at", path);
await writeJSON(path, {
...base,
...data,
dependencies: sortKeys({
...base.dependencies,
...data.dependencies
})
});
}
export interface InitArguments extends RootArguments {
force: boolean;
path?: string;
}
export const initCmd: Command<InitArguments> = {
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 exists(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": "^0.4.1",
"kubernetes-models": "^0.5.0"
}
});
const configPath = join(path, "kosko.toml");
debug("Writing config", configPath);
await writeFile(configPath, DEFAULT_CONFIG);
logger.success(
`We are almost there. Run "npm install" to finish the setup.`
);
}
};