Skip to content

Commit

Permalink
refactor: copy files smarter (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Aug 23, 2023
1 parent b140ad8 commit dd6eca6
Show file tree
Hide file tree
Showing 13 changed files with 864 additions and 586 deletions.
20 changes: 19 additions & 1 deletion __tests__/unit/lib/service/botHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('BotHandler', () => {
})

describe('when called with generateDelta true', () => {
it('should add and copy the related bot', async () => {
it('should add and copy the related parent bot', async () => {
const sut = new BotHandler(line, objectType, work, globalMetadata)

// Act
Expand All @@ -77,7 +77,25 @@ describe('BotHandler', () => {
new Set(['TestBot.v1'])
)
expect(copyFiles).toBeCalledTimes(4)
expect(copyFiles).toBeCalledWith(
work.config,
`force-app/main/default/bots/TestBot/v1.botVersion-meta.xml`
)
expect(copyFiles).toBeCalledWith(
work.config,
`force-app/main/default/bots/TestBot/v1.botVersion`
)
expect(copyFiles).toBeCalledWith(
work.config,
`force-app/main/default/bots/TestBot/TestBot.bot`
)
expect(copyFiles).toBeCalledWith(
work.config,
`force-app/main/default/bots/TestBot/TestBot.bot-meta.xml`
)
})
})
})

// TODO getMetaTypeFilePath
})
36 changes: 22 additions & 14 deletions __tests__/unit/lib/service/inResourceHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,30 @@ describe('InResourceHandler', () => {
)
})
it.each([
['staticresources', 'StaticResource', 'image', 'image.png', 3],
['staticresources', 'StaticResource', 'image', 'image/logo.png', 3],
[
'imageFile.png',
'staticresources',
'StaticResource',
'imageFile',
2,
],
[
'imageFolder/logo.png',
'staticresources',
'StaticResource',
'imageFolder',
3,
],
[
'my_experience_bundle/config/myexperiencebundle.json',
'experiences',
'ExperienceBundle',
'my_experience_bundle',
'my_experience_bundle/config/myexperiencebundle.json',
5,
3,
],
])(
'should copy the matching folder resource, matching meta file and subject file',
async (type, xmlName, entity, path, expectedCount) => {
'should copy the matching folder resource, matching meta file and subject file %s',
async (path, type, xmlName, entity, expectedCopyCount) => {
// Arrange
const base = 'force-app/main/default/'
const line = `A ${base}${type}/${path}`
Expand All @@ -97,15 +109,11 @@ describe('InResourceHandler', () => {

// Assert
expect(...work.diffs.package.get(xmlName)).toEqual(entity)
expect(copyFiles).toBeCalledTimes(expectedCount)
expect(copyFiles).toBeCalledTimes(expectedCopyCount)
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${base}${type}/${path}`
)
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${base}${type}/${path}${METAFILE_SUFFIX}`
)
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${base}${type}/${entity}.${
Expand All @@ -130,7 +138,7 @@ describe('InResourceHandler', () => {

// Assert
expect(...work.diffs.package.get(xmlName)).toEqual(entity)
expect(copyFiles).toBeCalledTimes(1)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${base}${type}/${path}`
Expand All @@ -139,7 +147,7 @@ describe('InResourceHandler', () => {
})

describe('when no matching resource exist', () => {
it('should not copy resource files', async () => {
it('should try copy resource files', async () => {
// Arrange
const sut = new InResourceHandler(
line,
Expand All @@ -156,7 +164,7 @@ describe('InResourceHandler', () => {
expect(...work.diffs.package.get(xmlName)).toEqual(element)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toHaveBeenCalledWith(work.config, `${entityPath}`)
expect(copyFiles).not.toHaveBeenCalledWith(
expect(copyFiles).toHaveBeenCalledWith(
work.config,
`${basePath}/${element}.${type}${METAFILE_SUFFIX}`
)
Expand Down
36 changes: 35 additions & 1 deletion __tests__/unit/lib/service/sharedFolderHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'
const SharedFolderHandler = require('../../../../src/service/sharedFolderHandler')
const { copyFiles } = require('../../../../src/utils/fsHelper')
const { METAFILE_SUFFIX } = require('../../../../src/utils/metadataConstants')

jest.mock('../../../../src/utils/fsHelper')

const objectType = 'discovery'
const entityName = 'DiscoveryAIModelTest'
const entityExtension = 'model'
const line = `A force-app/main/default/${objectType}/${entityName}.${entityExtension}`
const basePath = `force-app/main/default/`
const line = `A ${basePath}${objectType}/${entityName}.${entityExtension}`
const entityType = 'DiscoveryAIModel'

let work
Expand Down Expand Up @@ -34,4 +39,33 @@ describe('SharedFolderHandler', () => {
expect(work.diffs.package.get(entityType).size).toEqual(1)
expect(...work.diffs.package.get(entityType)).toEqual(entityName)
})
describe('when it should generate output file', () => {
beforeEach(() => {
work.config.generateDelta = true
})
it('should add and copy the metadata', async () => {
const sut = new SharedFolderHandler(
line,
objectType,
work,
globalMetadata
)

// Act
await sut.handleAddition()

// Assert
expect(work.diffs.package.get(entityType).size).toEqual(1)
expect(...work.diffs.package.get(entityType)).toEqual(entityName)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toBeCalledWith(
work.config,
`${basePath}${objectType}/${entityName}.${entityExtension}`
)
expect(copyFiles).toBeCalledWith(
work.config,
`${basePath}${objectType}/${entityName}.${entityExtension}${METAFILE_SUFFIX}`
)
})
})
})
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,30 @@
"upgrade:dependencies": "shx rm -rf yarn.lock ; shx touch yarn.lock ; yarn-upgrade-all ; yarn-audit-fix"
},
"devDependencies": {
"@commitlint/cli": "^17.6.7",
"@commitlint/config-conventional": "^17.6.7",
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@oclif/dev-cli": "^1.26.10",
"@salesforce/cli-plugins-testkit": "^4.2.8",
"@salesforce/cli-plugins-testkit": "^4.3.0",
"@salesforce/dev-config": "^4.0.1",
"@salesforce/ts-sinon": "^1.4.13",
"@salesforce/ts-sinon": "^1.4.14",
"@stryker-mutator/core": "^7.1.1",
"@stryker-mutator/jest-runner": "^7.1.1",
"@swc/core": "^1.3.74",
"@swc/core": "^1.3.76",
"@types/mocha": "^10.0.1",
"@types/node": "^20.4.8",
"@typescript-eslint/parser": "^6.2.1",
"@types/node": "^20.5.0",
"@typescript-eslint/parser": "^6.4.0",
"benchmark": "^2.1.4",
"chai": "^4.3.7",
"depcheck": "^1.4.3",
"eslint": "^8.46.0",
"eslint": "^8.47.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
"jest": "^29.6.2",
"lint-staged": "^13.2.3",
"lint-staged": "^14.0.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^3.0.1",
"prettier": "^3.0.2",
"shx": "^0.3.4",
"sinon": "^15.2.0",
"ts-node": "^10.9.1",
Expand Down
5 changes: 2 additions & 3 deletions src/post-processor/flowTranslationProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ class FlowTranslationProcessor extends BaseProcessor {
member: getTranslationName(translationPath),
})
if (this.config.generateDelta) {
const jsonTranslation = await this._getTranslationAsJSON(
translationPath
)
const jsonTranslation =
await this._getTranslationAsJSON(translationPath)
this._scrapTranslationFile(
jsonTranslation,
this.translationPaths.get(translationPath)
Expand Down
5 changes: 3 additions & 2 deletions src/service/botHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const SharedFolderHandler = require('./sharedFolderHandler')
const { fillPackageWithParameter } = require('../utils/packageHelper')
const { parse, sep } = require('path')
const { DOT } = require('../utils/fsHelper')

const BOT_TYPE = 'Bot'
const BOT_EXTENSION = 'bot'
Expand All @@ -13,15 +14,15 @@ class BotHandler extends SharedFolderHandler {
parsedPath.dir.split(sep).pop(),
parsedPath.name,
])
return [...elementName].join('.')
return [...elementName].join(DOT)
}
async handleAddition() {
await super.handleAddition()
await this._addParentBot()
}

async _addParentBot() {
const botName = this._getParsedPath().dir.split(sep).pop()
const botName = this.parentFolder.split(sep).pop()
fillPackageWithParameter({
store: this.diffs.package,
type: BOT_TYPE,
Expand Down
4 changes: 2 additions & 2 deletions src/service/inFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
} = require('../utils/metadataConstants')
const StandardHandler = require('./standardHandler')
const { basename } = require('path')
const { writeFile } = require('../utils/fsHelper')
const { writeFile, DOT } = require('../utils/fsHelper')
const {
getInFileAttributes,
isPackable,
Expand All @@ -16,7 +16,7 @@ const {
fillPackageWithParameter,
} = require('../utils/packageHelper')

const getRootType = line => basename(line).split('.')[0]
const getRootType = line => basename(line).split(DOT)[0]
const getNamePrefix = ({ subType, line }) =>
subType !== LABEL_XML_NAME ? `${getRootType(line)}.` : ''

Expand Down
4 changes: 1 addition & 3 deletions src/service/inFolderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ class InFolderHandler extends StandardHandler {
}

_isProcessable() {
const parsedLine = parse(this.line)
const parentFolder = parsedLine.dir.split(sep).pop()
return (
super._isProcessable() ||
parentFolder !== this.type ||
this._parentFolderIsNotTheType() ||
this.ext.endsWith(INFOLDER_SUFFIX)
)
}
Expand Down
70 changes: 31 additions & 39 deletions src/service/inResourceHandler.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
'use strict'
const StandardHandler = require('./standardHandler')
const { join, parse } = require('path')
const { pathExists, readDir } = require('../utils/fsHelper')
const { META_REGEX } = require('../utils/metadataConstants')
const { join, parse, sep } = require('path')
const { pathExists } = require('../utils/fsHelper')
const { META_REGEX, METAFILE_SUFFIX } = require('../utils/metadataConstants')
const { cleanUpPackageMember } = require('../utils/packageHelper')

const STATICRESOURCE_TYPE = 'staticresources'
const elementSrc = new Map()
const { DOT } = require('../utils/fsHelper')

class ResourceHandler extends StandardHandler {
constructor(line, type, work, metadata) {
super(line, type, work, metadata)
this.metadataName = this._getMetadataName()
}

async handleAddition() {
await super.handleAddition()
if (!this.config.generateDelta) return
const [, srcPath, elementFile] = this._parseLine()
await this._buildElementMap(srcPath)
const matchingFiles = this._buildMatchingFiles(elementFile)
const elementName = parse(elementFile).name
await Promise.all(
elementSrc
.get(srcPath)
.filter(
src =>
(this.type === STATICRESOURCE_TYPE &&
src.startsWith(elementName)) ||
matchingFiles.includes(src)
)
.map(src => this._copyWithMetaFile(join(srcPath, src)))
)

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

async handleDeletion() {
const [, srcPath, elementName] = this._parseLine()
const exists = await pathExists(join(srcPath, elementName), this.config)
const [, elementPath, elementName] = this._parseLine()
const exists = await pathExists(join(elementPath, elementName), this.config)
if (exists) {
await this.handleModification()
} else {
Expand All @@ -56,27 +44,31 @@ class ResourceHandler extends StandardHandler {
)
}

_buildMatchingFiles(elementName) {
const parsedElementName = parse(elementName).name
const matchingFiles = [parsedElementName]
if (this.metadata.get(this.type).metaFile) {
matchingFiles.push(this._getRelativeMetadataXmlFileName(elementName))
}
return matchingFiles
_isProcessable() {
return true
}

async _buildElementMap(srcPath) {
if (!elementSrc.has(srcPath)) {
const dirContent = await readDir(srcPath, this.config)
elementSrc.set(
srcPath,
dirContent.map(f => f.replace(/\/$/, ''))
)
_getMetadataName() {
const resourcePath = []
for (const pathElement of this.splittedLine) {
if (resourcePath.slice(-2)[0] === this.type) {
break
}
resourcePath.push(pathElement)
}
let lastPathElement = resourcePath[resourcePath.length - 1].split(DOT)
if (lastPathElement.length > 1) {
lastPathElement.pop()
}

resourcePath[resourcePath.length - 1] = lastPathElement.join(DOT)
return `${resourcePath.join(sep)}`
}

_isProcessable() {
return true
_getMetaTypeFilePath() {
return `${this.metadataName}.${
this.metadata.get(this.type).suffix
}${METAFILE_SUFFIX}`
}
}

Expand Down
Loading

0 comments on commit dd6eca6

Please sign in to comment.