From 30e927d7222655b9159f5626de050935933b1f79 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 26 Apr 2024 12:10:05 +0200 Subject: [PATCH 1/2] feat: refs #6202 dbWatcher script --- db/dbWatcher.js | 31 +++++++++++++++++++++++++++++++ gulpfile.js | 2 ++ package.json | 3 ++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 db/dbWatcher.js diff --git a/db/dbWatcher.js b/db/dbWatcher.js new file mode 100644 index 000000000..b3520450f --- /dev/null +++ b/db/dbWatcher.js @@ -0,0 +1,31 @@ +const fs = require('fs'); +const {spawn} = require('child_process'); + +function watchDatabaseChanges() { + console.log('Watching for changes in db/routines and db/versions'); + + fs.watch('db', {recursive: true}, (eventType, filename) => { + if (filename.endsWith('.sql')) { + let command; + + if (filename.startsWith('routines')) command = 'push'; + else if (filename.startsWith('versions')) command = 'run'; + + if (command) { + const process = spawn('myt', [command]); + + process.stdout.on('data', data => console.log(data.toString())); + + process.stderr.on('data', data => console.error(`stderr: ${data}`)); + + process.on('error', error => console.error(`error: ${error.message}`)); + + process.on('close', () => console.log('Watching for changes in db/routines and db/versions')); + } + } + }); +} + +if (require.main === module) watchDatabaseChanges(); + +module.exports = watchDatabaseChanges; diff --git a/gulpfile.js b/gulpfile.js index aa2b65bc1..045d3ac41 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -6,6 +6,7 @@ const log = require('fancy-log'); const Myt = require('@verdnatura/myt/myt'); const Run = require('@verdnatura/myt/myt-run'); const Start = require('@verdnatura/myt/myt-start'); +const watchDatabaseChanges = require('./db/dbWatcher'); // Configuration @@ -245,6 +246,7 @@ routes.description = 'Merges all module routes file into one file'; function watch(done) { gulp.watch(routeFiles, gulp.series(routes)); gulp.watch(localeFiles, gulp.series(locales)); + watchDatabaseChanges(); done(); } watch.description = `Watches for changes in routes and locale files`; diff --git a/package.json b/package.json index 033eafc40..2a5c06c3d 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,8 @@ "test:e2e": "node e2e/tests.js", "test:front": "jest --watch", "back": "nodemon --inspect -w modules ./node_modules/gulp/bin/gulp.js back", - "lint": "eslint ./ --cache --ignore-pattern .gitignore" + "lint": "eslint ./ --cache --ignore-pattern .gitignore", + "watch:db": "node ./db/dbWatcher.js" }, "jest": { "projects": [ From bf0db8928610429d4f1874d392523977dec7766b Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 9 May 2024 12:02:44 +0200 Subject: [PATCH 2/2] refactor: refs #6202 remove blanks --- db/dbWatcher.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/db/dbWatcher.js b/db/dbWatcher.js index b3520450f..e9389132d 100644 --- a/db/dbWatcher.js +++ b/db/dbWatcher.js @@ -3,23 +3,17 @@ const {spawn} = require('child_process'); function watchDatabaseChanges() { console.log('Watching for changes in db/routines and db/versions'); - fs.watch('db', {recursive: true}, (eventType, filename) => { if (filename.endsWith('.sql')) { let command; - if (filename.startsWith('routines')) command = 'push'; else if (filename.startsWith('versions')) command = 'run'; if (command) { const process = spawn('myt', [command]); - process.stdout.on('data', data => console.log(data.toString())); - process.stderr.on('data', data => console.error(`stderr: ${data}`)); - process.on('error', error => console.error(`error: ${error.message}`)); - process.on('close', () => console.log('Watching for changes in db/routines and db/versions')); } } @@ -27,5 +21,4 @@ function watchDatabaseChanges() { } if (require.main === module) watchDatabaseChanges(); - module.exports = watchDatabaseChanges;