|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to lint a target directory using the local assemblyscript-eslint-plugin |
| 4 | + |
| 5 | +set -e # Exit immediately if a command exits with a non-zero status. |
| 6 | + |
| 7 | +TARGET_DIR="$1" |
| 8 | +# Use .mjs for ES Module flat config |
| 9 | +CONFIG_FILE=".eslintrc.tmp.mjs" |
| 10 | +PLUGIN_NAME="assemblyscript" # Plugin name prefix used in rules |
| 11 | + |
| 12 | +# Check if target directory is provided |
| 13 | +if [ -z "$TARGET_DIR" ]; then |
| 14 | + echo "Usage: $0 <directory-to-lint>" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Check if target directory exists |
| 19 | +if [ ! -d "$TARGET_DIR" ]; then |
| 20 | + echo "Error: Directory '$TARGET_DIR' not found." |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# Create a temporary ESLint flat config file (.mjs) |
| 25 | +cat > "$CONFIG_FILE" << EOL |
| 26 | +// Import necessary modules for flat config |
| 27 | +import path from 'path'; |
| 28 | +import { fileURLToPath } from 'url'; |
| 29 | +import tsParser from '@typescript-eslint/parser'; |
| 30 | +// Import the local plugin using ES Module syntax |
| 31 | +import localPlugin from './index.js'; // Assumes index.js is the entry point |
| 32 | +
|
| 33 | +// Helper to get current directory in ES Modules |
| 34 | +const __filename = fileURLToPath(import.meta.url); |
| 35 | +const __dirname = path.dirname(__filename); |
| 36 | +
|
| 37 | +const allPluginRules = {}; |
| 38 | +const pluginName = '${PLUGIN_NAME}'; // Use the plugin name defined in bash |
| 39 | +
|
| 40 | +// Access rules from the default export of the plugin module |
| 41 | +if (localPlugin && localPlugin.rules) { |
| 42 | + for (const ruleName in localPlugin.rules) { |
| 43 | + // Construct the full rule name: 'plugin-name/rule-name' |
| 44 | + allPluginRules[\`\${pluginName}/\${ruleName}\`] = 'warn'; // Set default severity |
| 45 | + } |
| 46 | +} else { |
| 47 | + console.warn(\`Plugin '\${pluginName}' loaded from ./index.js does not seem to export rules correctly.\`); |
| 48 | +} |
| 49 | +
|
| 50 | +// Export the flat config array |
| 51 | +export default [ |
| 52 | + { |
| 53 | + // Apply to TypeScript files in the target directory |
| 54 | + files: ["${TARGET_DIR}/**/*.ts"], |
| 55 | + languageOptions: { |
| 56 | + parser: tsParser, |
| 57 | + parserOptions: { |
| 58 | + ecmaVersion: 'latest', |
| 59 | + sourceType: 'module', |
| 60 | + }, |
| 61 | + }, |
| 62 | + // Define the plugin using the imported object |
| 63 | + plugins: { |
| 64 | + [pluginName]: localPlugin, |
| 65 | + }, |
| 66 | + // Apply the dynamically generated rules |
| 67 | + rules: allPluginRules, |
| 68 | + } |
| 69 | +]; |
| 70 | +EOL |
| 71 | + |
| 72 | +echo "Linting directory: $TARGET_DIR with dynamically generated flat config $CONFIG_FILE" |
| 73 | + |
| 74 | +# Run ESLint using npx |
| 75 | +# ESLint v9+ should automatically detect eslint.config.js or use --config flag |
| 76 | +# Ensure @typescript-eslint/parser is installed as a dev dependency |
| 77 | +if npx eslint --config "$CONFIG_FILE" "${TARGET_DIR}/**/*.ts"; then |
| 78 | + echo "Linting completed successfully." |
| 79 | + EXIT_CODE=0 |
| 80 | +else |
| 81 | + echo "Linting failed." |
| 82 | + EXIT_CODE=1 |
| 83 | +fi |
| 84 | + |
| 85 | +# Clean up the temporary config file |
| 86 | +rm "$CONFIG_FILE" |
| 87 | +echo "Removed temporary config file: $CONFIG_FILE" |
| 88 | + |
| 89 | +exit $EXIT_CODE |
0 commit comments