Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save on reversion #140

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ class PackageJson {
.replace(/\n/g, eol)

if (fileContent.trim() !== this.#readFileContent.trim()) {
return await writeFile(this.filename, fileContent)
const written = await writeFile(this.filename, fileContent)
this.#readFileContent = fileContent
return written
}
}

Expand Down
125 changes: 3 additions & 122 deletions tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,137 +27,18 @@ exports[`test/index.js TAP load create:true existing parseable package.json > pa
`

exports[`test/index.js TAP load custom formatting > should save back custom format to package.json 1`] = `
{"name":"foo","version":"1.0.1","description":"Lorem ipsum dolor"}
{"name":"@npmcli/test","version":"1.0.1","description":"Lorem ipsum dolor"}
`

exports[`test/index.js TAP load read, update content and write > should properly save content to a package.json 1`] = `
{
"name": "foo",
"name": "@npmcli/test",
"version": "1.0.1",
"description": "Lorem ipsum dolor"
}

`

exports[`test/index.js TAP load sorts on save > should properly save content to a package.json 1`] = `
{
"name": "foo",
"version": "1.0.0",
"description": "A sample package",
"keywords": [
"sample",
"package"
],
"homepage": "https://example.com",
"bugs": {
"url": "https://example.com/bugs",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://example.com/repo.git"
},
"funding": "https://example.com/funding",
"license": "MIT",
"author": "Author Name <[email protected]>",
"maintainers": [
"Maintainer One <[email protected]>",
"Maintainer Two <[email protected]>"
],
"contributors": [
"Contributor One <[email protected]>",
"Contributor Two <[email protected]>"
],
"type": "module",
"imports": {
"#dep": "./src/dep.js"
},
"exports": {
".": "./src/index.js"
},
"main": "index.js",
"browser": "browser.js",
"types": "index.d.ts",
"bin": {
"my-cli": "./bin/cli.js"
},
"man": [
"./man/doc.1"
],
"directories": {
"lib": "lib",
"bin": "bin",
"man": "man"
},
"files": [
"lib/**/*.js",
"bin/**/*.js"
],
"workspaces": [
"packages/*"
],
"scripts": {
"start": "node index.js",
"test": "tap test/*.js"
},
"config": {
"port": "8080"
},
"dependencies": {
"some-dependency": "^1.0.0"
},
"devDependencies": {
"some-dev-dependency": "^1.0.0"
},
"peerDependencies": {
"some-peer-dependency": "^1.0.0"
},
"peerDependenciesMeta": {
"some-peer-dependency": {
"optional": true
}
},
"optionalDependencies": {
"some-optional-dependency": "^1.0.0"
},
"bundledDependencies": [
"some-bundled-dependency"
],
"bundleDependencies": [
"some-bundled-dependency"
],
"engines": {
"node": ">=14.0.0"
},
"os": [
"darwin",
"linux"
],
"cpu": [
"x64",
"arm64"
],
"publishConfig": {
"registry": "https://registry.example.com"
},
"devEngines": {
"node": ">=14.0.0"
},
"licenses": [
{
"type": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
],
"overrides": {
"some-dependency": {
"some-sub-dependency": "1.0.0"
}
}
}

`

exports[`test/index.js TAP load update long package.json > should only update the defined property 1`] = `
{
"version": "7.18.1",
Expand Down Expand Up @@ -649,7 +530,7 @@ exports[`test/index.js TAP load update long package.json > should properly write

exports[`test/index.js TAP read package > must match snapshot 1`] = `
Object {
"name": "foo",
"name": "@npmcli/test",
"version": "1.0.0",
}
`
4 changes: 2 additions & 2 deletions test/fixtures/all-fields-populated/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "foo",
"name": "@npmcli/test",
"version": "1.0.0",
"private": true,
"description": "A sample package",
Expand Down Expand Up @@ -93,4 +93,4 @@
"some-sub-dependency": "1.0.0"
}
}
}
}
40 changes: 34 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ t.test('load', t => {
t.test('read a valid package.json', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'foo',
name: '@npmcli/test',
version: '1.0.0',
}),
})

const pj = await PackageJson.load(path)
t.same(
pj.content,
{ name: 'foo', version: '1.0.0' },
{ name: '@npmcli/test', version: '1.0.0' },
'should return content for a valid package.json'
)
})
t.test('read, update content and write', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'foo',
name: '@npmcli/test',
version: '1.0.0',
}, null, 8),
})
Expand Down Expand Up @@ -65,7 +65,7 @@ t.test('load', t => {
)
})
t.test('do not overwite unchanged file on EOF line added/removed', async t => {
const originalPackageJsonContent = '{\n "name": "foo"\n}'
const originalPackageJsonContent = '{\n "name": "@npmcli/test"\n}'
const path = t.testdir({
'package.json': originalPackageJsonContent,
})
Expand Down Expand Up @@ -132,7 +132,7 @@ t.test('load', t => {
t.test('custom formatting', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'foo',
name: '@npmcli/test',
version: '1.0.0',
}, null, 0),
})
Expand Down Expand Up @@ -243,7 +243,7 @@ t.test('read package', async t => {
const { readPackage } = require('../lib/read-package')
const path = t.testdir({
'package.json': JSON.stringify({
name: 'foo',
name: '@npmcli/test',
version: '1.0.0',
}),
})
Expand Down Expand Up @@ -327,3 +327,31 @@ t.test('empty props at bottom', async t => {
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
)
})

t.test('reversion can still save', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: '@npmcli/test',
version: '1.0.0',
}),
})
const pkgJson = await PackageJson.load(path)
pkgJson.update({ version: '2.0.0' })
await pkgJson.save()
t.strictSame(
{
name: '@npmcli/test',
version: '2.0.0',
},
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
)
pkgJson.update({ version: '1.0.0' })
await pkgJson.save()
t.strictSame(
{
name: '@npmcli/test',
version: '1.0.0',
},
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
)
})
Loading