Skip to content

Commit

Permalink
feat(codegen): required devDeps are now listed in package.json (#761)
Browse files Browse the repository at this point in the history
* refactor(core): removing `oas` as a dependency on codegen'd SDKs

* feat(codegen): required devDeps are now listed in `package.json`
  • Loading branch information
erunion authored Oct 16, 2023
1 parent 4a06601 commit a217ac5
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/api/src/codegen/codegenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export default abstract class CodeGenerator {
requiredPackages!: Record<
string,
{
dependencyType: 'production' | 'development';
reason: string;
url: string;
url?: string;
version: string;
}
>;
Expand Down
28 changes: 23 additions & 5 deletions packages/api/src/codegen/languages/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class TSGenerator extends CodeGenerator {

this.requiredPackages = {
'@readme/api-core': {
dependencyType: 'production',
reason: "The core magic of your codegen'd SDK and is what is used for making requests.",
url: 'https://npm.im/api',
version:
Expand All @@ -95,10 +96,22 @@ export default class TSGenerator extends CodeGenerator {
: corePkg.version,
},
'json-schema-to-ts': {
dependencyType: 'production',
reason: 'Required for TypeScript type handling.',
url: 'https://npm.im/json-schema-to-ts',
version: '^2.9.2',
},
tsup: {
dependencyType: 'development',
reason: "Used for compiling your codegen'd SDK into code that can be used in JS environments.",
url: 'https://tsup.egoist.dev/',
version: '^7.2.0',
},
typescript: {
dependencyType: 'development',
reason: 'Required for `tsup`.',
version: '^5.2.2',
},
};

this.project = new Project({
Expand Down Expand Up @@ -163,10 +176,6 @@ export default class TSGenerator extends CodeGenerator {
async compile(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
const installDir = storage.getIdentifierStorageDir();

await execa('npm', ['install', 'tsup', 'typescript', '-D'], {
cwd: installDir,
});

await execa('npx', ['tsup'], {
cwd: installDir,
})
Expand Down Expand Up @@ -453,7 +462,11 @@ sdk.server('https://eu.api.example.com/v14');`),
};

const dependencies = Object.entries(this.requiredPackages)
.map(([dep, { version }]) => ({ [dep]: version }))
.map(([dep, { dependencyType, version }]) => (dependencyType === 'production' ? { [dep]: version } : {}))
.reduce((prev, next) => Object.assign(prev, next));

const devDependencies = Object.entries(this.requiredPackages)
.map(([dep, { dependencyType, version }]) => (dependencyType === 'development' ? { [dep]: version } : {}))
.reduce((prev, next) => Object.assign(prev, next));

const pkg: PackageJson = {
Expand All @@ -476,7 +489,12 @@ sdk.server('https://eu.api.example.com/v14');`),
}
: {}),
},
files: ['dist'],
scripts: {
prepare: 'tsup',
},
dependencies,
devDependencies,
tsup: tsupOptions as JsonObject,
};

Expand Down
7 changes: 6 additions & 1 deletion packages/api/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ cmd
if (generator.hasRequiredPackages()) {
logger(`${figures.warning} This generator requires some packages to be installed alongside it:`);
Object.entries(generator.requiredPackages).forEach(([pkg, pkgInfo]) => {
logger(` ${figures.pointerSmall} ${pkg}: ${pkgInfo.reason} ${pkgInfo.url}`);
let msg = ` ${figures.pointerSmall} ${pkg}: ${pkgInfo.reason}`;
if (pkgInfo.url) {
msg += ` ${pkgInfo.url}`;
}

logger(msg);
});

if (!options.yes) {
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/alby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/operationid-quirks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
"require": "./dist/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/optional-payload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/petstore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/readme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/api/test/__fixtures__/sdk/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
"require": "./dist/types.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tsup"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
"json-schema-to-ts": "^2.9.2"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"tsup": {
"cjsInterop": true,
"clean": true,
Expand Down

0 comments on commit a217ac5

Please sign in to comment.