Skip to content

Commit

Permalink
fix: issue copying resource, meta and folder for resource type (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Aug 2, 2024
1 parent 0f186ce commit 10fd87a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 188 deletions.
30 changes: 14 additions & 16 deletions __tests__/unit/lib/service/inResourceHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,36 @@ describe('InResourceHandler', () => {
work.config.generateDelta = true
})
describe('when matching resource exist', () => {
beforeEach(() => {
mockedReadDir.mockResolvedValue([
'other.resource-meta.xml',
'other/',
'image.resource-meta.xml',
'my_experience_bundle.site-meta.xml',
'my_experience_bundle/',
'other_experience_bundle.resource-meta.xml',
])
})
it.each([
['imageFile.png', staticResourceType, 'imageFile', 2],
['imageFolder/logo.png', staticResourceType, 'imageFolder', 3],
['imageFolder/logo.png', staticResourceType, 'imageFolder', 2],
[
'my_experience_bundle/config/myexperiencebundle.json',
experienceBundleType,
'my_experience_bundle',
3,
5,
],
[
'CustomerSupport/permissionSetFieldPermissions/Account.Test__c.permissionSetFieldPermission-meta.xml',
permissionSetType,
'CustomerSupport',
3,
2,
],
])(
'should copy the matching folder resource, matching meta file and subject file %s',
async (path, type, entity, expectedCopyCount) => {
// Arrange
const base = 'force-app/main/default/'
const line = `A ${base}${type.directoryName}/${path}`
mockedReadDir.mockResolvedValue([
`${base}${type.directoryName}/other.resource-meta.xml`,
`${base}${type.directoryName}/other/`,
`${base}${type.directoryName}/image.resource-meta.xml`,
`${base}${type.directoryName}/my_experience_bundle.site-meta.xml`,
`${base}${type.directoryName}/my_experience_bundle/`,
`${base}${type.directoryName}/my_experience_bundle/config/myexperiencebundle.json`,
`${base}${type.directoryName}/other_experience_bundle.resource-meta.xml`,
])
const sut = new InResourceHandler(line, type, work, globalMetadata)

// Act
Expand Down Expand Up @@ -153,7 +152,7 @@ describe('InResourceHandler', () => {
expect(work.diffs.package.get(type.xmlName)).toEqual(
new Set([entity])
)
expect(copyFiles).toBeCalledTimes(3)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${base}${type.directoryName}/${path}`
Expand Down Expand Up @@ -211,8 +210,7 @@ describe('InResourceHandler', () => {
expect(
Array.from(work.diffs.package.get(permissionSetType.xmlName)!)
).toEqual([metadataElement])
expect(copyFiles).toBeCalledTimes(3)
expect(copyFiles).toHaveBeenCalledWith(work.config, path)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toHaveBeenCalledWith(work.config, path)
})
})
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"author": "Sebastien Colladon <colladonsebastien@gmail.com>",
"dependencies": {
"@salesforce/command": "^5.3.9",
"@salesforce/core": "^8.2.6",
"@salesforce/core": "^8.2.7",
"async": "^3.2.5",
"fast-xml-parser": "^4.4.1",
"fs-extra": "^11.2.0",
Expand Down Expand Up @@ -234,8 +234,8 @@
"@types/jest": "^29.5.12",
"@types/mocha": "^10.0.7",
"@types/node": "^22.0.0",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"benchmark": "^2.1.4",
"chai": "^4.3.10",
"eslint": "^8.57.0",
Expand Down
39 changes: 21 additions & 18 deletions src/service/inResourceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,19 @@ import { join, parse } from 'path'

import { DOT, PATH_SEP } from '../constant/fsConstants'
import { METAFILE_SUFFIX, META_REGEX } from '../constant/metadataConstants'
import { MetadataRepository } from '../metadata/MetadataRepository'
import { Metadata } from '../types/metadata'
import type { Work } from '../types/work'
import { pathExists } from '../utils/fsHelper'
import { pathExists, readDir } from '../utils/fsHelper'

import StandardHandler from './standardHandler'

export default class ResourceHandler extends StandardHandler {
protected readonly metadataName: string

constructor(
line: string,
metadataDef: Metadata,
work: Work,
metadata: MetadataRepository
) {
super(line, metadataDef, work, metadata)
this.metadataName = this._getMetadataName()
}
protected metadataName: string | undefined

public override async handleAddition() {
this.metadataName = this._getMetadataName()
await super.handleAddition()
if (!this.config.generateDelta) return

if (this.line !== this.metadataName && this._parentFolderIsNotTheType()) {
await this._copy(this.metadataName)
}
await this._copyResourceFiles()
}

public override async handleDeletion() {
Expand All @@ -42,6 +28,23 @@ export default class ResourceHandler extends StandardHandler {
}
}

protected async _copyResourceFiles() {
const staticResourcePath = this.metadataName!.substring(
0,
this.metadataName!.lastIndexOf(PATH_SEP)
)
const allStaticResources = await readDir(
staticResourcePath,
this.work.config
)
const resourceFiles = allStaticResources.filter((file: string) =>
file.startsWith(this.metadataName!)
)
for (const resourceFile of resourceFiles) {
await this._copy(resourceFile)
}
}

protected override _getElementName() {
const parsedPath = this._getParsedPath()
return parsedPath.name
Expand Down
Loading

0 comments on commit 10fd87a

Please sign in to comment.