-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): generate an sdk with spec from npm
- Loading branch information
Showing
28 changed files
with
412 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env node | ||
|
||
/* | ||
* Update the OpenAPI spec from an NPM package | ||
*/ | ||
|
||
import * as minimist from 'minimist'; | ||
import { existsSync } from 'node:fs'; | ||
import { copyFile, readFile } from 'node:fs/promises'; | ||
import type { PackageJson } from 'type-fest'; | ||
import type { OpenApiToolsConfiguration } from '../src/fwk/open-api-tools-configuration'; | ||
|
||
const argv = minimist(process.argv.slice(2)); | ||
const packageName = argv._[0]; | ||
const { help, output, 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; | ||
|
||
if (help) { | ||
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] | ||
package-name The full identifier of the npm package (e.g. @my-scope/my-package) | ||
--package-path The relative path inside the npm package where to find the spec file (default: './openapi.yml') | ||
--output The path where the spec file should be copied (default: './openapi.yml') | ||
--quiet Don't log anything | ||
`); | ||
process.exit(0); | ||
} | ||
|
||
if (!packageName) { | ||
logger.error('Need to provide packageName, use `amasdk-update-spec-from-npm --help` for more information'); | ||
process.exit(-1); | ||
} | ||
|
||
void (async () => { | ||
let specSourcePath; | ||
if (!packagePath) { | ||
const packageJsonPath = require.resolve(`${packageName}/package.json`); | ||
const packageJson = JSON.parse(await readFile(packageJsonPath, {encoding: 'utf8'})) as PackageJson; | ||
const exportMatcher = new RegExp(`openapi\\.(?:${supportedExtensions.join('|')})$`); | ||
const matchingExport = packageJson.exports && Object.keys(packageJson.exports).find((exportPath) => exportMatcher.test(exportPath)); | ||
if (matchingExport) { | ||
specSourcePath = require.resolve(`${packageName}/${matchingExport}`); | ||
} | ||
} else { | ||
specSourcePath = require.resolve(`${packageName}/${packagePath}`); | ||
} | ||
if (!specSourcePath || !existsSync(specSourcePath)) { | ||
logger.error(`Unable to find source spec from ${packageName}, please make sure it is correctly exported in package.json`); | ||
process.exit(-2); | ||
} | ||
|
||
let specDestinationPath = output; | ||
if (!specDestinationPath) { | ||
const specSourceExtension = specSourcePath.split('.').pop(); | ||
specDestinationPath = `./openapi.${specSourceExtension}`; | ||
if (existsSync(openApiConfigDefaultPath)) { | ||
const openApiConfig = JSON.parse(await readFile(openApiConfigDefaultPath, {encoding: 'utf8'})) as OpenApiToolsConfiguration; | ||
const generators = Object.values(openApiConfig['generator-cli']?.generators ?? {}); | ||
if (generators.length === 1 && generators[0].inputSpec && generators[0].inputSpec.split('.').pop() === specSourceExtension) { | ||
specDestinationPath = generators[0].inputSpec; | ||
} | ||
} | ||
} | ||
|
||
logger.info(`Updating spec file from "${specSourcePath}" to "${specDestinationPath}"`); | ||
await copyFile(specSourcePath, specDestinationPath); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/@ama-sdk/core/src/fwk/open-api-tools-configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** Configuration of an Open API generator */ | ||
export interface OpenApiToolsGenerator { | ||
/** Location of the OpenAPI spec, as URL or file */ | ||
inputSpec: string; | ||
/** Output path for the generated SDK */ | ||
output: string; | ||
/** Generator to use */ | ||
generatorName: string; | ||
/** Path to configuration file. It can be JSON or YAML */ | ||
config?: string; | ||
/** Sets specified global properties */ | ||
globalProperty?: string | Record<string, any>; | ||
} | ||
|
||
/** Global configuration of Open API generators */ | ||
export interface OpenApiToolsGeneratorCli { | ||
/** Open API version */ | ||
version: string; | ||
/** Location of the generator JAR file */ | ||
storageDir?: string; | ||
/** Generators configuration */ | ||
generators: Record<string, OpenApiToolsGenerator>; | ||
} | ||
|
||
/** Global configuration of Open API Tools */ | ||
export interface OpenApiToolsConfiguration { | ||
/** Generators CLI configuration */ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'generator-cli': OpenApiToolsGeneratorCli; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.