Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 25, 2017
1 parent 627ccc5 commit 8bf43fd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
8 changes: 4 additions & 4 deletions bin/cli/get-main-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const jsonFuture = require('json-future')
const path = require('path')

module.exports = cli => {
const filepkg = jsonFuture.load(path.resolve(process.cwd(), 'package.json'))
const filenameFallback = filepkg.main || 'index.js'
const [filename = filenameFallback] = cli.input
const pkg = jsonFuture.load(path.resolve(process.cwd(), 'package.json'))
const { main: mainFile = 'index.js' } = pkg
const [filename = mainFile] = cli.input

return { filename, filepkg }
return { filename, pkg }
}
4 changes: 2 additions & 2 deletions bin/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const cli = require('meow')(
)
;(async () => {
try {
const { filename, filepkg } = getMainFile(cli)
require('../serve')({ filename, filepkg, cli, restarting: false })
const { filename, pkg } = getMainFile(cli)
require('../serve')({ filename, pkg, cli, restarting: false })
} catch (err) {
log.error(err.message || err)
process.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions bin/serve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path')
const listenMessage = require('./listen-message')
const getPort = require('./get-port')

module.exports = async ({ filename, filepkg, cli, restarting }) => {
module.exports = async ({ filename, pkg, cli, restarting }) => {
const { userPort, port, inUse } = await getPort(cli)

const filepath = path.resolve(process.cwd(), filename)
Expand All @@ -20,7 +20,7 @@ module.exports = async ({ filename, filepkg, cli, restarting }) => {
const server = app.listen(port, () => {
if (!restarting) {
const message = listenMessage({
appName: filepkg.name,
appName: pkg.name,
port,
inUse,
userPort
Expand All @@ -36,5 +36,5 @@ module.exports = async ({ filename, filepkg, cli, restarting }) => {
socket.once('close', () => sockets.splice(index, 1))
})

require('../watch')({ filename, filepkg, server, cli, sockets })
require('../watch')({ filename, pkg, server, cli, sockets })
}
27 changes: 19 additions & 8 deletions bin/watch/get-watch-config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
'use strict'

const getIgnoredFromGit = require('ignored')
const ignoredDirectories = require('ignore-by-default').directories()
const { existsSync, statSync } = require('fs')
const getIgnoredFromGit = require('ignored')
const path = require('path')

const getIgnoredFiles = ({ cli, filepkg }) => {
const isDirSync = path => existsSync(path) && statSync(path).isDirectory()

const getIgnoredFiles = ({ cli, pkg }) => {
const set = new Set()
const addIgnore = set.add.bind(set)
const addIgnore = value => set.add(value)

const gitignore = path.resolve(process.cwd(), '.gitignore')
getIgnoredFromGit(gitignore).forEach(addIgnore)

if (cli.flags.ignore) cli.flags.ignore.forEach(addIgnore)
if (filepkg.ignore) filepkg.ignore.forEach(addIgnore)
if (pkg.ignore) pkg.ignore.forEach(addIgnore)

ignoredDirectories.forEach(addIgnore)

return Array.from(set)
const rawIgnored = Array.from(set)

const ignored = rawIgnored.reduce((acc, ignore) => {
const file = path.resolve(process.cwd(), ignore)
acc.push(isDirSync(file) ? `**/${path.basename(file)}/**` : ignore)
return acc
}, [])

return { ignored, rawIgnored }
}

module.exports = ({ cli, filepkg }) => {
module.exports = ({ cli, pkg }) => {
const { poll: usePolling } = cli.flags
const ignored = getIgnoredFiles({ cli, filepkg })
const { ignored, rawIgnored } = getIgnoredFiles({ cli, pkg })

const watchConfig = {
usePolling,
ignoreInitial: true,
ignored
}

return watchConfig
return { watchConfig, ignored, rawIgnored }
}
59 changes: 42 additions & 17 deletions bin/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const getTimestamp = require('time-stamp')
const logSymbols = require('log-symbols')
const { watch } = require('chokidar')
const debounce = require('debounce')
const { watch } = require('chokidar')
const chalk = require('chalk')

const destroySockets = require('./destroy-sockets')
const getWatchConfig = require('./get-watch-config')
const destroySockets = require('./destroy-sockets')
const restartServer = require('./restart-server')

let isListenRestart = false
let firsTime = false

const logRestart = ({ filename, forcing }) => {
const offset = ' '
Expand All @@ -21,28 +21,53 @@ const logRestart = ({ filename, forcing }) => {
console.log(`${offset} ${symbol} ${timestamp} ${header} ${message}`)
}

module.exports = ({ filename, filepkg, server, cli, sockets }) => {
const watchConfig = getWatchConfig({ cli, filepkg })
const watcher = watch('.', watchConfig)
const doRestart = ({
ignored,
sockets,
server,
filename,
pkg,
forcing,
cli,
watcher
}) => {
logRestart({ filename, forcing })
destroySockets(sockets)
server.close(
restartServer.bind(this, { ignored, filename, pkg, cli, watcher })
)
}

module.exports = ({ filename, pkg, server, cli, sockets }) => {
const { watchConfig, rawIgnored: ignored } = getWatchConfig({ cli, pkg })
const watcher = watch(process.cwd(), watchConfig)

const doRestart = ({ filename, forcing }) => {
logRestart({ filename, forcing })
destroySockets(sockets)
server.close(restartServer.bind(this, { filename, filepkg, cli, watcher }))
}
const restart = ({ forcing }) =>
doRestart({
ignored,
sockets,
server,
filename,
pkg,
forcing,
cli,
watcher
})

watcher.once(
'all',
debounce((event, filename) => doRestart({ filename, forcing: false })),
debounce((event, filename) => restart({ forcing: false })),
10
)

if (!isListenRestart) {
isListenRestart = true

if (!firsTime) {
firsTime = true
process.stdin.on('data', data => {
const text = data.toString().trim().toLowerCase()
if (text === 'rs') doRestart({ filename, forcing: true })
const text = data
.toString()
.trim()
.toLowerCase()
if (text === 'rs') restart({ forcing: true })
})
}
}
11 changes: 8 additions & 3 deletions bin/watch/restart-server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const clearModule = require('clear-module')
const anymatch = require('anymatch')
const path = require('path')

module.exports = ({ filename, filepkg, cli, watcher }) => {
module.exports = ({ filename, pkg, cli, watcher, ignored }) => {
const watched = watcher.getWatched()
let toDelete = []

Expand All @@ -20,7 +21,11 @@ module.exports = ({ filename, filepkg, cli, watcher }) => {
}
}

toDelete = toDelete.map(filename => path.resolve(process.cwd(), filename))
const matchers = anymatch(ignored)

toDelete = toDelete
.filter(filename => !matchers(path.basename(filename)))
.map(filename => path.resolve(process.cwd(), filename))

// Remove file that changed from the `require` cache
for (const item of toDelete) {
Expand All @@ -36,5 +41,5 @@ module.exports = ({ filename, filepkg, cli, watcher }) => {
}

// Restart the server
require('../serve')({ filename, filepkg, cli, restarting: true })
require('../serve')({ filename, pkg, cli, restarting: true })
}

0 comments on commit 8bf43fd

Please sign in to comment.