From afaacfbd64b4fd68c2895a5153809d4edf4600c0 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Tue, 23 Jul 2019 23:33:38 -0700 Subject: [PATCH 1/6] Respect EOF --- index.js | 45 +++++++++++++++++++++++++++------------------ test.js | 14 ++++++++++++++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 43991c3..4c08b31 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,11 @@ const isPlainObj = require('is-plain-obj'); const readFile = promisify(fs.readFile); +const detectEOF = (file) => { + const detected = file.match(/\r?\n$/); + return detected ? detected[0] : ''; +}; + const init = (fn, filePath, data, options) => { if (!filePath) { throw new TypeError('Expected a filepath'); @@ -37,40 +42,44 @@ const init = (fn, filePath, data, options) => { const main = async (filePath, data, options) => { let {indent} = options; - - if (options.detectIndent) { - try { - const file = await readFile(filePath, 'utf8'); + let EOF = '\n'; + let file; + try { + const file = await readFile(filePath, 'utf8'); + EOF = detectEOF(file); + if (options.detectIndent) { indent = detectIndent(file).indent; - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } + } + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; } } const json = JSON.stringify(data, options.replacer, indent); - return writeFileAtomic(filePath, `${json}\n`, {mode: options.mode}); + return writeFileAtomic(filePath, `${json}${EOF}`, {mode: options.mode}); }; const mainSync = (filePath, data, options) => { let {indent} = options; - - if (options.detectIndent) { - try { - const file = fs.readFileSync(filePath, 'utf8'); + let EOF = '\n'; + let file; + try { + file = fs.readFileSync(filePath, 'utf8'); + EOF = detectEOF(file); + if (options.detectIndent) { indent = detectIndent(file).indent; - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } + } + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; } } const json = JSON.stringify(data, options.replacer, indent); - return writeFileAtomic.sync(filePath, `${json}\n`, {mode: options.mode}); + return writeFileAtomic.sync(filePath, `${json}${EOF}`, {mode: options.mode}); }; module.exports = async (filePath, data, options) => { diff --git a/test.js b/test.js index 26a7a3e..48f41f4 100644 --- a/test.js +++ b/test.js @@ -61,3 +61,17 @@ test('sync - `replacer` option', t => { writeJsonFile.sync(tempFile, {foo: true, bar: true}, {replacer: ['foo']}); t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); }); + +test('async - respect EOF', async t => { + const tempFile = tempy.file(); + fs.writeFileSync(tempFile, JSON.stringify({foo: true})); + await writeJsonFile(tempFile, {bar: true}); + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"bar": true\n}'); +}); + +test('sync - respect EOF', t => { + const tempFile = tempy.file(); + fs.writeFileSync(tempFile, JSON.stringify({foo: true})); + writeJsonFile.sync(tempFile, {bar: true}); + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"bar": true\n}'); +}); From b138214bb760173189054003e85dc7e4f513e3ac Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Tue, 23 Jul 2019 23:35:33 -0700 Subject: [PATCH 2/6] Linting --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 4c08b31..c0dde2e 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const isPlainObj = require('is-plain-obj'); const readFile = promisify(fs.readFile); -const detectEOF = (file) => { +const detectEOF = file => { const detected = file.match(/\r?\n$/); return detected ? detected[0] : ''; }; @@ -43,7 +43,6 @@ const init = (fn, filePath, data, options) => { const main = async (filePath, data, options) => { let {indent} = options; let EOF = '\n'; - let file; try { const file = await readFile(filePath, 'utf8'); EOF = detectEOF(file); @@ -64,9 +63,8 @@ const main = async (filePath, data, options) => { const mainSync = (filePath, data, options) => { let {indent} = options; let EOF = '\n'; - let file; try { - file = fs.readFileSync(filePath, 'utf8'); + const file = fs.readFileSync(filePath, 'utf8'); EOF = detectEOF(file); if (options.detectIndent) { indent = detectIndent(file).indent; From 7ab9c8178a664f4c6d64593436c47c96ef743df0 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 26 Jul 2019 16:33:36 -0700 Subject: [PATCH 3/6] Renamed detectEOF to hasTrailingNewline and only detect \n --- index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c0dde2e..1027026 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,7 @@ const isPlainObj = require('is-plain-obj'); const readFile = promisify(fs.readFile); -const detectEOF = file => { - const detected = file.match(/\r?\n$/); - return detected ? detected[0] : ''; -}; +const hasTrailingNewline = file => /\n$/.test(file); const init = (fn, filePath, data, options) => { if (!filePath) { @@ -45,7 +42,9 @@ const main = async (filePath, data, options) => { let EOF = '\n'; try { const file = await readFile(filePath, 'utf8'); - EOF = detectEOF(file); + if (!hasTrailingNewline(file)) { + EOF = ''; + } if (options.detectIndent) { indent = detectIndent(file).indent; } @@ -65,7 +64,9 @@ const mainSync = (filePath, data, options) => { let EOF = '\n'; try { const file = fs.readFileSync(filePath, 'utf8'); - EOF = detectEOF(file); + if (!hasTrailingNewline(file)) { + EOF = ''; + } if (options.detectIndent) { indent = detectIndent(file).indent; } From 03a1b0d084d802cda208b77f2fe255e94f1077f0 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 26 Jul 2019 17:07:27 -0700 Subject: [PATCH 4/6] Renamed EOF to trailingNewline --- index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 1027026..37ae776 100644 --- a/index.js +++ b/index.js @@ -39,12 +39,13 @@ const init = (fn, filePath, data, options) => { const main = async (filePath, data, options) => { let {indent} = options; - let EOF = '\n'; + let trailingNewline = '\n'; try { const file = await readFile(filePath, 'utf8'); if (!hasTrailingNewline(file)) { - EOF = ''; + trailingNewline = ''; } + if (options.detectIndent) { indent = detectIndent(file).indent; } @@ -56,17 +57,18 @@ const main = async (filePath, data, options) => { const json = JSON.stringify(data, options.replacer, indent); - return writeFileAtomic(filePath, `${json}${EOF}`, {mode: options.mode}); + return writeFileAtomic(filePath, `${json}${trailingNewline}`, {mode: options.mode}); }; const mainSync = (filePath, data, options) => { let {indent} = options; - let EOF = '\n'; + let trailingNewline = '\n'; try { const file = fs.readFileSync(filePath, 'utf8'); if (!hasTrailingNewline(file)) { - EOF = ''; + trailingNewline = ''; } + if (options.detectIndent) { indent = detectIndent(file).indent; } @@ -78,7 +80,7 @@ const mainSync = (filePath, data, options) => { const json = JSON.stringify(data, options.replacer, indent); - return writeFileAtomic.sync(filePath, `${json}${EOF}`, {mode: options.mode}); + return writeFileAtomic.sync(filePath, `${json}${trailingNewline}`, {mode: options.mode}); }; module.exports = async (filePath, data, options) => { From af941619c68f587a8941e88ebee4ce228433d836 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 26 Jul 2019 17:09:44 -0700 Subject: [PATCH 5/6] Corrected test names --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 48f41f4..8c64d37 100644 --- a/test.js +++ b/test.js @@ -62,14 +62,14 @@ test('sync - `replacer` option', t => { t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); }); -test('async - respect EOF', async t => { +test('async - respect trailing new line at end of file', async t => { const tempFile = tempy.file(); fs.writeFileSync(tempFile, JSON.stringify({foo: true})); await writeJsonFile(tempFile, {bar: true}); t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"bar": true\n}'); }); -test('sync - respect EOF', t => { +test('sync - respect trailing new line at end of file', t => { const tempFile = tempy.file(); fs.writeFileSync(tempFile, JSON.stringify({foo: true})); writeJsonFile.sync(tempFile, {bar: true}); From 1648c475bc4afaade328c8043ac8256c30ae2f83 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 30 Jul 2019 20:18:32 +0200 Subject: [PATCH 6/6] Update test.js --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 8c64d37..eb87b62 100644 --- a/test.js +++ b/test.js @@ -62,14 +62,14 @@ test('sync - `replacer` option', t => { t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); }); -test('async - respect trailing new line at end of file', async t => { +test('async - respect trailing newline at the end of the file', async t => { const tempFile = tempy.file(); fs.writeFileSync(tempFile, JSON.stringify({foo: true})); await writeJsonFile(tempFile, {bar: true}); t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"bar": true\n}'); }); -test('sync - respect trailing new line at end of file', t => { +test('sync - respect trailing newline at the end of the file', t => { const tempFile = tempy.file(); fs.writeFileSync(tempFile, JSON.stringify({foo: true})); writeJsonFile.sync(tempFile, {bar: true});