diff --git a/package.json b/package.json index 8eebb62..024d7ce 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "dev": "node esbuild.config.mjs", "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", - "version": "node version-bump.mjs && git add manifest.json versions.json" + "version": "node version-bump.mjs && git add manifest.json versions.json", + "release": "node scripts/release.js" }, "keywords": [], "author": "Jamie Steiner", diff --git a/scripts/release.js b/scripts/release.js new file mode 100644 index 0000000..af1d4f0 --- /dev/null +++ b/scripts/release.js @@ -0,0 +1,78 @@ +// Import required modules +const { execSync } = require("child_process"); +const { existsSync } = require("fs"); +const path = require("path"); + +// Function to execute shell commands +function execCommand(command) { + try { + return execSync(command, { stdio: "pipe" }).toString().trim(); + } catch (error) { + console.error(`Error executing command: ${command}`); + process.exit(1); + } +} + +// Ensure GitHub CLI is installed +try { + execCommand("gh --version"); +} catch { + console.error("GitHub CLI could not be found. Please install it from https://cli.github.com/."); + process.exit(1); +} + +// Get the current working directory +const currentDir = process.cwd(); + +// Change to the current directory +process.chdir(currentDir); + +// Push tags to the remote repository +execCommand("git push --tags"); + +// Get the full repository URL +const fullRepo = execCommand("git remote get-url origin"); + +// Extract the repository name +const repo = fullRepo.replace(/^https:\/\/github.com\//, "").replace(/\.git$/, ""); + +// Check if the required arguments are supplied +if (process.argv.length !== 3) { + console.error("Usage: node releaser.js "); + process.exit(1); +} + +// Command line parameters +const tag = execCommand("git describe --tags --abbrev=0"); +const releaseName = process.argv[2]; + +// Create a new release using the GitHub CLI +try { + execCommand( + `gh release create ${tag} --repo ${repo} --title "${releaseName}" --notes "Release created using GitHub CLI."` + ); +} catch { + console.error("Failed to create the release. Ensure that the repository and tag are correct."); + process.exit(2); +} + +// Files to upload +const files = ["main.js", "styles.css", "manifest.json"]; + +// Upload files to the release +files.forEach((file) => { + if (existsSync(file)) { + try { + execCommand(`gh release upload ${tag} ${file} --repo ${repo}`); + console.log(`Successfully uploaded ${file}.`); + } catch { + console.error(`Failed to upload ${file}. Ensure the file exists and the release was created successfully.`); + process.exit(3); + } + } else { + console.error(`File ${file} does not exist.`); + process.exit(3); + } +}); + +console.log("Release created and files uploaded successfully.");