Skip to content

Commit f7641f1

Browse files
committed
Fix tests and linting errors
1 parent f592935 commit f7641f1

4 files changed

Lines changed: 71 additions & 70 deletions

File tree

src/commands/theme/component/copy.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,19 @@ export default class Copy extends BaseCommand {
5353
}
5454

5555
const manifest = getManifest(path.join(destinationDir, 'component.manifest.json'))
56-
const sourceNodes = await getCollectionNodes(currentDir)
57-
const manifest = getManifest(path.join(themeDir, 'component.manifest.json'))
5856
const componentNodes = await getCollectionNodes(currentDir)
5957

6058
const duplicates = getDuplicateFiles(componentNodes);
6159

6260
if (duplicates.size > 0) {
6361
const message: string[] = []
64-
duplicates.forEach((nodes, key) => {
62+
for (const [key, nodes] of duplicates) {
6563
message.push(`Warning: Found duplicate files for ${key}:`)
66-
nodes.forEach(node => {
64+
for (const node of nodes) {
6765
message.push(` - ${node.file}`)
68-
})
69-
});
66+
}
67+
}
68+
7069
this.error(message.join('\n'))
7170
}
7271

@@ -78,12 +77,12 @@ export default class Copy extends BaseCommand {
7877
for (const [fileName, fileCollection] of Object.entries(manifest.files[fileType])) {
7978
if (fileCollection !== sourceName) continue;
8079

81-
const node = sourceNodes.find(node => node.name === fileName && node.themeFolder === fileType);
80+
const node = componentNodes.find(node => node.name === fileName && node.themeFolder === fileType);
8281

8382
if (!node) continue;
8483

8584
if (isThemeRepo(destinationDir)) {
86-
copyFileIfChanged(node.file, path.join(destinationDir, fileType, fileName));
85+
copyFileIfChanged(node.file, path.join(destinationDir, fileType, fileName));
8786
} else if (isComponentRepo(destinationDir)) {
8887
const dest = node.file.replace(currentDir, destinationDir)
8988
copyFileIfChanged(node.file, dest);
@@ -94,18 +93,18 @@ export default class Copy extends BaseCommand {
9493
const setupDestDir = path.join(path.dirname(dest), 'setup');
9594
const testSrcDir = path.join(path.dirname(node.file), 'test');
9695
const testDestDir = path.join(path.dirname(dest), 'test');
97-
96+
9897
if (fs.existsSync(setupSrcDir)) {
9998
fs.mkdirSync(setupDestDir, { recursive: true });
10099
fs.cpSync(setupSrcDir, setupDestDir, { recursive: true });
101100
}
102-
101+
103102
if (fs.existsSync(testSrcDir)) {
104-
fs.mkdirSync(testDestDir, { recursive: true });
103+
fs.mkdirSync(testDestDir, { recursive: true });
105104
fs.cpSync(testSrcDir, testDestDir, { recursive: true });
106105
}
107106
}
108-
}
107+
}
109108
}
110109
};
111110

src/commands/theme/component/manifest.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default class Manifest extends BaseCommand {
5757
const ignoreConflicts = this.flags[Flags.IGNORE_CONFLICTS]
5858
const ignoreOverrides = this.flags[Flags.IGNORE_OVERRIDES]
5959
const componentSelector = this.args[Args.COMPONENT_SELECTOR]
60-
60+
6161
const manifestPath = path.join(destinationDir, 'component.manifest.json')
6262
const manifest = getManifest(manifestPath);
6363

@@ -73,18 +73,19 @@ export default class Manifest extends BaseCommand {
7373

7474
if (duplicates.size > 0) {
7575
const message: string[] = []
76-
duplicates.forEach((nodes, key) => {
76+
for (const [key, nodes] of duplicates) {
7777
message.push(`Warning: Found duplicate files for ${key}:`)
78-
nodes.forEach(node => {
78+
for (const node of nodes) {
7979
message.push(` - ${node.file}`)
80-
})
81-
});
80+
}
81+
}
82+
8283
this.error(message.join('\n'))
8384
}
8485

8586
let destinationNodes: LiquidNode[]
8687
let destinationName: string
87-
88+
8889
if (isThemeRepo(destinationDir)) {
8990
destinationNodes = await getThemeNodes(destinationDir)
9091
destinationName = '@theme'

src/utilities/nodes.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function generateLiquidNode(file: string, type: LiquidNode['type'],
6565
assets = await getJsAssets(body)
6666
}
6767

68-
if (type === 'snippet') {
68+
if (type === 'snippet') {
6969
body = fs.readFileSync(file, 'utf8')
7070
assets = getJsImportsFromLiquid(body)
7171
}
@@ -118,27 +118,28 @@ export async function getCollectionNodes(collectionDir: string): Promise<LiquidN
118118

119119
export function getDuplicateFiles(nodes: LiquidNode[]): Map<string, LiquidNode[]> {
120120
const duplicateMap = new Map<string, LiquidNode[]>();
121-
122-
nodes.forEach(node => {
123-
if (node.themeFolder !== 'snippets' && node.themeFolder !== 'assets') return;
124-
const key = `${node.themeFolder}/${node.name}`;
125-
if (!duplicateMap.has(key)) {
126-
duplicateMap.set(key, [node]);
127-
} else {
128-
duplicateMap.get(key)!.push(node);
121+
122+
for (const node of nodes) {
123+
if (node.themeFolder === 'snippets' || node.themeFolder === 'assets') {
124+
const key = `${node.themeFolder}/${node.name}`;
125+
if (duplicateMap.has(key)) {
126+
duplicateMap.get(key)!.push(node);
127+
} else {
128+
duplicateMap.set(key, [node]);
129+
}
129130
}
130-
});
131+
}
131132

132133
// Filter to only return entries with duplicates
133134
return new Map(
134-
Array.from(duplicateMap.entries())
135+
[...duplicateMap.entries()]
135136
.filter(([_, nodes]) => nodes.length > 1)
136137
);
137138
}
138139

139140
export async function getThemeNodes(themeDir: string): Promise<LiquidNode[]> {
140141
const entryNodes = globSync(path.join(themeDir, '{layout,sections,blocks,templates}', '*.liquid'), { absolute: true })
141-
.map(file => {
142+
.map(file => {
142143
const parentFolderName = path.basename(path.dirname(file)) as LiquidNode['themeFolder']
143144
return generateLiquidNode(file, 'entry', parentFolderName)
144145
})

test/commands/theme/component/manifest.test.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {runCommand} from '@oclif/test'
2-
import {expect} from 'chai'
3-
import {execSync} from 'node:child_process'
1+
import { runCommand } from '@oclif/test'
2+
import { expect } from 'chai'
3+
import { execSync } from 'node:child_process'
44
import * as fs from 'node:fs'
55
import * as path from 'node:path'
6-
import {fileURLToPath} from 'node:url'
6+
import { fileURLToPath } from 'node:url'
77

88
const __dirname = path.dirname(fileURLToPath(import.meta.url))
99
const fixturesPath = path.join(__dirname, '../../../fixtures')
@@ -15,34 +15,34 @@ const testCollectionBPath = path.join(fixturesPath, 'test-collection-b')
1515
const testThemePath = path.join(fixturesPath, 'test-theme')
1616
describe('theme component manifest', () => {
1717
beforeEach(() => {
18-
fs.cpSync(collectionPath, testCollectionPath, {recursive: true})
19-
fs.cpSync(collectionBPath, testCollectionBPath, {recursive: true})
20-
fs.cpSync(themePath, testThemePath, {recursive: true})
18+
fs.cpSync(collectionPath, testCollectionPath, { recursive: true })
19+
fs.cpSync(collectionBPath, testCollectionBPath, { recursive: true })
20+
fs.cpSync(themePath, testThemePath, { recursive: true })
2121
process.chdir(testCollectionPath)
2222
})
2323

2424
afterEach(() => {
25-
fs.rmSync(testCollectionPath, {force: true, recursive: true})
26-
fs.rmSync(testCollectionBPath, {force: true, recursive: true})
27-
fs.rmSync(testThemePath, {force: true, recursive: true})
25+
fs.rmSync(testCollectionPath, { force: true, recursive: true })
26+
fs.rmSync(testCollectionBPath, { force: true, recursive: true })
27+
fs.rmSync(testThemePath, { force: true, recursive: true })
2828
})
2929

3030
it('should throw an error if the cwd is not a component collection', async () => {
3131
process.chdir(testThemePath)
32-
const {error} = await runCommand(['theme', 'component', 'manifest', testThemePath])
32+
const { error } = await runCommand(['theme', 'component', 'manifest', testThemePath])
3333
expect(error).to.be.instanceOf(Error)
3434
expect(error?.message).to.include('Warning: ')
3535
})
3636

3737
it('should throw an error if a theme directory is not provided', async () => {
38-
const {error} = await runCommand(['theme', 'component', 'manifest'])
38+
const { error } = await runCommand(['theme', 'component', 'manifest'])
3939
expect(error).to.be.instanceOf(Error)
4040
expect(error?.message).to.include('Missing 1 required arg:')
4141
})
4242

4343
it('creates a component.manifest.json in current theme directory if it does not exist', async () => {
4444
// Confirm that the file does not exist
45-
fs.rmSync(path.join(testThemePath, 'component.manifest.json'), {force: true})
45+
fs.rmSync(path.join(testThemePath, 'component.manifest.json'), { force: true })
4646
expect(fs.existsSync(path.join(testThemePath, 'component.manifest.json'))).to.be.false
4747

4848
await runCommand(['theme', 'component', 'manifest', testThemePath])
@@ -114,28 +114,28 @@ describe('theme component manifest', () => {
114114
})
115115

116116
it('throws a warning if there is a potential conflict with an entry in the current collection', async () => {
117-
const {stdout} = await runCommand(['theme', 'component', 'manifest', testThemePath])
117+
const { stdout } = await runCommand(['theme', 'component', 'manifest', testThemePath])
118118
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
119119
expect(stdout).to.include('Conflict Warning: Pre-existing file')
120120
expect(data.files.snippets['conflict.liquid']).to.equal('@theme')
121121
})
122122

123123
it('ignores conflicts if --ignore-conflicts flag is passed', async () => {
124-
const {stdout} = await runCommand(['theme', 'component', 'manifest', testThemePath, '--ignore-conflicts'])
124+
const { stdout } = await runCommand(['theme', 'component', 'manifest', testThemePath, '--ignore-conflicts'])
125125
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
126126
expect(stdout).to.not.include('Conflict Warning: Pre-existing file')
127127
expect(data.files.snippets['conflict.liquid']).to.equal('@archetype-themes/test-collection')
128128
})
129129

130130
it('throws a warning when an override is detected', async () => {
131-
const {stdout} = await runCommand(['theme', 'component', 'manifest', testThemePath])
131+
const { stdout } = await runCommand(['theme', 'component', 'manifest', testThemePath])
132132
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
133133
expect(stdout).to.include('Override Warning:')
134134
expect(data.files.snippets['override.liquid']).to.equal('@theme')
135135
})
136136

137137
it('overriden parent still references non-overridden child from collection', async () => {
138-
const {stdout} = await runCommand(['theme', 'component', 'manifest', testThemePath])
138+
const { stdout } = await runCommand(['theme', 'component', 'manifest', testThemePath])
139139
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
140140
expect(stdout).to.include('Override Warning:')
141141
expect(data.files.snippets['override-parent.liquid']).to.equal('@theme')
@@ -144,7 +144,7 @@ describe('theme component manifest', () => {
144144
})
145145

146146
it('ignores overrides if --ignore-overrides flag is passed', async () => {
147-
const {stdout} = await runCommand(['theme', 'component', 'manifest', testThemePath, '--ignore-overrides'])
147+
const { stdout } = await runCommand(['theme', 'component', 'manifest', testThemePath, '--ignore-overrides'])
148148
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
149149
expect(stdout).to.not.include('Override Warning:')
150150
expect(data.files.snippets['override.liquid']).to.equal('@archetype-themes/test-collection')
@@ -176,7 +176,7 @@ describe('theme component manifest', () => {
176176

177177
it('persists entries from other collections or @theme if those files still exist', async () => {
178178
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
179-
179+
180180
// Check that referenced files are present in map
181181
expect(beforeData.files.snippets['theme-component.liquid']).to.equal('@theme')
182182
expect(beforeData.files.assets['theme-component.css']).to.equal('@theme')
@@ -230,7 +230,7 @@ describe('theme component manifest', () => {
230230
await runCommand(['theme', 'component', 'manifest', testThemePath, 'new'])
231231

232232
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
233-
233+
234234
// Should include the selected component and its assets
235235
expect(data.files.snippets['new.liquid']).to.equal('@archetype-themes/test-collection')
236236
expect(data.files.assets['new.css']).to.equal('@archetype-themes/test-collection')
@@ -244,7 +244,7 @@ describe('theme component manifest', () => {
244244
await runCommand(['theme', 'component', 'manifest', testThemePath, 'new,parent'])
245245

246246
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
247-
247+
248248
// Should include both selected components and their dependencies
249249
expect(data.files.snippets['new.liquid']).to.equal('@archetype-themes/test-collection')
250250
expect(data.files.assets['new.css']).to.equal('@archetype-themes/test-collection')
@@ -257,37 +257,37 @@ describe('theme component manifest', () => {
257257
await runCommand(['theme', 'component', 'manifest', testThemePath, 'child'])
258258

259259
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
260-
260+
261261
// Should not include the snippet since it's not a component
262262
expect(data.files.snippets['child.liquid']).to.be.undefined
263263
})
264264

265265
it('should include all components when using "*" as component selector', async () => {
266266
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
267-
267+
268268
await runCommand(['theme', 'component', 'manifest', testThemePath, '*'])
269269

270270
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
271-
271+
272272
// Should include all components from the collection
273273
expect(data.files.snippets['new.liquid']).to.equal('@archetype-themes/test-collection')
274274
expect(data.files.assets['new.css']).to.equal('@archetype-themes/test-collection')
275275
expect(data.files.snippets['parent.liquid']).to.equal('@archetype-themes/test-collection')
276276
expect(data.files.snippets['child.liquid']).to.equal('@archetype-themes/test-collection')
277-
277+
278278
// Should still maintain other collection entries
279279
for (const [key, value] of Object.entries(beforeData.files.snippets)
280280
.filter(([_, value]) => value !== '@archetype-themes/test-collection')) {
281-
if (fs.existsSync(path.join(testThemePath, 'snippets', key))) {
282-
expect(data.files.snippets[key]).to.equal(value)
283-
}
281+
if (fs.existsSync(path.join(testThemePath, 'snippets', key))) {
282+
expect(data.files.snippets[key]).to.equal(value)
284283
}
284+
}
285285
})
286286

287287
it('should throw an error when no components match the selector', async () => {
288288
const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
289-
290-
const {error} = await runCommand(['theme', 'component', 'manifest', testThemePath, 'non-existent'])
289+
290+
const { error } = await runCommand(['theme', 'component', 'manifest', testThemePath, 'non-existent'])
291291

292292
// Should throw an error
293293
expect(error).to.be.instanceOf(Error)
@@ -312,29 +312,29 @@ describe('theme component manifest', () => {
312312
it('should detect JS imports from script tags with {{ "filename" | asset_url }} filter', async () => {
313313
await runCommand(['theme', 'component', 'manifest', testThemePath])
314314
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
315-
315+
316316
expect(data.files.assets['script-with-filter.js']).to.equal('@archetype-themes/test-collection')
317317
})
318318

319319
it('should detect JS imports snippets inside components', async () => {
320320
await runCommand(['theme', 'component', 'manifest', testThemePath])
321321
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
322-
322+
323323
expect(data.files.assets['script-snippet-import.js']).to.equal('@archetype-themes/test-collection')
324324
})
325325

326326
it('should detect JS imports from script tags with import statements', async () => {
327327
await runCommand(['theme', 'component', 'manifest', testThemePath])
328328
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
329-
329+
330330
expect(data.files.assets['script-with-import.js']).to.equal('@archetype-themes/test-collection')
331331
expect(data.files.assets['shared-min-other.js']).to.equal('@archetype-themes/test-collection')
332332
})
333333

334334
it('should not include commented out script imports', async () => {
335335
await runCommand(['theme', 'component', 'manifest', testThemePath])
336336
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
337-
337+
338338
expect(data.files.assets['commented-script.js']).to.be.undefined
339339
})
340340

@@ -345,9 +345,9 @@ describe('theme component manifest', () => {
345345
execSync('git config user.name "Test User"', { cwd: testCollectionPath })
346346
execSync('git add .', { cwd: testCollectionPath })
347347
execSync('git commit -m "Initial commit"', { cwd: testCollectionPath })
348-
348+
349349
// Get the commit hash we just created
350-
const expectedHash = execSync('git rev-parse HEAD', {
350+
const expectedHash = execSync('git rev-parse HEAD', {
351351
cwd: testCollectionPath,
352352
encoding: 'utf8'
353353
}).trim()
@@ -361,11 +361,11 @@ describe('theme component manifest', () => {
361361
it('should throw an error if there are duplicate files in the source collection', async () => {
362362
const src = path.join(testCollectionPath, 'components', 'duplicate', 'duplicate.liquid')
363363
const dest = path.join(testCollectionPath, 'components', 'duplicate', 'snippets', 'duplicate.liquid')
364-
365-
fs.cpSync(src, dest, {recursive: true})
366364

367-
const {error} = await runCommand(['theme', 'component', 'manifest', testThemePath])
365+
fs.cpSync(src, dest, { recursive: true })
366+
367+
const { error } = await runCommand(['theme', 'component', 'manifest', testThemePath])
368368
expect(error).to.be.instanceOf(Error)
369-
expect(error?.message).to.include('Error: Namespace conflict in source component collection with')
369+
expect(error?.message).to.include('Warning: Found duplicate files for')
370370
})
371371
})

0 commit comments

Comments
 (0)