Skip to content

Respect trailing newline at the end of the file #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const isPlainObj = require('is-plain-obj');

const readFile = promisify(fs.readFile);

const hasTrailingNewline = file => /\n$/.test(file);

const init = (fn, filePath, data, options) => {
if (!filePath) {
throw new TypeError('Expected a filepath');
Expand Down Expand Up @@ -37,40 +39,48 @@ const init = (fn, filePath, data, options) => {

const main = async (filePath, data, options) => {
let {indent} = options;
let trailingNewline = '\n';
try {
const file = await readFile(filePath, 'utf8');
if (!hasTrailingNewline(file)) {
trailingNewline = '';
}

if (options.detectIndent) {
try {
const file = await readFile(filePath, 'utf8');
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}${trailingNewline}`, {mode: options.mode});
};

const mainSync = (filePath, data, options) => {
let {indent} = options;
let trailingNewline = '\n';
try {
const file = fs.readFileSync(filePath, 'utf8');
if (!hasTrailingNewline(file)) {
trailingNewline = '';
}

if (options.detectIndent) {
try {
const file = fs.readFileSync(filePath, 'utf8');
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}${trailingNewline}`, {mode: options.mode});
};

module.exports = async (filePath, data, options) => {
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 newline at the end of the file', 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}');
});