summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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) {