|
| 1 | +import os from 'node:os' |
| 2 | +import { |
| 3 | + basename, |
| 4 | + delimiter, |
| 5 | + dirname, |
| 6 | + extname, |
| 7 | + isAbsolute, |
| 8 | + join, |
| 9 | + normalize, |
| 10 | + type ParsedPath, |
| 11 | + relative, |
| 12 | + resolve, |
| 13 | + sep, |
| 14 | + toNamespacedPath, |
| 15 | +} from 'node:path' |
| 16 | +import process from 'node:process' |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns the path to the `actions` directory. The `actions` directory |
| 20 | + * contains the core Stacks' actions. |
| 21 | + * |
| 22 | + * @param path - The relative path to the file or directory. |
| 23 | + * @returns The absolute path to the file or directory. |
| 24 | + * @example |
| 25 | + * ```ts |
| 26 | + * import { actionsPath } from '@stacksjs/path' |
| 27 | + * |
| 28 | + * console.log(actionsPath('path/to/action.ts')) // Outputs the absolute path to 'path/to/action.ts' within the `actions` directory |
| 29 | + * ``` |
| 30 | + */ |
| 31 | +export function actionsPath(path?: string): string { |
| 32 | + return corePath(`actions/${path || ''}`) |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +export function corePath(path?: string): string { |
| 37 | + return frameworkPath(`core/${path || ''}`) |
| 38 | +} |
| 39 | + |
| 40 | +export function frameworkPath(path?: string, options?: { relative?: boolean, cwd?: string }): string { |
| 41 | + const absolutePath = storagePath(`framework/${path || ''}`) |
| 42 | + |
| 43 | + if (options?.relative) |
| 44 | + return relative(options.cwd || process.cwd(), absolutePath) |
| 45 | + |
| 46 | + return absolutePath |
| 47 | +} |
| 48 | + |
| 49 | +export function storagePath(path?: string): string { |
| 50 | + return projectPath(`storage/${path || ''}`) |
| 51 | +} |
| 52 | + |
| 53 | +export function projectPath(filePath = '', options?: { relative: boolean }): string { |
| 54 | + let path = process.cwd() |
| 55 | + |
| 56 | + while (path.includes('storage')) path = resolve(path, '..') |
| 57 | + |
| 58 | + const finalPath = resolve(path, filePath) |
| 59 | + |
| 60 | + if (options?.relative) |
| 61 | + return relative(process.cwd(), finalPath) |
| 62 | + |
| 63 | + return finalPath |
| 64 | +} |
| 65 | + |
| 66 | +export function userActionsPath(path?: string, options?: { relative: true }): string { |
| 67 | + const absolutePath = appPath(`Actions/${path || ''}`) |
| 68 | + |
| 69 | + if (options?.relative) |
| 70 | + return relative(process.cwd(), absolutePath) |
| 71 | + |
| 72 | + return absolutePath |
| 73 | +} |
| 74 | + |
| 75 | +export function builtUserActionsPath(path?: string, options?: { relative: boolean }): string { |
| 76 | + const absolutePath = frameworkPath(`actions/${path || ''}`) |
| 77 | + |
| 78 | + if (options?.relative) |
| 79 | + return relative(process.cwd(), absolutePath) |
| 80 | + |
| 81 | + return absolutePath |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Returns the path to the home directory, optionally appending a given path. |
| 86 | + * |
| 87 | + * @param path - The relative path to append to the home directory path. |
| 88 | + * @returns The absolute path to the specified file or directory within the home directory. |
| 89 | + */ |
| 90 | +export function homeDir(path?: string): string { |
| 91 | + return os.homedir() + (path ? (path.startsWith('/') ? '' : '/') + path : '~') |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Returns the path to the library entry file, filtered by library type. |
| 96 | + * |
| 97 | + * @param type - The type of library ('vue-components', 'web-components', or 'functions'). |
| 98 | + * @returns The absolute path to the specified library entry file. |
| 99 | + */ |
| 100 | +export type LibraryType = 'vue-components' | 'web-components' | 'functions' |
| 101 | +export function libraryEntryPath(type: LibraryType): string { |
| 102 | + return libsEntriesPath(`${type}.ts`) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Returns the path to the `examples` directory within the framework directory, filtered by type. |
| 107 | + * |
| 108 | + * @param type - The type of examples to filter by ('vue-components' or 'web-components'). |
| 109 | + * @returns The absolute path to the specified type of examples within the `examples` directory. |
| 110 | + */ |
| 111 | +export function examplesPath(type?: 'vue-components' | 'web-components'): string { |
| 112 | + return frameworkPath(`examples/${type || ''}`) |
| 113 | +} |
| 114 | + |
| 115 | +export interface Path { |
| 116 | + actionsPath: (path?: string) => string |
| 117 | + userActionsPath: (path?: string) => string |
| 118 | + builtUserActionsPath: (path?: string, option?: { relative: boolean }) => string |
| 119 | + examplesPath: (type?: 'vue-components' | 'web-components') => string |
| 120 | + libraryEntryPath: (type: LibraryType) => string |
| 121 | + homeDir: (path?: string) => string |
| 122 | + parse: (path: string) => ParsedPath |
| 123 | + sep: () => '/' | '\\' |
| 124 | +} |
| 125 | + |
| 126 | +export const path: Path = { |
| 127 | + actionsPath, |
| 128 | + userActionsPath, |
| 129 | + builtUserActionsPath, |
| 130 | + homeDir, |
| 131 | + |
| 132 | + // some comment |
| 133 | + libraryEntryPath, |
| 134 | + examplesPath, |
| 135 | + parse, |
| 136 | + sep: () => sep, |
| 137 | +} |
| 138 | + |
| 139 | +export { basename, delimiter, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep, toNamespacedPath } |
0 commit comments