From 4bb74de38a50b7617341b36a19a110b2ffc4cf81 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Mon, 13 May 2019 14:04:35 -0500 Subject: [PATCH] Add dev task to watch and recompile on source changes --- package-lock.json | 6 ++++++ package.json | 4 +++- scripts/serve.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 scripts/serve.js diff --git a/package-lock.json b/package-lock.json index 693d67d..4e90bd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1745,6 +1745,12 @@ } } }, + "node-watch": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/node-watch/-/node-watch-0.6.2.tgz", + "integrity": "sha512-qmb/2ehVk1DC4gZYox7JgWXNucKnVk7uQmcoSeRaC7kzXsT8VOPIe7mbrR9Vc2TpGggS2yGUcgC98A2fKE53AA==", + "dev": true + }, "nopt": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", diff --git a/package.json b/package.json index d8c408a..0838ec2 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "repository": "https://github.com/OpenSprinkler/Weather-Weather", "scripts": { "test": "mocha --exit test", - "start": "node js/server", + "start": "npm run compile && node js/server", + "dev": "node scripts/serve", "compile": "tsc" }, "dependencies": { @@ -31,6 +32,7 @@ "hippie": "^0.5.2", "istanbul": "^0.4.5", "mocha": "^5.2.0", + "node-watch": "^0.6.2", "nock": "^9.6.1", "typescript": "^3.4.5" } diff --git a/scripts/serve.js b/scripts/serve.js new file mode 100644 index 0000000..6a6b075 --- /dev/null +++ b/scripts/serve.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node + +/** + * This is used for development only and simply launches a web server and + * injects a script to trigger a reload when a file changes in the serve + * directory. + */ + +const exec = require( "child_process" ).exec; +const path = require( "path" ); + +const routesPath = path.join( __dirname, "../routes/" ); +const serverPath = path.join( __dirname, "../server.ts" ); + +const watch = require( "node-watch" ); + +compile(); +console.log( "OpenSprinkler Development Server Started..." ); + +/** Start the web server */ +exec( `nodemon js/server` ); + +/** Watch for changes and recompile */ +watch( routesPath, { recursive: true }, recompile ); +watch( serverPath, { recursive: true }, recompile ); + +function recompile() { + console.log( "Changes detected, reloading..." ); + compile(); +} + +function compile() { + exec( `npm run compile` ); +} \ No newline at end of file