Skip to content

Commit

Permalink
Support writing to multiple paths within out files
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jun 16, 2020
1 parent 4965b5d commit 8d92b99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ The `path` option (default: `"version"`) can be used to change a different prope
}
}
```

Multiple paths can be provided using an array.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ class Bumper extends Plugin {
const parsed = await parse(data, type);
const indent = typeof data === 'string' ? detectIndent(data).indent || ' ' : null;

if (typeof parsed !== 'string') {
castArray(path).forEach(path => set(parsed, path, version));
}

switch (type) {
case 'json':
set(parsed, path, version);
return writeFile(file, JSON.stringify(parsed, null, indent) + '\n');
case 'yaml':
set(parsed, path, version);
return writeFile(file, yaml.safeDump(parsed, { indent: indent.length }) + '\n');
case 'toml':
set(parsed, path, version);
return writeFile(file, toml.stringify(parsed));
case 'ini':
set(parsed, path, version);
return writeFile(file, ini.encode(parsed));
default:
const versionMatch = new RegExp((latestVersion || '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ test('should write version at path', async () => {
assert.equal(readFile('./deep.json'), JSON.stringify({ deep: { sub: { version: '1.2.3' } } }, null, ' '));
});

test('should write version at multiple paths', async () => {
const options = {
[namespace]: { out: { file: './multi.json', path: ['version', 'deep.version', 'deep.sub.version'] } }
};
const plugin = factory(Plugin, { namespace, options });
await plugin.bump('1.2.3');
assert.equal(
readFile('./multi.json'),
JSON.stringify({ version: '1.2.3', deep: { version: '1.2.3', sub: { version: '1.2.3' } } }, null, ' ')
);
});

test('should write plain version text file', async () => {
const options = { [namespace]: { out: [{ file: './VERSION', type: 'text/plain' }] } };
const plugin = factory(Plugin, { namespace, options });
Expand Down

0 comments on commit 8d92b99

Please sign in to comment.