@@ -5,6 +5,7 @@ const program = require('commander');
55const inquirer = require ( 'inquirer' ) ;
66const chalk = require ( 'chalk' ) ;
77const latestSemver = require ( 'latest-semver' ) ;
8+ const os = require ( 'os' ) ;
89
910const createInstantSearchApp = require ( '../api' ) ;
1011const {
@@ -25,14 +26,14 @@ const {
2526} = require ( './getConfiguration' ) ;
2627const { version } = require ( '../../package.json' ) ;
2728
28- let appPath ;
29+ let appPathFromArgument ;
2930let options = { } ;
3031
3132program
3233 . version ( version , '-v, --version' )
3334 . arguments ( '<project-directory>' )
3435 . usage ( `${ chalk . green ( '<project-directory>' ) } [options]` )
35- . option ( '--name <name>' , 'The name of the application' )
36+ . option ( '--name <name>' , 'The name of the application or widget ' )
3637 . option ( '--app-id <appId>' , 'The application ID' )
3738 . option ( '--api-key <apiKey>' , 'The Algolia search API key' )
3839 . option ( '--index-name <indexName>' , 'The main index of your search' )
@@ -49,52 +50,18 @@ program
4950 . option ( '--config <config>' , 'The configuration file to get the options from' )
5051 . option ( '--no-installation' , 'Ignore dependency installation' )
5152 . action ( ( dest , opts ) => {
52- appPath = dest ;
53+ appPathFromArgument = dest ;
5354 options = opts ;
5455 } )
5556 . parse ( process . argv ) ;
5657
57- if ( ! appPath ) {
58- console . log ( 'Please specify the project directory:' ) ;
59- console . log ( ) ;
60- console . log (
61- ` ${ chalk . cyan ( 'create-instantsearch-app' ) } ${ chalk . green (
62- '<project-directory>'
63- ) } `
64- ) ;
65- console . log ( ) ;
66- console . log ( 'For example:' ) ;
67- console . log (
68- ` ${ chalk . cyan ( 'create-instantsearch-app' ) } ${ chalk . green (
69- 'my-instantsearch-app'
70- ) } `
71- ) ;
72- console . log ( ) ;
73- console . log (
74- `Run ${ chalk . cyan ( 'create-instantsearch-app --help' ) } to see all options.`
75- ) ;
76-
77- process . exit ( 1 ) ;
78- }
79-
80- const optionsFromArguments = getOptionsFromArguments ( options . rawArgs ) ;
81- const appName = optionsFromArguments . name || path . basename ( appPath ) ;
58+ const optionsFromArguments = getOptionsFromArguments ( options . rawArgs || [ ] ) ;
8259const attributesToDisplay = ( optionsFromArguments . attributesToDisplay || '' )
8360 . split ( ',' )
8461 . filter ( Boolean )
8562 . map ( x => x . trim ( ) ) ;
8663
87- try {
88- checkAppPath ( appPath ) ;
89- checkAppName ( appName ) ;
90- } catch ( err ) {
91- console . error ( err . message ) ;
92- console . log ( ) ;
93-
94- process . exit ( 1 ) ;
95- }
96-
97- const questions = {
64+ const getQuestions = ( { appName } ) => ( {
9865 application : [
9966 {
10067 type : 'list' ,
@@ -212,9 +179,55 @@ const questions = {
212179 } ,
213180 } ,
214181 ] ,
215- } ;
182+ } ) ;
216183
217184async function run ( ) {
185+ let appPath = appPathFromArgument ;
186+ if ( ! appPath ) {
187+ const answers = await inquirer . prompt ( [
188+ {
189+ type : 'input' ,
190+ name : 'appPath' ,
191+ message : 'Project directory' ,
192+ } ,
193+ ] ) ;
194+ appPath = answers . appPath ;
195+ }
196+ if ( appPath . startsWith ( '~/' ) ) {
197+ appPath = path . join ( os . homedir ( ) , appPath . slice ( 2 ) ) ;
198+ }
199+ try {
200+ checkAppPath ( appPath ) ;
201+ } catch ( err ) {
202+ console . error ( err . message ) ;
203+ console . log ( ) ;
204+
205+ process . exit ( 1 ) ;
206+ }
207+
208+ let appName = optionsFromArguments . name ;
209+ if ( ! appName ) {
210+ appName = (
211+ await inquirer . prompt ( [
212+ {
213+ type : 'input' ,
214+ name : 'appName' ,
215+ message : 'The name of the application or widget' ,
216+ default : path . basename ( appPath ) ,
217+ } ,
218+ ] )
219+ ) . appName ;
220+ }
221+
222+ try {
223+ checkAppName ( appName ) ;
224+ } catch ( err ) {
225+ console . error ( err . message ) ;
226+ console . log ( ) ;
227+
228+ process . exit ( 1 ) ;
229+ }
230+
218231 console . log ( ) ;
219232 console . log ( `Creating a new InstantSearch app in ${ chalk . green ( appPath ) } .` ) ;
220233 console . log ( ) ;
@@ -264,7 +277,7 @@ async function run() {
264277 templateConfig . category === 'Widget' ? 'widget' : 'application' ;
265278
266279 const answers = await inquirer . prompt (
267- questions [ implementationType ] . filter ( question =>
280+ getQuestions ( { appName } ) [ implementationType ] . filter ( question =>
268281 isQuestionAsked ( { question, args : optionsFromArguments } )
269282 ) ,
270283 { ...optionsFromArguments , template }
0 commit comments