Skip to content

Commit

Permalink
Keep final new line in package.json (#941)
Browse files Browse the repository at this point in the history
* fix package json formatting

* revert back to fs.writeFileSync

* fix tests
  • Loading branch information
aidant authored Dec 17, 2024
1 parent 75c4047 commit 8c63c3a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spec/cordova/plugin/add.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('cordova/plugin/add', function () {

spyOn(fs, 'readFileSync').and.returnValue('file');
return add(projectRoot, hook_mock, { plugins: ['cordova-plugin-device'], cli_variables: cli_plugin_variables, save: 'true' }).then(function () {
expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), JSON.stringify({ cordova: { plugins: { 'cordova-plugin-device': cli_plugin_variables } }, dependencies: {}, devDependencies: {} }, null, 2), 'utf8');
expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), JSON.stringify({ cordova: { plugins: { 'cordova-plugin-device': cli_plugin_variables } }, dependencies: {}, devDependencies: {} }, null, 2) + '\n', 'utf8');
});
});
it('should overwrite plugin information in config.xml after a successful installation', function () {
Expand Down
5 changes: 4 additions & 1 deletion src/cordova/platform/addHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const cordova_util = require('../util');
const promiseutil = require('../../util/promise-util');
const platforms = require('../../platforms');
const detectIndent = require('detect-indent');
const detectNewline = require('detect-newline');
const stringifyPackage = require('stringify-package');
const getPlatformDetailsFromDir = require('./getPlatformDetailsFromDir');
const preparePlatforms = require('../prepare/platforms');

Expand Down Expand Up @@ -226,7 +228,8 @@ function addHelper (cmd, hooksRunner, projectRoot, targets, opts) {
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
const newline = detectNewline(file);
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
}
});
}).then(function () {
Expand Down
5 changes: 4 additions & 1 deletion src/cordova/platform/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const cordova_util = require('../util');
const promiseutil = require('../../util/promise-util');
const platforms = require('../../platforms/platforms');
const detectIndent = require('detect-indent');
const detectNewline = require('detect-newline');
const stringifyPackage = require('stringify-package');

module.exports = remove;

Expand Down Expand Up @@ -71,7 +73,8 @@ function remove (hooksRunner, projectRoot, targets, opts) {
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
const newline = detectNewline(file);
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
}
}
}).then(function () {
Expand Down
5 changes: 4 additions & 1 deletion src/cordova/plugin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const path = require('node:path');
const semver = require('semver');
const url = require('url');
const detectIndent = require('detect-indent');
const detectNewline = require('detect-newline');
const stringifyPackage = require('stringify-package');
const cordova_util = require('../util');
const plugin_util = require('./util');
const cordova_pkgJson = require('../../../package.json');
Expand Down Expand Up @@ -154,7 +156,8 @@ function add (projectRoot, hooksRunner, opts) {
// Write to package.json
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
const newline = detectNewline(file);
fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
}

const src = module.exports.parseSource(target, opts);
Expand Down
5 changes: 4 additions & 1 deletion src/cordova/plugin/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const plugman = require('../../plugman/plugman');
const metadata = require('../../plugman/util/metadata');
const PluginInfoProvider = require('cordova-common').PluginInfoProvider;
const detectIndent = require('detect-indent');
const detectNewline = require('detect-newline');
const stringifyPackage = require('stringify-package');
const { Q_chainmap } = require('../../util/promise-util');
const preparePlatforms = require('../prepare/platforms');

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

0 comments on commit 8c63c3a

Please sign in to comment.