Skip to content

Commit

Permalink
Merge pull request #4572 from Shopify/add_shopifyignore_support
Browse files Browse the repository at this point in the history
Allow ignoring liquid files using .cli-liquid-bypass
  • Loading branch information
paulomarg authored Oct 17, 2024
2 parents b5a2378 + 09ca514 commit b9036b2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/app/src/cli/services/init/template/cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('cleanup', () => {
await Promise.all([
// should keep these
writeFile(joinPath(tmpDir, 'server.js'), 'console.log()'),
writeFile(joinPath(tmpDir, 'cli-liquid-bypass'), '*'),
mkdir(joinPath(tmpDir, 'node_modules')),

// should delete these
Expand Down Expand Up @@ -40,6 +41,7 @@ describe('cleanup', () => {
await expect(fileExists(joinPath(tmpDir, '.git'))).resolves.toBe(false)
await expect(fileExists(joinPath(tmpDir, '.github'))).resolves.toBe(false)
await expect(fileExists(joinPath(tmpDir, '.gitmodules'))).resolves.toBe(false)
await expect(fileExists(joinPath(tmpDir, '.cli-liquid-bypass'))).resolves.toBe(false)
await expect(fileExists(joinPath(tmpDir, 'frontend', '.git'))).resolves.toBe(false)
await expect(fileExists(joinPath(tmpDir, 'package.json.cli2'))).resolves.toBe(false)
})
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/cli/services/init/template/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default async function cleanup(webOutputDirectory: string) {
joinPath(webOutputDirectory, '**', '.git'),
joinPath(webOutputDirectory, '**', '.github'),
joinPath(webOutputDirectory, '**', '.gitmodules'),
joinPath(webOutputDirectory, '**', '.cli-liquid-bypass'),
joinPath(webOutputDirectory, 'LICENSE*'),
joinPath(webOutputDirectory, '**', 'frontend/LICENSE*'),
joinPath(webOutputDirectory, 'package.json.cli2'),
Expand Down
36 changes: 36 additions & 0 deletions packages/cli-kit/src/public/node/liquid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ describe('recursiveLiquidTemplateCopy', () => {
expect(JSON.parse(outPackageJson)).toEqual(packageJson)
})
})

test('ignores files and folders in .cli-liquid-bypass', async () => {
const bypassPatterns = ['ignored.liquid', 'ignored-folder']

// Given
await inTemporaryDirectory(async (tmpDir) => {
// Given
const from = joinPath(tmpDir, 'from')
const ignoredFolder = joinPath(from, 'ignored-folder')
const to = joinPath(tmpDir, 'to')
await mkdir(from)
await mkdir(ignoredFolder)
await mkdir(to)

const bypassFile = joinPath(from, '.cli-liquid-bypass')
const ignoredFile = joinPath(from, 'ignored.liquid')
const folderIgnoredFile = joinPath(ignoredFolder, 'ignored2.liquid')
const processedFile = joinPath(from, 'processed.md.liquid')
await writeFile(bypassFile, bypassPatterns.join('\n'))
await writeFile(ignoredFile, '# {{variable}}')
await writeFile(folderIgnoredFile, '# {{variable}}')
await writeFile(processedFile, '# {{variable}}')

// When
await recursiveLiquidTemplateCopy(from, to, {variable: 'test'})

// Then
const outFile = joinPath(to, 'ignored.liquid')
const outFolderFile = joinPath(to, 'ignored-folder/ignored2.liquid')
const outProcessedFile = joinPath(to, 'processed.md')

await expect(readFile(outFile)).resolves.toEqual('# {{variable}}')
await expect(readFile(outFolderFile)).resolves.toEqual('# {{variable}}')
await expect(readFile(outProcessedFile)).resolves.toEqual('# test')
})
})
})
28 changes: 26 additions & 2 deletions packages/cli-kit/src/public/node/liquid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import {mkdir, readFile, copyFile, chmod, isDirectory, writeFile, fileHasExecutablePermissions, glob} from './fs.js'
import {
mkdir,
readFile,
copyFile,
chmod,
isDirectory,
writeFile,
fileHasExecutablePermissions,
glob,
fileExists,
matchGlob,
} from './fs.js'
import {joinPath, dirname, relativePath} from './path.js'
import {outputContent, outputToken, outputDebug} from '../../public/node/output.js'
import {Liquid} from 'liquidjs'
Expand Down Expand Up @@ -29,16 +40,29 @@ export async function recursiveLiquidTemplateCopy(from: string, to: string, data
outputDebug(outputContent`Copying template from directory ${outputToken.path(from)} to ${outputToken.path(to)}`)
const templateFiles: string[] = await glob(joinPath(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
.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)
const bypass = bypassPatterns.some((pattern) => {
const path = relativePath(from, templateItemPath)
const cleanPattern = pattern.replace(/^\.\//, '')

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

if (await isDirectory(templateItemPath)) {
await mkdir(outputPath)
} else if (templateItemPath.endsWith('.liquid')) {
} else if (templateItemPath.endsWith('.liquid') && !bypass) {
await mkdir(dirname(outputPath))
const content = await readFile(templateItemPath)
const contentOutput = await renderLiquidTemplate(content, data)
Expand Down

0 comments on commit b9036b2

Please sign in to comment.