Skip to content

Commit e320f4d

Browse files
committed
add npm package support
1 parent 87c08d4 commit e320f4d

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
run: npm install
1919

2020
- name: Run test script
21-
run: bash ./test-lint.sh
21+
run: bash ./sanity-check.sh

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package-lock.json
22
node_modules
3-
dist/
3+
dist/
4+
sample_cases/

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import perfPlugin from './dist/plugins/perf-plugin.js';
2+
import asPlugin from './dist/plugins/as-plugin.js';
3+
4+
export default {
5+
rules: {
6+
...perfPlugin.rules,
7+
...asPlugin.rules,
8+
},
9+
};

lint-target.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919
"author": "wasm-ecosystem",
2020
"type": "module",
2121
"main": "index.js",
22+
"files": [
23+
"dist",
24+
"index.js",
25+
"README.md",
26+
"LICENSE"
27+
],
2228
"scripts": {
2329
"build": "npx tsc --build ./tsconfig.json",
2430
"test": "./test-lint.sh"
2531
},
32+
"peerDependencies": {
33+
"eslint": ">=8.0.0"
34+
},
2635
"devDependencies": {
2736
"@types/eslint": "^9.6.1",
2837
"@types/node": "^22.14.1",
File renamed without changes.

0 commit comments

Comments
 (0)