Skip to content
Merged
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
69 changes: 39 additions & 30 deletions src/utilities/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,6 @@ export async function generateManifestFiles(
}
}

for (const node of themeNodes) {
// Add theme nodes not present in the old import map
// They have been added manually by the user since the last time the import map was generated
if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) {
const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name)

if (collectionNode) {
if (options.ignoreConflicts) {
// If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry
continue;
} else {
// If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry
newFilesMap[node.themeFolder][node.name] = '@theme'
logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`)
}
} else {
// If the node does not exist in the collection, add it to the new import map as a @theme entry
newFilesMap[node.themeFolder][node.name] = '@theme'
}
}

// Persist prexisting asset entries from @theme or other collections
if (node.type === 'asset') {
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
}
}
}

function addFilesMapEntry(themeFolder: LiquidNode['themeFolder'], name: string) {
const oldImportMapValue = oldFilesMap[themeFolder]?.[name]
const newImportMapValue = newFilesMap[themeFolder]?.[name]
Expand Down Expand Up @@ -184,5 +154,44 @@ export async function generateManifestFiles(
}
}

for (const node of themeNodes) {
// Add theme nodes not present in the old import map
// They have been added manually by the user since the last time the import map was generated
if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) {
const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name)

if (collectionNode) {
if (options.ignoreConflicts) {
// If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry
continue;
} else {
// If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry
newFilesMap[node.themeFolder][node.name] = '@theme'
logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`)
}
} else {
// If the node does not exist in the collection, add it to the new import map as a @theme entry
newFilesMap[node.themeFolder][node.name] = '@theme'
}
}

// Persist prexisting asset entries from @theme or other collections
if (node.type === 'asset') {
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
}
}

// Persist prexisting snippet entries from @theme or other collections
if (node.type === 'snippet') {
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
const newImportMapValue = newFilesMap[node.themeFolder]?.[node.name]
if (newImportMapValue === undefined && oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
}
}
}

return newFilesMap
}
12 changes: 12 additions & 0 deletions test/commands/theme/component/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ describe('theme component map', () => {
expect(data.files.snippets['include-tag.liquid']).to.equal('@theme') // Should work with include tag
})

it('persists entries that are associated with @theme or another collection in the manifest', async () => {
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
expect(beforeData.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme')
expect(beforeData.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection')

await runCommand(['theme', 'component', 'map', testThemePath])

const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
expect(data.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme')
expect(data.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection')
})

it('adds entries for newly referenced components from current collection', async () => {
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
expect(beforeData.files.snippets['new.liquid']).to.be.undefined
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/theme/component.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"to-be-copied.liquid": "@archetype-themes/test-collection",
"to-be-copied-snippet.liquid": "@archetype-themes/test-collection",
"existing-subfolder.liquid": "@theme",
"removed-subfolder.liquid": "@theme"
"removed-subfolder.liquid": "@theme",
"unreferenced-theme-snippet.liquid": "@theme",
"unreferenced-other-collection-snippet.liquid": "@other/collection"
},
"assets": {
"other-collection-component.css": "@other/collection",
Expand Down
Empty file.