Skip to content

Commit 17c64fd

Browse files
committed
Add cli config options
1 parent 8316062 commit 17c64fd

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

package-lock.json

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"@koa/router": "^9.0.1",
30+
"commander": "^6.0.0",
3031
"core-js": "^2.6.5",
3132
"koa": "^2.12.0",
3233
"koa-static": "^5.0.0",
@@ -45,4 +46,4 @@
4546
"eslint-plugin-vue": "^5.0.0",
4647
"vue-template-compiler": "^2.5.21"
4748
}
48-
}
49+
}

server/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ const Koa = require('koa')
22
const serve = require('koa-static')
33
const Router = require('@koa/router')
44
const portfinder = require('portfinder')
5+
const program = require('commander')
56
const path = require('path')
67
const open = require('open')
78
const resolveConfig = require('tailwindcss/resolveConfig')
89

10+
// Setup CLI options
11+
program
12+
.option('-p, --port <port>', 'Port to run the viewer on', 3000)
13+
.option('-o, --open', 'Open the viewer in default browser')
14+
.option('-c, --config <path>', 'Path to your Tailwind config file', 'tailwind.config.js')
15+
.parse(process.argv)
16+
917
const app = new Koa()
1018
const router = new Router()
11-
const port = 3000
1219

1320
router.get('/config', (ctx) => {
14-
const tailwindConfigPath = path.resolve(process.argv[2] || (process.cwd(), 'tailwind.config.js'))
21+
const tailwindConfigPath = path.resolve(process.cwd(), program.config)
1522
// delete the require cache of tailwind config so we can pick up new changes
1623
delete require.cache[tailwindConfigPath]
1724
const tailwindConfig = require(tailwindConfigPath)
@@ -24,16 +31,15 @@ app
2431
.use(router.allowedMethods())
2532

2633
portfinder.getPort({
27-
port: 3000
34+
port: program.port
2835
}, (err, port) => {
2936
if (err) {
3037
throw (err)
3138
}
3239

3340
app.listen(port, async () => {
34-
console.log(__dirname)
35-
console.log('Server Started ∹ http://localhost:' + port.toString())
36-
open('http://localhost:' + port.toString())
41+
console.log('Server Started ∹ http://localhost:' + program.port.toString())
42+
if (program.open) open('http://localhost:' + program.port.toString())
3743
})
3844
})
3945

0 commit comments

Comments
 (0)