summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2022-08-11 18:31:28 -0500
committerGravatar GitHub <noreply@github.com> 2022-08-11 16:31:28 -0700
commit7127b1bb35ca4e8f419e18683e380a4917eca4bb (patch)
tree932df1735b030053d9eaa1f64bb78d3529f7535d
parenta70d05bd44e6b8e664ee656f5c82be5920d52d6d (diff)
downloadastro-7127b1bb35ca4e8f419e18683e380a4917eca4bb.tar.gz
astro-7127b1bb35ca4e8f419e18683e380a4917eca4bb.tar.zst
astro-7127b1bb35ca4e8f419e18683e380a4917eca4bb.zip
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>
-rw-r--r--.changeset/wise-cups-greet.md5
-rw-r--r--packages/astro/src/core/add/index.ts27
2 files changed, 28 insertions, 4 deletions
diff --git a/.changeset/wise-cups-greet.md b/.changeset/wise-cups-greet.md
new file mode 100644
index 000000000..1e5944ae2
--- /dev/null
+++ b/.changeset/wise-cups-greet.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Make third-party integration names nicer when using `astro add`
diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts
index 24025b055..25f0b0c83 100644
--- a/packages/astro/src/core/add/index.ts
+++ b/packages/astro/src/core/add/index.ts
@@ -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) {