Skip to content

Commit

Permalink
Merge pull request #57 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu committed Apr 11, 2019
2 parents 1149dbd + 49e60b1 commit 19dfbf5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/logic/smart-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ module.exports = (filepath, content, options = {}) => {
assert(content instanceof Object);
assert(options instanceof Object && !Array.isArray(options));

const ext = getExt(filepath);
const ctx = Object.assign({
treatAs: null,
treatAs: ext === 'js' ? 'txt' : ext,
mergeStrategy: (existing, changeset) => changeset
}, options);
assert(Object.keys(ctx).length === 2, 'Unexpected Option provided!');
assert(ctx.treatAs === null || typeof ctx.treatAs === 'string');
assert(typeof ctx.treatAs === 'string');
assert(typeof ctx.mergeStrategy === 'function');

const currentContent = fs.existsSync(filepath)
? smartRead(filepath, { treatAs: ctx.treatAs || (getExt(filepath) === 'js' ? 'txt' : null) })
? smartRead(filepath, { treatAs: ctx.treatAs })
: null;

const mergedContent = currentContent == null
Expand All @@ -33,7 +34,7 @@ module.exports = (filepath, content, options = {}) => {
if (!isEqual(currentContent, mergedContent)) {
fsExtra.ensureDirSync(path.dirname(filepath));
let contentString;
switch (ctx.treatAs || getExt(filepath)) {
switch (ctx.treatAs) {
case 'yml':
case 'yaml':
contentString = yaml.dump(mergedContent);
Expand Down

0 comments on commit 19dfbf5

Please sign in to comment.