Skip to content

Commit

Permalink
feat(sdk): generate an sdk with spec from npm
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Apr 18, 2024
1 parent 1801566 commit 254e4bf
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 14 deletions.
18 changes: 18 additions & 0 deletions packages/@ama-sdk/core/cli/update-spec-from-npm.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

/*
* Update the OpenAPI spec from an NPM package
*/

import { copyFile } from 'node:fs/promises';

void (async () => {
const packageName = process.argv[2];
const packagePath = process.argv[3];

const specSourcePath = require.resolve(`${packageName}/${packagePath}`);
// TODO get actual path from openapitools.json
const specDestinationPath = './openapi.yml';

await copyFile(specSourcePath, specDestinationPath);
})();
3 changes: 2 additions & 1 deletion packages/@ama-sdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"schematics": "./collection.json",
"bin": {
"amasdk-clear-index": "./dist/cli/clear-index.cjs",
"amasdk-files-pack": "./dist/cli/files-pack.cjs"
"amasdk-files-pack": "./dist/cli/files-pack.cjs",
"amasdk-update-spec-from-npm": "./dist/cli/update-spec-from-npm.cjs"
}
}
7 changes: 6 additions & 1 deletion packages/@ama-sdk/create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ npm create @ama-sdk typescript <project-name> -- --package-manager=yarn [...opti
- `--o3r-metrics`: Enable or disable the collection of anonymous data for Otter
- `--exact-o3r-version` : use a pinned version for [otter packages](https://github.com/AmadeusITGroup/otter/blob/main/docs/README.md).

- `--spec-package-name`: The npm package name where the spec file can be fetched
- `--spec-package-registry`: The npm registry where the spec file can be fetched
- `--spec-package-path`: The path inside the package where to find the spec file
- `--spec-package-version`: The version to target for the npm package where the spec file can be fetched

> [!NOTE]
> If the `--spec-path` is specified, the SDK will be generated based on this specification at the creation time.
> If `--spec-path` or `--spec-package-name` is specified, the SDK will be generated based on this specification at the creation time.
32 changes: 26 additions & 6 deletions packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ const getYarnVersion = () => {
}
};

const schematicArgs = [
if (argv['spec-path'] && argv['spec-package-name']) {
console.error('--spec-path cannot be set with --spec-package-name');
process.exit(-4);
}

const commonSchematicArgs = [
argv.debug !== undefined ? `--debug=${argv.debug as string}` : '--debug=false', // schematics enable debug mode per default when using schematics with relative path
...(name ? ['--name', name] : []),
'--package', pck,
Expand All @@ -74,28 +79,43 @@ const resolveTargetDirectory = resolve(process.cwd(), targetDirectory);

const run = () => {
const isSpecRelativePath = !!argv['spec-path'] && !parse(argv['spec-path']).root;
const shellSchematicArgs = [
...commonSchematicArgs,
...(argv['spec-package-name'] ? ['--spec-package-name', argv['spec-package-name']] : []),
...(argv['spec-package-registry'] ? ['--spec-package-registry', argv['spec-package-registry']] : []),
...(argv['spec-package-path'] ? ['--spec-package-path', argv['spec-package-path']] : []),
...(argv['spec-package-version'] ? ['--spec-package-version', argv['spec-package-version']] : [])
];
const coreSchematicArgs = [
...commonSchematicArgs,
'--spec-path', argv['spec-package-name'] ? './openapi.yml' : isSpecRelativePath ? relative(resolveTargetDirectory, resolve(process.cwd(), argv['spec-path'])) : argv['spec-path']
];

const runner = process.platform === 'win32' ? `${packageManager}.cmd` : packageManager;
const steps: { args: string[]; cwd?: string; runner?: string }[] = [
{ args: [binPath, `${schematicsPackage}:typescript-shell`, ...schematicArgs, '--directory', targetDirectory] },
{ args: [binPath, `${schematicsPackage}:typescript-shell`, ...shellSchematicArgs, '--directory', targetDirectory] },
...(
packageManager === 'yarn'
? [{ runner, args: ['set', 'version', getYarnVersion()], cwd: resolveTargetDirectory }]
: []
),
...(argv['spec-path'] ? [{
...(argv['spec-package-name'] ? [{
runner,
args: ['exec', 'amasdk-update-spec-from-npm', argv['spec-package-name'], argv['spec-package-path']],
cwd: resolveTargetDirectory
}] : []),
...((argv['spec-path'] || argv['spec-package-name']) ? [{
args: [
binPath,
`${schematicsPackage}:typescript-core`,
...schematicArgs,
'--spec-path', isSpecRelativePath ? relative(resolveTargetDirectory, resolve(process.cwd(), argv['spec-path'])) : argv['spec-path']
...coreSchematicArgs
],
cwd: resolveTargetDirectory
}] : [])
];

const errors = steps
.map((step) => spawnSync(step.runner || process.execPath, step.args, { stdio: 'inherit', cwd: step.cwd || process.cwd(), shell: true }))
.map((step) => spawnSync(step.runner || `"${process.execPath}"`, step.args, { stdio: 'inherit', cwd: step.cwd || process.cwd(), shell: true }))
.filter(({error, status}) => (error || status !== 0));

if (errors.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati
packageManager: getPackageManagerName(options.packageManager),
projectHosting: options.hosting,
exactO3rVersion: options.exactO3rVersion,
specPackageName: options.specPackageName,
specPackagePath: options.specPackagePath,
specPackageVersion: options.specPackageVersion,
sdkCoreRange: `${options.exactO3rVersion ? '' : '~'}${amaSdkSchematicsPackageJson.version}`,
sdkCoreVersion: amaSdkSchematicsPackageJson.version,
angularVersion: amaSdkSchematicsPackageJson.dependencies!['@angular-devkit/core'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
"type": "string",
"description": "Directory where to generate the SDK"
},
"specPackageName": {
"type": "string",
"description": "The npm package name where the spec file can be fetched"
},
"specPackageRegistry": {
"type": "string",
"description": "The npm registry where the spec file can be fetched"
},
"specPackagePath": {
"type": "string",
"description": "The path inside the package where to find the spec file",
"default": "openapi.yml"
},
"specPackageVersion": {
"type": "string",
"description": "The version to target for the npm package where the spec file can be fetched",
"default": "latest"
},
"skipInstall": {
"type": "boolean",
"description": "Skip NPM install",
Expand Down
12 changes: 12 additions & 0 deletions packages/@ama-sdk/schematics/schematics/typescript/shell/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ export interface NgGenerateTypescriptSDKShellSchematicsSchema extends SchematicO
/** Directory where to generate the SDK */
directory?: string | undefined;

/** The npm package name where the spec file can be fetched */
specPackageName?: string;

/** The npm registry where the spec file can be fetched */
specPackageRegistry?: string;

/** The path inside the package where to find the spec file */
specPackagePath: string;

/** The version to target for the npm package where the spec file can be fetched */
specPackageVersion?: string;

/** Package manager to be used in the generated SDK */
packageManager?: 'npm' | 'yarn' | undefined;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base",
"github>AmadeusITGroup/otter//tools/renovate/base",
"github>AmadeusITGroup/otter//tools/renovate/sdk",
"github>AmadeusITGroup/otter//tools/renovate/sdk-spec-upgrade(my-specification-package)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"resolve": "node -e 'process.stdout.write(require.resolve(process.argv[1]));'",
"generate": "schematics @ama-sdk/schematics:typescript-core",
"spec:regen": "<%=packageManager%> run generate <% if (packageManager === 'npm') {%>-- <%}%>--generator-key <%=projectName%>-<%=projectPackageName%> && amasdk-clear-index",
"spec:upgrade": "<% if (specPackageName) { %>amasdk-update-spec-from-npm <%=specPackageName%> <%=specPackagePath%> && <% } %><%=packageManager%> run spec:regen",
"files:pack": "amasdk-files-pack",
"test": "jest --passWithNoTests",
"publish:package": "npm publish ./dist",
Expand Down Expand Up @@ -106,7 +107,8 @@
"ts-jest": "<%= versions['ts-jest'] %>",
"typedoc": "~0.25.0",
"tsc-watch": "^6.0.0",
"typescript": "<%= versions['typescript'] %>"
"typescript": "<%= versions['typescript'] %>"<% if (specPackageName) { %>,
"<%=specPackageName%>": "<%=specPackageVersion%>"<% } %>
},<% if (exactO3rVersion) { %>
"<%= packageManager == 'yarn' ? 'resolutions' : 'overrides' %>": {
"@o3r/schematics": "<%= sdkCoreRange %>"
Expand Down
4 changes: 2 additions & 2 deletions packages/@o3r/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@
"@ngrx/store-devtools": "~17.2.0",
"@o3r/store-sync": "workspace:^",
"@types/jest": "~29.5.2",
"nx": "~18.2.0",
"nx": "~18.3.0",
"@typescript-eslint/parser": "^7.2.0",
"@stylistic/eslint-plugin-ts": "^1.5.4",
"cpy-cli": "^5.0.0",
"eslint": "^8.57.0",
"@nx/eslint-plugin": "~18.2.0",
"@nx/eslint-plugin": "~18.3.0",
"jsonc-eslint-parser": "~2.4.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-plugin-jest": "~27.9.0",
Expand Down
2 changes: 1 addition & 1 deletion tools/renovate/tasks/sdk-regenerate.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"postUpgradeTasks": {
"commands": [
"{{arg0}} install",
"{{arg0}} run swagger:generate"
"{{arg0}} run spec:upgrade"
],
"fileFilters": [
"!**/.{npmrc,yarnrc*}"
Expand Down
2 changes: 1 addition & 1 deletion tools/renovate/tasks/sdk-spec-regenerate.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"postUpgradeTasks": {
"commands": [
"{{arg0}} install",
"{{arg0}} run swagger:generate"
"{{arg0}} run spec:upgrade"
],
"executionMode": "branch",
"fileFilters": [
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ __metadata:
bin:
amasdk-clear-index: ./dist/cli/clear-index.cjs
amasdk-files-pack: ./dist/cli/files-pack.cjs
amasdk-update-spec-from-npm: ./dist/cli/update-spec-from-npm.cjs
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 254e4bf

Please sign in to comment.