Skip to content

Commit

Permalink
Fix astro add with third-party integrations (#4270)
Browse files Browse the repository at this point in the history
* fix: nicer third-party integration names

* chore: add changeset

* fix: better handling for package names

* update changelog

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
natemoo-re and natemoo-re authored Aug 11, 2022
1 parent a70d05b commit 7127b1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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:
// - @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

0 comments on commit 7127b1b

Please sign in to comment.