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

Fix astro add with third-party integrations #4270

Merged
merged 4 commits into from
Aug 11, 2022
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
5 changes: 5 additions & 0 deletions .changeset/wise-cups-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Make third-party integration names nicer when using `astro add`
27 changes: 23 additions & 4 deletions packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,30 @@ async function parseAstroConfig(configURL: URL): Promise<t.File> {
return result;
}

// Convert an arbitrary NPM package name into a JS identifier
// Some examples:
Copy link
Member

Choose a reason for hiding this comment

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

now that we're post v1.0 I'm feeling the burn to start adding some tests to anything like this that we missed for v1.0, but okay to keep kicking this can a bit longer until we're out of the release bug swarm

Copy link
Contributor

Choose a reason for hiding this comment

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

@FredKSchott +1! I vote using Vitest for this for snapshot testing though. The lack of snapshots in create-astro made for a tough test authoring experience and brittle tests we ended up removing.

// - @astrojs/image => image
// - @astrojs/markdown-component => markdownComponent
// - astro-cast => cast
// - markdown-astro => markdown
// - some-package => somePackage
// - example.com => exampleCom
// - under_score => underScore
// - 123numeric => numeric
// - @npm/thingy => npmThingy
// - @jane/foo.js => janeFoo
const toIdent = (name: string) => {
if (name.includes('-')) {
return name.split('-')[0];
}
return name;
const ident = name
.trim()
// Remove astro or (astrojs) prefix and suffix
.replace(/[-_\.]?astro(?:js)?[-_\.]?/g, '')
// drop .js suffix
.replace(/\.js/, '')
// convert to camel case
.replace(/(?:[\.\-\_\/]+)([a-zA-Z])/g, (_, w) => w.toUpperCase())
// drop invalid first characters
.replace(/^[^a-zA-Z$_]+/, '');
return `${ident[0].toLowerCase()}${ident.slice(1)}`;
};

function createPrettyError(err: Error) {
Expand Down