Skip to content

Commit

Permalink
feat: 实现recipe加载
Browse files Browse the repository at this point in the history
  • Loading branch information
luke358 committed Jan 30, 2024
1 parent f35b886 commit 373a6ac
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/helpers/recipe-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { parse } from 'node:path'
export function loadRecipeConfig(recipePath: string) {
try {
const configPath = `${recipePath}/package.json`;
// Delete module from cache
delete require.cache[require.resolve(configPath)];

// eslint-disable-next-line import/no-dynamic-require
const config = require(configPath);

const moduleConfigPath = require.resolve(configPath);
config.path = parse(moduleConfigPath).dir;
console.log(config, 'config loadRecipe')
return config;
} catch (error) {
console.error(error);
return null;
}
}
41 changes: 25 additions & 16 deletions src/store/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import { asarRecipesPath } from '@/helpers/asar-helpers'
import { ensureDirSync, pathExistsSync, readJsonSync, removeSync, copySync, readdirSync, statSync } from 'fs-extra';
import sleep from '@/helpers/async-helpers';
import tar, { x } from 'tar';
import { loadRecipeConfig } from '@/helpers/recipe-helpers';

export const useRecipeStore = defineStore('recipe',() => {
export const useRecipeStore = defineStore('recipe', () => {
const allRecipes = ref<Recipe[]>([])
const cacheRecipeByServiceId: Record<string, Recipe> = {}
const getRecipeByServiceId = async (service: Service) => {
const recipeId = service.recipeId
const serviceId = service.serviceId
if(cacheRecipeByServiceId[serviceId]) return cacheRecipeByServiceId[serviceId]
if (cacheRecipeByServiceId[recipeId]) return cacheRecipeByServiceId[recipeId]
const recipe = allRecipes.value.find((recipe) => recipe.id === recipeId)
if(recipe) return (cacheRecipeByServiceId[serviceId] = recipe);
if (recipe) return (cacheRecipeByServiceId[recipeId] = recipe);

const recipes = [service.recipeId]
// 加载 recipe
await _bulkRecipeCheck(recipes)

return cacheRecipeByServiceId[serviceId]
// return cacheRecipeByServiceId[serviceId]
return allRecipes.value.find((recipe) => recipe.id === recipeId)
}

const _bulkRecipeCheck = (unfilteredRecipes: string[]) => {
Expand All @@ -38,7 +38,6 @@ export const useRecipeStore = defineStore('recipe',() => {
recipes.map(async (recipeId: string) => {
let recipe = allRecipes.value.find(r => r.id === recipeId);
await getRecipePackage(recipeId);

if (!recipe) {
console.warn(
`Recipe '${recipeId}' not installed, trying to fetch from server`,
Expand All @@ -53,10 +52,9 @@ export const useRecipeStore = defineStore('recipe',() => {
return null;
}
}

return recipe;
}),
).catch(error => console.error("Can't load recipe", error));
).catch(error => console.error("Can't load recipe", error));
}

// 加载 recipe 文件
Expand Down Expand Up @@ -118,16 +116,27 @@ export const useRecipeStore = defineStore('recipe',() => {
statSync(join(recipesDirectory, file)).isDirectory() &&
file !== 'temp' &&
file !== 'dev',
);

paths.map(id => {
// const Recipe = require(id)(class RecipeModel {});

// console.log(new Recipe())
})
).map(id => `${recipesDirectory}/${id}`);

allRecipes.value = paths.map(id => {
// TODO: RecipeModel 实现
const Recipe = require(id)(class RecipeModel {
config: any
id: string
constructor(config: any) {
this.config = config
this.id = config.id
}
});
console.log(Recipe, 'allRecipescc', new Recipe(loadRecipeConfig(id)))
return new Recipe(loadRecipeConfig(id))
});
console.log(allRecipes.value, 'ddddd')

return allRecipes.value
}


return {
allRecipes,
getRecipeByServiceId
Expand Down
6 changes: 3 additions & 3 deletions src/store/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ export const useServiceStore = defineStore({
}
}
},
_initializeServiceRecipeInWebview(serviceId: string) {
async _initializeServiceRecipeInWebview(serviceId: string) {
const service = this.one(serviceId);
if (service && service._webview) {
const recipeStore = useRecipeStore()
recipeStore.getRecipeByServiceId(service)

const recipe = await recipeStore.getRecipeByServiceId(service)
console.log(recipe, 'ddddddd')
service._webview.send('initialize-recipe', {
version: '1.1'
}, {
Expand Down
1 change: 0 additions & 1 deletion src/types/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export enum LinkHandling {
}
export interface Service {
id: string
serviceId: string
name: string
url: string
iconUrl: string
Expand Down

0 comments on commit 373a6ac

Please sign in to comment.