diff --git a/.gitignore b/.gitignore index 323fbf1..a2ea707 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ Pipfile.lock tests/__pycache__/ testing/__pycache__/ pre_commit_hooks/__pycache__/ + +node_modules diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..62c8ec8 --- /dev/null +++ b/.npmrc @@ -0,0 +1,3 @@ +@turo:registry=https://turo.jfrog.io/turo/api/npm/npm/ +//turo.jfrog.io/turo/api/npm/npm/:_auth="${NPM_AUTH_TOKEN}" +//turo.jfrog.io/turo/api/npm/npm/:always-auth=true diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 7efb3f6..af0a2e7 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -95,3 +95,19 @@ language: script entry: hooks/yalc/yalc-check.sh files: package\.json +###################### +# Cspell hook +- id: cspell + name: cspell + description: "Checks for spelling mistakes & typos" + entry: cspell-cli + additional_dependencies: ["--userconfig=./.npmrc","--prefer-online","@turo/cspell@1"] + language: node + language_version: system + types: [text] + args: + - --no-must-find-files + - --no-progress + - --no-summary + - --gitignore + - --files # this needs to be the last argument so that the paths are passed correctly. diff --git a/hooks/cspell/index.js b/hooks/cspell/index.js new file mode 100644 index 0000000..6de7e3b --- /dev/null +++ b/hooks/cspell/index.js @@ -0,0 +1,48 @@ +#!/usr/bin/env node + +import { execSync } from 'child_process'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; + +// Function to get the installed version of a package +function getInstalledVersion(packageName, dir) { + try { + const packageJsonPath = path.join(dir, 'node_modules', packageName, 'package.json'); + if (fs.existsSync(packageJsonPath)) { + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + return packageJson.version; + } + } catch (error) { + return null; + } +} + +// Function to get the latest version of a package from npm +function getLatestVersion(packageName) { + try { + const result = execSync(`npm show ${packageName} version`, { encoding: 'utf8' }); + return result.trim(); + } catch (error) { + return null; + } +} + +// Get or create the fixed directory +const fixedDir = path.join(os.homedir(), '.cspell-local-dir'); +const packageName = '@turo/cspell'; + +try { + const installedVersion = getInstalledVersion(packageName, fixedDir); + const latestVersion = getLatestVersion(packageName); + + if (!installedVersion || installedVersion !== latestVersion) { + // Install the desired dependency in the fixed directory + execSync(`npm install --prefix ${fixedDir} ${packageName} --no-package-lock --prefer-online`); + } +} catch (error) { + console.error(`Failed to install or use ${packageName}:`, error); + process.exit(1); +} + +import "cspell/bin"; diff --git a/package.json b/package.json new file mode 100644 index 0000000..dafd87b --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "author": "Turo engineering ", + "bin": { + "cspell-cli": "./hooks/cspell/index.js" + }, + "dependencies": { + "cspell": "8.10.0" + }, + "homepage": "https://github.com/turo/pre-commit-hooks#readme", + "name": "@turo/pre-commit-hooks", + "private": true, + "repository": { + "type": "git", + "url": "git+https://github.com/turo/pre-commit-hooks.git" + }, + "type": "module", + "version": "4.0.0" +}