Skip to content

Conversation

@erunion
Copy link
Member

@erunion erunion commented Oct 11, 2023

🚥 Resolves RM-7990

🧰 Changes

  • Changes api install to no longer prompt for a language or target to install. JS is now the only export target.
  • Overhauls codegen internals to now use tsup to compile our constructed TS code into a JS SDK that can be used in CJS and ESM environments.
    • We were previously handling this with a lot of very touchy ts-morph hacks that didn't really support ESM environments.
  • Refactored codegen to now store generated SDKs within a src/ directory.
    • Doing this mostly for aesthetic reasons now that we're going to have a dist/ directory post-tsup compilation.
  • Upgrades ts-morph to the latest release. chore(deps): bump ts-morph from 17.0.1 to 20.0.0 #752

🧬 QA & Testing

  • Can you create an SDK?
  • Can you load the SDK from a .cjs, .mjs, .js, and .ts example file?

@erunion erunion changed the title feat(api): wip of proper esm support and codegen overhauls feat(api): proper esm support and codegen overhauls Oct 11, 2023
@erunion erunion added enhancement New feature or request refactor Issues about tackling technical debt area:core Issues related to `core`, which is the package that powers the SDKs at runtime area:api Issues related to the `api` CLI, which builds the SDKs labels Oct 11, 2023
* `ts-morph` unfortunately doesn't give us any options for programatically doing this
* so we need to resort to modifying the emitted JS code.
*/
code = code
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goodbye forever to this mess

const sourceFile = this.project.createSourceFile('schemas.ts', '');
const schemasDir = this.project.createDirectory('schemas');
private createSchemasFile(sourceDirectory: Directory) {
const sourceFile = sourceDirectory.createSourceFile('schemas.ts', '');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These sourceDirectory changes mean that these files we're creating here (schemas.ts and schemas/) are now being created in a src/ directory.

@erunion erunion marked this pull request as ready for review October 16, 2023 18:31
@erunion erunion requested a review from kanadgupta October 16, 2023 18:31
@kanadgupta kanadgupta mentioned this pull request Oct 16, 2023
7 tasks
kanadgupta and others added 2 commits October 16, 2023 13:32
* chore: minor tsconfig cleanup

* fix: point to correct api-core pkg version

* fix: install TS as dev-dep

* fix: add this to silence the JSON import warning

* refactor: slight cleanup
@erunion erunion added this to the v7 milestone Oct 16, 2023
Copy link
Contributor

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly cleanup suggestions but overall very happy with this! love how much logic we get to clean up thanks to tsup 🥳

"ssri": "^10.0.1",
"ts-morph": "^17.0.1",
"ts-morph": "^20.0.0",
"tsup": "^7.2.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we remove this as a dep since it's being installed in the codegen'd directory now?

requiredPackages!: Record<
string,
{
reason: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a follow-up to the work I added in #759, i was thinking it might make sense to have a devDep flag that splits out our required deps (e.g., @readme/api-core) from our required dev-deps (e.g., typescript)

Suggested change
reason: string;
devDependency: boolean;
reason: string;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do this in another PR since it's going to require a little bit of work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might wanna regenerate these now that this file has devDependencies

// });
// });

it('should be able to make an API request (JS + ESM)', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we added a test in this file that imports the generated cjs file and ensures that that works?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats going to require building a dist for these fixtures and keeping that dist in git. i want to move these fixtures into @api/test-utils at some point, will rework this then

}
: {}),
},
dependencies,
Copy link
Contributor

@kanadgupta kanadgupta Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per this comment, might be good to add the dev-deps here so we're not running npm i several times

async compile(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
const installDir = storage.getIdentifierStorageDir();

await execa('npm', ['install', 'tsup', 'typescript', '-D'], {
Copy link
Contributor

@kanadgupta kanadgupta Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per this comment, might be good to remove this and take care of the dev-dep installation in the earlier install command so we're not running npm i several times

kanadgupta and others added 5 commits October 16, 2023 16:00
Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com>
Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com>
@erunion erunion requested a review from kanadgupta October 16, 2023 21:16
@erunion erunion merged commit e739d30 into main Oct 16, 2023
@erunion erunion deleted the refactor/codegen branch October 16, 2023 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:api Issues related to the `api` CLI, which builds the SDKs area:core Issues related to `core`, which is the package that powers the SDKs at runtime enhancement New feature or request refactor Issues about tackling technical debt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants