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

Add functions to update a package.json file, target Node.js 16, bump deps #21

Merged
merged 29 commits into from
Jul 14, 2023

Conversation

tommy-mitchell
Copy link
Contributor

@tommy-mitchell tommy-mitchell commented Apr 15, 2023

Closes #13. Bumps dependencies / Node.js version (and adds Node.js 18/20 to CI), and adds new functions for updating a package.json file. Source/test files have been separated by function.

updatePackage, updatePackageSync

Updates/creates a package.json file. Uses deepmerge-ts to merge properties.

await writePackage({name: 'foo', version: '1.0.0'});
await updatePackage({version: '1.1.0', license: 'MIT'});
{
	"name": "foo",
	"version": "1.1.0",
	"license": "MIT"
}

addPackageDependences, addPackageDependencesSync

Adds dependencies to a package.json file. Uses updatePackage internally, so it will create a package.json file if one doesn't exist.

await writePackage({foo: true});
{
	"name": "foo"
}
await addPackageDependencies({foo: '1.0.0'});
{
	"name": "foo",
	"dependencies": {
		"foo": "1.0.0"
	}
}
await addPackageDependencies({dependencies: {foo: '1.1.0'}, devDependencies: {bar: '1.0.0'}});
{
	"name": "foo",
	"dependencies": {
		"foo": "1.1.0"
	},
	"devDependencies": {
		"bar": "1.0.0"
	}
}

removePackageDependences, removePackageDependencesSync

Removes dependencies from a package.json file. If the package.json file doesn't exist, it does not throw.

await writePackage({foo: true, dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
{
	"name": "foo",
	"dependencies": {
		"foo": "1.0.0"
	},
	"devDependencies": {
		"bar": "1.0.0"
	}
}
await removePackageDependences(['foo']);
{
	"name": "foo",
	"devDependencies": {
		"bar": "1.0.0"
	}
}
await removePackageDependencies({devDependencies: ['bar']});
{
	"name": "foo"
}

test.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

The current names are addPackageDependencies and addPackageDependenciesSync. Are these too verbose?

Looks good to me.

If addPackageDependencies is called in a directory without a package.json, should it create one or error?

I think it would be nice if it created. It should be clearly documented though. If someone actually wants it to throw, it could be added as an option later.

Am I missing any test cases? I think it's covered well, but I'm not sure

Some more ideas (you may have them already, didn't look too close):

  • package.json doesn't exist
  • package.json is invalid JSON

test.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@tommy-mitchell tommy-mitchell changed the title Add functions to modify dependencies, target Node.js 16, bump deps Add updatePackage, target Node.js 16, bump deps Jul 5, 2023
@tommy-mitchell
Copy link
Contributor Author

tommy-mitchell commented Jul 5, 2023

Updated per #13 (comment).

Still need to update the readme. Should probably add a note that writePackage will overwrite properties.

index.d.ts Outdated Show resolved Hide resolved
@tommy-mitchell tommy-mitchell marked this pull request as ready for review July 6, 2023 18:15
@tommy-mitchell
Copy link
Contributor Author

@sindresorhus that should be everything. There may be some inconsistencies between the readme and the descriptions in index.d.ts, but I think I've been staring at the words "package" and "dependencies" for too long now to notice.

@tommy-mitchell tommy-mitchell changed the title Add updatePackage, target Node.js 16, bump deps Add functions to update a package.json file, target Node.js 16, bump deps Jul 6, 2023

### addPackageDependencies(path?, dependencies, options?)

Returns a `Promise` that resolves when the `package.json` file has been written.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to include the code examples from the TS types for each of these methods.

https://github.com/sindresorhus/write-pkg/pull/21/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8R137-R147

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@sindresorhus sindresorhus merged commit d273a84 into sindresorhus:main Jul 14, 2023
3 checks passed
@sindresorhus
Copy link
Owner

Thanks :)

@tommy-mitchell
Copy link
Contributor Author

@sindresorhus just noticed I didn’t add the source directory to the files field in package.json

@sindresorhus
Copy link
Owner

np caught that mistake :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Persist unchanged dependencies
2 participants