forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
272 lines (239 loc) · 12.4 KB
/
cli.ts
File metadata and controls
272 lines (239 loc) · 12.4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { CALM_META_SCHEMA_DIRECTORY, DocifyMode, initLogger, runGenerate, SchemaDirectory, TemplateProcessingMode } from '@finos/calm-shared';
import { Option, Command } from 'commander';
import { version } from '../package.json';
import { promptUserForOptions } from './command-helpers/generate-options';
import { CalmChoice } from '@finos/calm-shared/dist/commands/generate/components/options';
import { buildDocumentLoader, DocumentLoader, DocumentLoaderOptions } from '@finos/calm-shared/dist/document-loader/document-loader';
import { loadCliConfig } from './cli-config';
// Shared options used across multiple commands
const ARCHITECTURE_OPTION = '-a, --architecture <file>';
const OUTPUT_OPTION = '-o, --output <file>';
const SCHEMAS_OPTION = '-s, --schema-directory <path>';
const VERBOSE_OPTION = '-v, --verbose';
// Generate command options
const PATTERN_OPTION = '-p, --pattern <file>';
const CALMHUB_URL_OPTION = '-c, --calm-hub-url <url>';
const CALMHUB_PLUGIN_OPTION = '--calm-hub-plugin <path>';
// Validate command options
const FORMAT_OPTION = '-f, --format <format>';
const STRICT_OPTION = '--strict';
// Server command options
const PORT_OPTION = '--port <port>';
// Template and Docify command options
const BUNDLE_OPTION = '-b, --bundle <path>';
const TEMPLATE_OPTION = '-t, --template <path>';
const TEMPLATE_DIR_OPTION = '-d, --template-dir <path>';
const URL_MAPPING_OPTION = '-u, --url-to-local-file-mapping <path>';
const CLEAR_OUTPUT_DIRECTORY_OPTION = '--clear-output-directory';
export function setupCLI(program: Command) {
program
.name('calm')
.version(version)
.description('A set of tools for interacting with the Common Architecture Language Model (CALM)');
program
.command('generate')
.description('Generate an architecture from a CALM pattern file.')
.requiredOption(PATTERN_OPTION, 'Path to the pattern file to use. May be a file path or a CalmHub URL.')
.requiredOption(OUTPUT_OPTION, 'Path location at which to output the generated file.', 'architecture.json')
.option(SCHEMAS_OPTION, 'Path to the directory containing the meta schemas to use.')
.option(CALMHUB_URL_OPTION, 'URL to CALMHub instance')
.option(CALMHUB_PLUGIN_OPTION, 'Plugin to support custom CALMHub access')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const debug = !!options.verbose;
const docLoaderOpts = await parseDocumentLoaderConfig(options);
const docLoader = buildDocumentLoader(docLoaderOpts);
const schemaDirectory = await buildSchemaDirectory(docLoader, debug);
const pattern: object = await docLoader.loadMissingDocument(options.pattern, 'pattern');
const choices: CalmChoice[] = await promptUserForOptions(pattern, options.verbose);
await runGenerate(pattern, options.output, debug, schemaDirectory, choices);
});
program
.command('validate')
.description('Validate that an architecture conforms to a given CALM pattern.')
.option(PATTERN_OPTION, 'Path to the pattern file to use. May be a file path or a URL.')
.option(ARCHITECTURE_OPTION, 'Path to the architecture file to use. May be a file path or a URL.')
.option(SCHEMAS_OPTION, 'Path to the directory containing the meta schemas to use.', CALM_META_SCHEMA_DIRECTORY)
.option(STRICT_OPTION, 'When run in strict mode, the CLI will fail if any warnings are reported.', false)
.addOption(
new Option(FORMAT_OPTION, 'The format of the output')
.choices(['json', 'junit', 'pretty'])
.default('json')
)
.option(OUTPUT_OPTION, 'Path location at which to output the generated file.')
.option(CALMHUB_URL_OPTION, 'URL to CALMHub instance')
.option(CALMHUB_PLUGIN_OPTION, 'Plugin to support custom CALMHub access')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const { checkValidateOptions, runValidate } = await import('./command-helpers/validate');
checkValidateOptions(program, options, PATTERN_OPTION, ARCHITECTURE_OPTION);
await runValidate({
architecturePath: options.architecture,
patternPath: options.pattern,
metaSchemaPath: options.schemaDirectory,
verbose: !!options.verbose,
strict: options.strict,
outputFormat: options.format,
outputPath: options.output,
calmHubUrl: options.calmHubUrl,
calmHubPlugin: options.calmHubPlugin
});
});
program
.command('server')
.description('Start a HTTP server to proxy CLI commands. (experimental)')
.option(PORT_OPTION, 'Port to run the server on', '3000')
.requiredOption(SCHEMAS_OPTION, 'Path to the directory containing the meta schemas to use.')
.option(CALMHUB_URL_OPTION, 'URL to CALMHub instance')
.option(CALMHUB_PLUGIN_OPTION, 'Plugin to support custom CALMHub access')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const { startServer } = await import('./server/cli-server');
const debug = !!options.verbose;
const docLoaderOpts = await parseDocumentLoaderConfig(options);
const docLoader = buildDocumentLoader(docLoaderOpts);
const schemaDirectory = await buildSchemaDirectory(docLoader, debug);
startServer(options.port, schemaDirectory, debug);
});
program
.command('template')
.description('Generate files from a CALM model using a template bundle, a single file, or a directory of templates')
.requiredOption(ARCHITECTURE_OPTION, 'Path to the CALM architecture JSON file')
.requiredOption(OUTPUT_OPTION, 'Path to output directory or file')
.option(CLEAR_OUTPUT_DIRECTORY_OPTION, 'Clear the output directory before processing', false)
.option(BUNDLE_OPTION, 'Path to the template bundle directory')
.option(TEMPLATE_OPTION, 'Path to a single .hbs or .md template file')
.option(TEMPLATE_DIR_OPTION, 'Path to a directory of .hbs/.md templates')
.option(URL_MAPPING_OPTION, 'Path to mapping file which maps URLs to local paths')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const { getUrlToLocalFileMap } = await import('./command-helpers/template');
const { TemplateProcessor } = await import('@finos/calm-shared');
if (options.verbose) {
process.env.DEBUG = 'true';
}
const localDirectory = getUrlToLocalFileMap(options.urlToLocalFileMapping);
let mode: TemplateProcessingMode;
let templatePath: string;
const flagsUsed = [options.template, options.templateDir, options.bundle].filter(Boolean);
if (flagsUsed.length !== 1) {
console.error('❌ Please specify exactly one of --template, --template-dir, or --bundle');
process.exit(1);
}
if (options.template) {
templatePath = options.template;
mode = 'template';
} else if (options.templateDir) {
templatePath = options.templateDir;
mode = 'template-directory';
} else {
templatePath = options.bundle;
mode = 'bundle';
}
const processor = new TemplateProcessor(
options.architecture,
templatePath,
options.output,
localDirectory,
mode,
false,
options.clearOutputDirectory
);
await processor.processTemplate();
});
program
.command('docify')
.description('Generate a documentation website from your CALM model using a template or template directory')
.requiredOption(ARCHITECTURE_OPTION, 'Path to the CALM architecture JSON file')
.requiredOption(OUTPUT_OPTION, 'Path to output directory')
.option(CLEAR_OUTPUT_DIRECTORY_OPTION, 'Clear the output directory before processing', false)
.option(TEMPLATE_OPTION, 'Path to a single .hbs or .md template file')
.option(TEMPLATE_DIR_OPTION, 'Path to a directory of .hbs/.md templates')
.option(URL_MAPPING_OPTION, 'Path to mapping file which maps URLs to local paths')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const { getUrlToLocalFileMap } = await import('./command-helpers/template');
const { Docifier } = await import('@finos/calm-shared');
if (options.verbose) {
process.env.DEBUG = 'true';
}
const localDirectory = getUrlToLocalFileMap(options.urlToLocalFileMapping);
const flagsUsed = [options.template, options.templateDir].filter(Boolean);
if (flagsUsed.length > 1) {
console.error('❌ Please specify only one of --template or --template-dir');
process.exit(1);
}
let docifyMode: DocifyMode = 'WEBSITE';
let templateProcessingMode: TemplateProcessingMode = 'bundle';
let templatePath: string | undefined = undefined;
if (options.template) {
docifyMode = 'USER_PROVIDED';
templateProcessingMode = 'template';
templatePath = options.template;
} else if (options.templateDir) {
docifyMode = 'USER_PROVIDED';
templateProcessingMode = 'template-directory';
templatePath = options.templateDir;
}
const docifier = new Docifier(
docifyMode,
options.architecture,
options.output,
localDirectory,
templateProcessingMode,
templatePath,
options.clearOutputDirectory
);
await docifier.docify();
});
program
.command('copilot-chatmode')
.description('Augment a git repository with a CALM VSCode chatmode for AI assistance')
.option('-d, --directory <path>', 'Target directory (defaults to current directory)', '.')
.option(VERBOSE_OPTION, 'Enable verbose logging.', false)
.action(async (options) => {
const { setupAiTools } = await import('./command-helpers/ai-tools');
if (options.verbose) {
process.env.DEBUG = 'true';
}
await setupAiTools(options.directory, !!options.verbose);
});
}
export async function parseDocumentLoaderConfig(options): Promise<DocumentLoaderOptions> {
const logger = initLogger(options.verbose, 'calm-cli');
const docLoaderOpts: DocumentLoaderOptions = {
calmHubUrl: options.calmHubUrl,
calmHubPlugin: options.calmHubPlugin,
schemaDirectoryPath: options.schemaDirectory,
debug: !!options.verbose
};
const userConfig = await loadCliConfig();
// Priority:
// HIGHEST: Command line options
// MEDIUM: Environment variables
// LOWEST: Config file
if (!docLoaderOpts.calmHubUrl) {
if (process.env.CALM_HUB_URL) {
logger.info('Using CALMHub URL from environment variable: ' + process.env.CALM_HUB_URL);
docLoaderOpts.calmHubUrl = process.env.CALM_HUB_URL;
}
else if (userConfig && userConfig.calmHubUrl) {
logger.info('Using CALMHub URL from config file: ' + userConfig.calmHubUrl);
docLoaderOpts.calmHubUrl = userConfig.calmHubUrl;
}
}
if (!docLoaderOpts.calmHubPlugin) {
if (process.env.CALM_HUB_PLUGIN) {
logger.info('Using CALMHub Plugin from environment variable: ' + process.env.CALM_HUB_PLUGIN);
docLoaderOpts.calmHubPlugin = process.env.CALM_HUB_PLUGIN;
}
else if (userConfig && userConfig.calmHubPlugin) {
logger.info('Using CALMHub Plugin from config file: ' + userConfig.calmHubPlugin);
docLoaderOpts.calmHubPlugin = userConfig.calmHubPlugin;
}
}
return docLoaderOpts;
}
export async function buildSchemaDirectory(docLoader: DocumentLoader, debug: boolean): Promise<SchemaDirectory> {
return new SchemaDirectory(docLoader, debug);
}