Skip to content

Commit 8c63c3a

Browse files
authored
Keep final new line in package.json (#941)
* fix package json formatting * revert back to fs.writeFileSync * fix tests
1 parent 75c4047 commit 8c63c3a

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

spec/cordova/plugin/add.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe('cordova/plugin/add', function () {
145145

146146
spyOn(fs, 'readFileSync').and.returnValue('file');
147147
return add(projectRoot, hook_mock, { plugins: ['cordova-plugin-device'], cli_variables: cli_plugin_variables, save: 'true' }).then(function () {
148-
expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), JSON.stringify({ cordova: { plugins: { 'cordova-plugin-device': cli_plugin_variables } }, dependencies: {}, devDependencies: {} }, null, 2), 'utf8');
148+
expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), JSON.stringify({ cordova: { plugins: { 'cordova-plugin-device': cli_plugin_variables } }, dependencies: {}, devDependencies: {} }, null, 2) + '\n', 'utf8');
149149
});
150150
});
151151
it('should overwrite plugin information in config.xml after a successful installation', function () {

src/cordova/platform/addHelper.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const cordova_util = require('../util');
2727
const promiseutil = require('../../util/promise-util');
2828
const platforms = require('../../platforms');
2929
const detectIndent = require('detect-indent');
30+
const detectNewline = require('detect-newline');
31+
const stringifyPackage = require('stringify-package');
3032
const getPlatformDetailsFromDir = require('./getPlatformDetailsFromDir');
3133
const preparePlatforms = require('../prepare/platforms');
3234

@@ -226,7 +228,8 @@ function addHelper (cmd, hooksRunner, projectRoot, targets, opts) {
226228
if (modifiedPkgJson === true) {
227229
const file = fs.readFileSync(pkgJsonPath, 'utf8');
228230
const indent = detectIndent(file).indent || ' ';
229-
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
231+
const newline = detectNewline(file);
232+
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
230233
}
231234
});
232235
}).then(function () {

src/cordova/platform/remove.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const cordova_util = require('../util');
2525
const promiseutil = require('../../util/promise-util');
2626
const platforms = require('../../platforms/platforms');
2727
const detectIndent = require('detect-indent');
28+
const detectNewline = require('detect-newline');
29+
const stringifyPackage = require('stringify-package');
2830

2931
module.exports = remove;
3032

@@ -71,7 +73,8 @@ function remove (hooksRunner, projectRoot, targets, opts) {
7173
if (modifiedPkgJson === true) {
7274
const file = fs.readFileSync(pkgJsonPath, 'utf8');
7375
const indent = detectIndent(file).indent || ' ';
74-
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
76+
const newline = detectNewline(file);
77+
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
7578
}
7679
}
7780
}).then(function () {

src/cordova/plugin/add.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const path = require('node:path');
2222
const semver = require('semver');
2323
const url = require('url');
2424
const detectIndent = require('detect-indent');
25+
const detectNewline = require('detect-newline');
26+
const stringifyPackage = require('stringify-package');
2527
const cordova_util = require('../util');
2628
const plugin_util = require('./util');
2729
const cordova_pkgJson = require('../../../package.json');
@@ -154,7 +156,8 @@ function add (projectRoot, hooksRunner, opts) {
154156
// Write to package.json
155157
const file = fs.readFileSync(pkgJsonPath, 'utf8');
156158
const indent = detectIndent(file).indent || ' ';
157-
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
159+
const newline = detectNewline(file);
160+
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
158161
}
159162

160163
const src = module.exports.parseSource(target, opts);

src/cordova/plugin/remove.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const plugman = require('../../plugman/plugman');
2828
const metadata = require('../../plugman/util/metadata');
2929
const PluginInfoProvider = require('cordova-common').PluginInfoProvider;
3030
const detectIndent = require('detect-indent');
31+
const detectNewline = require('detect-newline');
32+
const stringifyPackage = require('stringify-package');
3133
const { Q_chainmap } = require('../../util/promise-util');
3234
const preparePlatforms = require('../prepare/platforms');
3335

@@ -145,7 +147,8 @@ function remove (projectRoot, targets, hooksRunner, opts) {
145147
// Write out new package.json with plugin removed correctly.
146148
const file = fs.readFileSync(pkgJsonPath, 'utf8');
147149
const indent = detectIndent(file).indent || ' ';
148-
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
150+
const newline = detectNewline(file);
151+
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
149152
}
150153
}
151154
}

0 commit comments

Comments
 (0)