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

feat: update package.json while preserve indent & eol style #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"consola": "^3.2.3",
"convert-gitmoji": "^0.1.3",
"execa": "^7.1.1",
"jsonc-parser": "^3.2.0",
"mri": "^1.2.0",
"node-fetch-native": "^1.2.0",
"ofetch": "^1.1.1",
Expand All @@ -63,4 +64,4 @@
"vitest": "^0.33.0"
},
"packageManager": "[email protected]"
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions src/package.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { promises as fsp } from "node:fs";
import { resolve } from "pathe";
import consola from "consola";
import {
PackageJson,
readPackageJSON as _readPackageJSON,
writePackageJSON as _writePackageJSON,
} from "pkg-types";
import { readPackageJSON as _readPackageJSON } from "pkg-types";
import { isCI, provider } from "std-env";
import { modify, applyEdits } from "jsonc-parser";
import type { ChangelogConfig } from "./config";

import { execCommand } from "./exec";

export function readPackageJSON(config: ChangelogConfig) {
const path = resolve(config.cwd, "package.json");
return _readPackageJSON(path);
}

export function writePackageJSON(config: ChangelogConfig, pkg: PackageJson) {
const path = resolve(config.cwd, "package.json");
return _writePackageJSON(path, pkg);
async function updatePackageFile(cwd: string, prop: string, value: any) {
const path = resolve(cwd, "package.json");
const blob = await fsp.readFile(path, "utf8");
await fsp.writeFile(path, applyEdits(blob, modify(blob, [prop], value, {})));
}

export async function renamePackage(config: ChangelogConfig, newName: string) {
Expand All @@ -30,7 +28,14 @@ export async function renamePackage(config: ChangelogConfig, newName: string) {
}
consola.info(`Renaming npm package from \`${pkg.name}\` to \`${newName}\``);
pkg.name = newName;
await writePackageJSON(config, pkg);
await updatePackageFile(config.cwd, "name", pkg.name);
}

export async function updatePackageVersion(
config: ChangelogConfig,
newVersion: string
) {
await updatePackageFile(config.cwd, "version", newVersion);
}

export async function npmPublish(config: ChangelogConfig) {
Expand Down
4 changes: 2 additions & 2 deletions src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import semver from "semver";
import consola from "consola";
import type { ChangelogConfig } from "./config";
import type { GitCommit } from "./git";
import { readPackageJSON, writePackageJSON } from "./package";
import { readPackageJSON, updatePackageVersion } from "./package";

export type SemverBumpType =
| "major"
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function bumpVersion(
`Bumping npm package version from \`${currentVersion}\` to \`${pkg.version}\` (${originalType})`
);

await writePackageJSON(config, pkg);
await updatePackageVersion(config, pkg.version);

return pkg.version;
}