0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix astro add with third-party integrations (#4270)

* 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>
This commit is contained in:
Nate Moore 2022-08-11 18:31:28 -05:00 committed by GitHub
parent a70d05bd44
commit 7127b1bb35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Make third-party integration names nicer when using `astro add`

View file

@ -289,11 +289,30 @@ async function parseAstroConfig(configURL: URL): Promise<t.File> {
return result; 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) => { const toIdent = (name: string) => {
if (name.includes('-')) { const ident = name
return name.split('-')[0]; .trim()
} // Remove astro or (astrojs) prefix and suffix
return name; .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) { function createPrettyError(err: Error) {