-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit-command.ts
More file actions
111 lines (98 loc) · 4.29 KB
/
Copy pathinit-command.ts
File metadata and controls
111 lines (98 loc) · 4.29 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* T050: Register init command
* Commander subcommand with --ci, --non-interactive, --artifact-dir, --environments flags
* Wire to init-service
*/
import { Command } from 'commander';
import { InitConfig } from '../models/config.js';
import { initService } from '../services/init-service.js';
import { logger } from '../lib/logger.js';
/**
* Interface for init command options (from CLI flags)
*/
interface InitOptions {
ci?: string;
nonInteractive: boolean;
artifactDir: string;
environments: string;
cliPackage?: string;
force: boolean;
}
/**
* Create and return the init command for Commander
*/
export function createInitCommand(): Command {
const init = new Command('init')
.description('Initialize APIM repository with CI/CD pipelines and configuration templates')
.option('--ci <provider>', 'CI/CD provider: github-actions or azure-devops')
.option('--non-interactive', 'Skip interactive prompts (requires --ci)', false)
.option('--artifact-dir <dir>', 'Artifact directory path', './apim-artifacts')
.option('--environments <list>', 'Comma-separated environment names', 'dev,prod')
.option('--cli-package <path>', 'Path to apiops npm tarball (from npm pack). If not provided, uses @peterhauge/apiops-cli from npm registry')
.option('--force', 'Overwrite existing files without prompting', false)
.action(async (options: InitOptions) => {
try {
// Use pretty log format for init (human-facing command)
logger.setFormat('pretty');
// Validate CI provider if specified
if (options.ci && options.ci !== 'github-actions' && options.ci !== 'azure-devops') {
logger.error('Invalid CI provider. Must be "github-actions" or "azure-devops"');
process.exit(1);
}
// Parse environments
const environments = options.environments
.split(',')
.map((env) => env.trim())
.filter((env) => env.length > 0);
if (environments.length === 0) {
logger.error('At least one environment must be specified');
process.exit(1);
}
// Build config
const config: InitConfig = {
ciProvider: options.ci as 'github-actions' | 'azure-devops' | undefined,
nonInteractive: options.nonInteractive,
artifactDir: options.artifactDir,
environments,
outputDir: process.cwd(),
cliPackage: options.cliPackage,
force: options.force,
};
// Run init service
const generatedFiles = await initService.run(config);
// Output file listing
const allFiles = [
...generatedFiles.pipelines,
...generatedFiles.configs,
];
logger.info(`\nGenerated ${allFiles.length} file(s):`);
allFiles.forEach((file) => logger.info(` - ${file.startsWith('./') ? file : './' + file}`));
logger.info(`\nCreated ${generatedFiles.directories.length} directory/directories:`);
generatedFiles.directories.forEach((dir) => logger.info(` - ${dir.startsWith('./') ? dir : './' + dir}`));
// Determine which CI provider was actually used by checking generated files
const isGitHub = allFiles.some((f) => f.includes('IDENTITY-SETUP-GITHUB.md'));
logger.info('\nNext steps:');
logger.info(' 1. Review and customize the generated configuration files');
logger.info(' 2. Commit the generated files to your repository');
logger.info(' 3. Set up CI/CD identity authentication:');
if (isGitHub) {
logger.info(' - Follow ./IDENTITY-SETUP-GITHUB.md for manual setup, OR');
logger.info(' - Open ./.github/prompts/apiops-setup-identity.prompt.md with GitHub Copilot for guided setup');
} else {
logger.info(' - Follow ./IDENTITY-SETUP-AZDO.md for manual setup, OR');
logger.info(' - Open ./.github/prompts/apiops-setup-identity.prompt.md with GitHub Copilot for guided setup');
}
logger.info('');
} catch (error) {
if (error instanceof Error) {
logger.error('Init failed:', error.message);
} else {
logger.error('Init failed with unknown error');
}
process.exit(1);
}
});
return init;
}