Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/cli-kit/src/public/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,16 @@ export async function copyDirectoryContents(srcDir: string, destDir: string): Pr
}

// Get all files and directories in the source directory
const items = await glob(joinPath(srcDir, '**/*'))
// Optimization: Using cwd in glob is faster and avoids manual string replacement for relative paths.
const items = await glob('**/*', {cwd: srcDir})

const filesToCopy = []

for (const item of items) {
const relativePath = item.replace(srcDir, '').replace(/^[/\\]/, '')
for (const relativePath of items) {
const srcPath = joinPath(srcDir, relativePath)
const destPath = joinPath(destDir, relativePath)

filesToCopy.push(copyFile(item, destPath))
filesToCopy.push(copyFile(srcPath, destPath))
}

await Promise.all(filesToCopy)
Expand Down
16 changes: 9 additions & 7 deletions packages/cli-kit/src/public/node/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
fileExists,
matchGlob,
} from './fs.js'
import {joinPath, dirname, relativePath} from './path.js'
import {joinPath, dirname} from './path.js'
import {outputContent, outputToken, outputDebug} from './output.js'
import {Liquid} from 'liquidjs'

Expand Down Expand Up @@ -38,26 +38,28 @@ export function renderLiquidTemplate(templateContent: string, data: object): Pro
*/
export async function recursiveLiquidTemplateCopy(from: string, to: string, data: object): Promise<void> {
outputDebug(outputContent`Copying template from directory ${outputToken.path(from)} to ${outputToken.path(to)}`)
const templateFiles: string[] = await glob(joinPath(from, '**/*'), {dot: true})
// Optimization: Using cwd in glob is faster and avoids manual string replacement for relative paths.
const templateFilesRelative: string[] = await glob('**/*', {cwd: from, dot: true})

const bypassPaths = joinPath(from, '.cli-liquid-bypass')
let bypassPatterns: string[] = []
if (await fileExists(bypassPaths)) {
bypassPatterns = (await readFile(bypassPaths)).split('\n').filter((line) => line.trim().length > 0)
}

const sortedTemplateFiles = templateFiles
const sortedTemplateFilesRelative = templateFilesRelative
.map((path) => path.split('/'))
.sort((lhs, rhs) => (lhs.length < rhs.length ? 1 : -1))
.map((components) => components.join('/'))

await Promise.all(
sortedTemplateFiles.map(async (templateItemPath) => {
const outputPath = await renderLiquidTemplate(joinPath(to, relativePath(from, templateItemPath)), data)
sortedTemplateFilesRelative.map(async (relativePath) => {
const templateItemPath = joinPath(from, relativePath)
const outputPath = await renderLiquidTemplate(joinPath(to, relativePath), data)
const bypass = bypassPatterns.some((pattern) => {
const path = relativePath(from, templateItemPath)
const cleanPattern = pattern.replace(/^\.\//, '')

return matchGlob(path, cleanPattern) || path.startsWith(cleanPattern)
return matchGlob(relativePath, cleanPattern) || relativePath.startsWith(cleanPattern)
})

if (await isDirectory(templateItemPath)) {
Expand Down
Loading