Skip to content

Commit

Permalink
feat: add eslint support
Browse files Browse the repository at this point in the history
  • Loading branch information
b-x-wu committed Apr 21, 2024
1 parent eb99fa5 commit 7372ad0
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 9 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
flags: ["", "-t"]
flags: ["", "-t", "-l", "-t -l"]

steps:
- name: Checkout
Expand All @@ -35,6 +35,11 @@ jobs:
run: npx create-react-sandbox ${{ matrix.flags }}
working-directory: ./temp

- name: Run linter
if: ${{ contains(matrix.flags, '-l') }}
run: npx eslint
working-directory: ./temp/app

- name: Run Package Build Script
run: npm run build
working-directory: ./temp/app
Expand Down
26 changes: 26 additions & 0 deletions bin/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ function installDependencies(sandboxDirectory, onData, onErr, onExit) {
npmInit.on('exit', onExit)
}

/**
* Configure Eslint depending on specification
* @param sandboxDirectory the name of the directory housing the sandbox
* @param isTypescript if the sandbox uses typescript or not
* @param isEslint if the sandbox uses eslint or not
*/
async function handleEslint(sandboxDirectory, isTypescript, isEslint) {
if (!isEslint) {
await fs.unlink(path.join(sandboxDirectory, 'eslint.config.js'))
return
}

await modifyFile(path.join(sandboxDirectory, 'package.json'), (packageJson) => {
const packageObject = JSON.parse(packageJson)
packageObject.devDependencies['eslint'] = '^8.57.0'
packageObject.devDependencies['@eslint/js'] = '^9.0.0'
if (isTypescript) {
packageObject.devDependencies['typescript-eslint'] = '^7.6.0'
}
return JSON.stringify(packageObject, null, 2)
})
}

/**
* Run the main thread of the program
*/
Expand All @@ -93,6 +116,8 @@ async function main() {
jsonObject.name = sandboxDirectory
return JSON.stringify(jsonObject, null, 2)
})

await handleEslint(sandboxDirectory, isTypescript, isEslint)

loadingSpinner.stop()
process.stdout.write('\rInitialized the sandbox!\n')
Expand Down Expand Up @@ -121,3 +146,4 @@ main().catch((err) => {
loadingSpinner.stop()
console.log('\n' + err.message)
})

14 changes: 14 additions & 0 deletions bin/static/js/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import js from '@eslint/js'

export default [
js.configs.recommended,
{
ignores: ['**/dist/**'],
languageOptions: {
globals: {
"process": true,
},
},
},
]

3 changes: 2 additions & 1 deletion bin/static/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand All @@ -25,4 +26,4 @@
"webpack-dev-server": "^4.11.1"
}
}


10 changes: 7 additions & 3 deletions bin/static/js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const path = require("path")
const HtmlWebpackPlugin = require('html-webpack-plugin')
import path from 'path'
import { fileURLToPath } from 'url'
import HtmlWebpackPlugin from 'html-webpack-plugin'

module.exports = {
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

export default {
mode: process.env.NODE_ENV ?? "development",
entry: "./src/index.jsx",
plugins: [
Expand Down
17 changes: 17 additions & 0 deletions bin/static/ts/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @ts-check

import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{
ignores: ['**/dist/**'],
languageOptions: {
globals: {
"process": true,
},
},
},
eslint.configs.recommended,
...tseslint.configs.recommended,
)
3 changes: 2 additions & 1 deletion bin/static/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand All @@ -29,4 +30,4 @@
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
}
}
}
10 changes: 7 additions & 3 deletions bin/static/ts/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import path from 'path'
import { fileURLToPath } from 'url'
import HtmlWebpackPlugin from 'html-webpack-plugin'

const path = require("path")
const HtmlWebpackPlugin = require('html-webpack-plugin')
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

module.exports = {
export default {
mode: process.env.NODE_ENV ?? "development",
entry: "./src/index.tsx",
plugins: [
Expand Down Expand Up @@ -39,3 +42,4 @@ module.exports = {
]
}
}

0 comments on commit 7372ad0

Please sign in to comment.