(args: {
sqlite: SqliteWasmDatabase;
lix: Lix;
/**
* The account that loaded the project.
*
* Defaults to an anonymous/new account if undefined.
*
* @example
* const account = localStorage.getItem("account")
* const project = await loadProject({ account })
*/
account?: Account;
/**
* Provide plugins to the project.
*
* This is useful for testing or providing plugins that are
* app specific. Keep in mind that provided plugins
* are not shared with other instances.
*/
providePlugins?: InlangPlugin[];
/**
* Function that preprocesses the plugin before importing it.
*
* The callback can be used to process plugins as needed in the
* environment of the app. For example, Sherlock uses this to convert
* ESM, which all inlang plugins are written in, to CJS which Sherlock
* runs in.
*
* @example
* const project = await loadProject({ preprocessPluginBeforeImport: (moduleText) => convertEsmToCjs(moduleText) })
*
*/
preprocessPluginBeforeImport?: PreprocessPluginBeforeImportFunction;
})
| 20 | * Common load project logic. |
| 21 | */ |
| 22 | export async function loadProject(args: { |
| 23 | sqlite: SqliteWasmDatabase; |
| 24 | lix: Lix; |
| 25 | /** |
| 26 | * The account that loaded the project. |
| 27 | * |
| 28 | * Defaults to an anonymous/new account if undefined. |
| 29 | * |
| 30 | * @example |
| 31 | * const account = localStorage.getItem("account") |
| 32 | * const project = await loadProject({ account }) |
| 33 | */ |
| 34 | account?: Account; |
| 35 | /** |
| 36 | * Provide plugins to the project. |
| 37 | * |
| 38 | * This is useful for testing or providing plugins that are |
| 39 | * app specific. Keep in mind that provided plugins |
| 40 | * are not shared with other instances. |
| 41 | */ |
| 42 | providePlugins?: InlangPlugin[]; |
| 43 | /** |
| 44 | * Function that preprocesses the plugin before importing it. |
| 45 | * |
| 46 | * The callback can be used to process plugins as needed in the |
| 47 | * environment of the app. For example, Sherlock uses this to convert |
| 48 | * ESM, which all inlang plugins are written in, to CJS which Sherlock |
| 49 | * runs in. |
| 50 | * |
| 51 | * @example |
| 52 | * const project = await loadProject({ preprocessPluginBeforeImport: (moduleText) => convertEsmToCjs(moduleText) }) |
| 53 | * |
| 54 | */ |
| 55 | preprocessPluginBeforeImport?: PreprocessPluginBeforeImportFunction; |
| 56 | }): Promise<InlangProject> { |
| 57 | const db = initDb({ sqlite: args.sqlite }); |
| 58 | |
| 59 | await maybeMigrateFirstProjectId({ lix: args.lix }); |
| 60 | |
| 61 | const settingsFile = await args.lix.db |
| 62 | .selectFrom("file") |
| 63 | .select("data") |
| 64 | .where("path", "=", "/settings.json") |
| 65 | .executeTakeFirstOrThrow(); |
| 66 | |
| 67 | const settings = withLanguageTagToLocaleMigration( |
| 68 | JSON.parse(new TextDecoder().decode(settingsFile.data)) as ProjectSettings |
| 69 | ); |
| 70 | |
| 71 | const importedPlugins = await importPlugins({ |
| 72 | settings, |
| 73 | lix: args.lix, |
| 74 | preprocessPluginBeforeImport: args.preprocessPluginBeforeImport, |
| 75 | }); |
| 76 | |
| 77 | const plugins = [...(args.providePlugins ?? []), ...importedPlugins.plugins]; |
| 78 | |
| 79 | // const state = createProjectState({ |
no test coverage detected