Skip to content

Commit

Permalink
feat: preserve new line at the end of package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Feb 22, 2022
1 parent d6852da commit 8ebaa39
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/removeNPMAbsolutePaths.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const path = require('path');
const { stat, readdir, readFile, writeFile } = require('fs').promises;
const {
stat, readdir, readFile, writeFile,
} = require('fs').promises;

const errno = require('./errno');

Expand Down Expand Up @@ -47,7 +49,7 @@ async function processFile(filePath, opts) {

if (shouldWriteFile || opts.force) {
try {
await writeFile(filePath, JSON.stringify(obj, null, ' '));
await writeFile(filePath, `${JSON.stringify(obj, null, ' ')}${data.endsWith('\n') ? '\n' : ''}`);
} catch (err) {
throw new ProcessingError(`Can't write processed file to "${filePath}"`, err);
}
Expand Down
57 changes: 57 additions & 0 deletions test/data/underscore_field_no_end_newline/module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"_from": "removeNPMAbsolutePaths",
"_id": "removeNPMAbsolutePaths@0.0.3",
"_inBundle": false,
"_integrity": "sha1-NkErwn66yL4JsbULf3MIl5K4xfE=",
"_location": "/removeNPMAbsolutePaths",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "removeNPMAbsolutePaths",
"name": "removeNPMAbsolutePaths",
"escapedName": "removeNPMAbsolutePaths",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/removeNPMAbsolutePaths/-/removeNPMAbsolutePaths-0.0.3.tgz",
"_shasum": "36412bc27ebac8be09b1b50b7f73089792b8c5f1",
"_spec": "removeNPMAbsolutePaths",
"_where": "/Users/juanjo/test",
"author": {
"name": "Juanjo Diaz",
"email": "juanjo.diazmo@gmail.com"
},
"bin": {
"removeNPMAbsolutePaths": "bin/removeNPMAbsolutePaths"
},
"bugs": {
"url": "https://github.com/removeNPMAbsolutePaths/issues",
"email": "juanjo.diazmo@gmail.com"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Remove the fields containing local aboslute paths created by NPM",
"homepage": "https://github.com/juanjoDiaz/removeNPMAbsolutePaths#readme",
"keywords": [
"npm",
"modules"
],
"license": "MIT",
"main": "src/removeNPMAbsolutePaths.js",
"name": "removeNPMAbsolutePaths",
"preferGlobal": "true",
"repository": {
"type": "git",
"url": "git://github.com/juanjoDiaz/removeNPMAbsolutePaths.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.0.3"
}
66 changes: 66 additions & 0 deletions test/removeNPMAbsolutePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,40 @@ describe('removeNPMAbsolutePaths.js', async function () {
expect(Object.keys(packageJson).find((key) => key[0] === '_')).to.not.exist;
});

it('preserve newline at the end of packa.json if rewriting', async function () {
const dirPath = path.join(__dirname, 'data', 'underscore_fields');
const filePath = path.join(dirPath, 'module', 'package.json');
const promise = removeNPMAbsolutePaths(dirPath);
const results = await expect(promise).be.fulfilled;
expect(results).to.be.an('array').that.have.lengthOf(3);
const fileResults = results.find((result) => result.filePath === filePath);
expect(fileResults).to.include({ success: true, rewritten: true });
expect(fileResults.err).to.not.exist;
expect(stat).to.have.been.called;
expect(readdir).to.have.been.called;
expect(readFile).to.have.been.calledOnce.and.calledWith(filePath);
expect(writeFile).to.have.been.calledOnce.and.calledWith(filePath);
expect(writeFile.getCall(0).args[1]).to.satisfy((newPackageJson) => newPackageJson.endsWith('\n'));

readdir.resetHistory();
readFile.resetHistory();
writeFile.resetHistory();

const dirPath2 = path.join(__dirname, 'data', 'underscore_field_no_end_newline');
const filePath2 = path.join(dirPath2, 'module', 'package.json');
const promise2 = removeNPMAbsolutePaths(dirPath2);
const results2 = await expect(promise2).be.fulfilled;
expect(results2).to.be.an('array').that.have.lengthOf(3);
const fileResults2 = results2.find((result) => result.filePath === filePath2);
expect(fileResults2).to.include({ success: true, rewritten: true });
expect(fileResults2.err).to.not.exist;
expect(stat).to.have.been.called;
expect(readdir).to.have.been.called;
expect(readFile).to.have.been.calledOnce.and.calledWith(filePath2);
expect(writeFile).to.have.been.calledOnce.and.calledWith(filePath2);
expect(writeFile.getCall(0).args[1]).to.satisfy((newPackageJson) => !newPackageJson.endsWith('\n'));
});

describe('force', async function () {
it('doesn\'t rewrite pacakge.json if doesn\'t contain _fields and force option isn\'t passed', async function () {
const dirPath = path.join(__dirname, 'data', 'no_underscore_fields');
Expand Down Expand Up @@ -260,6 +294,38 @@ describe('removeNPMAbsolutePaths.js', async function () {
expect(Object.keys(packageJson).find((key) => key[0] === '_')).to.not.exist;
});

it('preserve newline at the end of packa.json if rewriting', async function () {
const filePath = path.join(__dirname, 'data', 'underscore_fields', 'module', 'package.json');
const promise = removeNPMAbsolutePaths(filePath);
const results = await expect(promise).be.fulfilled;
expect(results).to.be.an('array').that.have.lengthOf(1);
const fileResults = results.find((result) => result.filePath === filePath);
expect(fileResults).to.include({ success: true, rewritten: true });
expect(fileResults.err).to.not.exist;
expect(stat).to.have.been.called;
expect(readdir).to.not.have.been.called;
expect(readFile).to.have.been.calledOnce.and.calledWith(filePath);
expect(writeFile).to.have.been.calledOnce.and.calledWith(filePath);
expect(writeFile.getCall(0).args[1]).to.satisfy((newPackageJson) => newPackageJson.endsWith('\n'));

readdir.resetHistory();
readFile.resetHistory();
writeFile.resetHistory();

const filePath2 = path.join(__dirname, 'data', 'underscore_field_no_end_newline', 'module', 'package.json');
const promise2 = removeNPMAbsolutePaths(filePath2);
const results2 = await expect(promise2).be.fulfilled;
expect(results2).to.be.an('array').that.have.lengthOf(1);
const fileResults2 = results2.find((result) => result.filePath === filePath2);
expect(fileResults2).to.include({ success: true, rewritten: true });
expect(fileResults2.err).to.not.exist;
expect(stat).to.have.been.called;
expect(readdir).to.not.have.been.called;
expect(readFile).to.have.been.calledOnce.and.calledWith(filePath2);
expect(writeFile).to.have.been.calledOnce.and.calledWith(filePath2);
expect(writeFile.getCall(0).args[1]).to.satisfy((newPackageJson) => !newPackageJson.endsWith('\n'));
});

describe('force', async function () {
it('doesn\'t rewrite file if doesn\'t contain _fields and force option isn\'t passed', async function () {
const filePath = path.join(__dirname, 'data', 'no_underscore_fields', 'module', 'package.json');
Expand Down

0 comments on commit 8ebaa39

Please sign in to comment.