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 e669b21
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 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
46 changes: 31 additions & 15 deletions packages/@ama-sdk/schematics/schematics/typescript/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
url
} from '@angular-devkit/schematics';
import type { Operation, PathObject } from '@ama-sdk/core';
import { readFileSync } from 'node:fs';
import { existsSync, 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,31 @@ 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.json');
specDefaultPath = existsSync(specDefaultPath) ? 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();

Check failure on line 178 in packages/@ama-sdk/schematics/schematics/typescript/core/index.ts

View workflow job for this annotation

GitHub Actions / checks / test (windows-latest)

Typescript Core Generator › should update readme

TypeError: fetch failed at schematics/typescript/core/index.ts:178:74 at callRuleAsync (../../../.yarn/cache/@angular-devkit-schematics-npm-17.3.1-37da5b6ea3-1c1a7b1e17.zip/node_modules/@angular-devkit/schematics/src/rules/call.js:80:18)

Check failure on line 178 in packages/@ama-sdk/schematics/schematics/typescript/core/index.ts

View workflow job for this annotation

GitHub Actions / checks / test (windows-latest)

Typescript Core Generator › should clean previous install

TypeError: fetch failed at schematics/typescript/core/index.ts:178:74 at callRuleAsync (../../../.yarn/cache/@angular-devkit-schematics-npm-17.3.1-37da5b6ea3-1c1a7b1e17.zip/node_modules/@angular-devkit/schematics/src/rules/call.js:80:18)

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 +205,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 +235,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 +261,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
return chain([
clearGeneratedCode,
generateSource,
updateSpec,
updateSpecVersion,
runGeneratorRule
]);
};
Expand Down

0 comments on commit e669b21

Please sign in to comment.