From 12f029a29422aee68d2bf3f4b8beb0ef4d5cf95f Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Sat, 29 Jun 2024 17:06:49 -0700 Subject: [PATCH] feat(root): Add changeset maker --- .changeset/1719705972004.md | 5 ++ commit | 2 + .../pockethost/changesetx.ts | 50 +++++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 .changeset/1719705972004.md create mode 100755 commit rename changesex.ts => packages/pockethost/changesetx.ts (70%) diff --git a/.changeset/1719705972004.md b/.changeset/1719705972004.md new file mode 100644 index 00000000..bbb4de24 --- /dev/null +++ b/.changeset/1719705972004.md @@ -0,0 +1,5 @@ +--- +'pockethost': minor +--- + +Add changeset maker \ No newline at end of file diff --git a/commit b/commit new file mode 100755 index 00000000..dc873df4 --- /dev/null +++ b/commit @@ -0,0 +1,2 @@ +#!/bin/bash +tsx packages/pockethost/changesetx.ts "$1" \ No newline at end of file diff --git a/changesex.ts b/packages/pockethost/changesetx.ts similarity index 70% rename from changesex.ts rename to packages/pockethost/changesetx.ts index 614f7e0d..a6c98bac 100755 --- a/changesex.ts +++ b/packages/pockethost/changesetx.ts @@ -1,14 +1,22 @@ #!/usr/bin/env tsx - import { execSync } from 'child_process' +import { sync } from 'conventional-commits-parser' import fs, { readFileSync } from 'fs' -import inquirer from 'inquirer' +import minimist from 'minimist' import { dirname, join } from 'path' import { fileURLToPath } from 'url' const __dirname = dirname(fileURLToPath(import.meta.url)) +const repoRootDir = join(__dirname, `../..`) + +const argv = minimist(process.argv.slice(2)) +const commitMessage = argv._[0]! +const { type, subject } = sync(commitMessage) +if (!type) { + console.log('Invalid commit message type') + process.exit(1) +} -let commitMessage = '' function getStagedPackageNames(filePaths: string[]): string[] { const packageNames = new Set() @@ -24,7 +32,7 @@ function getStagedPackageNames(filePaths: string[]): string[] { function getPackageName(filePath: string): string | null { const packagePath = filePath.split('/').slice(0, 2).join('/') - const packageJsonPath = join(__dirname, `${packagePath}/package.json`) + const packageJsonPath = join(repoRootDir, `${packagePath}/package.json`) const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) try { return packageJson.name @@ -52,35 +60,33 @@ async function createChangesetFile() { } console.log('Staged packages:', packageNames) - const answers = await inquirer.prompt([ - { - type: 'list', - name: 'versionType', - message: 'Select the version type:', - choices: ['major', 'minor', 'patch'], - }, - { - type: 'input', - name: 'commitMessage', - message: 'Enter the commit message:', - }, - ]) + const prefixMap = { + feat: 'minor', + enh: 'minor', + fix: 'patch', + docs: 'patch', + chore: 'patch', + } as const - commitMessage = answers.commitMessage - const { versionType } = answers + if (!(type! in prefixMap)) { + console.log('Invalid prefix found in commit message') + process.exit(1) + } + + const versionType = prefixMap[type as keyof typeof prefixMap] const changesetFile = [ `---`, ...packageNames.map((packageName) => `'${packageName}': ${versionType}`), `---`, ``, - commitMessage, + subject, ].join('\n') return changesetFile } async function writeChangesetFile(changesetFile: string) { - const filePath = join(__dirname, `.changeset`, `${+new Date()}.md`) + const filePath = join(repoRootDir, `.changeset`, `${+new Date()}.md`) try { fs.writeFileSync(filePath, changesetFile) console.log(`Changeset file written successfully at ${filePath}`) @@ -92,7 +98,7 @@ async function writeChangesetFile(changesetFile: string) { } function commit() { - execSync(`git commit -m "${commitMessage}"`).toString() + execSync(`git commit -m "${commitMessage}"`) } async function main() {