Skip to content

Commit

Permalink
Some small tweaks to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Nov 27, 2024
1 parent c8e66e1 commit b74859f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 98 deletions.
15 changes: 9 additions & 6 deletions scripts/publish-release.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as cp from 'node:child_process';

import { fileExists, readJson } from './utils/fs.js';
import { createRelease } from './utils/github-releases.js';
import { getPackageDir, hasJsrJson, readJsrJson, readPackageJson } from './utils/packages.js';
import { getPackageDir, getPackageFile } from './utils/packages.js';
import { logAndExec } from './utils/process.js';
import { isValidVersion } from './utils/semver.js';

Expand Down Expand Up @@ -50,7 +51,8 @@ console.log(`Publishing release ${tag} ...`);
console.log();

// 3) Publish to npm
let packageJson = readPackageJson(packageName);
let packageJsonFile = getPackageFile(packageName, 'package.json');
let packageJson = readJson(packageJsonFile);
if (packageJson.version !== version) {
console.error(
`Tag does not match package.json version: ${version} !== ${packageJson.version} (${tag})`,
Expand All @@ -65,16 +67,17 @@ logAndExec(`npm publish --access public`, {
console.log();

// 4) Publish to jsr (if applicable)
if (hasJsrJson(packageName)) {
let jsrJson = readJsrJson(packageName);
let jsrJsonFile = getPackageFile(packageName, 'jsr.json');
if (fileExists(jsrJsonFile)) {
let jsrJson = readJson(jsrJsonFile);
if (jsrJson.version !== version) {
console.error(
`Tag does not match jsr.json version: ${version} !== ${jsrJson.version} (${tag})`,
);
process.exit(1);
}

logAndExec(`pnpm dlx jsr publish`, {
logAndExec(`npx jsr publish`, {
cwd: getPackageDir(packageName),
env: process.env,
});
Expand All @@ -84,6 +87,6 @@ if (hasJsrJson(packageName)) {
// 5) Publish to GitHub Releases
console.log(`Publishing ${tag} on GitHub Releases ...`);
let releaseUrl = await createRelease(packageName, version);
console.log(`Published at: ${releaseUrl}`);
console.log(`Done, see ${releaseUrl}`);

console.log();
35 changes: 15 additions & 20 deletions scripts/tag-release.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import * as cp from 'node:child_process';

import {
getPackageFile,
readChangelog,
writeChangelog,
hasJsrJson,
readJsrJson,
writeJsrJson,
readPackageJson,
writePackageJson,
} from './utils/packages.js';
import { fileExists, readFile, readJson, writeFile, writeJson } from './utils/fs.js';
import { getPackageFile } from './utils/packages.js';
import { logAndExec } from './utils/process.js';
import { getNextVersion } from './utils/semver.js';

Expand All @@ -25,7 +17,8 @@ if (packageName.startsWith('@mjackson/')) {
packageName = packageName.slice('@mjackson/'.length);
}

let packageJson = readPackageJson(packageName);
let packageJsonFile = getPackageFile(packageName, 'package.json');
let packageJson = readJson(packageJsonFile);
let nextVersion = getNextVersion(packageJson.version, releaseType);
let tag = `${packageName}@${nextVersion}`;

Expand All @@ -40,18 +33,20 @@ console.log(`Tagging release ${tag} ...`);
console.log();

// 2) Update package.json with the new release version
writePackageJson(packageName, { ...packageJson, version: nextVersion });
logAndExec(`git add ${getPackageFile(packageName, 'package.json')}`);
writeJson(packageJsonFile, { ...packageJson, version: nextVersion });
logAndExec(`git add ${packageJsonFile}`);

// 4) Update jsr.json (if applicable) with the new release version
if (hasJsrJson(packageName)) {
let jsrJson = readJsrJson(packageName);
writeJsrJson(packageName, { ...jsrJson, version: nextVersion });
logAndExec(`git add ${getPackageFile(packageName, 'jsr.json')}`);
let jsrJsonFile = getPackageFile(packageName, 'jsr.json');
if (fileExists(jsrJsonFile)) {
let jsrJson = readJson(jsrJsonFile);
writeJson(jsrJsonFile, { ...jsrJson, version: nextVersion });
logAndExec(`git add ${jsrJsonFile}`);
}

// 3) Swap out "## HEAD" in CHANGELOG.md with the new release version + date
let changelog = readChangelog(packageName);
let changelogFile = getPackageFile(packageName, 'CHANGELOG.md');
let changelog = readFile(packageName);
let match = /^## HEAD\n/m.exec(changelog);
if (match) {
let [today] = new Date().toISOString().split('T');
Expand All @@ -61,8 +56,8 @@ if (match) {
`## v${nextVersion} (${today})\n` +
changelog.slice(match.index + match[0].length);

writeChangelog(packageName, changelog);
logAndExec(`git add ${getPackageFile(packageName, 'CHANGELOG.md')}`);
writeFile(changelogFile, changelog);
logAndExec(`git add ${changelogFile}`);
}

// 5) Commit and tag
Expand Down
42 changes: 42 additions & 0 deletions scripts/utils/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'node:fs';

/** @type (filename: string) => boolean */
export function fileExists(filename) {
return fs.existsSync(filename);
}

/** @type (filename: string) => string */
export function readFile(filename) {
try {
return fs.readFileSync(filename, 'utf-8');
} catch (error) {
if (isFsError(error) && error.code === 'ENOENT') {
console.error(`Not found: "${filename}"`);
process.exit(1);
} else {
throw error;
}
}
}

/** @type (filename: string, data: string) => void */
export function writeFile(filename, data) {
fs.writeFileSync(filename, data);
}

/** @type (filename: string) => any */
export function readJson(filename) {
return JSON.parse(readFile(filename));
}

/** @type (filename: string, data: any) => void */
export function writeJson(filename, data) {
writeFile(filename, JSON.stringify(data, null, 2) + '\n');
}

/** @type (error: unknown) => error is { code: string } */
function isFsError(error) {
return (
typeof error === 'object' && error != null && 'code' in error && typeof error.code === 'string'
);
}
72 changes: 0 additions & 72 deletions scripts/utils/packages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from 'node:fs';
import * as path from 'node:path';

const __dirname = path.dirname(new URL(import.meta.url).pathname);
Expand All @@ -17,74 +16,3 @@ export function getPackageDir(packageName) {
export function getPackageFile(packageName, filename) {
return path.join(getPackageDir(packageName), filename);
}

/** @type (packageName: string) => boolean */
export function hasChangelog(packageName) {
return fs.existsSync(getPackageFile(packageName, 'CHANGELOG.md'));
}

/** @type (packageName: string) => string */
export function readChangelog(packageName) {
return fs.readFileSync(getPackageFile(packageName, 'CHANGELOG.md'), 'utf-8');
}

/** @type (packageName: string, data: string) => void */
export function writeChangelog(packageName, data) {
fs.writeFileSync(getPackageFile(packageName, 'CHANGELOG.md'), data);
}

/** @type (packageName: string) => boolean */
export function hasJsrJson(packageName) {
return fs.existsSync(getPackageFile(packageName, 'jsr.json'));
}

/** @type (packageName: string) => any */
export function readJsrJson(packageName) {
return readJson(getPackageFile(packageName, 'jsr.json'));
}

/** @type (packageName: string, data: any) => void */
export function writeJsrJson(packageName, data) {
writeJson(getPackageFile(packageName, 'jsr.json'), data);
}

/** @type (packageName: string) => boolean */
export function hasPackageJson(packageName) {
return fs.existsSync(getPackageFile(packageName, 'package.json'));
}

/** @type (packageName: string) => any */
export function readPackageJson(packageName) {
return readJson(getPackageFile(packageName, 'package.json'));
}

/** @type (packageName: string, data: any) => void */
export function writePackageJson(packageName, data) {
writeJson(getPackageFile(packageName, 'package.json'), data);
}

/** @type (file: string) => any */
function readJson(file) {
try {
return JSON.parse(fs.readFileSync(file, 'utf-8'));
} catch (error) {
if (isFsError(error) && error.code === 'ENOENT') {
console.error(`Not found: "${file}"`);
process.exit(1);
} else {
throw error;
}
}
}

/** @type (filename: string, data: any) => void */
function writeJson(filename, data) {
fs.writeFileSync(filename, JSON.stringify(data, null, 2) + '\n');
}

/** @type (error: unknown) => error is { code: string } */
function isFsError(error) {
return (
typeof error === 'object' && error != null && 'code' in error && typeof error.code === 'string'
);
}

0 comments on commit b74859f

Please sign in to comment.