-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add try catch when initializing user code * refactor error handling * update comment --------- Co-authored-by: Alex Kempton <alex@funwithtriangles.net>
- Loading branch information
1 parent
0920ff4
commit 7e98aad
Showing
3 changed files
with
71 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,61 @@ | ||
import { Result } from './types' | ||
import { SketchConfig, SketchModule, SketchModuleItem } from '@store/types' | ||
import { createUniqueId } from '@utils/createUniqueId' | ||
|
||
export const importSketchModule = async ( | ||
baseUrl: string, | ||
moduleId: string, | ||
): Promise<SketchModuleItem> => { | ||
const cacheBust = createUniqueId() | ||
): Promise<Result<SketchModuleItem>> => { | ||
try { | ||
const cacheBust = createUniqueId() | ||
|
||
// Get the sketch module | ||
const sketchPath = `${baseUrl}/${moduleId}/index.js?${cacheBust}` | ||
if ((await fetch(sketchPath)).status !== 200) { | ||
return Promise.reject(`Sketch module not found: ${sketchPath}`) | ||
} | ||
const sketchModule = await import(/* @vite-ignore */ sketchPath) | ||
const module: SketchModule = sketchModule.default | ||
// Get the sketch module | ||
const sketchPath = `${baseUrl}/${moduleId}/index.js?${cacheBust}` | ||
if ((await fetch(sketchPath)).status !== 200) { | ||
return Promise.reject(`Sketch module not found: ${sketchPath}`) | ||
} | ||
const sketchModule = await import(/* @vite-ignore */ sketchPath) | ||
const module: SketchModule = sketchModule.default | ||
|
||
// Get the sketch config | ||
const configPath = `${baseUrl}/${moduleId}/config.js?${cacheBust}` | ||
let config: SketchConfig | ||
if ((await fetch(configPath)).status !== 200) { | ||
// No config file found | ||
// Try instancing the sketch, and call getConfig() on it | ||
const tempModule = new module() | ||
config = tempModule.getConfig?.() | ||
if (!config) { | ||
return Promise.reject( | ||
`Sketch config not found: ${configPath} and no valid getConfig() function found in sketch`, | ||
) | ||
// Get the sketch config | ||
const configPath = `${baseUrl}/${moduleId}/config.js?${cacheBust}` | ||
let config: SketchConfig | ||
if ((await fetch(configPath)).status !== 200) { | ||
// No config file found | ||
// Try instancing the sketch, and call getConfig() on it | ||
const tempModule = new module() | ||
config = tempModule.getConfig?.() | ||
if (!config) { | ||
return Promise.reject( | ||
`Sketch config not found: ${configPath} and no valid getConfig() function found in sketch`, | ||
) | ||
} | ||
} else { | ||
const configModule = await import(/* @vite-ignore */ configPath) | ||
config = configModule.default | ||
} | ||
} else { | ||
const configModule = await import(/* @vite-ignore */ configPath) | ||
config = configModule.default | ||
} | ||
|
||
// A config could be missing a title, but it is a required parameter | ||
if (!config.title) { | ||
config.title = moduleId | ||
} | ||
// A config could be missing a title, but it is a required parameter | ||
if (!config.title) { | ||
config.title = moduleId | ||
} | ||
|
||
return { | ||
success: true, | ||
error: undefined, | ||
data: { | ||
moduleId, | ||
config, | ||
module, | ||
}, | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
|
||
return { | ||
moduleId, | ||
config, | ||
module, | ||
return { | ||
data: undefined, | ||
success: false, | ||
error: `[HEDRON] Sketch module failed to import: ${moduleId}`, | ||
} | ||
} | ||
} |
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,3 @@ | ||
export type Result<T> = | ||
| { success: true; data: T; error: undefined } | ||
| { success: false; error: string; data: undefined } |