Skip to content

Commit

Permalink
fix(ama-sdk): support of URL as spec-path input
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Mar 29, 2024
1 parent d5269bc commit 135d844
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const schematicArgs = [
const resolveTargetDirectory = resolve(process.cwd(), targetDirectory);

const run = () => {
const isSpecPathUrl = url.URL.canParse(argv['spec-path']);
const isSpecPathUrl = !!argv['spec-path'] && url.URL.canParse(argv['spec-path']);

const runner = process.platform === 'win32' ? `${packageManager}.cmd` : packageManager;
const steps: { args: string[]; cwd?: string; runner?: string }[] = [
Expand Down
43 changes: 29 additions & 14 deletions packages/@ama-sdk/schematics/schematics/typescript/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import type { Operation, PathObject } from '@ama-sdk/core';
import { readFileSync } from 'node:fs';
import * as path from 'node:path';
import { URL } from 'node:url';
import * as semver from 'semver';
import * as sway from 'sway';

Expand Down Expand Up @@ -166,9 +167,30 @@ const getGeneratorOptions = (tree: Tree, context: SchematicContext, options: NgG
*/
function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematicsSchema): Rule {

return (tree, context) => {
return async (tree, context) => {
const targetPath = options.directory || '';
const generatorOptions = getGeneratorOptions(tree, context, options);
let isJson = false;
let specDefaultPath = path.posix.join(targetPath, 'openapi.yaml');
generatorOptions.specPath ||= specDefaultPath;

const specContent = URL.canParse(generatorOptions.specPath) ? await (await fetch(generatorOptions.specPath)).text() : readFileSync(generatorOptions.specPath, {encoding: 'utf-8'}).toString();

try {
JSON.parse(specContent);
isJson = true;
} catch (e) {
isJson = false;
}
const defaultFileName = `openapi.${isJson ? 'json' : 'yaml'}`;
specDefaultPath = path.posix.join(targetPath, defaultFileName);
generatorOptions.specPath = specDefaultPath;

if (tree.exists(specDefaultPath)) {
tree.overwrite(specDefaultPath, specContent);
} else {
tree.create(specDefaultPath, specContent);
}

/**
* rule to clear previous SDK generation
Expand All @@ -182,7 +204,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic

const generateOperationFinder = async (): Promise<PathObject[]> => {
const swayOptions = {
definition: path.resolve(generatorOptions.specPath!)
definition: specDefaultPath
};
const swayApi = await sway.create(swayOptions);
const extraction = swayApi.getPaths().map((obj) => ({
Expand Down Expand Up @@ -212,26 +234,19 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
};

/**
* Update local swagger spec file
* Update readme version
*/
const updateSpec = () => {
const updateSpecVersion = () => {
const readmeFile = path.posix.join(targetPath, 'readme.md');
const specContent = readFileSync(generatorOptions.specPath!).toString();
if (tree.exists(readmeFile)) {
const specVersion = /version: *([0-9]+\.[0-9]+\.[0-9]+(?:-[^ ]+)?)/.exec(specContent);

if (specVersion) {
const readmeContent = tree.read(readmeFile)!.toString('utf8');
tree.overwrite(readmeFile, readmeContent.replace(/Based on (OpenAPI|Swagger) spec .*/i, `Based on $1 spec ${specVersion[1]}`));
tree.overwrite(readmeFile, readmeContent.replace(/Based on OpenAPI spec .*/i, `Based on $1 spec ${specVersion[1]}`));
}
}

if (tree.exists(path.posix.join(targetPath, 'swagger-spec.yaml'))) {
tree.overwrite(path.posix.join(targetPath, 'swagger-spec.yaml'), specContent);
} else {
tree.create(path.posix.join(targetPath, 'swagger-spec.yaml'), specContent);
}
return () => tree;
return tree;
};

const runGeneratorRule = () => {
Expand All @@ -245,7 +260,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
return chain([
clearGeneratedCode,
generateSource,
updateSpec,
updateSpecVersion,
runGeneratorRule
]);
};
Expand Down

0 comments on commit 135d844

Please sign in to comment.