-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·85 lines (70 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env node
import { Builder } from "selenium-webdriver"
import { Options } from "selenium-webdriver/chrome.js"
import chokidar from 'chokidar'
import chalk from 'chalk'
function logInfo(message) {
console.info(`[INFO] ${message}`)
}
function logWarn(message) {
console.warn(chalk.yellow(`[WARN] ${message}`))
}
function logError(message) {
console.error(chalk.red(`[ERROR] ${message}`))
}
function logSuccess(message) {
console.info(chalk.green(`[SUCCESS] ${message}`))
}
function logDebug(message) {
console.log(chalk.magenta(`[DEBUG] ${message}`))
}
function buildWatchPaths(config) {
const watchPaths = config.watchFolders
.flatMap(wf => config.watchFileExtensions.map(ext => `${wf}/**/*.${ext}`))
.map(e => e.replace('//', '/'))
if (config.debug) {
logDebug(`Paths to watch:\n${watchPaths.join('\n')}`)
}
return watchPaths
}
logInfo('Starting Habanero Refresh...')
let conf = {
browser: 'chrome',
url: 'http://localhost:8080',
watchFolders: ['./'],
watchFileExtensions: ['html', 'css', 'js', 'twig', 'jinja', 'php', 'py'],
ignored: ['node_modules', '.git'],
debug: false
}
await import(process.cwd() + '/habanero-refresh.conf.mjs')
.then((importedConf => conf = {...conf, ...importedConf.default}))
.catch(() => logInfo('No config provided, loading defaults...'))
if (conf.debug) {
const configText = Object.entries(conf).map(e => `\n${e[0]}: ${e[1]}`).join('')
logDebug(`Used configuration:${configText}`)
}
const driver = await new Builder()
.forBrowser(conf.browser)
.setChromeOptions(new Options().excludeSwitches('enable-automation'))
.build()
.catch((e) => {
if (conf.debug) {
console.error(e)
process.exit(1)
} else {
logError(`Could not open browser "${conf.browser}", is a suitable WebDriver in your PATH?`)
process.exit(1)
}
})
await driver.navigate().to(conf.url).catch(() => {
logWarn(`Could not open URL "${conf.url}"...`)
})
const fileWatcher = chokidar.watch(buildWatchPaths(conf))
fileWatcher.on('change', () => driver.navigate().refresh())
const interval = setInterval(() => {
driver.getAllWindowHandles().catch(() => {
logInfo("Exiting Habanero Refresh...")
fileWatcher.close()
clearInterval(interval)
})}, 1000)
logSuccess('Habanero Refresh is up and running, productive coding!')