Skip to content

Commit

Permalink
fix: issue with inResource meta file not copied for corner cases (#179)
Browse files Browse the repository at this point in the history
* fix: issue with inResource meta file not copied for corner cases

* perf: do not copy already copied files
  • Loading branch information
scolladon authored Aug 4, 2021
1 parent ee56c9f commit 909eba3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/metadata/a48.json
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "",
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/metadata/v47.json
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/metadata/v49.json
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/metadata/v50.json
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/metadata/v51.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/metadata/v52.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@
"directoryName": "experiences",
"inFolder": false,
"metaFile": true,
"suffix": "site",
"xmlName": "ExperienceBundle"
},
{
Expand Down
34 changes: 28 additions & 6 deletions src/service/inResourceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const fs = require('fs')
const mc = require('../utils/metadataConstants')

const STATICRESOURCE_TYPE = 'staticresources'
const elementSrc = {}

class ResourceHandler extends StandardHandler {
Expand All @@ -20,14 +21,16 @@ class ResourceHandler extends StandardHandler {
'u'
)
)
this._buildElementMap(srcPath)

const parsedElementName = path.parse(elementName)
if (!Object.prototype.hasOwnProperty.call(elementSrc, srcPath)) {
elementSrc[srcPath] = fs.readdirSync(srcPath)
}

const matchingFiles = this._buildMatchingFiles(elementName)
elementSrc[srcPath]
.filter(src => path.parse(src).name === parsedElementName.name)
.filter(
src =>
(this.type === STATICRESOURCE_TYPE &&
src.startsWith(path.parse(elementName).name)) ||
matchingFiles.includes(src)
)
.forEach(src =>
this._copyFiles(
path.normalize(path.join(srcPath, src)),
Expand Down Expand Up @@ -70,6 +73,25 @@ class ResourceHandler extends StandardHandler {
.replace(this.suffixRegex, '')
)
}

_buildMatchingFiles(elementName) {
const parsedElementName = path.parse(elementName).name
const matchingFiles = [parsedElementName]
if (StandardHandler.metadata[this.type].metaFile) {
matchingFiles.push(
`${parsedElementName}.${StandardHandler.metadata[this.type].suffix}${
mc.METAFILE_SUFFIX
}`
)
}
return matchingFiles
}

_buildElementMap(srcPath) {
if (!Object.prototype.hasOwnProperty.call(elementSrc, srcPath)) {
elementSrc[srcPath] = fs.readdirSync(srcPath)
}
}
}

module.exports = ResourceHandler
5 changes: 4 additions & 1 deletion src/service/standardHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const FSE_COPYSYNC_OPTION = {
preserveTimestamps: false,
}

const copiedFiles = new Set()

class StandardHandler {
static metadata

Expand Down Expand Up @@ -98,8 +100,9 @@ class StandardHandler {
}

_copyFiles(src, dst) {
if (fse.pathExistsSync(src)) {
if (!copiedFiles.has(src) && fse.pathExistsSync(src)) {
fse.copySync(src, dst, FSE_COPYSYNC_OPTION)
copiedFiles.add(src)
}
}

Expand Down

0 comments on commit 909eba3

Please sign in to comment.