-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed601a3
commit fedbd0a
Showing
4 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* @adonisjs/inertia | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { locatePath } from 'locate-path' | ||
import { Application } from '@adonisjs/core/app' | ||
|
||
export class FilesDetector { | ||
constructor(protected app: Application<any>) {} | ||
|
||
/** | ||
* Try to locate the entrypoint file based | ||
* on the conventional locations | ||
*/ | ||
async detectEntrypoint(defaultPath: string) { | ||
const possiblesLocations = [ | ||
'./resources/app.ts', | ||
'./resources/app.tsx', | ||
'./resources/application/app.ts', | ||
'./resources/application/app.tsx', | ||
'./resources/app.jsx', | ||
'./resources/app.js', | ||
'./resources/application/app.jsx', | ||
'./resources/application/app.js', | ||
] | ||
|
||
const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot }) | ||
return this.app.makePath(path || defaultPath) | ||
} | ||
|
||
/** | ||
* Try to locate the SSR entrypoint file based | ||
* on the conventional locations | ||
*/ | ||
async detectSsrEntrypoint(defaultPath: string) { | ||
const possiblesLocations = [ | ||
'./resources/ssr.ts', | ||
'./resources/ssr.tsx', | ||
'./resources/application/ssr.ts', | ||
'./resources/application/ssr.tsx', | ||
'./resources/ssr.jsx', | ||
'./resources/ssr.js', | ||
'./resources/application/ssr.jsx', | ||
'./resources/application/ssr.js', | ||
] | ||
|
||
const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot }) | ||
return this.app.makePath(path || defaultPath) | ||
} | ||
|
||
/** | ||
* Try to locate the SSR bundle file based | ||
* on the conventional locations | ||
*/ | ||
async detectSsrBundle(defaultPath: string) { | ||
const possiblesLocations = ['./ssr/ssr.js', './ssr/ssr.mjs'] | ||
|
||
const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot }) | ||
return this.app.makePath(path || defaultPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { join } from 'node:path' | ||
import { test } from '@japa/runner' | ||
import { AppFactory } from '@adonisjs/core/factories/app' | ||
|
||
import { FilesDetector } from '../src/files_detector.js' | ||
|
||
test.group('Files detector', () => { | ||
test('detect entrypoint', async ({ assert, fs }) => { | ||
const app = new AppFactory().create(fs.baseUrl, () => {}) | ||
const detector = new FilesDetector(app) | ||
|
||
await fs.create('resources/app.ts', '') | ||
|
||
const entrypoint = await detector.detectEntrypoint('resources/foo.ts') | ||
|
||
assert.deepEqual(entrypoint, join(fs.basePath, 'resources/app.ts')) | ||
}) | ||
|
||
test('detect tsx entrypoint', async ({ assert, fs }) => { | ||
const app = new AppFactory().create(fs.baseUrl, () => {}) | ||
const detector = new FilesDetector(app) | ||
|
||
await fs.create('resources/app.tsx', '') | ||
|
||
const entrypoint = await detector.detectEntrypoint('resources/foo.ts') | ||
|
||
assert.deepEqual(entrypoint, join(fs.basePath, 'resources/app.tsx')) | ||
}) | ||
|
||
test('detect ssr entrypoint', async ({ assert, fs }) => { | ||
const app = new AppFactory().create(fs.baseUrl, () => {}) | ||
const detector = new FilesDetector(app) | ||
|
||
await fs.create('resources/ssr.ts', '') | ||
|
||
const entrypoint = await detector.detectSsrEntrypoint('resources/foo.ts') | ||
|
||
assert.deepEqual(entrypoint, join(fs.basePath, 'resources/ssr.ts')) | ||
}) | ||
|
||
test('detect ssr bundle', async ({ assert, fs }) => { | ||
const app = new AppFactory().create(fs.baseUrl, () => {}) | ||
const detector = new FilesDetector(app) | ||
|
||
await fs.create('ssr/ssr.js', '') | ||
|
||
const entrypoint = await detector.detectSsrBundle('ssr/foo.js') | ||
|
||
assert.deepEqual(entrypoint, join(fs.basePath, 'ssr/ssr.js')) | ||
}) | ||
}) |