Skip to content

Commit

Permalink
fix: wrong type detection in subfolders (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Jul 29, 2021
1 parent cda31f2 commit c722481
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
26 changes: 25 additions & 1 deletion __tests__/unit/lib/service/typeHandlerFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('the type handler factory', () => {
],
[InFolder, ['dashboards', 'documents', 'reports']],
[InResource, ['staticresources', 'aura', 'lwc']],
[Standard, ['objects']],
[Standard, ['classes']],
])('give %p handler', (handler, types) => {
test.each(types)('for %s folder', type => {
expect(
Expand All @@ -45,4 +45,28 @@ describe('the type handler factory', () => {
).toBeInstanceOf(handler)
})
})

test('can handle SubCustomObject', () => {
expect(
typeHandlerFactory.getTypeHandler(
`Z force-app/main/default/objects/Account/fields/Test__c`
)
).toBeInstanceOf(SubCustomObject)
})

test('can handle sub folder with SubCustomObject', () => {
expect(
typeHandlerFactory.getTypeHandler(
`Z force-app/main/default/objects/folder/Account/fields/Test__c`
)
).toBeInstanceOf(SubCustomObject)
})

test('can handle sub folder with non SubCustomObject', () => {
expect(
typeHandlerFactory.getTypeHandler(
`Z force-app/main/default/documents/classes/TestDocument`
)
).toBeInstanceOf(InFolder)
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"test:clear:cache": "jest --clearCache",
"test:coverage": "jest --coverage --runInBand",
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
"test:debug:break": "node --inspect-brk node_modules/.bin/jest --runInBand",
"postpack": "rm -f oclif.manifest.json",
"postrelease": "npm run release:tags && npm run release:github",
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
Expand Down Expand Up @@ -114,4 +115,4 @@
"sfdc": {
"latestApiVersion": "52"
}
}
}
46 changes: 27 additions & 19 deletions src/service/customObjectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,44 @@ const mc = require('../utils/metadataConstants')
const path = require('path')
const fs = require('fs')

const OBJECT_TYPE = 'objects'
const readFileSyncOptions = {
encoding: gc.UTF8_ENCODING,
}

class CustomObjectHandler extends StandardHandler {
handleAddition() {
super.handleAddition()
if (!this.config.generateDelta || this.type !== OBJECT_TYPE) return
if (!this.config.generateDelta) return
this._handleMasterDetailException()
}

_handleMasterDetailException() {
if (this.type !== CustomObjectHandler.OBJECT_TYPE) return

const fieldsFolder = path.resolve(
this.config.repo,
path.join(path.parse(this.line).dir, mc.FIELD_DIRECTORY_NAME)
)
if (fs.existsSync(fieldsFolder)) {
fs.readdirSync(fieldsFolder)
.filter(fieldPath =>
fs
.readFileSync(
path.resolve(this.config.repo, fieldsFolder, fieldPath),
{
encoding: gc.UTF8_ENCODING,
}
)
.includes(mc.MASTER_DETAIL_TAG)
)
.forEach(field =>
this._copyFiles(
path.resolve(this.config.repo, fieldsFolder, field),
path.resolve(this.config.output, fieldsFolder, field)
if (!fs.existsSync(fieldsFolder)) return

fs.readdirSync(fieldsFolder)
.filter(fieldPath =>
fs
.readFileSync(
path.resolve(this.config.repo, fieldsFolder, fieldPath),
readFileSyncOptions
)
.includes(mc.MASTER_DETAIL_TAG)
)
.forEach(field =>
this._copyFiles(
path.resolve(this.config.repo, fieldsFolder, field),
path.resolve(this.config.output, fieldsFolder, field)
)
}
)
}

static OBJECT_TYPE = 'objects'
}

module.exports = CustomObjectHandler
19 changes: 10 additions & 9 deletions src/service/typeHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@ const classes = {
workflows: InFile,
}

const EMPTY_STRING = ''
const haveSubTypes = [CustomObject.OBJECT_TYPE, EMPTY_STRING]

module.exports = class HandlerFactory {
constructor(work, metadata) {
this.work = work
this.metadata = metadata
}

getTypeHandler(line) {
const type = line
.split(path.sep)
.reduce(
(acc, value) =>
Object.prototype.hasOwnProperty.call(this.metadata, value)
? value
: acc,
''
)
const type = line.split(path.sep).reduce((acc, value, _, arr) => {
acc = Object.prototype.hasOwnProperty.call(this.metadata, value)
? value
: acc
if (!haveSubTypes.includes(acc)) arr.splice(1)
return acc
}, EMPTY_STRING)

return classes[type]
? new classes[type](line, type, this.work, this.metadata)
Expand Down

0 comments on commit c722481

Please sign in to comment.