99 lines
2.7 KiB
TypeScript
99 lines
2.7 KiB
TypeScript
import { mkdir, rename, rm } from "node:fs/promises"
|
|
|
|
const segments = "ui,model,api"
|
|
const args = Bun.argv.slice(2)
|
|
|
|
function printUsageAndExit(): never {
|
|
console.error(
|
|
"Ошибка: Необходимо указать <название слоя> и <название компонента>",
|
|
)
|
|
console.log("Пример: bun gc features MyButton")
|
|
process.exit(1)
|
|
}
|
|
|
|
function runShell(command: string): void {
|
|
console.log(`Выполняется: ${command}`)
|
|
|
|
const result = Bun.spawnSync({
|
|
cmd: ["sh", "-lc", command],
|
|
stdin: "inherit",
|
|
stdout: "inherit",
|
|
stderr: "inherit",
|
|
})
|
|
|
|
if (result.exitCode !== 0) {
|
|
const suffix = result.signalCode ? ` (signal ${result.signalCode})` : ""
|
|
throw new Error(`Команда завершилась с кодом ${result.exitCode}${suffix}`)
|
|
}
|
|
}
|
|
|
|
if (args.length < 2) {
|
|
printUsageAndExit()
|
|
}
|
|
|
|
const layer = args[0]
|
|
const component = args[1]
|
|
const otherArgs = args.slice(2).join(" ")
|
|
|
|
let componentPath = "./src/"
|
|
|
|
if (layer === "shared" || layer === "app") {
|
|
componentPath += `${layer}/${component}`
|
|
} else if (layer === "entity" || layer === "entities") {
|
|
componentPath += `entities/${component}`
|
|
} else {
|
|
componentPath += `${layer}s/${component}`
|
|
}
|
|
|
|
const fsdCommand = `fsd ${layer} ${component} ${otherArgs} --segments ${segments} -r src`
|
|
|
|
try {
|
|
runShell(fsdCommand)
|
|
} catch (error) {
|
|
console.error(`Ошибка выполнения команды: ${(error as Error).message}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
console.log(`Удаление индекс файла: ${componentPath}/index.ts`)
|
|
|
|
try {
|
|
await rm(`${componentPath}/index.ts`)
|
|
} catch (error) {
|
|
console.error(`Ошибка выполнения команды: ${(error as Error).message}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
const createTsx = `bunx generate-react-cli component ${component} --flat --path ${componentPath}`
|
|
|
|
try {
|
|
runShell(createTsx)
|
|
} catch (error) {
|
|
console.error(`Ошибка выполнения команды: ${(error as Error).message}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
console.log("Перемещение файлов...")
|
|
|
|
const oldPath = `${componentPath}/${component}`
|
|
const newPath = `${componentPath}`
|
|
|
|
try {
|
|
await mkdir(`${newPath}/ui`, { recursive: true })
|
|
await mkdir(`${newPath}/model`, { recursive: true })
|
|
|
|
await rename(`${oldPath}.tsx`, `${newPath}/ui/${component}.tsx`)
|
|
console.log("TSX файл перемещен")
|
|
|
|
await rename(
|
|
`${oldPath}.module.scss`,
|
|
`${newPath}/ui/${component}.module.scss`,
|
|
)
|
|
console.log("SCSS файл перемещен")
|
|
|
|
await rename(`${oldPath}.d.ts`, `${newPath}/model/${component}.d.ts`)
|
|
console.log("D.TS файл перемещен")
|
|
} catch (error) {
|
|
console.error(`Ошибка выполнения команды: ${(error as Error).message}`)
|
|
process.exit(1)
|
|
}
|