Skip to content

Commit

Permalink
fix: folder copy (#101)
Browse files Browse the repository at this point in the history
* Add UI Download Template for authDefault theme

* Refactor file and folder creation logic in defineDownload function

* Add dependencies and implement file scanning

* Fix file name bug in defineDownload function

* Add tempOutput option to DefineDownloadOptions

* version(cli): release 0.3.11

* version(nuxt): release 0.5.0-beta.20

* Update Pergel configuration and template paths

* Update Pergel auto-generated files

* version(nuxt): release 0.5.0-beta.21

* Update pergel version and add auth middleware

* Add createdAt, updatedAt, and username fields to seed1 function

* Add @pergel/nuxt as a dependency and configure pergel projects

* Add projectName option to download command

* Add new directories and update UI download command in README.yaml

* version(cli): release 0.3.12

* version(nuxt): release 0.5.0-beta.22

* Add new file write operation and replace operations in defineDownload function

* version(cli): release 0.3.13

* Update Pergel theme with new options

* Refactor defineDownload function in core.ts

* version(cli): release 0.3.14

* Fix variable assignment and string replacement in core.ts

* version(cli): release 0.3.15

* Refactor file and folder name replacements in core.ts

* version(cli): release 0.3.16

* version(cli): release 0.3.17

* version(cli): release 0.3.18

* Fix variable name replacements in defineDownload function

* Update package.json and README.yaml

* Update package.json, templates.json, and core.ts

* Add resolution for @pergel/nuxt package

* fix: lint issues
  • Loading branch information
productdevbook authored Jan 2, 2024
1 parent 616a830 commit 61237b8
Show file tree
Hide file tree
Showing 46 changed files with 1,126 additions and 115 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"vite": "5.0.10",
"vitest": "^1.1.1"
},
"resolutions": {
"@pergel/nuxt": "link:./packages/nuxt"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pergel",
"type": "module",
"version": "0.3.10",
"version": "0.3.18",
"packageManager": "pnpm@8.10.0",
"description": "Full Stack Nuxt Application. It contains the necessary toolkits for a software developer and a fast, clean, tested toolkit.",
"author": "Mehmet @productdevbook",
Expand Down Expand Up @@ -77,6 +77,7 @@
"consola": "^3.2.3",
"giget": "^1.2.1",
"globby": "^14.0.0",
"pathe": "^1.1.1",
"picocolors": "^1.0.0",
"shelljs": "^0.8.5",
"yaml": "^2.3.4"
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/playground/pergel/README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ pergel:
# This file is generated by pergel. Do not edit it manually.
# Version: 0.0.22
project1:
ui:
# UI Download Template
themes:
authDefault: pergel download -t=default-auth-1 -j=templates -p=rocket
drizzle:
# Script Commands
scripts:
Expand Down
15 changes: 0 additions & 15 deletions packages/cli/playground/pergel/templates/auths.json

This file was deleted.

64 changes: 64 additions & 0 deletions packages/cli/playground/pergel/templates/templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"version": "0.1.0",
"templates": {
"default-auth-1": {
"version": "0.0.1",
"branch": "main",
"file": {
"dir": "themes/pergel-auth",
"path": [
{
"fileName": "app.vue",
"outputFileName": "app.vue",
"forceClean": true
},
{
"fileName": "pages/index.vue",
"outputFileName": "pages/index.vue"
},
{
"fileName": "assets/pergel.css",
"outputFileName": "assets/pergel.css"
}
]
},
"folder": [
{
"dir": "themes/pergel-auth/pages/auth",
"output": "pages/auth",
"forceClean": true
},
{
"dir": "themes/pergel-auth/components",
"output": "components",
"forceClean": true
},
{
"dir": "themes/pergel-auth/layouts",
"output": "layouts",
"forceClean": true
},
{
"dir": "themes/pergel-auth/middleware",
"output": "middleware",
"forceClean": true
},
{
"dir": "themes/pergel-auth/composables",
"output": "composables",
"forceClean": true
},
{
"dir": "themes/pergel-auth/server/api/auth",
"output": "server/api/auth",
"forceClean": true
},
{
"dir": "themes/pergel-auth/server/middleware",
"output": "server/middleware",
"forceClean": true
}
]
}
}
}
18 changes: 15 additions & 3 deletions packages/cli/src/commands/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ export default defineCommand({
alias: 't',
description: 'Download file',
},
projectName: {
alias: 'p',
description: 'Project name',
},
},
async run({ args }) {
const template = args.template as string
const jsonFile = args.jsonFile as string
const projectName = args.projectName as string

const file = await loadConfig<ResolvedPergelConfig>({
cwd: process.cwd(),
configFile: 'pergel.config.ts',
defaultConfig: {
src: 'pergel',
templateDir: 'pergel/templates',
dir: {
pergel: 'pergel',
template: 'pergel/templates',
},
filePath: {
nuxtConfig: 'nuxt.config.ts',
},
},
})

Expand All @@ -46,7 +56,7 @@ export default defineCommand({
return
}

const templateDir = resolve(file.config.templateDir)
const templateDir = resolve(file.config.dir.template)

const data = readFileSync(join(templateDir, `${jsonFile}.json`), 'utf-8')

Expand Down Expand Up @@ -74,6 +84,8 @@ export default defineCommand({
file: _template.file,
folder: _template.folder,
branch: _template.branch,
tempOutput: '.tempPergel',
projectName,
})

await data({
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { defineCommand } from 'citty'
const template = `import { definePergel } from 'pergel/core'
export default definePergel({
src: 'pergel',
templateDir: 'pergel/templates',
dir: {
templateDir: 'pergel/templates',
pergelDir: 'pergel',
},
filePath: {
nuxtConfig: 'nuxt.config.ts',
}
})
`

Expand Down
15 changes: 10 additions & 5 deletions packages/cli/src/commands/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { loadConfig } from 'c12'
import { consola } from 'consola'
import { parse } from 'yaml'
import { parseNa, run } from '@antfu/ni'
import type { PergelConfig, PergelYaml } from '../types'
import type { PergelYaml, ResolvedPergelConfig } from '../types'

export default defineCommand({
meta: {
Expand All @@ -32,12 +32,17 @@ export default defineCommand({
},
async run({ args }) {
try {
const file = await loadConfig<Required<PergelConfig>>({
const file = await loadConfig<Required<ResolvedPergelConfig>>({
cwd: process.cwd(),
configFile: 'pergel.config.ts',
defaultConfig: {
src: 'pergel',
templateDir: 'pergel/templates',
dir: {
pergel: 'pergel',
template: 'pergel/templates',
},
filePath: {
nuxtConfig: 'nuxt.config.ts',
},
},
})

Expand All @@ -46,7 +51,7 @@ export default defineCommand({
return
}

const readmeString = readFileSync(resolve(file.config.src, 'README.yaml')).toString()
const readmeString = readFileSync(resolve(file.config.dir.pergel, 'README.yaml')).toString()
const json = parse(readmeString) as PergelYaml

const project = json[args.project]?.[args.module]
Expand Down
108 changes: 83 additions & 25 deletions packages/cli/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { copyFileSync, existsSync, mkdirSync, rmSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { downloadTemplate } from 'giget'
import { defu } from 'defu'
import { consola } from 'consola'
import { filename } from 'pathe/utils'
import { extname } from 'pathe'
import type { DefineDownloadOptions, PergelConfig } from './types'
import { scanAnyFiles } from './scan'

const logger = consola.create({
defaults: {
Expand All @@ -21,36 +24,43 @@ export function defineDownload(options: DefineDownloadOptions) {
}) {
const { cwd } = data
const githubRepo = 'github:oku-ui/pergel'
const projectName = options.projectName
const firstLetterProjectName = projectName.charAt(0).toUpperCase() + projectName.slice(1)

options = defu(options, {
file: {
tempOutput: '.tempPergel',
},
tempOutput: '.tempPergel',
branch: 'main',
}) as DefineDownloadOptions

if (options.file?.dir) {
const { dir } = await downloadTemplate(join(githubRepo, `${options.file.dir}#${options.branch}`), {
dir: options.file.tempOutput,
dir: options.tempOutput,
cwd,
force: true,
})

for (const file of options.file.path) {
const output = resolve(join(cwd, file.outputFileName))
const _dirname = dirname(output)

if (!existsSync(output)) {
// remove last .file extends and name for create folder
if (output.split('/').pop()?.includes('.')) {
const folder = output.split('/').slice(0, -1).join('/')
mkdirSync(resolve(folder), {
if (!existsSync(_dirname)) {
mkdirSync(_dirname, {
recursive: true,
})
}

copyFileSync(
join(dir, file.fileName),
let readFile = readFileSync(join(dir, file.fileName), 'utf-8')

if (file.replace?.from && file.replace?.to)
readFile.replace(file.replace?.from, file.replace?.to)

readFile = readFile.replace(`/changeName/g`, projectName)
.replace(`/ChangeName/g`, firstLetterProjectName)

writeFileSync(
resolve(output),
readFile,
)
}
else if (file.forceClean) {
Expand All @@ -59,41 +69,89 @@ export function defineDownload(options: DefineDownloadOptions) {
force: true,
})

if (output.split('/').pop()?.includes('.')) {
const folder = output.split('/').slice(0, -1).join('/')
mkdirSync(resolve(folder), {
if (!existsSync(_dirname)) {
mkdirSync(_dirname, {
recursive: true,
})
}

copyFileSync(
join(dir, file.fileName),
let readFile = readFileSync(join(dir, file.fileName), 'utf-8')

if (file.replace?.from && file.replace?.to)
readFile.replace(file.replace?.from, file.replace?.to)

readFile = readFile.replace(`/changeName/g`, projectName)
.replace(`/ChangeName/g`, firstLetterProjectName)

writeFileSync(
resolve(output),
readFile,
)
}

logger.success(`Downloaded template file: ${output}`)
}

rmSync(dir, {
recursive: true,
force: true,
retryDelay: 100,
})
}

if (options.folder && options.folder.length) {
for (const folder of options.folder) {
const { dir } = await downloadTemplate(join(githubRepo, `${folder.dir}#${options.branch}`), {
dir: folder.output,
dir: options.tempOutput,
cwd,
force: true,
forceClean: folder.forceClean !== false,
})

logger.success(`Downloaded template folder: ${dir}`)
// check folder
if (!existsSync(resolve(folder.output))) {
mkdirSync(resolve(folder.output), {
recursive: true,
})
}

const scanDir = await scanAnyFiles({
options: {
scanDirs: [folder.dir],
},
}, dir)

if (scanDir.length > 0) {
for (const file of scanDir) {
const _output = join(folder.output, file.replace(dir, ''))

const _dirname = dirname(_output)
const _file = filename(_output) + extname(_output)

if (!existsSync(_output)) {
mkdirSync(_dirname, {
recursive: true,
})
}

let readFile = readFileSync(join(file), 'utf-8')

if (folder.replace?.from !== 'changeName')
readFile.replace(folder.replace?.from || 'changeName', folder.replace?.to || projectName)

readFile = readFile
.replace(/changeName/g, projectName)
.replace(/ChangeName/g, firstLetterProjectName)

writeFileSync(
join(_output),
readFile,
)

logger.success(`Downloaded template folder: ${_file}`)
}
}
}
}

rmSync(options.tempOutput, {
recursive: true,
force: true,
})
}
return setup
}
Loading

0 comments on commit 61237b8

Please sign in to comment.