From 5b44502795a8794f41c34ac2647840059547491e Mon Sep 17 00:00:00 2001 From: Fauzi Kliman Date: Tue, 4 Apr 2023 18:46:24 -0700 Subject: [PATCH 1/2] chore(dep): add stringify-package to project source --- lib/stringify-package.js | 18 ++++++++++++++++ lib/updaters/types/json.js | 2 +- package-lock.json | 11 ---------- package.json | 1 - test/stringify-package.spec.js | 39 ++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 lib/stringify-package.js create mode 100644 test/stringify-package.spec.js diff --git a/lib/stringify-package.js b/lib/stringify-package.js new file mode 100644 index 000000000..cd291f295 --- /dev/null +++ b/lib/stringify-package.js @@ -0,0 +1,18 @@ +'use strict' + +module.exports = stringifyPackage + +const DEFAULT_INDENT = 2 +const CRLF = '\r\n' +const LF = '\n' + +function stringifyPackage (data, indent, newline) { + indent = indent || (indent === 0 ? 0 : DEFAULT_INDENT) + const json = JSON.stringify(data, null, indent) + + if (newline === CRLF) { + return json.replace(/\n/g, CRLF) + CRLF + } + + return json + LF +} diff --git a/lib/updaters/types/json.js b/lib/updaters/types/json.js index fdc2a8907..e0cba511c 100644 --- a/lib/updaters/types/json.js +++ b/lib/updaters/types/json.js @@ -1,4 +1,4 @@ -const stringifyPackage = require('stringify-package') +const stringifyPackage = require('../../stringify-package') const detectIndent = require('detect-indent') const detectNewline = require('detect-newline') diff --git a/package-lock.json b/package-lock.json index 5b859a8ed..7fd5a1002 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,6 @@ "find-up": "^5.0.0", "git-semver-tags": "^4.0.0", "semver": "^7.1.1", - "stringify-package": "^1.0.1", "yargs": "^17.0.0" }, "bin": { @@ -5258,11 +5257,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/stringify-package": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/stringify-package/-/stringify-package-1.0.1.tgz", - "integrity": "sha512-sa4DUQsYciMP1xhKWGuFM04fB0LG/9DlluZoSVywUMRNvzid6XucHK0/90xGxRoHrAaROrcHK1aPKaijCtSrhg==" - }, "node_modules/strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", @@ -9660,11 +9654,6 @@ "es-abstract": "^1.19.5" } }, - "stringify-package": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/stringify-package/-/stringify-package-1.0.1.tgz", - "integrity": "sha512-sa4DUQsYciMP1xhKWGuFM04fB0LG/9DlluZoSVywUMRNvzid6XucHK0/90xGxRoHrAaROrcHK1aPKaijCtSrhg==" - }, "strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", diff --git a/package.json b/package.json index f9d9207bd..bb87f4ca5 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "find-up": "^5.0.0", "git-semver-tags": "^4.0.0", "semver": "^7.1.1", - "stringify-package": "^1.0.1", "yargs": "^17.0.0" }, "devDependencies": { diff --git a/test/stringify-package.spec.js b/test/stringify-package.spec.js new file mode 100644 index 000000000..08a2dd908 --- /dev/null +++ b/test/stringify-package.spec.js @@ -0,0 +1,39 @@ +/* global describe it */ + +'use strict' + +const stringifyPackage = require('../lib/stringify-package') + +require('chai').should() + +describe('stringifyPackage()', function () { + const dummy = { name: 'dummy' } + + it('with no params uses \\n', function () { + stringifyPackage(dummy).should.match(/\n$/m) + }) + + it('uses \\n', function () { + stringifyPackage(dummy, 2, '\n').should.match(/\n$/m) + }) + + it('uses \\r\\n', function () { + stringifyPackage(dummy, 2, '\r\n').should.match(/\r\n$/m) + }) + + it('with no params uses 2-space indent', function () { + stringifyPackage(dummy).should.match(/^ {2}"name": "dummy"/m) + }) + + it('uses 2-space indent', function () { + stringifyPackage(dummy, 2, '\n').should.match(/^ {2}"name": "dummy"/m) + }) + + it('uses 4-space indent', function () { + stringifyPackage(dummy, 4, '\n').should.match(/^ {4}"name": "dummy"/m) + }) + + it('0 works', function () { + stringifyPackage(dummy, 0).split(/\r\n|\r|\n/).length.should.equal(2) + }) +}) From 5fddabf9739bf41a7379e291776ed8bc9bf8cb4b Mon Sep 17 00:00:00 2001 From: Fauzi Kliman Date: Tue, 4 Apr 2023 19:19:55 -0700 Subject: [PATCH 2/2] chore: add reference to ISC license from stringify-package --- lib/stringify-package.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/stringify-package.js b/lib/stringify-package.js index cd291f295..46594cf78 100644 --- a/lib/stringify-package.js +++ b/lib/stringify-package.js @@ -1,3 +1,21 @@ +/* +Copyright npm, Inc + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +https://github.com/npm/stringify-package/blob/main/LICENSE +*/ + 'use strict' module.exports = stringifyPackage