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

Commit

Permalink
feat: tweaks
Browse files Browse the repository at this point in the history
- Remove cli flags aliases
- Rename `pwd` into `cwd`
- Add `depth` support
- Add default name if it is not detected
- Ensure `watchFiles` is always an array
  • Loading branch information
Kikobeats committed Jun 3, 2019
1 parent a0c4478 commit 926d065
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 50 deletions.
5 changes: 2 additions & 3 deletions bin/cli/get-main-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
const loadJsonFile = require('load-json-file')
const path = require('path')

module.exports = ({ pwd, input }) => {
const pkg = loadJsonFile.sync(path.resolve(pwd, 'package.json'))
module.exports = ({ cwd, input }) => {
const pkg = loadJsonFile.sync(path.resolve(cwd, 'package.json'))
const { main: mainFile = 'index.js' } = pkg
const [filename = mainFile] = input

return { filename, pkg }
}
30 changes: 13 additions & 17 deletions bin/cli/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@ const chalk = require('chalk')
module.exports = `
${chalk.bold('Usage')}
$ svr [path][options]
$ svr ${chalk.gray('[path][options]')}
${chalk.bold('Options')}
-p, --port Port to listen on ${chalk.gray('[default=3000]')}
-H, --host The host on which svr will run ${chalk.gray("[default='localhost']")}
-d, --pwd A directory to start ${chalk.gray('[default=cwd]')}
-L, --poll Poll for code changes rather than using events ${chalk.gray(
'[default=false]'
--depth If set, limits how many levels of subdirectories will be traversed. ${chalk.gray(
'[default=undefined]'
)}
-i, --ignore Ignore watching a file, directory, or glob ${chalk.gray(
--help Show this usage information
--host The host on which svr will run ${chalk.gray("[default='localhost']")}
--ignore Ignore watching a file, directory, or glob ${chalk.gray(
'[default=.gitignore, pkg.ignore]'
)}
-w, --watch Add more files than the project path to watch
-v, --version Output the version number
-h, --help Show this usage information
--poll Poll for code changes rather than using events ${chalk.gray('[default=false]')}
--port Port to listen on ${chalk.gray('[default=3000]')}
--pwd A directory to start ${chalk.gray('[default=process.cwd()]')}
--version Output the version number
--watch Add more files than the project path to watch ${chalk.gray(
'[default=process.cwd()]'
)}
${chalk.bold('Examples')}
Expand Down
22 changes: 10 additions & 12 deletions bin/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,44 @@ const cli = require('meow')(require('./help'), {
flags: {
port: {
type: 'number',
alias: 'p',
default: process.env.port || process.env.PORT || 3000
},
poll: {
type: 'boolean',
alias: 'L',
default: false
},
ignore: {
alias: 'i',
type: 'array',
default: []
},
host: {
alias: 'H',
default: '::'
},
watch: {
alias: 'w',
type: 'array',
default: []
},
pwd: {
depth: {
type: 'number',
default: undefined
},
cwd: {
default: process.cwd()
}
}
})
;(async () => {
const { input } = cli
const { pwd, port, watch: watchFiles, ...opts } = cli.flags
const { filename, pkg } = getMainFile({ input, pwd })

const filepath = path.resolve(pwd, filename)
const { cwd, port, watch: watchFiles, ...opts } = cli.flags
const { filename, pkg } = getMainFile({ input, cwd })
const filepath = path.resolve(cwd, filename)

require('../serve')({
filepath,
pkg,
pwd,
cwd,
port,
watchFiles,
watchFiles: Array.isArray(watchFiles) ? watchFiles : [watchFiles],
...opts
})
})()
2 changes: 1 addition & 1 deletion bin/serve/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = async ({ userPort, inUse, pkg, port, host, restarting, filepath
server = app.listen(port, host, () => {
if (!restarting) {
const message = listenMessage({
appName: pkg.name,
appName: pkg.name || 'svr',
userPort,
port,
inUse
Expand Down
16 changes: 9 additions & 7 deletions bin/watch/get-watch-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const ignoredDirectories = require('ignore-by-default').directories()
const getIgnoredFromGit = require('ignored')
const path = require('path')

const getIgnoredFiles = ({ ignore = [], pkg, pwd }) => {
const getIgnoredFiles = ({ ignore = [], pkg, cwd }) => {
const set = new Set()
const addIgnore = value => set.add(value)

const gitignore = path.resolve(pwd, '.gitignore')
const gitignore = path.resolve(cwd, '.gitignore')
getIgnoredFromGit(gitignore).forEach(addIgnore)
;[].concat(ignore).forEach(addIgnore)
if (pkg.ignore) pkg.ignore.forEach(addIgnore)
Expand All @@ -18,19 +18,21 @@ const getIgnoredFiles = ({ ignore = [], pkg, pwd }) => {
const rawIgnored = Array.from(set)

return rawIgnored.reduce((acc, ignore) => {
const file = path.resolve(pwd, ignore)
const file = path.resolve(cwd, ignore)
acc.push(file)
return acc
}, [])
}

module.exports = ({ poll: usePolling, ...opts }) => {
const ignored = getIgnoredFiles(opts)
module.exports = ({ cwd, depth, poll: usePolling, ...opts }) => {
const ignored = getIgnoredFiles({ cwd, ...opts })

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

return watchConfig
Expand Down
14 changes: 7 additions & 7 deletions bin/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const doRestart = ({
filename,
filepath,
pkg,
pwd,
cwd,
forcing,
cli,
watchFiles,
Expand All @@ -43,22 +43,22 @@ const doRestart = ({
filepath,
filename,
pkg,
pwd,
cwd,
cli,
watcher,
port
})
)
}

module.exports = ({ filepath, watchFiles, pwd, pkg, server, sockets, ...opts }) => {
module.exports = ({ filepath, watchFiles, cwd, pkg, server, sockets, ...opts }) => {
const watchConfig = getWatchConfig({
pwd,
cwd,
pkg,
...opts
})

const toWatch = [].concat(watchFiles, pwd)
const toWatch = [].concat(watchFiles, cwd)
const watcher = watch(toWatch, watchConfig)

const restart = ({ forcing, filename }) =>
Expand All @@ -69,7 +69,7 @@ module.exports = ({ filepath, watchFiles, pwd, pkg, server, sockets, ...opts })
server,
filename,
filepath,
pwd,
cwd,
pkg,
forcing,
watcher,
Expand All @@ -79,7 +79,7 @@ module.exports = ({ filepath, watchFiles, pwd, pkg, server, sockets, ...opts })
watcher.once(
'all',
debounce(
(event, filename) => restart({ forcing: false, filename: path.relative(pwd, filename) }),
(event, filename) => restart({ forcing: false, filename: path.relative(cwd, filename) }),
10
)
)
Expand Down
6 changes: 3 additions & 3 deletions bin/watch/restart-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = ({
filepath,
filename,
pkg,
pwd,
cwd,
cli,
watchFiles,
port,
Expand All @@ -36,7 +36,7 @@ module.exports = ({

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

// Remove file that changed from the `require` cache
for (const item of toDelete) {
Expand All @@ -56,7 +56,7 @@ module.exports = ({
port,
filepath,
pkg,
pwd,
cwd,
cli,
restarting: true,
watchFiles
Expand Down

0 comments on commit 926d065

Please sign in to comment.