Skip to content

Commit 3c5dcc9

Browse files
authored
fix: Persist entries from theme or other collections (#15)
1 parent c0c80b0 commit 3c5dcc9

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

src/utilities/manifest.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,6 @@ export async function generateManifestFiles(
8686
}
8787
}
8888

89-
for (const node of themeNodes) {
90-
// Add theme nodes not present in the old import map
91-
// They have been added manually by the user since the last time the import map was generated
92-
if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) {
93-
const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name)
94-
95-
if (collectionNode) {
96-
if (options.ignoreConflicts) {
97-
// If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry
98-
continue;
99-
} else {
100-
// If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry
101-
newFilesMap[node.themeFolder][node.name] = '@theme'
102-
logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`)
103-
}
104-
} else {
105-
// If the node does not exist in the collection, add it to the new import map as a @theme entry
106-
newFilesMap[node.themeFolder][node.name] = '@theme'
107-
}
108-
}
109-
110-
// Persist prexisting asset entries from @theme or other collections
111-
if (node.type === 'asset') {
112-
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
113-
if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
114-
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
115-
}
116-
}
117-
}
118-
11989
function addFilesMapEntry(themeFolder: LiquidNode['themeFolder'], name: string) {
12090
const oldImportMapValue = oldFilesMap[themeFolder]?.[name]
12191
const newImportMapValue = newFilesMap[themeFolder]?.[name]
@@ -184,5 +154,44 @@ export async function generateManifestFiles(
184154
}
185155
}
186156

157+
for (const node of themeNodes) {
158+
// Add theme nodes not present in the old import map
159+
// They have been added manually by the user since the last time the import map was generated
160+
if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) {
161+
const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name)
162+
163+
if (collectionNode) {
164+
if (options.ignoreConflicts) {
165+
// If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry
166+
continue;
167+
} else {
168+
// If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry
169+
newFilesMap[node.themeFolder][node.name] = '@theme'
170+
logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`)
171+
}
172+
} else {
173+
// If the node does not exist in the collection, add it to the new import map as a @theme entry
174+
newFilesMap[node.themeFolder][node.name] = '@theme'
175+
}
176+
}
177+
178+
// Persist prexisting asset entries from @theme or other collections
179+
if (node.type === 'asset') {
180+
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
181+
if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
182+
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
183+
}
184+
}
185+
186+
// Persist prexisting snippet entries from @theme or other collections
187+
if (node.type === 'snippet') {
188+
const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name]
189+
const newImportMapValue = newFilesMap[node.themeFolder]?.[node.name]
190+
if (newImportMapValue === undefined && oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') {
191+
newFilesMap[node.themeFolder][node.name] = oldImportMapValue
192+
}
193+
}
194+
}
195+
187196
return newFilesMap
188197
}

test/commands/theme/component/map.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ describe('theme component map', () => {
7676
expect(data.files.snippets['include-tag.liquid']).to.equal('@theme') // Should work with include tag
7777
})
7878

79+
it('persists entries that are associated with @theme or another collection in the manifest', async () => {
80+
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
81+
expect(beforeData.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme')
82+
expect(beforeData.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection')
83+
84+
await runCommand(['theme', 'component', 'map', testThemePath])
85+
86+
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
87+
expect(data.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme')
88+
expect(data.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection')
89+
})
90+
7991
it('adds entries for newly referenced components from current collection', async () => {
8092
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
8193
expect(beforeData.files.snippets['new.liquid']).to.be.undefined

test/fixtures/theme/component.manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"to-be-copied.liquid": "@archetype-themes/test-collection",
1717
"to-be-copied-snippet.liquid": "@archetype-themes/test-collection",
1818
"existing-subfolder.liquid": "@theme",
19-
"removed-subfolder.liquid": "@theme"
19+
"removed-subfolder.liquid": "@theme",
20+
"unreferenced-theme-snippet.liquid": "@theme",
21+
"unreferenced-other-collection-snippet.liquid": "@other/collection"
2022
},
2123
"assets": {
2224
"other-collection-component.css": "@other/collection",

test/fixtures/theme/snippets/unreferenced-other-collection-snippet.liquid

Whitespace-only changes.

test/fixtures/theme/snippets/unreferenced-theme-snippet.liquid

Whitespace-only changes.

0 commit comments

Comments
 (0)