From aa9ffc1764ef771c6bf52d0013314be99a5ca6d5 Mon Sep 17 00:00:00 2001 From: rharkor Date: Wed, 4 Sep 2024 08:46:50 +0200 Subject: [PATCH 1/4] refactor: task --- .../src/utils/template-config/apply.ts | 66 ++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/packages/scripts/src/utils/template-config/apply.ts b/packages/scripts/src/utils/template-config/apply.ts index 3a348b47..3669daaf 100644 --- a/packages/scripts/src/utils/template-config/apply.ts +++ b/packages/scripts/src/utils/template-config/apply.ts @@ -10,29 +10,38 @@ export const applyConfigurationTask = async ({ configFileName, pluginsDirectory, root, + noTask, }: { pluginsDirectory: string configFileName: string root: string + noTask: boolean }) => { - const applyConfigTask = await task.startTask({ - name: "Apply template config... 🧰", - }) + let applyConfigTask: null | Awaited> = null + if (!noTask) { + applyConfigTask = await task.startTask({ + name: "Apply template config... 🧰", + }) + } //* Check if the plugins directory exists - applyConfigTask.log("Checking if the plugins directory exists") + if (applyConfigTask) applyConfigTask.log("Checking if the plugins directory exists") + else logger.info("Checking if the plugins directory exists") if (!(await fs.exists(pluginsDirectory))) { - applyConfigTask.error(`The plugins directory doesn't exist at ${pluginsDirectory}`) - applyConfigTask.stop() + if (applyConfigTask) applyConfigTask.error(`The plugins directory doesn't exist at ${pluginsDirectory}`) + else logger.error(`The plugins directory doesn't exist at ${pluginsDirectory}`) + applyConfigTask?.stop() throw new Error(`The plugins directory doesn't exist at ${pluginsDirectory}`) } //* Retrieve config - applyConfigTask.log("Retrieving the config") + if (applyConfigTask) applyConfigTask.log("Retrieving the config") + else logger.info("Retrieving the config") const configPath = !root.endsWith(configFileName) ? path.join(root, configFileName) : root if (!(await fs.exists(configPath))) { - applyConfigTask.error(`The config file ${configPath} doesn't exist`) - applyConfigTask.stop() + if (applyConfigTask) applyConfigTask.error(`The config file ${configPath} doesn't exist`) + else logger.error(`The config file ${configPath} doesn't exist`) + applyConfigTask?.stop() throw new Error(`The config file ${configPath} doesn't exist`) } @@ -40,21 +49,24 @@ export const applyConfigurationTask = async ({ try { configSchema.parse(config) } catch (error) { - applyConfigTask.error(`The config file ${configPath} is not valid`) - applyConfigTask.stop() + if (applyConfigTask) applyConfigTask.error(`The config file ${configPath} is not valid`) + else logger.error(`The config file ${configPath} is not valid`) + applyConfigTask?.stop() logger.error(error) throw error } //* Apply plugins - applyConfigTask.log("Applying plugins") + if (applyConfigTask) applyConfigTask.log("Applying plugins") + else logger.info("Applying plugins") for (const plugin of config.plugins) { const pluginName = typeof plugin === "string" ? plugin : plugin.name const pluginPath = path.join(pluginsDirectory, pluginName) if (!(await fs.exists(pluginPath))) { - applyConfigTask.error(`The plugin ${pluginName} doesn't exist at ${pluginPath}`) - applyConfigTask.stop() + if (applyConfigTask) applyConfigTask.error(`The plugin ${pluginName} doesn't exist at ${pluginPath}`) + else logger.error(`The plugin ${pluginName} doesn't exist at ${pluginPath}`) + applyConfigTask?.stop() throw new Error(`The plugin ${pluginName} doesn't exist at ${pluginPath}`) } const pluginConfigPath = path.join(pluginPath, configFileName) @@ -62,16 +74,20 @@ export const applyConfigurationTask = async ({ try { pluginConfigSchema.parse(pluginConfig) } catch (error) { - applyConfigTask.error(`The plugin config file ${pluginConfigPath} is not valid. Contact developer`) - applyConfigTask.stop() + if (applyConfigTask) + applyConfigTask.error(`The plugin config file ${pluginConfigPath} is not valid. Contact developer`) + else logger.error(`The plugin config file ${pluginConfigPath} is not valid. Contact developer`) + applyConfigTask?.stop() logger.error(error) throw error } //? Check if the plugin config exists if (!(await fs.exists(pluginConfigPath))) { - applyConfigTask.error(`The plugin config for ${pluginName} doesn't exist at ${pluginConfigPath}`) - applyConfigTask.stop() + if (applyConfigTask) + applyConfigTask.error(`The plugin config for ${pluginName} doesn't exist at ${pluginConfigPath}`) + else logger.error(`The plugin config for ${pluginName} doesn't exist at ${pluginConfigPath}`) + applyConfigTask?.stop() throw new Error(`The plugin config for ${pluginName} doesn't exist at ${pluginConfigPath}`) } @@ -81,15 +97,17 @@ export const applyConfigurationTask = async ({ for (const relativeDestinationPath of relativeDestinationPaths) { const destinationPath = path.join(root, relativeDestinationPath) if (await fs.exists(destinationPath)) { - applyConfigTask.error(`A file/folder already exists at the destination ${destinationPath}`) - applyConfigTask.stop() + if (applyConfigTask) applyConfigTask.error(`A file/folder already exists at the destination ${destinationPath}`) + else logger.error(`A file/folder already exists at the destination ${destinationPath}`) + applyConfigTask?.stop() throw new Error(`A file/folder already exists at the destination ${destinationPath}`) } } } //* Apply the plugins - applyConfigTask.log("Applying the plugins") + if (applyConfigTask) applyConfigTask.log("Applying the plugins") + else logger.info("Applying the plugins") for (const plugin of config.plugins) { const pluginName = typeof plugin === "string" ? plugin : plugin.name const pluginPath = path.join(pluginsDirectory, pluginName) @@ -99,7 +117,8 @@ export const applyConfigurationTask = async ({ for (const { from, to } of pluginConfig.paths) { const sourcePath = path.join(pluginPath, from) const destinationPath = path.join(root, to) - applyConfigTask.log(`Copying the plugin ${pluginName} to the destination ${destinationPath}`) + if (applyConfigTask) applyConfigTask.log(`Copying the plugin ${pluginName} to the destination ${destinationPath}`) + else logger.info(`Copying the plugin ${pluginName} to the destination ${destinationPath}`) await fs.copy(sourcePath, destinationPath) } } @@ -108,6 +127,7 @@ export const applyConfigurationTask = async ({ // applyConfigTask.log("Deleting the config file") // await fs.remove(configPath) - applyConfigTask.stop("The template config has been applied! 🎉") + if (applyConfigTask) applyConfigTask.stop("The template config has been applied! 🎉") + else logger.success("The template config has been applied! 🎉") logger.info("You can now delete the config file if you want") } From c353b54c570a08603f8892b7cfb4ff4bc67c3279 Mon Sep 17 00:00:00 2001 From: rharkor Date: Wed, 4 Sep 2024 08:56:25 +0200 Subject: [PATCH 2/4] fix: plugins apply --- .../app/src/api/configuration/mutations.ts | 1 + .../src/app/src/lib/configuration/index.ts | 4 ++-- .../cli/src/functions/apply-config/index.ts | 1 + .../src/utils/template-config/apply.ts | 5 +++-- .../src/utils/template-config/index.ts | 19 ++++++++++++------- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/app/src/api/configuration/mutations.ts b/packages/cli/src/app/src/api/configuration/mutations.ts index a7c9cd4a..f1ca7f32 100644 --- a/packages/cli/src/app/src/api/configuration/mutations.ts +++ b/packages/cli/src/app/src/api/configuration/mutations.ts @@ -48,6 +48,7 @@ export const applyConfiguration = async ({}: apiInputFromSchema> = { configuration } diff --git a/packages/cli/src/app/src/lib/configuration/index.ts b/packages/cli/src/app/src/lib/configuration/index.ts index cd5d7745..8cf768c6 100644 --- a/packages/cli/src/app/src/lib/configuration/index.ts +++ b/packages/cli/src/app/src/lib/configuration/index.ts @@ -31,13 +31,13 @@ const webConfigToApiConfig = (webConfig: TConfiguration): z.infer { return { name: plugin.sourcePath, - paths: plugin.paths.map((p) => p.to), + paths: plugin.paths, } }), }) return content } catch (error) { - logger.error(error) + logger.error("Failed to convert the web configuration", error) throw new TRPCError({ message: `Failed to convert the web configuration`, code: "INTERNAL_SERVER_ERROR", diff --git a/packages/cli/src/functions/apply-config/index.ts b/packages/cli/src/functions/apply-config/index.ts index d1c71d89..8ac6a23a 100644 --- a/packages/cli/src/functions/apply-config/index.ts +++ b/packages/cli/src/functions/apply-config/index.ts @@ -37,6 +37,7 @@ export const applyConfig = async () => { configFileName, pluginsDirectory, root, + noTask: false, }).catch(() => { process.exit(1) }) diff --git a/packages/scripts/src/utils/template-config/apply.ts b/packages/scripts/src/utils/template-config/apply.ts index 3669daaf..95292d33 100644 --- a/packages/scripts/src/utils/template-config/apply.ts +++ b/packages/scripts/src/utils/template-config/apply.ts @@ -56,7 +56,7 @@ export const applyConfigurationTask = async ({ throw error } - //* Apply plugins + //* Verify plugins if (applyConfigTask) applyConfigTask.log("Applying plugins") else logger.info("Applying plugins") @@ -114,7 +114,8 @@ export const applyConfigurationTask = async ({ const pluginConfigPath = path.join(pluginPath, configFileName) const pluginConfig = (await fs.readJson(pluginConfigPath)) as TPluginConfig // Copy the plugin to the destination - for (const { from, to } of pluginConfig.paths) { + for (const { from, to: defaultTo } of pluginConfig.paths) { + const to = typeof plugin === "string" ? defaultTo : (plugin.paths.find((p) => p.from === from)?.to ?? defaultTo) const sourcePath = path.join(pluginPath, from) const destinationPath = path.join(root, to) if (applyConfigTask) applyConfigTask.log(`Copying the plugin ${pluginName} to the destination ${destinationPath}`) diff --git a/packages/scripts/src/utils/template-config/index.ts b/packages/scripts/src/utils/template-config/index.ts index b035d386..4f838456 100644 --- a/packages/scripts/src/utils/template-config/index.ts +++ b/packages/scripts/src/utils/template-config/index.ts @@ -17,21 +17,26 @@ export function isPathInCurrentScope(filePath: string): boolean { export const fullPluginSchema = z.object({ name: z.string(), paths: z.array( - z - .string() - .optional() - .refine( + z.object({ + from: z.string().refine( (value) => { - if (value === undefined) { - return true + if (!isPathInCurrentScope(value)) { + return false } + return true + }, + { message: "The path should be relative and in the current directory" } + ), + to: z.string().refine( + (value) => { if (!isPathInCurrentScope(value)) { return false } return true }, { message: "The path should be relative and in the current directory" } - ) + ), + }) ), }) From 1a31f5bd51075dcfd5fc111452e375a7d8488f31 Mon Sep 17 00:00:00 2001 From: rharkor Date: Wed, 4 Sep 2024 09:18:58 +0200 Subject: [PATCH 3/4] fix: update conf --- .../app/components/current-configuration.tsx | 91 +++++++++++-------- .../src/app/src/lib/configuration/index.ts | 22 ++++- .../src/utils/template-config/index.ts | 13 +++ 3 files changed, 85 insertions(+), 41 deletions(-) diff --git a/packages/cli/src/app/src/app/components/current-configuration.tsx b/packages/cli/src/app/src/app/components/current-configuration.tsx index 9fc59c7e..f5d13bbb 100644 --- a/packages/cli/src/app/src/app/components/current-configuration.tsx +++ b/packages/cli/src/app/src/app/components/current-configuration.tsx @@ -256,17 +256,35 @@ function Plugin({ onClose: onEditClose, } = useDisclosure() - const [overridedTo, setOverridedTo] = useState>({}) + const onEdit = async (e: React.FormEvent) => { + e.preventDefault() + await _onEdit(plugin) + onEditClose() + } - const onEdit = async () => { - await _onEdit({ - ...plugin, - paths: plugin.paths.map((p) => ({ - from: plugin.sourcePath, - to: overridedTo[p.to] ?? p.to, - })), + const [overridedTo, setOverridedTo] = useState>( + plugin.paths.reduce( + (acc, p) => { + acc[p.from] = p.overridedTo ?? p.to + return acc + }, + {} as Record + ) + ) + const editPath = (fromKey: string) => (to: string) => { + setOverridedTo((prev) => ({ + ...prev, + [fromKey]: to, + })) + plugin.paths = plugin.paths.map((p) => { + if (p.from === fromKey) { + return { + ...p, + overridedTo: to, + } + } + return p }) - onEditClose() } return ( @@ -352,35 +370,32 @@ function Plugin({ {(onClose) => ( <> {dictionary.pluginSettings} - - {plugin.paths.map((p, i) => ( - - {i !== 0 && } -
- - { - setOverridedTo((prev) => ({ - ...prev, - [p.to]: value, - })) - }} - placeholder={p.to} - label={dictionary.outputPath} - /> -
-
- ))} -
- - - - +
+ + {plugin.paths.map((p, i) => ( + + {i !== 0 && } +
+ + +
+
+ ))} +
+ + + + +
)} diff --git a/packages/cli/src/app/src/lib/configuration/index.ts b/packages/cli/src/app/src/lib/configuration/index.ts index 8cf768c6..546c708d 100644 --- a/packages/cli/src/app/src/lib/configuration/index.ts +++ b/packages/cli/src/app/src/lib/configuration/index.ts @@ -29,10 +29,18 @@ const webConfigToApiConfig = (webConfig: TConfiguration): z.infer { - return { + const fullP = { name: plugin.sourcePath, - paths: plugin.paths, + paths: plugin.paths.map((p) => ({ + from: p.from, + to: p.overridedTo || p.to, + })), + } + //? If there's no override, or the override is the same as the original path, return the name + if (!plugin.paths.some((p) => p.overridedTo) || plugin.paths.every((p) => p.to === p.overridedTo)) { + return fullP.name } + return fullP }), }) return content @@ -76,7 +84,15 @@ const apiConfigToWebConfig = async (apiConfig: z.infer { + const overridedTo = + typeof plugin === "string" ? undefined : plugin.paths.find((p) => p.from === path.from)?.to + return { + from: path.from, + to: path.to, + overridedTo, + } + }), } }), } diff --git a/packages/scripts/src/utils/template-config/index.ts b/packages/scripts/src/utils/template-config/index.ts index 4f838456..cd8d1122 100644 --- a/packages/scripts/src/utils/template-config/index.ts +++ b/packages/scripts/src/utils/template-config/index.ts @@ -76,6 +76,19 @@ export const pluginConfigSchema = z.object({ }, { message: "The path should be relative and in the current directory" } ), + overridedTo: z + .string() + .optional() + .refine( + (value) => { + if (value === undefined) return true + if (!isPathInCurrentScope(value)) { + return false + } + return true + }, + { message: "The path should be relative and in the current directory" } + ), }) ) .refine( From f118a5bcdae441d8190184c1239088b04f03e60a Mon Sep 17 00:00:00 2001 From: rharkor Date: Wed, 4 Sep 2024 09:26:50 +0200 Subject: [PATCH 4/4] fix: handle apply error --- packages/cli/src/app/.env.example | 3 ++- packages/cli/src/app/.gitignore | 3 ++- .../cli/src/app/src/app/components/current-configuration.tsx | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/app/.env.example b/packages/cli/src/app/.env.example index 5e025e66..ef304eb6 100644 --- a/packages/cli/src/app/.env.example +++ b/packages/cli/src/app/.env.example @@ -1,2 +1,3 @@ LOGGER_ENV=development -ROOT_PATH=/workspace/packages/cli/src/app \ No newline at end of file +ENV=development +ROOT_PATH=/workspace/packages/cli/src/app/out \ No newline at end of file diff --git a/packages/cli/src/app/.gitignore b/packages/cli/src/app/.gitignore index 0cffcb34..b6a7c111 100644 --- a/packages/cli/src/app/.gitignore +++ b/packages/cli/src/app/.gitignore @@ -1 +1,2 @@ -config.json \ No newline at end of file +config.json +out \ No newline at end of file diff --git a/packages/cli/src/app/src/app/components/current-configuration.tsx b/packages/cli/src/app/src/app/components/current-configuration.tsx index f5d13bbb..ea73ece3 100644 --- a/packages/cli/src/app/src/app/components/current-configuration.tsx +++ b/packages/cli/src/app/src/app/components/current-configuration.tsx @@ -59,7 +59,7 @@ export default function CurrentConfiguration({ } const applyConfigurationMutation = trpc.configuration.applyConfiguration.useMutation({ - onSettled: async () => { + onSuccess: async () => { toast.success(dictionary.configurationApplied) }, })