Skip to content

Commit

Permalink
Fix EOL (closes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jul 25, 2020
1 parent b0cc774 commit 2cbbfa1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const util = require('util');
const { EOL } = require('os');
const glob = require('fast-glob');
const get = require('lodash.get');
const set = require('lodash.set');
Expand Down Expand Up @@ -41,7 +42,7 @@ const parse = async (data, type) => {
case 'ini':
return ini.parse(data);
default:
return (data || '').toString().trim();
return (data || '').toString();
}
};

Expand All @@ -54,7 +55,7 @@ class Bumper extends Plugin {
const type = getFileType(file, mimeType);
const data = await readFile(file, 'utf8').catch(() => '{}');
const parsed = await parse(data, type);
return typeof parsed === 'string' ? parsed : get(parsed, path);
return typeof parsed === 'string' ? parsed.trim() : get(parsed, path);
}
return null;
}
Expand Down Expand Up @@ -100,8 +101,8 @@ class Bumper extends Plugin {
case 'ini':
return writeFile(file, ini.encode(parsed));
default:
const versionMatch = new RegExp((latestVersion || '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
const write = parsed ? parsed.replace(versionMatch, version) : version;
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = parsed ? parsed.replace(versionMatch, version) : version + EOL;
return writeFile(file, write);
}
})
Expand Down
56 changes: 28 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const { factory, runTasks } = require('release-it/test/util');
const Plugin = require('.');

mock({
'./bower.json': JSON.stringify({ version: '1.0.0' }),
'./foo.txt': '1.0.0\n',
'./foo2.txt': '1.0.0\n',
'./foo.php': '/* comments\nversion: v1.0.0 */ <? echo <p>hello world</p>; ?>\n',
'./manifest.json': '{}',
'./dryrun.json': JSON.stringify({ version: '1.0.0' }),
'./foo.toml': '[tool.test]\nversion = "1.0.0"',
'./foo.ini': `path.version=1.0.0\npath.name=fake`
'./bower.json': JSON.stringify({ version: '1.0.0' }) + EOL,
'./foo.txt': `1.0.0${EOL}`,
'./foo2.txt': `1.0.0${EOL}`,
'./foo.php': `/* comments${EOL}version: v1.0.0 */ <? echo <p>hello world</p>; ?>${EOL}`,
'./manifest.json': `{}${EOL}`,
'./dryrun.json': JSON.stringify({ version: '1.0.0' }) + EOL,
'./foo.toml': `[tool.test]${EOL}version = "1.0.0"${EOL}`,
'./foo.ini': `path.version=1.0.0${EOL}path.name=fake${EOL}`
});

const namespace = 'bumper';

const readFile = file => fs.readFileSync(file).toString().trim();
const readFile = file => fs.readFileSync(file).toString();

test('should not throw', async () => {
const options = { [namespace]: {} };
Expand Down Expand Up @@ -52,21 +52,21 @@ test('should write indented JSON file', async () => {
const options = { [namespace]: { out: './manifest.json' } };
const plugin = factory(Plugin, { namespace, options });
await plugin.bump('1.2.3');
assert.equal(readFile('./manifest.json'), `{${EOL} "version": "1.2.3"${EOL}}`);
assert.equal(readFile('./manifest.json'), `{${EOL} "version": "1.2.3"${EOL}}${EOL}`);
});

test('should write new, indented JSON file', async () => {
const options = { [namespace]: { out: ['./null.json'] } };
const plugin = factory(Plugin, { namespace, options });
await plugin.bump('0.0.0');
assert.equal(readFile('./null.json'), `{${EOL} "version": "0.0.0"${EOL}}`);
assert.equal(readFile('./null.json'), `{${EOL} "version": "0.0.0"${EOL}}${EOL}`);
});

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

test('should write version at multiple paths', async () => {
Expand All @@ -77,50 +77,50 @@ test('should write version at multiple paths', async () => {
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, ' ')
JSON.stringify({ version: '1.2.3', deep: { version: '1.2.3', sub: { version: '1.2.3' } } }, null, ' ') + EOL
);
});

test('should write plain version text file', async () => {
const options = { [namespace]: { out: [{ file: './VERSION', type: 'text/plain' }] } };
const plugin = factory(Plugin, { namespace, options });
await plugin.bump('3.2.1');
assert.equal(readFile('./VERSION'), '3.2.1');
assert.equal(readFile('./VERSION'), `3.2.1${EOL}`);
});

test('should write plain version text file (default text type)', async () => {
const options = { [namespace]: { out: [{ file: './VERSION' }] } };
const plugin = factory(Plugin, { namespace, options });
await plugin.bump('3.2.1');
assert.equal(readFile('./VERSION'), '3.2.1');
assert.equal(readFile('./VERSION'), `3.2.1${EOL}`);
});

test('should write toml file', async () => {
const options = { [namespace]: { out: { file: './foo.toml', type: 'application/toml', path: 'tool.test.version' } } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.toml'), '[tool.test]\nversion = "1.0.1"');
assert.equal(readFile('./foo.toml'), `[tool.test]${EOL}version = "1.0.1"${EOL}`);
});

test('should write toml file (.toml)', async () => {
const options = { [namespace]: { out: { file: './foo.toml', path: 'tool.test.version' } } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.toml'), '[tool.test]\nversion = "1.0.1"');
assert.equal(readFile('./foo.toml'), `[tool.test]${EOL}version = "1.0.1"${EOL}`);
});

test('should write ini file', async () => {
const options = { [namespace]: { out: { file: './foo.ini', path: 'path.version' } } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.ini'), 'path.version=1.0.1\npath.name=fake');
assert.equal(readFile('./foo.ini'), `path.version=1.0.1${EOL}path.name=fake${EOL}`);
});

test('should write plain text file', async () => {
const options = { [namespace]: { in: './bower.json', out: { file: './foo.php', type: 'text/php' } } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.php'), '/* comments\nversion: v1.0.1 */ <? echo <p>hello world</p>; ?>');
assert.equal(readFile('./foo.php'), `/* comments${EOL}version: v1.0.1 */ <? echo <p>hello world</p>; ?>${EOL}`);
});

test('should read/write plain text file', async () => {
Expand All @@ -129,7 +129,7 @@ test('should read/write plain text file', async () => {
};
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), '1.0.1');
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
});

test('should read/write plain text file (.txt)', async () => {
Expand All @@ -138,7 +138,7 @@ test('should read/write plain text file (.txt)', async () => {
};
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), '1.0.1');
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
});

test('should read one and write multiple files', async () => {
Expand All @@ -147,8 +147,8 @@ test('should read one and write multiple files', async () => {
};
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), '1.0.1');
assert.equal(readFile('./foo2.txt'), '1.0.1');
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./foo2.txt'), `1.0.1${EOL}`);
});

test('should write various file types', async () => {
Expand All @@ -157,15 +157,15 @@ test('should write various file types', async () => {
};
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), '1.0.1');
assert.equal(readFile('./foo2.txt'), '1.0.1');
assert.equal(readFile('./bower.json'), '{\n "version": "1.0.1"\n}');
assert.equal(readFile('./manifest.json'), '{\n "version": "1.0.1"\n}');
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./foo2.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./bower.json'), `{${EOL} "version": "1.0.1"${EOL}}${EOL}`);
assert.equal(readFile('./manifest.json'), `{${EOL} "version": "1.0.1"${EOL}}${EOL}`);
});

test('should not write in dry run', async () => {
const options = { [namespace]: { in: './dryrun.json' } };
const plugin = factory(Plugin, { namespace, options, global: { isDryRun: true } });
await plugin.bump('1.0.1');
assert.equal(readFile('./dryrun.json'), `{"version":"1.0.0"}`);
assert.equal(readFile('./dryrun.json'), `{"version":"1.0.0"}${EOL}`);
});

0 comments on commit 2cbbfa1

Please sign in to comment.