-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add overwrite: false property to added files
By default template-oss allows for written files to be configured on a per-repo basis. This is helpful for different repos to create their own templated files and apply those. With this change, a repo can now set `overwrite: false` to a templated file and have those updates be applied after the default template-oss changes are made.
- Loading branch information
1 parent
83f3699
commit 38df407
Showing
6 changed files
with
144 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,53 @@ | ||
const { mergeWith } = require('lodash') | ||
|
||
const merge = (...objects) => mergeWith({}, ...objects, (value, srcValue, key) => { | ||
if (Array.isArray(srcValue)) { | ||
// Dont merge arrays, last array wins | ||
return srcValue | ||
} | ||
}) | ||
const mergeWithCustomizers = (...customizers) => { | ||
const sourceStack = [] | ||
const pathStack = [] | ||
return (...objects) => mergeWith({}, ...objects, (value, srcValue, key, target, source) => { | ||
let currentPath | ||
while (true) { | ||
if (!sourceStack.length) { | ||
sourceStack.push(source) | ||
pathStack.push([]) | ||
} | ||
const prevSource = sourceStack[sourceStack.length - 1] | ||
const prevPath = pathStack[pathStack.length - 1] | ||
if (source === prevSource) { | ||
currentPath = prevPath.concat(key) | ||
sourceStack.push(srcValue) | ||
pathStack.push(currentPath) | ||
break | ||
} | ||
sourceStack.pop() | ||
pathStack.pop() | ||
} | ||
for (const cusomtizer of customizers) { | ||
const result = cusomtizer(value, srcValue, key, target, source, currentPath) | ||
if (result !== undefined) { | ||
return result | ||
} | ||
} | ||
}) | ||
} | ||
|
||
const mergeWithArrays = (...keys) => | ||
(...objects) => mergeWith({}, ...objects, (value, srcValue, key) => { | ||
const customizers = { | ||
overwriteArrays: (value, srcValue) => { | ||
if (Array.isArray(srcValue)) { | ||
// Dont merge arrays, last array wins | ||
return srcValue | ||
} | ||
}, | ||
mergeArrays: (...keys) => (value, srcValue, key) => { | ||
if (Array.isArray(srcValue)) { | ||
if (keys.includes(key)) { | ||
return (Array.isArray(value) ? value : []).concat(srcValue) | ||
} | ||
return srcValue | ||
} | ||
}) | ||
}, | ||
} | ||
|
||
module.exports = merge | ||
module.exports.withArrays = mergeWithArrays | ||
module.exports = mergeWithCustomizers(customizers.overwriteArrays) | ||
module.exports.withArrays = (...keys) => mergeWithCustomizers(customizers.mergeArrays(...keys)) | ||
module.exports.withCustomizers = mergeWithCustomizers | ||
module.exports.customizers = customizers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const t = require('tap') | ||
const setup = require('../setup.js') | ||
|
||
t.test('json merge', async (t) => { | ||
const s = await setup(t, { | ||
ok: true, | ||
package: { | ||
templateOSS: { | ||
content: 'content', | ||
}, | ||
}, | ||
testdir: { | ||
content: { | ||
'index.js': `module.exports=${JSON.stringify({ | ||
rootModule: { | ||
add: { | ||
'package.json': { | ||
file: 'more-package.json', | ||
overwrite: false, | ||
}, | ||
}, | ||
}, | ||
})}`, | ||
'more-package.json': JSON.stringify({ | ||
scripts: { | ||
test: 'tap test/', | ||
}, | ||
}), | ||
}, | ||
}, | ||
}) | ||
|
||
await s.apply() | ||
|
||
const pkg = await s.readJson('package.json') | ||
t.equal(pkg.scripts.test, 'tap test/') | ||
t.equal(pkg.scripts.snap, 'tap') | ||
|
||
t.strictSame(await s.check(), []) | ||
}) |