This is an automated email from the ASF dual-hosted git repository.

dpogue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-lib.git


The following commit(s) were added to refs/heads/master by this push:
     new 8c63c3ac Keep final new line in package.json (#941)
8c63c3ac is described below

commit 8c63c3ac146c6ad99820d66aaf51c98fd3ab94ed
Author: Aidan Temple <15520814+aid...@users.noreply.github.com>
AuthorDate: Tue Dec 17 16:01:01 2024 +1100

    Keep final new line in package.json (#941)
    
    * fix package json formatting
    
    * revert back to fs.writeFileSync
    
    * fix tests
---
 spec/cordova/plugin/add.spec.js   | 2 +-
 src/cordova/platform/addHelper.js | 5 ++++-
 src/cordova/platform/remove.js    | 5 ++++-
 src/cordova/plugin/add.js         | 5 ++++-
 src/cordova/plugin/remove.js      | 5 ++++-
 5 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/spec/cordova/plugin/add.spec.js b/spec/cordova/plugin/add.spec.js
index 02092795..ec96cf60 100644
--- a/spec/cordova/plugin/add.spec.js
+++ b/spec/cordova/plugin/add.spec.js
@@ -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 () {
diff --git a/src/cordova/platform/addHelper.js 
b/src/cordova/platform/addHelper.js
index 98793a70..db5694e7 100644
--- a/src/cordova/platform/addHelper.js
+++ b/src/cordova/platform/addHelper.js
@@ -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');
 
@@ -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 () {
diff --git a/src/cordova/platform/remove.js b/src/cordova/platform/remove.js
index 9d2a84c9..e2b03e75 100644
--- a/src/cordova/platform/remove.js
+++ b/src/cordova/platform/remove.js
@@ -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;
 
@@ -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 () {
diff --git a/src/cordova/plugin/add.js b/src/cordova/plugin/add.js
index 6b656470..91af02a5 100644
--- a/src/cordova/plugin/add.js
+++ b/src/cordova/plugin/add.js
@@ -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');
@@ -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);
diff --git a/src/cordova/plugin/remove.js b/src/cordova/plugin/remove.js
index 1524f386..9d93a612 100644
--- a/src/cordova/plugin/remove.js
+++ b/src/cordova/plugin/remove.js
@@ -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');
 
@@ -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');
         }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to