Skip to content

Commit

Permalink
fix: default specs filename on sdk generation; publish showcase specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrednic-1A committed May 23, 2024
1 parent d90fbaa commit 8ea051a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 deletions.
14 changes: 10 additions & 4 deletions packages/@ama-sdk/core/cli/update-spec-from-npm.cts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import { extname, join } from 'node:path';
import { copyFile, readFile } from 'node:fs/promises';
import type { PackageJson } from 'type-fest';
import type { OpenApiToolsConfiguration } from '../src/fwk/open-api-tools-configuration';
import { DEFAULT_SPEC_FILENAME_IN_NPM_MODULE, LOCAL_SPEC_FILENAME, SPEC_JSON_EXTENSION, SPEC_YAML_EXTENSION, SPEC_YML_EXTENSION } from '../src/utils/generators';

const argv = minimist(process.argv.slice(2));
const packageName = argv._[0];
const { help, output, 'package-path': packagePath, quiet } = argv;
const openApiConfigDefaultPath = './openapitools.json';
const supportedExtensions = ['json', 'yaml', 'yml'];
const noop = () => undefined;
const logger = quiet ? {error: noop, warn: noop, log: noop, info: noop, debug: noop} : console;

const supportedExtensions = [SPEC_JSON_EXTENSION, SPEC_YAML_EXTENSION, SPEC_YML_EXTENSION];

if (help) {
// eslint-disable-next-line no-console
console.log(`This script can be used to update your local spec file from a given locally installed npm package.
Usage: amasdk-update-spec-from-npm <package-name> [--package-path] [--output] [--quiet]
Expand All @@ -43,7 +46,7 @@ void (async () => {
const packageJsonPath = appRequire.resolve(`${packageName}/package.json`);
if (!packagePath) {
const packageJson = JSON.parse(await readFile(packageJsonPath, {encoding: 'utf8'})) as PackageJson;
const exportMatcher = new RegExp(`openapi\\.(?:${supportedExtensions.join('|')})$`);
const exportMatcher = new RegExp(`${DEFAULT_SPEC_FILENAME_IN_NPM_MODULE}\\.(?:${supportedExtensions.join('|')})$`);
const matchingExport = packageJson.exports && Object.keys(packageJson.exports).find((exportPath) => exportMatcher.test(exportPath));
if (matchingExport) {
specSourcePath = appRequire.resolve(`${packageName}/${matchingExport}`);
Expand All @@ -58,8 +61,11 @@ void (async () => {

let specDestinationPath = output;
if (!specDestinationPath) {
const specSourceExtension = extname(specSourcePath);
specDestinationPath = `./openapi${specSourceExtension}`;
let specSourceExtension = extname(specSourcePath);
if (specSourceExtension === `.${SPEC_YAML_EXTENSION}` || `.${SPEC_YML_EXTENSION}`) {
specSourceExtension = `.${SPEC_YML_EXTENSION}`;
}
specDestinationPath = `./${LOCAL_SPEC_FILENAME}${specSourceExtension}`;
if (existsSync(openApiConfigDefaultPath)) {
const openApiConfig = JSON.parse(await readFile(openApiConfigDefaultPath, {encoding: 'utf8'})) as OpenApiToolsConfiguration;
const generators = Object.values(openApiConfig['generator-cli']?.generators ?? {});
Expand Down
10 changes: 10 additions & 0 deletions packages/@ama-sdk/core/src/utils/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** Name of the specification file copied locally (without extension) */
export const LOCAL_SPEC_FILENAME = 'openapi';
/** Extension of the Specification file in YAML format */
export const SPEC_YAML_EXTENSION = 'yaml';
/** Extension of the Specification file in YAML format */
export const SPEC_YML_EXTENSION = 'yml';
/** Extension of the Specification file in JSON format */
export const SPEC_JSON_EXTENSION = 'json';
/* Default path of the specification file inside npm module (without extension)*/
export const DEFAULT_SPEC_FILENAME_IN_NPM_MODULE = 'openapi';
1 change: 1 addition & 0 deletions packages/@ama-sdk/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './ie11';
export * from './json-token';
export * from './mime-types';
export * from './generic-api';
export * from './generators';
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ npm create @ama-sdk typescript <project-name> -- --package-manager=yarn [...opti

- `--spec-path`: Path to the swagger/open-api specification used to generate the SDK
- `--spec-package-name`: The npm package name where the spec file can be fetched
- `--spec-package-path`: The path inside the package where to find the spec file
- `--spec-package-path`: The path inside the package where to find the spec file. Defaults to _openapi.yml_
- `--spec-package-version`: The version to target for the npm package where the spec file can be fetched
- `--spec-package-registry`: The npm registry where the spec file can be fetched

Expand Down
16 changes: 14 additions & 2 deletions packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-console */

import { execSync, spawnSync } from 'node:child_process';
import { dirname, join, parse, relative, resolve } from 'node:path';
import { dirname, extname, join, parse, relative, resolve } from 'node:path';
import * as minimist from 'minimist';

const packageManagerEnv = process.env.npm_config_user_agent?.split('/')[0];
Expand Down Expand Up @@ -64,6 +64,17 @@ if (argv['spec-path'] && argv['spec-package-name']) {
console.error('--spec-path cannot be set with --spec-package-name');
process.exit(-4);
}
/* Default path of the specification file inside npm module */
const DEFAUL_SPEC_FILEPATH_IN_NPM_MODULE = './openapi.yaml';
/* Name of the specification file copied locally (without extension) */
const LOCAL_SPEC_FILENAME = 'openapi';
/* The local file (path) which will be created in case of generation using specs from an npm module */
let localFilePathToBeCreated: string | undefined;

if (argv['spec-package-name']) {
const specPathInNpmPackage = argv['spec-package-path'] || DEFAUL_SPEC_FILEPATH_IN_NPM_MODULE;
localFilePathToBeCreated = `./${LOCAL_SPEC_FILENAME}${extname(specPathInNpmPackage)}`;
}

const commonSchematicArgs = [
argv.debug !== undefined ? `--debug=${argv.debug as string}` : '--debug=false', // schematics enable debug mode per default when using schematics with relative path
Expand All @@ -88,7 +99,8 @@ const run = () => {
];
const coreSchematicArgs = [
...commonSchematicArgs,
'--spec-path', argv['spec-package-name'] ? './openapi.yml' : isSpecRelativePath ? relative(resolveTargetDirectory, resolve(process.cwd(), argv['spec-path'])) : argv['spec-path']
'--spec-path',
localFilePathToBeCreated || (isSpecRelativePath ? relative(resolveTargetDirectory, resolve(process.cwd(), argv['spec-path'])) : argv['spec-path'])
];

const runner = process.platform === 'win32' ? `${packageManager}.cmd` : packageManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OpenApiToolsConfiguration, OpenApiToolsGenerator, PathObject } from '@ama-sdk/core';
import { LOCAL_SPEC_FILENAME, SPEC_JSON_EXTENSION, SPEC_YAML_EXTENSION } from '@ama-sdk/core';
import {
apply,
chain,
Expand Down Expand Up @@ -27,15 +28,6 @@ import { generateOperationFinderFromSingleFile } from './helpers/path-extractor'
const JAVA_OPTIONS = ['specPath', 'specConfigPath', 'globalProperty', 'outputPath'];
const OPEN_API_TOOLS_OPTIONS = ['generatorName', 'output', 'inputSpec', 'config', 'globalProperty'];

// TODO: Change to `open-api` when #1735 is done
/** Name of the specification file copied locally (without extension) */
export const LOCAL_SPEC_FILENAME = 'swagger-spec';
/** Extension of the Specification file in YAML format */
export const SPEC_YAML_EXTENSION = 'yaml';
// TODO: Change to `json` when #1735 is done
/** Extension of the Specification file in JSON format */
export const SPEC_JSON_EXTENSION = 'yaml';

const getRegexpTemplate = (regexp: RegExp) => `new RegExp('${regexp.toString().replace(/\/(.*)\//, '$1').replace(/\\\//g, '/')}')`;

const getPathObjectTemplate = (pathObj: PathObject) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@ama-sdk/showcase-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@ama-sdk/showcase-sdk",
"private": true,
"version": "0.0.0-placeholder",
"description": "showcase-sdk - SDK",
"source": "src/index.ts",
Expand All @@ -13,6 +12,9 @@
"esm2015": "esm2015/index.js",
"types": "index.d.ts",
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"exports": {
"./package.json": {
"default": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/@ama-sdk/showcase-sdk/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
}
}
},
"tags": ["private", "showcase"]
"tags": ["showcase"]
}
5 changes: 4 additions & 1 deletion packages/@ama-sdk/showcase-sdk/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Showcase SDK

### Based on Swagger spec 1.0.17
>Note:
This package is intended for **testing purposes only**. It is not suitable for production use.

### Based on openapi specs 3.0.2

The SDK contains 2 different parts:

Expand Down

0 comments on commit 8ea051a

Please sign in to comment.