diff --git a/README.md b/README.md index cbb78d48..63e50647 100644 --- a/README.md +++ b/README.md @@ -1348,8 +1348,14 @@ Here's how to set up a dev env to hack on Roosevelt: - Create or find a Roosevelt app you want to test against. - To make a Roosevelt app, run `npx mkroosevelt` - Your changes to Roosevelt need to be copied to your app's `node_modules/roosevelt` directory. - - If you do not want to sync these directories automatically, then run the `dev_sync.sh` script. To do that: + - If you do not want to sync these directories automatically, then run the `dev_sync.js` script. To do that: - Install [fswatch](https://github.com/emcrisostomo/fswatch) and [rsync](https://en.wikipedia.org/wiki/Rsync) and ensure they are in your PATH. - Set a `DEST_DIR` environment variable: `export DEST_DIR=/path/to/your/roosevelt/app` - - Run the script: `sh dev_sync.sh` - - Or in one command: `export DEST_DIR=/path/to/your/roosevelt/app && sh dev_sync.sh` + - Run the script: `node dev_sync.js` + - Or in one command: `export DEST_DIR=/path/to/your/roosevelt/app && node dev_sync.js` + -For Windows + - Set a `DEST_DIR` environment variable: `$env:DEST_DIR="/path/to/your/roosevelt/app"` + - Run the script: `node dev_sync.js` + -To Exit the script + - To end the script press: `control^ + C` + - To end the script type: `Exit` or `Close` diff --git a/dev_sync.js b/dev_sync.js new file mode 100644 index 00000000..2c561c53 --- /dev/null +++ b/dev_sync.js @@ -0,0 +1,151 @@ +const DEST_DIR = process.env.DEST_DIR +const Rsync = require('rsync') +const SRC_DIR = __dirname +const fs = require('fs') +const { Glob } = require('glob') +const prompts = require('prompts') +const rosvltPath = `${SRC_DIR}/**/*.js` +const gitignoreScanner = require('./lib/tools/gitignoreScanner') +const gitignoreFiles = gitignoreScanner('./gitignore') +const glob = new Glob(rosvltPath, { ignore: 'node_modules/**' }) +const globalList = [] + +for (const file of glob) { + if (!gitignoreFiles.includes(file)) { + globalList.push(file) + } +} +function promptSetup (DEST_DIR) { + const Logger = require('roosevelt-logger') + this.logger = new Logger() + + try { + if (DEST_DIR === '' || DEST_DIR === undefined) { + this.logger.error('ERROR: DEST_DIR is an empty variable') + const questions = [ + { + type: 'text', + name: 'DEST_DIR', + message: 'Path to Roosevelt Sample App?' + } + ]; + (async () => { + const response = await prompts(questions) + DEST_DIR = response.DEST_DIR + fsClose(DEST_DIR) + })() + } else if (DEST_DIR === SRC_DIR) { + this.logger.error('ERROR: DEST_DIR is pointing to the same path as SRC_DIR ') + } else { + if (fs.existsSync(`${DEST_DIR}/rooseveltConfig.json`) || fs.existsSync(`${DEST_DIR}/node_modules/roosevelt/`)) { + fsWatch(DEST_DIR) + } else { + this.logger.info('') + this.logger.warn('Make sure the above directories are correct or this could delete unwanted files!') + this.logger.info('💭', 'We are not in a Roosevelt app ...') + this.logger.info('') + const questions = [ + { + type: 'text', + name: 'DEST_DIR', + message: 'Path to Roosevelt Sample App?' + } + ]; + (async () => { + const response = await prompts(questions) + DEST_DIR = response.DEST_DIR + fsClose(DEST_DIR) + })() + } + } + } catch (err) { console.log(err) } +} + +async function fsWatch (DEST_DIR) { + const Logger = require('roosevelt-logger') + this.logger = new Logger() + const watch = await import('watcher') + const Watcher = watch.default + const watcher = new Watcher(globalList, { recursive: true }) + + watcher.on('error', error => { + this.logger.err(error) + }) + + watcher.on('ready', () => { + this.logger.info('') + this.logger.info('') + this.logger.info('💭', 'Roosevelt fswatch rsync tool running...') + this.logger.info('') + this.logger.info('💭', `Now watching: ${SRC_DIR}`) + this.logger.info('💭', `Will copy to: ${DEST_DIR}/node_modules/roosevelt/`) + this.logger.info('') + }) + + watcher.on('change', filePath => { + const rosvlt = filePath.split('roosevelt')[1] + const rsync = new Rsync() + .flags('avz') + .delete() + .exclude('.DS_Store') + .source(filePath) + .destination(DEST_DIR + '/node_modules/roosevelt/' + rosvlt) + + rsync.execute(function (error, code, cmd) { + if (error) { + this.logger.error(`ERROR: ${error.message}`) + } + }) + }) + + const questions = [ + { + type: 'text', + name: 'INPUT', + message: 'Type "Exit" or "Close" to end fsWatcher"' + } + ]; + (async () => { + const response = await prompts(questions) + const input = response.INPUT + if (input === undefined) { + console.log('') + console.log('') + console.log('💭') + console.log('💭 Closing fswatch') + console.log('💭') + watcher.close() + process.exit() + } else if (input.toLowerCase() === 'exit' || input.toLowerCase() === 'close') { + console.log('') + console.log('') + console.log('💭') + console.log('💭 Closing fswatch') + console.log('💭') + watcher.close() + process.exit() + } + })() +} + +async function fsClose (DEST_DIR) { + if (DEST_DIR === undefined) { + console.log('') + console.log('') + console.log('💭') + console.log('💭 Closing fswatch') + console.log('💭') + process.exit() + } else if (DEST_DIR.toLowerCase() === 'exit' || DEST_DIR.toLowerCase() === 'close') { + console.log('') + console.log('💭') + console.log('💭 Closing fswatch') + console.log('💭') + console.log('') + process.exit() + } else { + promptSetup(DEST_DIR) + } +} + +promptSetup(DEST_DIR) diff --git a/dev_sync.sh b/dev_sync.sh deleted file mode 100644 index b20701ef..00000000 --- a/dev_sync.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -SRC_DIR="." - -echo "Roosevelt fswatch rsync tool running..." -echo "" -echo "Now watching:" $SRC_DIR/ -echo "Will copy to:" $DEST_DIR/node_modules/roosevelt/ -echo "" -echo "Make sure the above directories are correct or this could delete unwanted files!" -echo "" - -fswatch -0 $SRC_DIR | while read -d "" event -do - rsync -avz --delete --exclude=.DS_Store "$SRC_DIR/" "$DEST_DIR"/node_modules/roosevelt/ -done diff --git a/package-lock.json b/package-lock.json index 3a5a597f..920097e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,15 +42,19 @@ "codecov": "3.8.3", "eslint": "8.52.0", "eslint-plugin-mocha": "10.2.0", + "glob": "10.3.3", "less": "4.2.0", "mocha": "10.2.0", + "prompts": "2.4.2", "proxyquire": "2.1.3", + "rsync": "0.6.1", "sass": "1.69.5", "sinon": "17.0.0", "standard": "17.1.0", "stylus": "0.60.0", "supertest": "6.3.3", - "teddy": "0.6.4" + "teddy": "0.6.4", + "watcher": "2.3.0" }, "engines": { "node": ">=18.0.0" @@ -1995,6 +1999,12 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dettle": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dettle/-/dettle-1.0.1.tgz", + "integrity": "sha512-/oD3At60ZfhgzpofJtyClNTrIACyMdRe+ih0YiHzAniN0IZnLdLpEzgR6RtGs3kowxUkTnvV/4t1FBxXMUdusQ==", + "dev": true + }, "node_modules/dezalgo": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", @@ -6388,6 +6398,12 @@ "node": ">=0.1.90" } }, + "node_modules/rsync": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/rsync/-/rsync-0.6.1.tgz", + "integrity": "sha512-39HcwWuM67CQ9tHloazShXWUOWa2m3SGqX6XQhQMSj0VCQMkSI9PodoxM7/+hKf2p4v2umbhfoarYqd1gwII/w==", + "dev": true + }, "node_modules/run-parallel": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", @@ -7091,6 +7107,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/stubborn-fs": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/stubborn-fs/-/stubborn-fs-1.2.5.tgz", + "integrity": "sha512-H2N9c26eXjzL/S/K+i/RHHcFanE74dptvvjM8iwzwbVcWY/zjBbgRqF3K0DY4+OD+uTTASTBvDoxPDaPN02D7g==", + "dev": true + }, "node_modules/stubs": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", @@ -7439,6 +7461,12 @@ "next-tick": "1" } }, + "node_modules/tiny-readdir": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tiny-readdir/-/tiny-readdir-2.2.0.tgz", + "integrity": "sha512-bO4IgID5M2x5YQFBru/wREgT30YuA8aoOd/F8Rd6LKRIn1gOe9aREjT74J9EVukHqI/2RB+4SM1RgXM0gwxoWw==", + "dev": true + }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", @@ -7765,6 +7793,17 @@ "node": ">=0.10.48" } }, + "node_modules/watcher": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/watcher/-/watcher-2.3.0.tgz", + "integrity": "sha512-6hVpT1OhmYTZhsgUND2o2gTL79TosB1rH8DWzDO7KBlyR9Yuxg/LXUGeHJqjjvwpnyHT7uUdDwWczprJuqae9Q==", + "dev": true, + "dependencies": { + "dettle": "^1.0.1", + "stubborn-fs": "^1.2.5", + "tiny-readdir": "^2.2.0" + } + }, "node_modules/watchpack": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.0.tgz", diff --git a/package.json b/package.json index e5c6bf94..f6a4b3f4 100644 --- a/package.json +++ b/package.json @@ -56,15 +56,19 @@ "codecov": "3.8.3", "eslint": "8.52.0", "eslint-plugin-mocha": "10.2.0", + "glob": "10.3.3", "less": "4.2.0", "mocha": "10.2.0", + "prompts": "2.4.2", "proxyquire": "2.1.3", + "rsync": "0.6.1", "sass": "1.69.5", "sinon": "17.0.0", "standard": "17.1.0", "stylus": "0.60.0", "supertest": "6.3.3", - "teddy": "0.6.4" + "teddy": "0.6.4", + "watcher": "2.3.0" }, "repository": { "type": "git",