-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
d265e44
commit 7479051
Showing
18 changed files
with
167 additions
and
86 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
66 changes: 53 additions & 13 deletions
66
sources/@roots/bud-framework/src/Framework/methods/path.ts
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 |
---|---|---|
@@ -1,30 +1,70 @@ | ||
import {join} from 'path' | ||
import {join, resolve, sep as slash} from 'node:path' | ||
|
||
import {Framework} from '../..' | ||
|
||
export interface path { | ||
(base?: string, ...segments: Array<string>): string | ||
} | ||
|
||
/** | ||
* Transform `@alias` path | ||
* | ||
* @param app - Framework instance | ||
* @param base - Path segment | ||
* @returns string | ||
* | ||
* @public | ||
*/ | ||
const transformShorthandBase = (app: Framework, base: string): string => { | ||
const parts = base.includes('/') ? base.split('/') : [base] | ||
parts[0] = app.hooks.filter(`location.${parts[0]}`) | ||
return parts.join('/') | ||
/** | ||
* If path contains multiple segments, explode into an array | ||
* If path contains one segment, insert it into a blank array | ||
*/ | ||
const [ident, ...parts] = base.includes(slash) | ||
? base.split(slash) | ||
: [base] | ||
|
||
!app.hooks.has(`location.${ident}`) && | ||
app.error( | ||
`\`${ident}\` is not a registered path. It must be defined with bud.setPath`, | ||
) | ||
|
||
/** | ||
* Replace | ||
*/ | ||
const value = app.hooks.filter(`location.${ident}`) | ||
|
||
return join(value, ...(parts ?? []).filter(Boolean)) | ||
} | ||
|
||
export const path: path = function ( | ||
base?: string, | ||
...segments: Array<string> | ||
): string { | ||
const ctx = this as Framework | ||
const app = this as Framework | ||
/** | ||
* If no base path was provided return the project directory | ||
*/ | ||
if (!base) return app.context.projectDir | ||
|
||
/** | ||
* If base path starts with `/` return the joined path and segments (if any) | ||
*/ | ||
if (base.startsWith('/')) | ||
return segments.length ? join(base, ...segments) : base | ||
|
||
/** | ||
* Replace any `@alias` aliases with their corresponding entry | ||
*/ | ||
const normalized = base.startsWith(`@`) | ||
? transformShorthandBase(app, base) | ||
: base | ||
|
||
if (!base || base.startsWith(`/`)) return ctx.context.projectDir | ||
const absolutePath = base.startsWith(`/`) | ||
? normalized | ||
: resolve(app.context.projectDir, normalized) | ||
|
||
return join( | ||
ctx.context.projectDir, | ||
...[ | ||
base.startsWith(`@`) ? transformShorthandBase(ctx, base) : base, | ||
...(segments ?? []), | ||
].filter(Boolean), | ||
) | ||
return segments.length | ||
? resolve(absolutePath, ...(segments ?? []).filter(Boolean)) | ||
: absolutePath | ||
} |
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
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
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
Oops, something went wrong.