From ce8acec28c2424136f4dd806f67e3faf38b087b0 Mon Sep 17 00:00:00 2001 From: "Alexander S." Date: Sat, 2 Nov 2024 02:44:54 +0100 Subject: [PATCH] codegen: use current package version as default clone version (#208) When running `pnpm run clone` without an argument the latest oxlint version will be cloned (currently `0.10.3`). But the project main branch can be differently (currently on `0.10.1`) If someone want to contribute they will get new rules from `0.10.3`. This PR will prevents it and used the current package.json. --- scripts/oxlint-version.ts | 38 --------------------------------- scripts/rules-generator.test.ts | 12 +---------- scripts/sparse-clone.ts | 35 +++++++++++++++--------------- 3 files changed, 19 insertions(+), 66 deletions(-) delete mode 100644 scripts/oxlint-version.ts diff --git a/scripts/oxlint-version.ts b/scripts/oxlint-version.ts deleted file mode 100644 index 1426dba..0000000 --- a/scripts/oxlint-version.ts +++ /dev/null @@ -1,38 +0,0 @@ -import shell from 'shelljs'; -import { VERSION_PREFIX } from './constants.js'; -import fs from 'node:fs'; - -export function getLatestVersionFromClonedRepo( - targetDirectory: string, - versionPrefix = VERSION_PREFIX -) { - if (!fs.existsSync(targetDirectory)) { - return ''; - } - - shell.cd(targetDirectory); - - const oxlintTags = shell - .exec( - `git describe --tags --match='${versionPrefix}*' $(git rev-list --tags)`, - { - silent: true, - } - ) - .stdout.trim() - .split('\n'); - - let latestVersion = oxlintTags[0]; - for (let index = 1; index < oxlintTags.length; index++) { - const current = oxlintTags[index]; - const latestNumber = Number.parseInt( - latestVersion.replace(versionPrefix, '') - ); - const currentNumber = Number.parseInt(current.replace(versionPrefix, '')); - if (currentNumber > latestNumber) { - latestVersion = current; - } - } - - return latestVersion; -} diff --git a/scripts/rules-generator.test.ts b/scripts/rules-generator.test.ts index ddf3ef9..c428d02 100644 --- a/scripts/rules-generator.test.ts +++ b/scripts/rules-generator.test.ts @@ -1,18 +1,8 @@ -import { vi, test, suite, vitest, beforeEach, afterEach, expect } from 'vitest'; +import { test, suite, expect } from 'vitest'; import { RulesGenerator, RulesGrouping } from './rules-generator.js'; import type { Rule } from './traverse-rules.js'; suite('RulesGenerator', () => { - beforeEach(() => { - vi.mock('./oxlint-version.ts', () => ({ - getLatestVersionFromClonedRepo: vitest.fn().mockReturnValue('1.0.0'), - })); - }); - - afterEach(() => { - vi.resetAllMocks(); - }); - test('RulesGenerator generates rules correctly', async () => { const successResultArray: Rule[] = [ { diff --git a/scripts/sparse-clone.ts b/scripts/sparse-clone.ts index 0dbdf63..569b4e7 100644 --- a/scripts/sparse-clone.ts +++ b/scripts/sparse-clone.ts @@ -5,9 +5,18 @@ import { SPARSE_CLONE_DIRECTORY, VERSION_PREFIX, } from './constants.js'; -import { getLatestVersionFromClonedRepo } from './oxlint-version.js'; +import { version } from '../package.json'; -const version = process.argv[2] ?? undefined; +/** + * Run this file in CLI like `pnpm run clone` + * It clones the oxc_linter git project into your local file. + * + * You can run it with a version argument like `pnpm run clone 0.10.0`. + * When no argument is provided, the current package.json version is used. + * + */ + +const checkoutVersion = process.argv[2] ?? version; // Function to initialize or reconfigure sparse-checkout function configureSparseCheckout(cloneDirectory: string) { @@ -28,14 +37,8 @@ function configureSparseCheckout(cloneDirectory: string) { } } -function checkoutVersionTag( - targetDirectory: string, - cloneDirectory: string, - version?: string -) { - const tag = version - ? `${VERSION_PREFIX}${version}` - : getLatestVersionFromClonedRepo(targetDirectory); +function checkoutVersionTag(version: string) { + const tag = `${VERSION_PREFIX}${version}`; // Checkout the specified directory if (shell.exec(`git checkout ${tag}`, { silent: true }).code !== 0) { @@ -43,9 +46,7 @@ function checkoutVersionTag( shell.exit(1); } - console.log( - `Successfully cloned ${cloneDirectory} into ${targetDirectory}/${cloneDirectory} at ${tag}` - ); + console.log(`Successfully checkout git tag ${tag}`); } // Function to clone or update a repository @@ -53,7 +54,7 @@ function cloneOrUpdateRepo( repositoryUrl: string, targetDirectory: string, cloneDirectory: string, - version?: string + version: string ) { // Check if the target directory exists and is a Git repository if ( @@ -65,7 +66,7 @@ function cloneOrUpdateRepo( shell.cd(targetDirectory); configureSparseCheckout(cloneDirectory); - checkoutVersionTag(targetDirectory, cloneDirectory, version); + checkoutVersionTag(version); } else { console.log(`Cloning new repository into ${targetDirectory}...`); @@ -82,7 +83,7 @@ function cloneOrUpdateRepo( shell.cd(targetDirectory); configureSparseCheckout(cloneDirectory); - checkoutVersionTag(targetDirectory, cloneDirectory, version); + checkoutVersionTag(version); } } @@ -90,5 +91,5 @@ cloneOrUpdateRepo( 'https://github.com/oxc-project/oxc.git', TARGET_DIRECTORY, SPARSE_CLONE_DIRECTORY, - version + checkoutVersion );