[ https://issues.apache.org/jira/browse/CB-12810?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16283990#comment-16283990 ]
ASF GitHub Bot commented on CB-12810: ------------------------------------- stevengill closed pull request #606: CB-12810 : keep existing formatting in package.json and fix eslint errors URL: https://github.com/apache/cordova-lib/pull/606 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/package.json b/package.json index 07879c3d1..71a732dc6 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "cordova-js": "4.2.2", "cordova-serve": "^2.0.0", "dep-graph": "1.1.0", + "detect-indent": "^5.0.0", "elementtree": "0.1.6", "glob": "7.1.1", "init-package-json": "^1.2.0", diff --git a/spec/cordova/platform/addHelper.spec.js b/spec/cordova/platform/addHelper.spec.js index 72f017401..c01f66216 100644 --- a/spec/cordova/platform/addHelper.spec.js +++ b/spec/cordova/platform/addHelper.spec.js @@ -284,6 +284,7 @@ describe('cordova/platform/addHelper', function () { describe('if the project contains a package.json', function () { it('should write out the platform just added/updated to the cordova.platforms property of package.json', function (done) { + spyOn(fs, 'readFileSync').and.returnValue('file'); fs.existsSync.and.callFake(function (filePath) { if (path.basename(filePath) === 'package.json') { return true; diff --git a/spec/cordova/platform/remove.spec.js b/spec/cordova/platform/remove.spec.js index 5483ec0a0..9e2e7b3e4 100644 --- a/spec/cordova/platform/remove.spec.js +++ b/spec/cordova/platform/remove.spec.js @@ -95,6 +95,7 @@ describe('cordova/platform/remove', function () { it('should remove from package.json', function (done) { package_json_mock.cordova = {'platforms': ['atari']}; cordova_util.requireNoCache.and.returnValue(package_json_mock); + spyOn(fs, 'readFileSync').and.returnValue('file'); fs.existsSync.and.callFake(function (filePath) { if (path.basename(filePath) === 'package.json') { return true; diff --git a/spec/cordova/plugin/add.spec.js b/spec/cordova/plugin/add.spec.js index 2059ad2a7..e60fdd41b 100644 --- a/spec/cordova/plugin/add.spec.js +++ b/spec/cordova/plugin/add.spec.js @@ -167,6 +167,7 @@ describe('cordova/plugin/add', function () { } }); + spyOn(fs, 'readFileSync').and.returnValue('file'); 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': {}}, null, 2), 'utf8'); }).fail(function (e) { diff --git a/spec/cordova/plugin/remove.spec.js b/spec/cordova/plugin/remove.spec.js index 6721b309b..ec59b11da 100644 --- a/spec/cordova/plugin/remove.spec.js +++ b/spec/cordova/plugin/remove.spec.js @@ -165,6 +165,7 @@ describe('cordova/plugin/remove', function () { }); it('should remove provided plugins from package.json (if exists)', function (done) { + spyOn(fs, 'readFileSync').and.returnValue('file'); spyOn(cordova_util, 'requireNoCache').and.returnValue(package_json_mock); remove.validatePluginId.and.returnValue('cordova-plugin-splashscreen'); fs.existsSync.and.returnValue(true); diff --git a/src/cordova/platform/addHelper.js b/src/cordova/platform/addHelper.js index 1c0b39fc3..1f4123746 100644 --- a/src/cordova/platform/addHelper.js +++ b/src/cordova/platform/addHelper.js @@ -34,6 +34,7 @@ var config = require('../config'); var lazy_load = require('../lazy_load'); var platformMetadata = require('../platform_metadata'); var platforms = require('../../platforms/platforms'); +var detectIndent = require('detect-indent'); module.exports = addHelper; module.exports.getVersionFromConfigFile = getVersionFromConfigFile; @@ -269,8 +270,9 @@ function addHelper (cmd, hooksRunner, projectRoot, targets, opts) { }); // Save to package.json. if (modifiedPkgJson === true) { - pkgJson.cordova.platforms = pkgJson.cordova.platforms; - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + var file = fs.readFileSync(pkgJsonPath, 'utf8'); + var indent = detectIndent(file).indent || ' '; + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } }); }).then(function () { diff --git a/src/cordova/platform/remove.js b/src/cordova/platform/remove.js index cf7cc41cd..7490f3274 100644 --- a/src/cordova/platform/remove.js +++ b/src/cordova/platform/remove.js @@ -28,6 +28,7 @@ var config = require('../config'); var platformMetadata = require('../platform_metadata'); var promiseutil = require('../../util/promise-util'); var platforms = require('../../platforms/platforms'); +var detectIndent = require('detect-indent'); module.exports = remove; @@ -72,7 +73,9 @@ function remove (hooksRunner, projectRoot, targets, opts) { }); // Write out new package.json if changes have been made. if (modifiedPkgJson === true) { - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + var file = fs.readFileSync(pkgJsonPath, 'utf8'); + var indent = detectIndent(file).indent || ' '; + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } } }).then(function () { diff --git a/src/cordova/plugin/add.js b/src/cordova/plugin/add.js index 3b9b78627..dd6058b76 100644 --- a/src/cordova/plugin/add.js +++ b/src/cordova/plugin/add.js @@ -34,6 +34,7 @@ var path = require('path'); var fs = require('fs'); var semver = require('semver'); var url = require('url'); +var detectIndent = require('detect-indent'); module.exports = add; module.exports.determinePluginTarget = determinePluginTarget; @@ -159,7 +160,9 @@ function add (projectRoot, hooksRunner, opts) { events.emit('log', 'Adding ' + pluginInfo.id + ' to package.json'); // Write to package.json - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + var file = fs.readFileSync(pkgJsonPath, 'utf8'); + var indent = detectIndent(file).indent || ' '; + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } var src = module.exports.parseSource(target, opts); diff --git a/src/cordova/plugin/remove.js b/src/cordova/plugin/remove.js index 6f45dbd5e..aee5e44d8 100644 --- a/src/cordova/plugin/remove.js +++ b/src/cordova/plugin/remove.js @@ -29,6 +29,7 @@ var Q = require('q'); var path = require('path'); var fs = require('fs'); var PluginInfoProvider = require('cordova-common').PluginInfoProvider; +var detectIndent = require('detect-indent'); module.exports = remove; module.exports.validatePluginId = validatePluginId; @@ -107,7 +108,9 @@ function remove (projectRoot, targets, hooksRunner, opts) { // Remove plugin from package.json delete pkgJson.cordova.plugins[target]; // Write out new package.json with plugin removed correctly. - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + var file = fs.readFileSync(pkgJsonPath, 'utf8'); + var indent = detectIndent(file).indent || ' '; + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } } }).then(function () { diff --git a/src/cordova/restore-util.js b/src/cordova/restore-util.js index 012de2f71..2c3f8c998 100644 --- a/src/cordova/restore-util.js +++ b/src/cordova/restore-util.js @@ -27,6 +27,7 @@ var cordovaPlatform = require('./platform'); var semver = require('semver'); var platformsList = require('../platforms/platforms.js'); var promiseutil = require('../util/promise-util'); +var detectIndent = require('detect-indent'); exports.installPluginsFromConfigXML = installPluginsFromConfigXML; exports.installPlatformsFromConfigXML = installPlatformsFromConfigXML; @@ -48,10 +49,14 @@ function installPlatformsFromConfigXML (platforms, opts) { var mergedPlatformSpecs = {}; var key; var installAllPlatforms = !platforms || platforms.length === 0; + var file; + var indent; // Check if path exists and require pkgJsonPath. if (fs.existsSync(pkgJsonPath)) { pkgJson = require(pkgJsonPath); + file = fs.readFileSync(pkgJsonPath, 'utf8'); + indent = detectIndent(file).indent || ' '; } if (pkgJson !== undefined && pkgJson.cordova !== undefined && pkgJson.cordova.platforms !== undefined) { @@ -80,7 +85,7 @@ function installPlatformsFromConfigXML (platforms, opts) { if (cfg.name()) { pkgJson.displayName = cfg.name(); } - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } configPlatforms = engines.map(function (Engine) { @@ -185,7 +190,7 @@ function installPlatformsFromConfigXML (platforms, opts) { } pkgJson.dependencies[prefixKey] = mergedPlatformSpecs[key]; } - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } if (modifiedConfigXML === true) { cfg.write(); @@ -337,7 +342,9 @@ function installPluginsFromConfigXML (args) { for (key in mergedPluginSpecs) { pkgJson.dependencies[key] = mergedPluginSpecs[key]; } - fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2), 'utf8'); + var file = fs.readFileSync(pkgJsonPath, 'utf8'); + var indent = detectIndent(file).indent || ' '; + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8'); } } // Write to config.xml (only if it is different from package.json in content) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > keep existing formatting in package.json > ---------------------------------------- > > Key: CB-12810 > URL: https://issues.apache.org/jira/browse/CB-12810 > Project: Apache Cordova > Issue Type: Improvement > Components: cordova-lib > Affects Versions: 7.0.0 > Reporter: Steve Gill > Assignee: Audrey So > Priority: Minor > Labels: backlog, cordova-8.0.0, cordova-next, tools-next > > We will write our own indentation in package.json. For commands that shell > out to cordova-fetch and therefore npm, we have no control over it (cordova > platform/plugin add/rm). > We could theoretically keep indentaiton for commands where we write to > package.json (also platform/plugin add/rm to cordova key & during restore > when we sync config.xml & package.json). > This issue is probably not worth implementing. > npm@5 seems like it will respect existing formatting. > Previous issue with npm formatting: https://github.com/npm/npm/issues/5694 > npm5 respecting indentation: https://github.com/npm/npm/issues/16244 -- This message was sent by Atlassian JIRA (v6.4.14#64029) --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For additional commands, e-mail: issues-h...@cordova.apache.org