Skip to content

Commit

Permalink
fix: update conf
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Sep 4, 2024
1 parent c353b54 commit 1a31f5b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 41 deletions.
91 changes: 53 additions & 38 deletions packages/cli/src/app/src/app/components/current-configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,35 @@ function Plugin({
onClose: onEditClose,
} = useDisclosure()

const [overridedTo, setOverridedTo] = useState<Record<string, string>>({})
const onEdit = async (e: React.FormEvent<HTMLFormElement>) => {
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<Record<string, string>>(
plugin.paths.reduce(
(acc, p) => {
acc[p.from] = p.overridedTo ?? p.to
return acc
},
{} as Record<string, string>
)
)
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 (
Expand Down Expand Up @@ -352,35 +370,32 @@ function Plugin({
{(onClose) => (
<>
<ModalHeader className="flex flex-col gap-1">{dictionary.pluginSettings}</ModalHeader>
<ModalBody>
{plugin.paths.map((p, i) => (
<Fragment key={p.from}>
{i !== 0 && <Divider orientation="horizontal" />}
<div className="flex flex-col gap-1">
<Input isDisabled isReadOnly value={plugin.sourcePath} label={dictionary.sourcePath} />
<Input
value={overridedTo[p.to] ?? ""}
onValueChange={(value) => {
setOverridedTo((prev) => ({
...prev,
[p.to]: value,
}))
}}
placeholder={p.to}
label={dictionary.outputPath}
/>
</div>
</Fragment>
))}
</ModalBody>
<ModalFooter>
<Button variant="light" onPress={onClose} isDisabled={isPending}>
{dictionary.close}
</Button>
<Button color="primary" onPress={onEdit} isLoading={isPending}>
{dictionary.save}
</Button>
</ModalFooter>
<form onSubmit={onEdit}>
<ModalBody>
{plugin.paths.map((p, i) => (
<Fragment key={p.from}>
{i !== 0 && <Divider orientation="horizontal" />}
<div className="flex flex-col gap-1">
<Input isDisabled isReadOnly value={p.from} label={dictionary.sourcePath} />
<Input
value={overridedTo[p.from] ?? ""}
onValueChange={editPath(p.from)}
placeholder={p.to}
label={dictionary.outputPath}
/>
</div>
</Fragment>
))}
</ModalBody>
<ModalFooter>
<Button variant="light" onPress={onClose} isDisabled={isPending}>
{dictionary.close}
</Button>
<Button color="primary" type="submit" isLoading={isPending}>
{dictionary.save}
</Button>
</ModalFooter>
</form>
</>
)}
</ModalContent>
Expand Down
22 changes: 19 additions & 3 deletions packages/cli/src/app/src/lib/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ const webConfigToApiConfig = (webConfig: TConfiguration): z.infer<typeof optiona
const content = optionalConfigSchema.parse({
name: webConfig.name,
plugins: (webConfig.plugins ?? []).map((plugin) => {
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
Expand Down Expand Up @@ -76,7 +84,15 @@ const apiConfigToWebConfig = async (apiConfig: z.infer<typeof optionalConfigSche
description: foundPlugin.description,
id: foundPlugin.id,
sourcePath: foundPlugin.sourcePath,
paths: foundPlugin.paths,
paths: foundPlugin.paths.map((path) => {
const overridedTo =
typeof plugin === "string" ? undefined : plugin.paths.find((p) => p.from === path.from)?.to
return {
from: path.from,
to: path.to,
overridedTo,
}
}),
}
}),
}
Expand Down
13 changes: 13 additions & 0 deletions packages/scripts/src/utils/template-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1a31f5b

Please sign in to comment.