UNPKG

4.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const config_1 = require("@kosko/config");
5const generate_1 = require("@kosko/generate");
6const require_1 = require("@kosko/require");
7const path_1 = require("path");
8const debug_1 = tslib_1.__importDefault(require("../cli/debug"));
9const error_1 = require("../cli/error");
10const debug = debug_1.default.extend("generate");
11function localRequire(id, cwd) {
12 return tslib_1.__awaiter(this, void 0, void 0, function* () {
13 debug("Finding module %s in %s", id, cwd);
14 const path = yield require_1.resolve(id, { basedir: cwd });
15 debug("Importing %s from %s", id, path);
16 return require_1.requireDefault(path);
17 });
18}
19function importEnv(cwd) {
20 return tslib_1.__awaiter(this, void 0, void 0, function* () {
21 return localRequire("@kosko/env", cwd);
22 });
23}
24function resolveConfig(base, args) {
25 const { components = [], require = [] } = args.env
26 ? config_1.getConfig(base, args.env)
27 : base;
28 return {
29 components: args.components && args.components.length ? args.components : components,
30 require: [...require, ...(args.require || [])]
31 };
32}
33/* istanbul ignore next */
34function generateBuilder(argv) {
35 return argv
36 .option("env", {
37 type: "string",
38 describe: "Environment name",
39 alias: "e"
40 })
41 .option("require", {
42 type: "array",
43 describe: "Require modules. Modules set in config file will also be required.",
44 default: [],
45 alias: "r"
46 })
47 .positional("components", {
48 describe: "Components to generate. This overrides components set in config file."
49 });
50}
51exports.generateBuilder = generateBuilder;
52function generateHandler(args) {
53 return tslib_1.__awaiter(this, void 0, void 0, function* () {
54 // Load config
55 const globalConfig = yield config_1.searchConfig(args.cwd);
56 const config = Object.assign({}, globalConfig, resolveConfig(globalConfig, args));
57 if (!config.components.length) {
58 throw new error_1.CLIError("No components are given", {
59 output: "No components are given. Set components in a config file or in arguments."
60 });
61 }
62 // Set env
63 if (args.env) {
64 const env = yield importEnv(args.cwd);
65 env.cwd = args.cwd;
66 env.env = args.env;
67 if (config.paths && config.paths.environment) {
68 const paths = config.paths.environment;
69 if (paths.global)
70 env.paths.global = paths.global;
71 if (paths.component)
72 env.paths.component = paths.component;
73 }
74 debug("Set env as", args.env);
75 }
76 // Require external modules
77 for (const id of config.require) {
78 yield localRequire(id, args.cwd);
79 }
80 // Generate manifests
81 const result = yield generate_1.generate({
82 path: path_1.join(args.cwd, "components"),
83 components: config.components,
84 extensions: config.extensions,
85 validate: args.validate
86 });
87 if (!result.manifests.length) {
88 throw new error_1.CLIError("No manifests are exported from components", {
89 output: `No manifests are exported from components. Make sure there are exported manifests in components.`
90 });
91 }
92 return result;
93 });
94}
95exports.generateHandler = generateHandler;
96exports.generateCmd = {
97 command: "generate [components..]",
98 describe: "Generate Kubernetes manifests",
99 builder(argv) {
100 /* istanbul ignore next */
101 return generateBuilder(argv)
102 .option("output", {
103 type: "string",
104 describe: "Output format",
105 default: generate_1.PrintFormat.YAML,
106 choices: Object.keys(generate_1.PrintFormat).map(key => generate_1.PrintFormat[key]),
107 alias: "o"
108 })
109 .option("validate", {
110 type: "boolean",
111 describe: "Validate components",
112 default: true
113 })
114 .example("$0 generate", "Generate manifests")
115 .example("$0 generate foo bar", "Specify components")
116 .example("$0 generate foo_*", "Use glob pattern")
117 .example("$0 generate --env foo", "Set environment")
118 .example("$0 generate -r ts-node/register", "Require external modules");
119 },
120 handler(args) {
121 return tslib_1.__awaiter(this, void 0, void 0, function* () {
122 const result = yield generateHandler(args);
123 generate_1.print(result, {
124 format: args.output,
125 writer: process.stdout
126 });
127 });
128 }
129};
130//# sourceMappingURL=generate.js.map
\No newline at end of file