Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick from main - fix(ama-sdk/create): enable create feature for yarn pnp #539

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@ama-sdk/create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ yarn create @ama-sdk typescript <project-name> [...options]

- `--spec-path`: Path to the swagger/open-api specification used to generate the SDK
- `--package-manager`: Node package manager to be used (`npm` and `yarn` are available).
- `--debug`: Enable schematics debug mode (including dry run).

> **Note**: if the `--spec-path` is specified, the SDK will be generated based on this specification at the creation time.
3 changes: 1 addition & 2 deletions packages/@ama-sdk/create/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "packages/@ama-sdk/create/testing/jest.config.it.js",
"silent": false,
"passWithNoTests": true
"silent": false
}
},
"publish": {
Expand Down
38 changes: 24 additions & 14 deletions packages/@ama-sdk/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const sdkPackagePath = path.join(execAppOptions.cwd!.toString(), sdkPackageName.
* Publish all the packages of the Otter monorepo on it.
* Can be accessed during the tests with url http://localhost:4873
*/
function setupLocalRegistry() {
function setupLocalRegistryRules() {
let shouldHandleVerdaccio = false;

beforeAll(async () => {
Expand Down Expand Up @@ -79,6 +79,7 @@ function setupYarn(yarnVersion: string) {

execSync('yarn config set enableStrictSsl false', execAppOptions);
execSync(`yarn set version ${yarnVersion}`, execAppOptions);
execSync(`yarn config set cacheFolder ${cacheFolderPath}`, execAppOptions);
execSync(`yarn config set npmScopes.ama-sdk.npmRegistryServer ${registry}`, execAppOptions);
execSync(`yarn config set npmScopes.o3r.npmRegistryServer ${registry}`, execAppOptions);
execSync('yarn config set unsafeHttpWhitelist localhost', execAppOptions);
Expand All @@ -89,10 +90,19 @@ function setupYarn(yarnVersion: string) {
// copy npmrc config to generated SDK
mkdirSync(sdkPackagePath, { recursive: true });
cpSync(path.join(execAppOptions.cwd.toString(), '.yarnrc.yml'), path.join(sdkPackagePath, '.yarnrc.yml'));
cpSync(path.join(execAppOptions.cwd.toString(), '.yarn'), path.join(sdkPackagePath, '.yarn'), {recursive: true});
});
}

function setupCache() {
function setupPackageManagerRules() {
if (packageManager.startsWith('yarn')) {
setupYarn(o3rPackageJson?.packageManager?.split('@')?.[1] || '3.5.0');
} else {
setupNpm();
}
}

function setupCacheRules() {
beforeEach(() => {
try {
rmSync(sdkFolderPath, { recursive: true, force: true });
Expand All @@ -117,24 +127,24 @@ function setupCache() {
});
}

describe('new Otter sdk', () => {
setupLocalRegistry();
setupCache();
setupNpm();
if (packageManager.startsWith('yarn')) {
setupYarn(o3rPackageJson?.packageManager?.split('@')?.[1] || '3.5.0');
}
describe('Create new sdk command', () => {
setupLocalRegistryRules();
setupCacheRules();
setupPackageManagerRules();

test('should build from full generation', () => {
beforeEach(() => {
cpSync(path.join(__dirname, '..', 'testing', 'mocks', 'MOCK_swagger_updated.yaml'), path.join(sdkFolderPath, 'swagger-spec.yml'));
expect(() => execSync(`npm create @ama-sdk typescript ${sdkPackageName} -- --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
});

test('should generate a full SDK when the specification is provided', () => {
// eslint-disable-next-line max-len
expect(() => execSync(`${packageManager} create @ama-sdk typescript ${sdkPackageName}${packageManager.startsWith('npm') ? ' --' : ''} --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
// TODO: uncomment when the generation is fixed for NPM
// expect(() => execSync(`${packageManager} run build`, { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
});

test('should build after new generation', () => {
cpSync(path.join(__dirname, '..', 'testing', 'mocks', 'MOCK_swagger_updated.yaml'), path.join(sdkFolderPath, 'swagger-spec.yml'));
expect(() => execSync(`npm create @ama-sdk typescript ${sdkPackageName}`, execAppOptions)).not.toThrow();
test('should generate an empty SDK ready to be used', () => {
expect(() => execSync(`${packageManager} create @ama-sdk typescript ${sdkPackageName}`, execAppOptions)).not.toThrow();
expect(() =>
execSync(
`${packageManager} exec schematics @ama-sdk/schematics:typescript-core --spec-path ${path.join(path.relative(sdkPackagePath, execAppOptions.cwd.toString()), 'swagger-spec.yml')}`,
Expand Down
15 changes: 11 additions & 4 deletions packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

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

const defaultScope = 'sdk';
Expand All @@ -12,6 +12,7 @@ const argv = minimist(args);
if (argv._.length < 2) {
// eslint-disable-next-line no-console
console.error('The SDK type and project name are mandatory');
console.info('usage: create typescript <@scope/package>');
process.exit(-1);
}

Expand All @@ -31,9 +32,10 @@ if (!pck) {
}

const targetDirectory = join('.', name, pck);
const schematicsPackage = dirname(require.resolve('@ama-sdk/schematics/package.json'));
const schematicsToRun = [
'@ama-sdk/schematics:typescript-shell',
...(argv['spec-path'] ? ['@ama-sdk/schematics:typescript-core'] : [])
`${schematicsPackage}:typescript-shell`,
...(argv['spec-path'] ? [`${schematicsPackage}:typescript-core`] : [])
];

const packageManagerEnv = process.env.npm_config_user_agent?.split('/')[0];
Expand All @@ -46,9 +48,14 @@ const packageManager = argv['package-manager'] || defaultPackageManager;

const run = () => {
const schematicArgs = [
'--name', name, '--package', pck, '--package-manager', packageManager, '--directory', targetDirectory,
argv.debug !== undefined ? `--debug=${argv.debug as string}` : '--debug=false', // schematics enable debug mode per default when using schematics with relative path
'--name', name,
'--package', pck,
'--package-manager', packageManager,
'--directory', targetDirectory,
...(argv['spec-path'] ? ['--spec-path', argv['spec-path']] : [])
];

const errors = schematicsToRun
.map((schematic) => spawnSync(process.execPath, [binPath, schematic, ...schematicArgs], { stdio: 'inherit', cwd: process.cwd()}))
.map(({error}) => error)
Expand Down
3 changes: 2 additions & 1 deletion packages/@ama-sdk/schematics/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "packages/@ama-sdk/schematics/testing/jest.config.it.js",
"silent": false
"silent": false,
"passWithNoTests": true
}
},
"prepare-publish": {
Expand Down
115 changes: 0 additions & 115 deletions packages/@ama-sdk/schematics/schematics/index.it.spec.ts

This file was deleted.