From 2cbbfa1e9198d3a76a7f9604dcba68f8164e922c Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sat, 25 Jul 2020 09:05:34 +0200 Subject: [PATCH] Fix EOL (closes #12) --- index.js | 9 +++++---- test.js | 56 ++++++++++++++++++++++++++++---------------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index ddaa003..1363c5d 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -41,7 +42,7 @@ const parse = async (data, type) => { case 'ini': return ini.parse(data); default: - return (data || '').toString().trim(); + return (data || '').toString(); } }; @@ -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; } @@ -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); } }) diff --git a/test.js b/test.js index c64a428..6630fc4 100644 --- a/test.js +++ b/test.js @@ -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 */ hello world

; ?>\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 */ hello world

; ?>${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]: {} }; @@ -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 () => { @@ -77,7 +77,7 @@ 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 ); }); @@ -85,42 +85,42 @@ 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 */ hello world

; ?>'); + assert.equal(readFile('./foo.php'), `/* comments${EOL}version: v1.0.1 */ hello world

; ?>${EOL}`); }); test('should read/write plain text file', async () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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}`); });