@@ -2,16 +2,23 @@ const Koa = require('koa')
22const serve = require ( 'koa-static' )
33const Router = require ( '@koa/router' )
44const portfinder = require ( 'portfinder' )
5+ const program = require ( 'commander' )
56const path = require ( 'path' )
67const open = require ( 'open' )
78const 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+
917const app = new Koa ( )
1018const router = new Router ( )
11- const port = 3000
1219
1320router . 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 )
2431 . use ( router . allowedMethods ( ) )
2532
2633portfinder . 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