Skip to content

Commit ea1b76d

Browse files
committed
fix(update-helpers): manage nested directories paths
1 parent b0dc0a0 commit ea1b76d

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
"moduleNameMapper": {
5656
"^@db/(.*)$": "<rootDir>/common/db/$1",
5757
"^@common/(.*)$": "<rootDir>/common/$1",
58-
"^@auth/(.*)$": "<rootDir>/auth/$1"
58+
"^@auth/(.*)$": "<rootDir>/auth/$1",
59+
"^@models/(.*)$": "<rootDir>/models/$1",
60+
"^@bundler/(.*)$": "<rootDir>/bundler/$1"
5961
},
6062
"rootDir": "src",
6163
"testEnvironment": "node",

src/bundler/services/updates-helpers.service.spec.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('includeUpdatesHelpers', () => {
3535
productionScripts: ['src/bookmarks.ts'],
3636
})
3737

38-
globMock.mockResolvedValue(['src/bookmarks.ts'])
38+
globMock.mockResolvedValue(['esbuild/bookmarks.js'])
3939
})
4040

4141
it('should not do anything if not enabled', async () => {
@@ -431,3 +431,86 @@ describe('dropUpdateHelpers', () => {
431431
expect(Object.keys(finalWorkflow.uidata).every((k) => !k.includes(MANAGED_BY_FAST_ALFRED_PREFIX))).toBe(true)
432432
})
433433
})
434+
435+
describe('Nested directory handling', () => {
436+
const globMock = glob as unknown as jest.Mock
437+
const readConfigFileMock = readConfigFile as jest.Mock
438+
const readWorkflowMetadataMock = readWorkflowMetadata as jest.Mock
439+
const writeWorkflowMetadataMock = writeWorkflowMetadata as jest.Mock
440+
const buildOptionsMock = require('../utils/bundler.utils').buildOptions as jest.Mock
441+
442+
beforeEach(() => {
443+
jest.clearAllMocks()
444+
readConfigFileMock.mockResolvedValue({
445+
updates: {
446+
bundleHelpers: true,
447+
},
448+
})
449+
})
450+
451+
it('should match nested script paths with ** glob pattern', async () => {
452+
buildOptionsMock.mockResolvedValue({
453+
targetDir: 'esbuild',
454+
assetsDir: 'assets',
455+
productionScripts: ['src/main/**/*.ts'],
456+
})
457+
458+
globMock.mockResolvedValue([
459+
'esbuild/translate/translate.js',
460+
'esbuild/translate/select-language.js',
461+
'esbuild/tones.js',
462+
])
463+
464+
const mockWorkflow = JSON.parse(JSON.stringify(workflowMock))
465+
mockWorkflow.objects.push({
466+
type: 'alfred.workflow.input.scriptfilter',
467+
uid: 'NESTED-UID',
468+
config: {
469+
script: 'esbuild/translate/translate.js',
470+
},
471+
})
472+
mockWorkflow.connections['NESTED-UID'] = [{ destinationuid: 'ACTION-UID', modifiers: 0 }]
473+
mockWorkflow.uidata['NESTED-UID'] = { xpos: 100, ypos: 100 }
474+
475+
readWorkflowMetadataMock.mockResolvedValue(mockWorkflow)
476+
477+
await includeUpdatesHelpers()
478+
479+
expect(writeWorkflowMetadataMock).toHaveBeenCalledTimes(1)
480+
const finalWorkflow = writeWorkflowMetadataMock.mock.calls[0][0]
481+
const conditionalUid = CONDITIONAL_OBJECT_UID('NESTED-UID', 'ACTION-UID')
482+
483+
expect(finalWorkflow.connections['NESTED-UID'][0].destinationuid).toBe(conditionalUid)
484+
})
485+
486+
it('should match flat script paths with single-level glob', async () => {
487+
buildOptionsMock.mockResolvedValue({
488+
targetDir: 'esbuild',
489+
assetsDir: 'assets',
490+
productionScripts: ['src/main/*.ts'],
491+
})
492+
493+
globMock.mockResolvedValue(['esbuild/tones.js'])
494+
495+
const mockWorkflow = JSON.parse(JSON.stringify(workflowMock))
496+
mockWorkflow.objects.push({
497+
type: 'alfred.workflow.input.scriptfilter',
498+
uid: 'FLAT-UID',
499+
config: {
500+
script: 'esbuild/tones.js',
501+
},
502+
})
503+
mockWorkflow.connections['FLAT-UID'] = [{ destinationuid: 'ACTION-UID', modifiers: 0 }]
504+
mockWorkflow.uidata['FLAT-UID'] = { xpos: 100, ypos: 100 }
505+
506+
readWorkflowMetadataMock.mockResolvedValue(mockWorkflow)
507+
508+
await includeUpdatesHelpers()
509+
510+
expect(writeWorkflowMetadataMock).toHaveBeenCalledTimes(1)
511+
const finalWorkflow = writeWorkflowMetadataMock.mock.calls[0][0]
512+
const conditionalUid = CONDITIONAL_OBJECT_UID('FLAT-UID', 'ACTION-UID')
513+
514+
expect(finalWorkflow.connections['FLAT-UID'][0].destinationuid).toBe(conditionalUid)
515+
})
516+
})

src/bundler/services/updates-helpers.service.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { glob } from 'glob'
2-
import { basename, extname, join } from 'node:path'
2+
import { basename, join, relative } from 'node:path'
33
import { readConfigFile } from '@common/user-config.service'
44
import { readWorkflowMetadata, writeWorkflowMetadata } from '@common/workflow-metadata.service'
55
import { UserConfigVariables } from '@models/client-updates-config.model'
@@ -123,15 +123,12 @@ async function getTargetUids(
123123
return []
124124
}
125125

126-
const sourceFiles = await glob(productionScripts)
126+
const builtJsFiles = await glob(`${targetDir}/**/*.js`)
127127

128-
const mainScriptPaths = sourceFiles.map((file) => {
129-
const suffix = extname(file)
130-
const baseName = basename(file, suffix)
131-
132-
const targetedFile = `${baseName}.js`
128+
const mainScriptPaths = builtJsFiles.map((filePath) => {
129+
const relativePath = relative(targetDir, filePath)
133130
const targetDirName = basename(targetDir)
134-
return join(targetDirName, targetedFile)
131+
return join(targetDirName, relativePath)
135132
})
136133

137134
const targetUids: string[] = workflow?.objects

0 commit comments

Comments
 (0)