diff --git a/package-lock.json b/package-lock.json index 13c32a1..bbd68c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "plugin-devkit", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "plugin-devkit", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "dependencies": { "@oclif/core": "^4", diff --git a/src/utilities/nodes.ts b/src/utilities/nodes.ts index d33250c..4680508 100644 --- a/src/utilities/nodes.ts +++ b/src/utilities/nodes.ts @@ -7,7 +7,7 @@ import { LiquidNode } from './types.js' const LIQUID_BLOCK_REGEX = /{%-?.*?-?%}/gs const LIQUID_COMMENTS_REGEX = /{%-?\s*comment\s*-?%}[\S\s]*?{%-?\s*endcomment\s*-?%}/gi -const LIQUID_RENDER_REGEX = /\srender\s+'([^']+)'/gs +const LIQUID_RENDER_REGEX = /\s(?:render|include)\s+'([^']+)'/gs const ASSET_URL_REGEX = /{{\s*'([^']+\.js)'\s*\|\s*asset_url\s*}}/g const SCRIPT_TAG_REGEX = /]*>[\S\s]*?<\/script>/g const SCRIPT_IMPORT_REGEX = /import\s+["']([^"']+)["']/g @@ -119,10 +119,27 @@ export async function getCollectionNodes(collectionDir: string): Promise(['layout', 'sections', 'blocks', 'templates']) + let currentDir = path.dirname(file) + + while (currentDir !== themeDir && currentDir !== path.dirname(themeDir)) { + const folderName = path.basename(currentDir) + if (themeFolders.has(folderName as LiquidNode['themeFolder'])) { + return folderName as LiquidNode['themeFolder'] + } + + currentDir = path.dirname(currentDir) + } + + // Fallback to the immediate parent directory if no theme folder is found + return path.basename(path.dirname(file)) as LiquidNode['themeFolder'] +} + export async function getThemeNodes(themeDir: string): Promise { const entryNodes = globSync(path.join(themeDir, '{layout,sections,blocks,templates}', '**/*.liquid'), { absolute: true }) .map(file => { - const parentFolderName = path.basename(path.dirname(file)) as LiquidNode['themeFolder'] + const parentFolderName = findThemeFolder(file, themeDir) return generateLiquidNode(file, 'entry', parentFolderName) }) const themeSnippets = globSync(path.join(themeDir, 'snippets', '**/*.liquid'), { absolute: true }) diff --git a/test/commands/theme/component/map.test.ts b/test/commands/theme/component/map.test.ts index 29c2e53..4f06d27 100644 --- a/test/commands/theme/component/map.test.ts +++ b/test/commands/theme/component/map.test.ts @@ -64,14 +64,16 @@ describe('theme component map', () => { expect(beforeData.files.assets['missing.css']).to.be.undefined expect(beforeData.files.snippets['missing.liquid']).to.be.undefined expect(beforeData.files.snippets['missing-subfolder.liquid']).to.be.undefined + expect(beforeData.files.snippets['include-tag.liquid']).to.be.undefined await runCommand(['theme', 'component', 'map', testThemePath]) // Check that missing entries are present in map const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) - expect(data.files.assets['missing.css']).to.equal('@theme') - expect(data.files.snippets['missing.liquid']).to.equal('@theme') - expect(data.files.snippets['missing-subfolder.liquid']).to.equal('@theme') + expect(data.files.assets['missing.css']).to.equal('@theme') // Should work with assets + expect(data.files.snippets['missing.liquid']).to.equal('@theme') // Should work with render tag + expect(data.files.snippets['missing-subfolder.liquid']).to.equal('@theme') // Should work with subfolders + expect(data.files.snippets['include-tag.liquid']).to.equal('@theme') // Should work with include tag }) it('adds entries for newly referenced components from current collection', async () => { diff --git a/test/fixtures/theme/sections/include-tag.liquid b/test/fixtures/theme/sections/include-tag.liquid new file mode 100644 index 0000000..df66057 --- /dev/null +++ b/test/fixtures/theme/sections/include-tag.liquid @@ -0,0 +1 @@ +{% include 'include-tag' %} \ No newline at end of file diff --git a/test/fixtures/theme/snippets/include-tag.liquid b/test/fixtures/theme/snippets/include-tag.liquid new file mode 100644 index 0000000..9cf79a9 --- /dev/null +++ b/test/fixtures/theme/snippets/include-tag.liquid @@ -0,0 +1 @@ +{% comment %} This snippet is included using the include tag instead of render {% endcomment %} \ No newline at end of file