11#!/usr/bin/env node
22
3- import findUp from 'find-up' ;
4- import yargs from 'yargs' ;
5- import log from '../lib/log' ;
6-
7- import build from './build' ;
8- import newFile from './new' ;
9- import watch from './watch' ;
10- import clean from './clean' ;
11- import init from './init' ;
12- import serve from './serve' ;
13-
14- const commands = {
15- build,
16- new : newFile ,
17- watch,
18- clean,
19- init,
20- serve,
21- } ;
3+ const path = require ( 'path' ) ;
4+ const findUp = require ( 'find-up' ) ;
5+ const yargs = require ( 'yargs' ) ;
226
237yargs
248 . command ( 'init' , 'scaffold a new site' )
@@ -45,33 +29,36 @@ yargs
4529 . help ( 'help' )
4630 . default ( 'help' ) ;
4731
48- const argv = yargs . argv ;
32+ module . exports = function reptarCli ( { log, libPath } ) {
33+ const argv = yargs . argv ;
4934
50- process . stdout . write ( 'reptar\n\n' ) ;
35+ process . stdout . write ( 'reptar\n\n' ) ;
5136
52- if ( argv . version ) {
53- let packageJson ;
54- try {
55- // eslint-disable-next-line
56- packageJson = require ( findUp . sync ( 'package.json' , {
57- cwd : __dirname ,
58- } ) ) ;
59- } catch ( e ) { /* noop */ }
37+ if ( argv . version ) {
38+ let packageJson ;
39+ try {
40+ // eslint-disable-next-line
41+ packageJson = require ( findUp . sync ( 'package.json' , {
42+ cwd : __dirname ,
43+ } ) ) ;
44+ } catch ( e ) { /* noop */ }
6045
61- process . stdout . write ( packageJson . version ) ;
62- process . stdout . write ( '\n' ) ;
63- } else if ( argv . _ . length === 0 ) {
64- yargs . showHelp ( 'log' ) ;
65- process . exit ( 0 ) ;
66- } else {
67- const command = argv . _ [ 0 ] ;
68- const commandHandler = commands [ command ] ;
69-
70- if ( ! commandHandler ) {
71- log . error ( `Unknown command: ${ argv . _ . join ( ' ' ) } ` ) ;
46+ process . stdout . write ( packageJson . version ) ;
7247 process . stdout . write ( '\n' ) ;
73- yargs . showHelp ( ) ;
48+ } else if ( argv . _ . length === 0 ) {
49+ yargs . showHelp ( 'log' ) ;
50+ process . exit ( 0 ) ;
7451 } else {
75- commandHandler ( argv ) ;
52+ const command = argv . _ [ 0 ] ;
53+ const commandPath = path . join ( libPath , 'cli' , command ) ;
54+
55+ try {
56+ const commandHandler = require ( commandPath ) . default ; // eslint-disable-line
57+ commandHandler ( argv ) ;
58+ } catch ( e ) {
59+ log . error ( `Unknown command: ${ argv . _ . join ( ' ' ) } ` ) ;
60+ process . stdout . write ( '\n' ) ;
61+ yargs . showHelp ( ) ;
62+ }
7663 }
77- }
64+ } ;
0 commit comments