From 1cf11989411cc20cbf1a48476306649d9492a4ce Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 20 Dec 2023 15:07:34 -0600 Subject: [PATCH] feat: add resolveEntrypoint utility --- packages/internal-helpers/package.json | 9 ++++- packages/internal-helpers/src/path.ts | 17 +++++++++ packages/internal-helpers/test/path.test.js | 41 +++++++++++++++++++++ pnpm-lock.yaml | 15 +++++++- 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 packages/internal-helpers/test/path.test.js diff --git a/packages/internal-helpers/package.json b/packages/internal-helpers/package.json index 650665bfe5..120b1a48a8 100644 --- a/packages/internal-helpers/package.json +++ b/packages/internal-helpers/package.json @@ -29,10 +29,15 @@ "build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json", "build:ci": "astro-scripts build \"src/**/*.ts\"", "postbuild": "astro-scripts copy \"src/**/*.js\"", - "dev": "astro-scripts dev \"src/**/*.ts\"" + "dev": "astro-scripts dev \"src/**/*.ts\"", + "test": "mocha --exit --timeout 20000" }, "devDependencies": { - "astro-scripts": "workspace:*" + "@types/chai": "^4.3.10", + "@types/mocha": "^10.0.4", + "astro-scripts": "workspace:*", + "chai": "^4.3.7", + "mocha": "^10.2.0" }, "keywords": [ "astro", diff --git a/packages/internal-helpers/src/path.ts b/packages/internal-helpers/src/path.ts index cc9954ef2c..eee3bffe9e 100644 --- a/packages/internal-helpers/src/path.ts +++ b/packages/internal-helpers/src/path.ts @@ -56,6 +56,23 @@ export function isRelativePath(path: string) { return startsWithDotDotSlash(path) || startsWithDotSlash(path); } +function createRootRelativePath(value: URL, root: URL) { + return '/' + removeLeadingForwardSlash(value.toString().replace(root.toString(), '')); +} + +export function resolveEntrypoint(entrypoint: string | URL, root: URL) { + if (entrypoint instanceof URL) { + return createRootRelativePath(entrypoint, root); + } + if (startsWithForwardSlash(entrypoint)) { + return entrypoint; + } + if (isRelativePath(entrypoint)) { + return createRootRelativePath(new URL(entrypoint, root), root); + } + return entrypoint; +} + function isString(path: unknown): path is string { return typeof path === 'string' || path instanceof String; } diff --git a/packages/internal-helpers/test/path.test.js b/packages/internal-helpers/test/path.test.js new file mode 100644 index 0000000000..87814eae73 --- /dev/null +++ b/packages/internal-helpers/test/path.test.js @@ -0,0 +1,41 @@ +import { resolveEntrypoint } from '../dist/path.js'; +import { expect } from 'chai'; + +describe('resolveEntrypoint', () => { + it('handles fully resolved URLs', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = new URL('./src/item.ts', root); + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal('/src/item.ts'); + }) + it('handles relative paths', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = './src/item.ts'; + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal('/src/item.ts'); + }) + it('handles root-relative specifiers', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = '/src/item.ts'; + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal('/src/item.ts'); + }) + it('handles virtual modules', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = 'virtual:test-entrypoint'; + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal(entrypoint); + }) + it('handles fully resolved node_modules paths', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = new URL('file://test/path/usr/node_modules/@astrojs/preact/dist/server.js'); + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal('/node_modules/@astrojs/preact/dist/server.js'); + }) + it('handles package specifier', () => { + const root = new URL('file://test/path/usr/'); + const entrypoint = '@astrojs/preact/server'; + const output = resolveEntrypoint(entrypoint, root); + expect(output).to.equal(entrypoint); + }) +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ac222b3274..dd55ba0be5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4871,9 +4871,21 @@ importers: packages/internal-helpers: devDependencies: + '@types/chai': + specifier: ^4.3.10 + version: 4.3.10 + '@types/mocha': + specifier: ^10.0.4 + version: 10.0.4 astro-scripts: specifier: workspace:* version: link:../../scripts + chai: + specifier: ^4.3.7 + version: 4.3.10 + mocha: + specifier: ^10.2.0 + version: 10.2.0 packages/markdown/remark: dependencies: @@ -7308,12 +7320,11 @@ packages: /@types/chai-subset@1.3.4: resolution: {integrity: sha512-CCWNXrJYSUIojZ1149ksLl3AN9cmZ5djf+yUoVVV+NuYrtydItQVlL2ZDqyC6M6O9LWRnVf8yYDxbXHO2TfQZg==} dependencies: - '@types/chai': 4.3.9 + '@types/chai': 4.3.10 dev: false /@types/chai@4.3.10: resolution: {integrity: sha512-of+ICnbqjmFCiixUnqRulbylyXQrPqIGf/B3Jax1wIF3DvSheysQxAWvqHhZiW3IQrycvokcLcFQlveGp+vyNg==} - dev: true /@types/chai@4.3.9: resolution: {integrity: sha512-69TtiDzu0bcmKQv3yg1Zx409/Kd7r0b5F1PfpYJfSHzLGtB53547V4u+9iqKYsTu/O2ai6KTb0TInNpvuQ3qmg==}