From 6d391f76a90879b0bccec02e73b53d1377d2b605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Mon, 25 Jul 2022 16:41:27 +0200 Subject: [PATCH] Remove husky Run script on file change with vite plugin --- .husky/pre-commit | 4 --- package.json | 4 +-- site/package.json | 1 - site/scripts/preCommit/index.ts | 31 ----------------- .../execOnFileChange.ts | 0 site/scripts/runScriptOnFileChange/index.ts | 33 +++++++++++++++++++ site/vite.config.ts | 10 ++++++ yarn.lock | 10 ------ 8 files changed, 44 insertions(+), 49 deletions(-) delete mode 100755 .husky/pre-commit delete mode 100644 site/scripts/preCommit/index.ts rename site/scripts/{preCommit => runScriptOnFileChange}/execOnFileChange.ts (100%) create mode 100644 site/scripts/runScriptOnFileChange/index.ts diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index 765baae31..000000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -yarn pre-commit diff --git a/package.json b/package.json index d753fd9fd..57ff99779 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "site" ], "scripts": { - "pre-commit": "yarn workspaces foreach -pi run pre-commit", "scalingo-postbuild": "CI=true ; yarn test:type && yarn workspaces focus api && yarn workspace api run build && yarn workspaces focus --all && yarn test && yarn workspaces focus api --production", "lint:eslintrc": "npx eslint-config-prettier .eslintrc.cjs", "lint:eslint": "NODE_OPTIONS='--max-old-space-size=4096' eslint .", @@ -22,7 +21,7 @@ "lint:prettier:fix": "yarn lint:prettier --write", "lint:fix": "yarn lint:eslint:fix ; yarn lint:prettier:fix", "lint": "yarn lint:eslintrc && yarn lint:eslint && yarn lint:prettier", - "postinstall": "husky install && yarn workspaces foreach -piv --exclude site run prepack", + "postinstall": "yarn workspaces foreach -piv --exclude site run prepack", "test": "CI=true yarn workspaces foreach run test", "test:type": "yarn workspaces foreach -pi run tsc --skipLibCheck --noEmit", "clean": "yarn workspaces foreach run clean && rimraf node_modules", @@ -51,7 +50,6 @@ "eslint-plugin-promise": "^6.0.0", "eslint-plugin-react": "^7.30.0", "eslint-plugin-react-hooks": "^4.5.0", - "husky": "^8.0.0", "prettier": "^2.6.2", "rimraf": "^3.0.2" }, diff --git a/site/package.json b/site/package.json index 62356152f..6954abba7 100644 --- a/site/package.json +++ b/site/package.json @@ -18,7 +18,6 @@ "not ie < 11" ], "scripts": { - "pre-commit": "ts-node-esm scripts/preCommit/index.ts", "build:yaml-to-dts": "ts-node-esm scripts/build-yaml-to-dts.ts", "postinstall": "node scripts/prepare.js", "start": "vite dev", diff --git a/site/scripts/preCommit/index.ts b/site/scripts/preCommit/index.ts deleted file mode 100644 index d0574abdd..000000000 --- a/site/scripts/preCommit/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { execOnFileChange } from './execOnFileChange.js' - -console.log('Search for changed file...') - -const results = await execOnFileChange({ - basePath: './', - depsPath: '.deps.json', - options: [ - { - paths: [ - './source/pages/Simulateurs/EconomieCollaborative/activités.yaml', - './source/pages/Simulateurs/EconomieCollaborative/activités.en.yaml', - ], - run: 'yarn build:yaml-to-dts', - }, - ], -}) - -results - .filter((x: null | T): x is T => !!x) - .forEach(({ fileChanged, run, result }) => { - console.log('Changed file detected:', fileChanged) - console.log('Execute:', run, '\n') - - if (result.stdout) { - console.log(result.stdout) - } - if (result.stderr) { - console.error(result.stderr) - } - }) diff --git a/site/scripts/preCommit/execOnFileChange.ts b/site/scripts/runScriptOnFileChange/execOnFileChange.ts similarity index 100% rename from site/scripts/preCommit/execOnFileChange.ts rename to site/scripts/runScriptOnFileChange/execOnFileChange.ts diff --git a/site/scripts/runScriptOnFileChange/index.ts b/site/scripts/runScriptOnFileChange/index.ts new file mode 100644 index 000000000..8e1ca3947 --- /dev/null +++ b/site/scripts/runScriptOnFileChange/index.ts @@ -0,0 +1,33 @@ +import { execOnFileChange } from './execOnFileChange.js' + +export const runScriptOnFileChange = async () => { + console.log('Search for changed file...') + + const results = await execOnFileChange({ + basePath: './', + depsPath: '.deps.json', + options: [ + { + paths: [ + './source/pages/Simulateurs/EconomieCollaborative/activités.yaml', + './source/pages/Simulateurs/EconomieCollaborative/activités.en.yaml', + ], + run: 'yarn build:yaml-to-dts', + }, + ], + }) + + results + .filter((x: null | T): x is T => !!x) + .forEach(({ fileChanged, run, result }) => { + console.log('Changed file detected:', fileChanged) + console.log('Execute:', run, '\n') + + if (result.stdout) { + console.log(result.stdout) + } + if (result.stderr) { + console.error(result.stderr) + } + }) +} diff --git a/site/vite.config.ts b/site/vite.config.ts index 1d3e40f54..fc723a98e 100644 --- a/site/vite.config.ts +++ b/site/vite.config.ts @@ -10,6 +10,7 @@ import serveStatic from 'serve-static' import { defineConfig, loadEnv, Plugin } from 'vite' import { VitePWA } from 'vite-plugin-pwa' import shimReactPdf from 'vite-plugin-shim-react-pdf' +import { runScriptOnFileChange } from './scripts/runScriptOnFileChange' const env = (mode: string) => loadEnv(mode, process.cwd(), '') @@ -23,6 +24,15 @@ export default defineConfig(({ command, mode }) => ({ sourcemap: true, }, plugins: [ + { + name: 'run-script-on-file-change', + apply: 'serve', + buildStart() { + if (mode === 'development') { + void runScriptOnFileChange() + } + }, + }, command === 'build' && replace({ __SENTRY_DEBUG__: false, diff --git a/yarn.lock b/yarn.lock index c959362f3..80b022cf8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17566,15 +17566,6 @@ __metadata: languageName: node linkType: hard -"husky@npm:^8.0.0": - version: 8.0.1 - resolution: "husky@npm:8.0.1" - bin: - husky: lib/bin.js - checksum: 943a73a13d0201318fd30e83d299bb81d866bd245b69e6277804c3b462638dc1921694cb94c2b8c920a4a187060f7d6058d3365152865406352e934c5fff70dc - languageName: node - linkType: hard - "hyphen@npm:^1.6.4": version: 1.6.4 resolution: "hyphen@npm:1.6.4" @@ -24821,7 +24812,6 @@ __metadata: eslint-plugin-promise: ^6.0.0 eslint-plugin-react: ^7.30.0 eslint-plugin-react-hooks: ^4.5.0 - husky: ^8.0.0 prettier: ^2.6.2 rimraf: ^3.0.2 languageName: unknown