diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..26189a3 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,101 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + schedule: + - cron: "35 16 * * 1" + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: "ubuntu-latest" + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: actions + build-mode: none + - language: javascript-typescript + build-mode: none + # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v6 + + # Add any setup steps before running the `github/codeql-action/init` action. + # This includes steps like installing compilers or runtimes (`actions/setup-node` + # or others). This is typically only required for manual builds. + # - name: Setup runtime (example) + # uses: actions/setup-example@v1 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v4 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # â„šī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - name: Run manual build steps + if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v4 + with: + category: "/language:${{matrix.language}}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 91a882a..a104994 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Push Code +name: Release Pipeline on: pull_request_review: @@ -12,10 +12,18 @@ permissions: packages: write id-token: write +concurrency: + group: release-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + NODE_VERSION: "25" + jobs: check: + name: Validate runs-on: ubuntu-latest - if: | + if: > github.event_name == 'push' || (github.event_name == 'pull_request_review' && github.event.review.state == 'approved') @@ -27,19 +35,20 @@ jobs: - name: Setup Node.js (with npm cache) uses: actions/setup-node@v4.0.2 with: - node-version: "25" + node-version: ${{ env.NODE_VERSION }} cache: "npm" registry-url: "https://npm.pkg.github.com" env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Install dependencies - run: npm install + run: npm ci - name: Check Compatibility run: npm run check release_git: + name: Publish GitHub Release needs: check if: github.event_name == 'push' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest @@ -53,14 +62,14 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4.0.2 with: - node-version: "25" + node-version: ${{ env.NODE_VERSION }} cache: "npm" registry-url: "https://npm.pkg.github.com" env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Install dependencies - run: npm install + run: npm ci - name: Install git-cliff uses: taiki-e/install-action@git-cliff @@ -73,6 +82,7 @@ jobs: GITHUB_SHA: ${{ github.sha }} release_npm: + name: Publish npm Package needs: check environment: npm if: github.event_name == 'push' && github.ref == 'refs/heads/main' @@ -87,12 +97,12 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4.0.2 with: - node-version: "25" + node-version: ${{ env.NODE_VERSION }} cache: "npm" registry-url: "https://registry.npmjs.org" - name: Install dependencies - run: npm install + run: npm ci - name: Run npm release script run: node scripts/actions/npm.js diff --git a/scripts/actions/github.js b/scripts/actions/github.js index 72948cb..7276b43 100644 --- a/scripts/actions/github.js +++ b/scripts/actions/github.js @@ -6,24 +6,28 @@ import { tagExists, createRelease, } from "../utils/github.js"; -import { checkVersionExists, GITHUB_URL, getNpmDistTag } from "../utils/npm.js"; -import { exec, generateChangelog, isMain } from "../utils/util.js"; +import { + checkVersionExists, + GITHUB_URL, + publishTarball, +} from "../utils/npm.js"; +import { generateChangelog, isMain } from "../utils/util.js"; -async function buildProject() { - const github_token = process.env.GITHUB_TOKEN; - if (!github_token) { +async function runGitHubRelease() { + const githubToken = process.env.GITHUB_TOKEN; + if (!githubToken) { throw new Error("Github Token Not Found"); } const { owner, repo } = getRepoInfo(); const sha = getSha(); - const tempjson = { + const releasePackageJson = { ...packageJson, name: `@${owner}/${packageJson.name}`, }; - const version = tempjson.version; + const version = releasePackageJson.version; if (!version) { throw new Error("package.json version not found"); @@ -34,12 +38,16 @@ async function buildProject() { console.log(`Version: ${version}`); console.log(`Current commit: ${sha.slice(0, 7)}`); - const npmVerExists = checkVersionExists(tempjson.name, version, GITHUB_URL); + const npmVerExists = checkVersionExists( + releasePackageJson.name, + version, + GITHUB_URL + ); const githubTagExists = tagExists(version); let buildPath = null; const ensureBuildPath = async () => { - buildPath ??= await build(tempjson); + buildPath ??= await build(releasePackageJson); return buildPath; }; @@ -67,11 +75,7 @@ async function buildProject() { const tarballPath = await ensureBuildPath(); - const distTag = getNpmDistTag(version); - const tagArg = distTag === "latest" ? "" : ` --tag ${distTag}`; - exec(`npm publish "${tarballPath}" --registry=${GITHUB_URL}${tagArg}`, { - stdio: "inherit", - }); + publishTarball(tarballPath, GITHUB_URL, version); } catch (error) { console.log(error); throw new Error("Failed to publish package to GitHub Packages"); @@ -80,7 +84,7 @@ async function buildProject() { } if (isMain(import.meta.url)) { - buildProject().catch((err) => { + runGitHubRelease().catch((err) => { console.error("Patch failed:", err); process.exit(1); }); diff --git a/scripts/actions/npm.js b/scripts/actions/npm.js index 619ab2e..9489da7 100644 --- a/scripts/actions/npm.js +++ b/scripts/actions/npm.js @@ -1,19 +1,19 @@ import packageJson from "../../package.json" with { type: "json" }; import build from "../utils/build.js"; import { getSha } from "../utils/github.js"; -import { checkVersionExists, NPM_URL, getNpmDistTag } from "../utils/npm.js"; -import { exec, isMain } from "../utils/util.js"; +import { checkVersionExists, NPM_URL, publishTarball } from "../utils/npm.js"; +import { isMain } from "../utils/util.js"; -async function buildProject() { +async function runNpmRelease() { const owner = process.env.NPM_ORG; const sha = getSha(); - const tempjson = { + const releasePackageJson = { ...packageJson, name: owner ? `@${owner}/${packageJson.name}` : packageJson.name, }; - const version = tempjson.version; + const version = releasePackageJson.version; if (!version) { throw new Error("package.json version not found"); @@ -23,11 +23,15 @@ async function buildProject() { console.log(`Version: ${version}`); console.log(`Current commit: ${sha.slice(0, 7)}`); - const npmVerExists = checkVersionExists(tempjson.name, version, NPM_URL); + const npmVerExists = checkVersionExists( + releasePackageJson.name, + version, + NPM_URL + ); let buildPath = null; const ensureBuildPath = async () => { - buildPath ??= await build(tempjson); + buildPath ??= await build(releasePackageJson); return buildPath; }; @@ -39,14 +43,7 @@ async function buildProject() { const tarballPath = await ensureBuildPath(); - const distTag = getNpmDistTag(version); - const tagArg = distTag === "latest" ? "" : ` --tag ${distTag}`; - exec( - `npm publish "${tarballPath}" --provenance --registry=${NPM_URL}${tagArg}`, - { - stdio: "inherit", - } - ); + publishTarball(tarballPath, NPM_URL, version, { provenance: true }); } catch (error) { console.log(error); throw new Error("Failed to publish package to npm"); @@ -55,7 +52,7 @@ async function buildProject() { } if (isMain(import.meta.url)) { - buildProject().catch((err) => { + runNpmRelease().catch((err) => { console.error("Patch failed:", err); process.exit(1); }); diff --git a/scripts/utils/npm.js b/scripts/utils/npm.js index 2993eaa..3a07f75 100644 --- a/scripts/utils/npm.js +++ b/scripts/utils/npm.js @@ -1,4 +1,4 @@ -import { exec } from "./util.js"; +import { execFileSync } from "node:child_process"; export const NPM_URL = "https://registry.npmjs.org"; export const GITHUB_URL = "https://npm.pkg.github.com"; @@ -10,11 +10,30 @@ export function getNpmDistTag(version) { export function checkVersionExists(packageName, version, registry) { try { - exec(`npm view ${packageName}@${version} --registry=${registry}`, { - stdio: "ignore", - }); + execFileSync( + "npm", + ["view", `${packageName}@${version}`, `--registry=${registry}`], + { + stdio: "ignore", + } + ); return true; } catch { return false; } } + +export function publishTarball(tarballPath, registry, version, options = {}) { + const { provenance = false } = options; + const distTag = getNpmDistTag(version); + const args = ["publish", tarballPath]; + if (provenance) { + args.push("--provenance"); + } + args.push(`--registry=${registry}`); + if (distTag !== "latest") { + args.push("--tag", distTag); + } + + execFileSync("npm", args, { stdio: "inherit" }); +} diff --git a/src/structures/builder/Builder.ts b/src/structures/builder/Builder.ts index c8ca12f..e9afc78 100644 --- a/src/structures/builder/Builder.ts +++ b/src/structures/builder/Builder.ts @@ -1,10 +1,7 @@ -import { - SlashCommandBuilder, - RESTPostAPIChatInputApplicationCommandsJSONBody, -} from "discord.js"; +import { SlashCommandBuilder } from "discord.js"; import { Client } from "@structures/core/index.js"; import { normalizeArray } from "@utils/normalizeArray.js"; - +import type { RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord.js"; export interface ApplicationJSONBody extends RESTPostAPIChatInputApplicationCommandsJSONBody { prefix_support: boolean; slash_support: boolean; diff --git a/src/structures/builder/Event.ts b/src/structures/builder/Event.ts index 0714716..7547ba5 100644 --- a/src/structures/builder/Event.ts +++ b/src/structures/builder/Event.ts @@ -1,4 +1,4 @@ -import { ClientEvents } from "discord.js"; +import type { ClientEvents } from "discord.js"; import type { MaybePromise } from "#types/extra.js"; import { currentClient } from "@context"; import { Client } from "@structures/index.js"; diff --git a/tsconfig.json b/tsconfig.json index 3349fa9..006b559 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -44,8 +44,8 @@ "noFallthroughCasesInSwitch": true, "noImplicitReturns": false, - "noUnusedLocals": false, - "noUnusedParameters": false, + "noUnusedLocals": true, + "noUnusedParameters": true, "preserveConstEnums": true, "pretty": true, @@ -56,7 +56,7 @@ "composite": false, "sourceMap": false, - "verbatimModuleSyntax": false + "verbatimModuleSyntax": true }, "include": ["src/**/*", "types/**/*"], "exclude": ["node_modules/**/*", "dist/**/*", "scripts/**/*"]