File size: 5,088 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
import * as path from 'path'
import * as fs from 'fs'
import { ConfigurationError } from './errors/index.js'
import { relativeFilePathResolver } from '../../utils/file-resolver.js'
export interface ComponentDetails {
overrides: boolean
filePath: string
}
export class ComponentLoader {
protected components: Record<string, ComponentDetails> = {}
public add(name: string, filePath: string, caller = 'add') {
const resolvedFilePath = ComponentLoader.resolveFilePath(filePath, caller)
if ((this.components[name] && this.components[name].filePath !== resolvedFilePath)
|| ComponentLoader.defaultComponents.includes(name)
) {
throw new Error(`Component '${name}' is already defined, use .override() instead`)
}
this.components[name] = {
overrides: false,
filePath: resolvedFilePath,
}
return name
}
public override(name: string, filePath: string, caller = 'override') {
const resolvedFilePath = ComponentLoader.resolveFilePath(filePath, caller)
if (!this.components[name]
&& !ComponentLoader.defaultComponents.includes(name)
) {
throw new Error(`Component '${name}' is not defined, use .add() instead`)
}
this.components[name] = {
overrides: true,
filePath: resolvedFilePath,
}
return name
}
public __unsafe_addWithoutChecks(name: string, filePath: string, caller = '__unsafe_addWithoutChecks') {
const resolvedFilePath = ComponentLoader.resolveFilePath(filePath, caller)
this.components[name] = {
overrides: false,
filePath: resolvedFilePath,
}
return name
}
public clear() {
this.components = {}
}
public getComponents() {
return Object
.entries(this.components)
.reduce(
(result, [key, component]) => {
result[key] = component.filePath
return result
},
{} as Record<string, string>,
)
}
public __unsafe_merge(componentLoader: ComponentLoader): void {
this.components = {
...componentLoader.components,
...this.components,
}
}
public static resolveFilePath(filePath: string, caller: string): string {
const extensions = ['.jsx', '.js', '.ts', '.tsx', '.mts', '.mjs']
const src = path.isAbsolute(filePath)
? filePath
: relativeFilePathResolver(filePath, new RegExp(`.*.{1}${caller}`))
const { ext: originalFileExtension } = path.parse(src)
for (const extension of extensions) {
const forcedExt = extensions.includes(originalFileExtension) ? '' : extension
const { root, dir, name, ext } = path.parse(src + forcedExt)
const fileName = path.format({ root, dir, name, ext })
if (fs.existsSync(fileName)) {
return path.format({ root, dir, name })
}
}
throw new ConfigurationError(`Trying to bundle file '${src}' but it doesn't exist`, 'AdminJS.html')
}
protected static defaultComponents = [
'LoggedIn',
'NoRecords',
'SidebarResourceSection',
'SidebarFooter',
'SidebarBranding',
'Sidebar',
'TopBar',
'Breadcrumbs',
'FilterDrawer',
'NoticeBox',
'Version',
'SidebarPages',
'PropertyHeader',
'RecordInList',
'RecordsTableHeader',
'RecordsTable',
'SelectedRecords',
'StyledBackButton',
'ActionHeader',
'ActionButton',
'BulkActionRoute',
'DashboardRoute',
'RecordActionRoute',
'ResourceActionRoute',
'ResourceRoute',
'PageRoute',
'RouteWrapper',
'Application',
'DefaultEditAction',
'DefaultBulkDeleteAction',
'DefaultListAction',
'DefaultNewAction',
'DefaultShowAction',
'DefaultArrayShowProperty',
'DefaultArrayListProperty',
'DefaultArrayEditProperty',
'DefaultBooleanEditProperty',
'DefaultBooleanFilterProperty',
'DefaultBooleanListProperty',
'DefaultBooleanShowProperty',
'BooleanPropertyValue',
'DefaultCurrencyEditProperty',
'DefaultCurrencyShowProperty',
'DefaultCurrencyListProperty',
'DefaultCurrencyFilterProperty',
'CurrencyPropertyInputWrapper',
'DefaultDatetimeEditProperty',
'DefaultDatetimeShowProperty',
'DefaultDatetimeListProperty',
'DefaultDatetimeFilterProperty',
'DefaultPropertyValue',
'DefaultShowProperty',
'DefaultListProperty',
'DefaultEditProperty',
'DefaultFilterProperty',
'DefaultMixedShowProperty',
'DefaultMixedListProperty',
'DefaultMixedEditProperty',
'DefaultPasswordEditProperty',
'DefaultPhoneEditProperty',
'DefaultPhoneFilterProperty',
'DefaultPhoneListProperty',
'DefaultPhoneShowProperty',
'DefaultReferenceEditProperty',
'DefaultReferenceShowProperty',
'DefaultReferenceListProperty',
'DefaultReferenceFilterProperty',
'DefaultReferenceValue',
'DefaultRichtextEditProperty',
'DefaultRichtextListProperty',
'DefaultRichtextShowProperty',
'DefaultTextareaEditProperty',
'DefaultTextareaShowProperty',
'PropertyDescription',
'PropertyLabel',
'Login',
'AuthenticationBackgroundComponent',
'Footer',
]
}
|