Skip to content

Commit

Permalink
feat: edit dest
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Sep 3, 2024
1 parent baa296b commit e993c21
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 85 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@nextui-org/button": "^2.0.37",
"@nextui-org/checkbox": "^2.1.4",
"@nextui-org/chip": "^2.0.32",
"@nextui-org/divider": "^2.0.31",
"@nextui-org/dropdown": "^2.1.29",
"@nextui-org/input": "^2.2.4",
"@nextui-org/link": "^2.0.34",
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src/app/src/api/configuration/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { fullPluginSchema } from "@/lib/plugins/store"
export const configurationSchema = () =>
z.object({
name: z.string().optional(),
plugins: z
.array(
fullPluginSchema.extend({
outputPath: z.string().optional(),
})
)
.optional(),
plugins: z.array(fullPluginSchema).optional(),
})
export type TConfiguration = z.infer<ReturnType<typeof configurationSchema>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const CurrentConfigurationDr = dictionaryRequirements(
outputPath: true,
save: true,
close: true,
pluginSettings: true,
},
NoConfigurationDr,
HeaderDr
Expand Down
43 changes: 32 additions & 11 deletions packages/cli/src/app/src/app/components/current-configuration.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react"
import { Fragment, useRef, useState } from "react"

import { AnimatePresence, motion } from "framer-motion"
import { Eye, MoreHorizontal, Pencil } from "lucide-react"
Expand All @@ -15,7 +15,9 @@ import { TDictionary } from "@/lib/langs"
import { trpc } from "@/lib/trpc/client"
import { RouterOutputs } from "@/lib/trpc/utils"
import { Button } from "@nextui-org/button"
import { Divider } from "@nextui-org/divider"
import { Dropdown, DropdownItem, DropdownMenu, DropdownTrigger } from "@nextui-org/dropdown"
import { Input } from "@nextui-org/input"
import { Modal, ModalBody, ModalContent, ModalFooter, useDisclosure } from "@nextui-org/modal"
import { Spinner } from "@nextui-org/spinner"

Expand Down Expand Up @@ -234,8 +236,16 @@ function Plugin({
onClose: onEditClose,
} = useDisclosure()

const [overridedTo, setOverridedTo] = useState<Record<string, string>>({})

const onEdit = async () => {
await _onEdit(plugin)
await _onEdit({
...plugin,
paths: plugin.paths.map((p) => ({
from: plugin.sourcePath,
to: overridedTo[p.to] ?? p.to,
})),
})
onEditClose()
}

Expand Down Expand Up @@ -321,16 +331,27 @@ function Plugin({
<ModalContent>
{(onClose) => (
<>
<ModalHeader className="flex flex-col gap-1">Modal Title</ModalHeader>
<ModalHeader className="flex flex-col gap-1">{dictionary.pluginSettings}</ModalHeader>
<ModalBody>
{/* //TODO EDIT ALL PATHS */}
{/* <Input isDisabled isReadOnly value={plugin.sourcePath} label={dictionary.sourcePath} />
<Input
value={newOutputPath ?? ""}
onValueChange={setNewOutputPath}
placeholder={plugin.suggestedPath}
label={dictionary.outputPath}
/> */}
{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}>
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/app/src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,6 @@
"replaceConfiguration": "Replace configuration",
"sourcePath": "Source path",
"outputPath": "Output path",
"close": "Close"
"close": "Close",
"pluginSettings": "Plugin settings"
}
3 changes: 2 additions & 1 deletion packages/cli/src/app/src/langs/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,6 @@
"replaceConfiguration": "Remplacer la configuration",
"sourcePath": "Chemin d'origine",
"outputPath": "Chemin de destination",
"close": "Fermer"
"close": "Fermer",
"pluginSettings": "Paramètres du plugin"
}
7 changes: 1 addition & 6 deletions packages/cli/src/app/src/lib/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ const webConfigToApiConfig = (webConfig: TConfiguration): z.infer<typeof optiona
const content = optionalConfigSchema.parse({
name: webConfig.name,
plugins: (webConfig.plugins ?? []).map((plugin) => {
if (!plugin.outputPath) {
return plugin.sourcePath
}
return {
name: plugin.sourcePath,
path: plugin.outputPath,
paths: plugin.paths.map((p) => p.to),
}
}),
})
Expand Down Expand Up @@ -73,15 +70,13 @@ const apiConfigToWebConfig = async (apiConfig: z.infer<typeof optionalConfigSche
code: "INTERNAL_SERVER_ERROR",
})
}
const outputPath = typeof plugin === "string" ? undefined : plugin.path

return {
name: foundPlugin.name,
description: foundPlugin.description,
id: foundPlugin.id,
sourcePath: foundPlugin.sourcePath,
paths: foundPlugin.paths,
outputPath,
}
}),
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/app/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

40 changes: 18 additions & 22 deletions packages/scripts/src/ci-check/templates-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import { z } from "zod"

import { cdAtRoot, cwdAtRoot } from "@/utils"
import { pluginConfigSchema, templateSchema, TPluginConfig } from "@/utils/template-config"
import { logger } from "@rharkor/logger"
import { templateSchema } from "@/utils/template-config"
import { task } from "@rharkor/task"

type TConfig = z.infer<typeof templateSchema>
Expand Down Expand Up @@ -64,34 +63,31 @@ for (const template of templates) {
const pluginName = typeof reference === "string" ? reference : reference.name
const pluginPath = path.join(pluginsDirectory, pluginName)
if (!(await fs.exists(pluginPath))) {
validateTemplateTask.error(`The plugin ${pluginName} referenced by the config doesn't exist`)
validateTemplateTask.error(`The plugin ${pluginName} referenced by the config ${template.path} doesn't exist`)
validateTemplateTask.stop()
process.exit(1)
}

//? Check if the plugin confg exists
const pluginConfigPath = path.join(pluginPath, configFileName)
const pluginContentName = (await fs.readdir(pluginPath)).find((file) => file.startsWith("index"))
if (!pluginContentName) {
validateTemplateTask.error(`The plugin ${pluginName} referenced by the config doesn't exist`)
validateTemplateTask.stop()
process.exit(1)
}
const pluginContentPath = path.join(pluginPath, pluginContentName)
const pluginConfig = (await fs.readJson(pluginConfigPath)) as TPluginConfig
try {
pluginConfigSchema.parse(pluginConfig)
} catch (error) {
validateTemplateTask.error(`The plugin config file ${pluginConfigPath} is not valid`)
if (!(await fs.exists(pluginConfigPath))) {
validateTemplateTask.error(`The plugin config for ${pluginName} doesn't exist`)
validateTemplateTask.stop()
logger.error(error)
process.exit(1)
}
}
}

//? Check if the plugin exists
if (!pluginContentPath || !(await fs.exists(pluginContentPath))) {
validateTemplateTask.error(`The plugin ${pluginName} referenced by the config doesn't exist`)
validateTemplateTask.stop()
process.exit(1)
}
//* No plugin duplicates
validateTemplateTask.log("Checking for plugin duplicates")
for (const template of templates) {
const pluginNames = template.config.plugins.map((reference) =>
typeof reference === "string" ? reference : reference.name
)
if (new Set(pluginNames).size !== pluginNames.length) {
validateTemplateTask.error(`The template ${template.path} has duplicate plugins`)
validateTemplateTask.stop()
process.exit(1)
}
}

Expand Down
84 changes: 48 additions & 36 deletions packages/scripts/src/utils/template-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ export function isPathInCurrentScope(filePath: string): boolean {

export const fullPluginSchema = z.object({
name: z.string(),
path: z
.string()
.optional()
.refine(
(value) => {
if (value === undefined) {
paths: z.array(
z
.string()
.optional()
.refine(
(value) => {
if (value === undefined) {
return true
}
if (!isPathInCurrentScope(value)) {
return false
}
return true
}
if (!isPathInCurrentScope(value)) {
return false
}
return true
},
{ message: "The path should be relative and in the current directory" }
),
},
{ message: "The path should be relative and in the current directory" }
)
),
})

export const configSchema = z.object({
Expand All @@ -48,28 +50,38 @@ export const templateSchema = z.object({
export const pluginConfigSchema = z.object({
name: z.string(),
description: z.string().max(300),
paths: z.array(
z.object({
from: z.string().refine(
(value) => {
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" }
),
})
),
paths: z
.array(
z.object({
from: z.string().refine(
(value) => {
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" }
),
})
)
.refine(
(value) => {
const paths = value.map((path) => path.to)
return new Set(paths).size === paths.length
},
{
message: "The 'to' paths should be unique",
}
),
})

export type TPluginConfig = z.infer<typeof pluginConfigSchema>

0 comments on commit e993c21

Please sign in to comment.