Skip to content

Commit 81a6003

Browse files
author
Peter Hauge
committed
Enable using apiops from public npm registry
1 parent faf69e1 commit 81a6003

4 files changed

Lines changed: 40 additions & 28 deletions

File tree

src/cli/init-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface InitOptions {
1717
nonInteractive: boolean;
1818
artifactDir: string;
1919
environments: string;
20-
cliPackage: string;
20+
cliPackage?: string;
2121
force: boolean;
2222
}
2323

@@ -31,7 +31,7 @@ export function createInitCommand(): Command {
3131
.option('--non-interactive', 'Skip interactive prompts (requires --ci)', false)
3232
.option('--artifact-dir <dir>', 'Artifact directory path', './apim-artifacts')
3333
.option('--environments <list>', 'Comma-separated environment names', 'dev,prod')
34-
.requiredOption('--cli-package <path>', 'Path to apiops npm tarball (from npm pack)')
34+
.option('--cli-package <path>', 'Path to apiops npm tarball (from npm pack). If not provided, uses @peterhauge/apiops-cli from npm registry')
3535
.option('--force', 'Overwrite existing files without prompting', false)
3636
.action(async (options: InitOptions) => {
3737
try {

src/models/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ export interface InitConfig {
8888
artifactDir: string;
8989
environments: string[];
9090
outputDir: string;
91-
cliPackage: string;
91+
cliPackage?: string;
9292
force: boolean;
9393
}

src/services/init-service.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class InitServiceImpl implements InitService {
4949
async run(config: InitConfig): Promise<GeneratedFiles> {
5050
logger.info('Starting APIM repository initialization...');
5151

52-
// Validate that the CLI package tarball exists
53-
await this.validateCliPackage(config.cliPackage);
52+
// Validate that the CLI package tarball exists (only if provided)
53+
if (config.cliPackage) {
54+
await this.validateCliPackage(config.cliPackage);
55+
}
5456

5557
// Gather configuration (interactive or from flags)
5658
const finalConfig = await this.gatherConfiguration(config);
@@ -247,17 +249,24 @@ class InitServiceImpl implements InitService {
247249
await fs.writeFile(gitkeepPath, '');
248250
generatedFiles.directories.push(config.artifactDir);
249251

250-
// Copy CLI tarball into .apiops/ directory
251-
const apiopsDir = path.join(config.outputDir, '.apiops');
252-
await fs.mkdir(apiopsDir, { recursive: true });
253-
const tarballFilename = path.basename(config.cliPackage);
254-
const tarballDest = path.join(apiopsDir, tarballFilename);
255-
await fs.copyFile(path.resolve(config.cliPackage), tarballDest);
256-
generatedFiles.directories.push('.apiops');
257-
258-
// Generate package.json with local tarball dependency
259-
const tarballRelPath = path.join('.apiops', tarballFilename);
260-
const packageJsonContent = generatePackageJson({ tarballRelPath });
252+
// Generate package.json - mode depends on whether cliPackage is provided
253+
let packageJsonContent: string;
254+
if (config.cliPackage) {
255+
// Local tarball mode: copy tarball and reference via file: dependency
256+
const apiopsDir = path.join(config.outputDir, '.apiops');
257+
await fs.mkdir(apiopsDir, { recursive: true });
258+
const tarballFilename = path.basename(config.cliPackage);
259+
const tarballDest = path.join(apiopsDir, tarballFilename);
260+
await fs.copyFile(path.resolve(config.cliPackage), tarballDest);
261+
generatedFiles.directories.push('.apiops');
262+
263+
const tarballRelPath = path.join('.apiops', tarballFilename);
264+
packageJsonContent = generatePackageJson({ mode: 'local', tarballRelPath });
265+
} else {
266+
// Public npm mode: use registry package
267+
packageJsonContent = generatePackageJson({ mode: 'npm' });
268+
}
269+
261270
const packageJsonPath = path.join(config.outputDir, 'package.json');
262271
await fs.writeFile(packageJsonPath, packageJsonContent);
263272
generatedFiles.configs.push('package.json');
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
/**
22
* Generates a package.json for the target repo that references
3-
* the apiops CLI tarball via a local file dependency.
3+
* either a local apiops CLI tarball or the public npm package.
44
*/
55

6-
export interface PackageJsonConfig {
7-
/** Relative path from the target repo root to the copied tarball (e.g. '.apiops/apiops-0.1.0.tgz') */
8-
tarballRelPath: string;
9-
}
6+
export type PackageJsonConfig =
7+
| { mode: 'local'; tarballRelPath: string }
8+
| { mode: 'npm' };
109

1110
export function generatePackageJson(config: PackageJsonConfig): string {
12-
// Use forward slashes in the file: dependency regardless of OS
13-
const posixPath = config.tarballRelPath.replace(/\\/g, '/');
14-
15-
const pkg = {
11+
const pkg: Record<string, unknown> = {
1612
name: 'apim-ops-repo',
1713
version: '1.0.0',
1814
private: true,
1915
description: 'Azure API Management configuration-as-code repository',
20-
dependencies: {
21-
apiops: `file:${posixPath}`,
22-
},
16+
dependencies: {},
2317
};
2418

19+
if (config.mode === 'local') {
20+
// Use forward slashes in the file: dependency regardless of OS
21+
const posixPath = config.tarballRelPath.replace(/\\/g, '/');
22+
(pkg.dependencies as Record<string, string>).apiops = `file:${posixPath}`;
23+
} else {
24+
// Public npm registry mode
25+
(pkg.dependencies as Record<string, string>)['@peterhauge/apiops-cli'] = 'latest';
26+
}
27+
2528
return JSON.stringify(pkg, null, 2) + '\n';
2629
}

0 commit comments

Comments
 (0)