feat(root): Add changeset maker

This commit is contained in:
Ben Allfree 2024-06-29 17:06:49 -07:00
parent 40bbb3c774
commit 12f029a294
3 changed files with 35 additions and 22 deletions

View File

@ -0,0 +1,5 @@
---
'pockethost': minor
---
Add changeset maker

2
commit Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
tsx packages/pockethost/changesetx.ts "$1"

View File

@ -1,14 +1,22 @@
#!/usr/bin/env tsx #!/usr/bin/env tsx
import { execSync } from 'child_process' import { execSync } from 'child_process'
import { sync } from 'conventional-commits-parser'
import fs, { readFileSync } from 'fs' import fs, { readFileSync } from 'fs'
import inquirer from 'inquirer' import minimist from 'minimist'
import { dirname, join } from 'path' import { dirname, join } from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.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[] { function getStagedPackageNames(filePaths: string[]): string[] {
const packageNames = new Set<string>() const packageNames = new Set<string>()
@ -24,7 +32,7 @@ function getStagedPackageNames(filePaths: string[]): string[] {
function getPackageName(filePath: string): string | null { function getPackageName(filePath: string): string | null {
const packagePath = filePath.split('/').slice(0, 2).join('/') 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')) const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'))
try { try {
return packageJson.name return packageJson.name
@ -52,35 +60,33 @@ async function createChangesetFile() {
} }
console.log('Staged packages:', packageNames) console.log('Staged packages:', packageNames)
const answers = await inquirer.prompt([ const prefixMap = {
{ feat: 'minor',
type: 'list', enh: 'minor',
name: 'versionType', fix: 'patch',
message: 'Select the version type:', docs: 'patch',
choices: ['major', 'minor', 'patch'], chore: 'patch',
}, } as const
{
type: 'input',
name: 'commitMessage',
message: 'Enter the commit message:',
},
])
commitMessage = answers.commitMessage if (!(type! in prefixMap)) {
const { versionType } = answers console.log('Invalid prefix found in commit message')
process.exit(1)
}
const versionType = prefixMap[type as keyof typeof prefixMap]
const changesetFile = [ const changesetFile = [
`---`, `---`,
...packageNames.map((packageName) => `'${packageName}': ${versionType}`), ...packageNames.map((packageName) => `'${packageName}': ${versionType}`),
`---`, `---`,
``, ``,
commitMessage, subject,
].join('\n') ].join('\n')
return changesetFile return changesetFile
} }
async function writeChangesetFile(changesetFile: string) { async function writeChangesetFile(changesetFile: string) {
const filePath = join(__dirname, `.changeset`, `${+new Date()}.md`) const filePath = join(repoRootDir, `.changeset`, `${+new Date()}.md`)
try { try {
fs.writeFileSync(filePath, changesetFile) fs.writeFileSync(filePath, changesetFile)
console.log(`Changeset file written successfully at ${filePath}`) console.log(`Changeset file written successfully at ${filePath}`)
@ -92,7 +98,7 @@ async function writeChangesetFile(changesetFile: string) {
} }
function commit() { function commit() {
execSync(`git commit -m "${commitMessage}"`).toString() execSync(`git commit -m "${commitMessage}"`)
} }
async function main() { async function main() {