11#! /usr/bin/env node
2- ' use strict' ;
3- const fs = require ( ' fs-extra' ) ;
4- const path = require ( ' path' ) ;
5- const yargv = require ( ' yargs' ) ;
6- const validateNPM = require ( ' validate-npm-package-name' ) ;
7- const execFile = require ( ' child_process' ) . execFile ;
8- const prompt = require ( ' prompt' ) ;
9- const filepath = require ( ' filepath' ) ;
10- const validatePath = require ( ' ./lib/helpers' ) . validatePath ;
11- const isRelativePath = require ( ' is-relative' ) ;
12- const colors = require ( ' colors/safe' ) ;
2+ " use strict" ;
3+ const fs = require ( " fs-extra" ) ;
4+ const path = require ( " path" ) ;
5+ const yargs = require ( " yargs" ) ;
6+ const validateNPM = require ( " validate-npm-package-name" ) ;
7+ const execFile = require ( " child_process" ) . execFile ;
8+ const prompt = require ( " prompt" ) ;
9+ const filepath = require ( " filepath" ) ;
10+ const validatePath = require ( " ./lib/helpers" ) . validatePath ;
11+ const isRelativePath = require ( " is-relative" ) ;
12+ const colors = require ( " colors/safe" ) ;
1313
1414// configure prompt
15- prompt . message = '' ;
16- prompt . delimiter = colors . green ( ':' ) ;
15+ prompt . message = "" ;
16+ prompt . delimiter = colors . green ( ":" ) ;
1717// set default values
18- const defaultNPMName = ' my-staffbase-backend' ;
19- const scaffoldFolder = path . resolve ( __dirname , ' ./scaffoldTpl' ) ;
18+ const defaultNPMName = " my-staffbase-backend" ;
19+ const scaffoldFolder = path . resolve ( __dirname , " ./scaffoldTpl" ) ;
2020
21- yargv
22- . usage ( 'Usage: create-staffbase-sso-server <project-directory> [Options]' )
23- . alias ( 'name' , 'N' )
24- . string ( 'name' )
25- . describe ( 'name' , 'a sepcific package.json name of your app' )
26- . version ( '0.0.1' )
27- . help ( 'help' )
28- . epilogue ( `for more information,\please see the README at:
29- https://github.com/Staffbase/create-staffbase-plugin-nodejs/blob/main/README.MD` ) ;
30- // console.log('YARGS Parsed Data:\n', yargv.argv);
31- const packageJSON = fs . readJSONSync ( path . join ( scaffoldFolder , 'package.json' ) ) ;
21+ const yargv = yargs ( process . argv . slice ( 2 ) )
22+ . usage ( "Usage: create-staffbase-sso-server <project-directory> [Options]" )
23+ . alias ( "name" , "N" )
24+ . string ( "name" )
25+ . describe ( "name" , "a specific package.json name of your app" )
26+ . version ( "0.0.1" )
27+ . help ( "help" )
28+ . epilogue ( `for more information,\please see the README at:
29+ https://github.com/Staffbase/create-staffbase-plugin-nodejs/blob/main/README.MD` )
30+ . argv ;
31+ // console.log('YARGS Parsed Data:\n', yargv);
32+ const packageJSON = fs . readJSONSync ( path . join ( scaffoldFolder , "package.json" ) ) ;
3233// Defaults package name to current folder name
33- const nameParam = yargv . argv . N || yargv . argv . name || defaultNPMName ;
34+ const nameParam = yargv . N || yargv . name || defaultNPMName ;
3435prompt . override = {
35- name : yargv . argv . N ,
36- path : yargv . argv . _ [ 0 ] ,
36+ name : yargv . N ,
37+ path : yargv . _ [ 0 ] ,
3738} ;
3839/**
3940 * Prompts user for just the npm name value.
@@ -44,25 +45,24 @@ function promptName(name) {
4445 const namePromptSchema = {
4546 properties : {
4647 name : {
47- description : ' What is the npm name of your plugin?' ,
48- type : ' string' ,
48+ description : " What is the npm name of your plugin?" ,
49+ type : " string" ,
4950 default : name ,
50- message : ' Name must be npm.js compatible' ,
51+ message : " Name must be npm.js compatible" ,
5152 required : true ,
52- conform : function ( value ) {
53+ conform : function ( value ) {
5354 return validateNPM ( value ) . validForNewPackages ;
5455 } ,
5556 } ,
5657 } ,
5758 } ;
58- return new Promise ( function ( resolve , reject ) {
59- prompt . start ( )
60- . get ( namePromptSchema , function ( err , res ) {
61- if ( err ) {
62- return reject ( err ) ;
63- }
64- return resolve ( res ) ;
65- } ) ;
59+ return new Promise ( function ( resolve , reject ) {
60+ prompt . start ( ) . get ( namePromptSchema , function ( err , res ) {
61+ if ( err ) {
62+ return reject ( err ) ;
63+ }
64+ return resolve ( res ) ;
65+ } ) ;
6666 } ) ;
6767}
6868/**
@@ -75,33 +75,34 @@ function promptPath(promtedName) {
7575 const pathPromptSchema = {
7676 properties : {
7777 path : {
78- description : ' Please enter the folder path for the App' ,
79- type : ' string' ,
78+ description : " Please enter the folder path for the new plugin project" ,
79+ type : " string" ,
8080 message :
81- ' Entered path is invalid or an already present file on the File System. Please enter a correct filepath,' ,
81+ " Entered path is invalid or an already present file on the File System. Please enter a correct filepath." ,
8282 default : defPath ,
8383 required : true ,
8484 conform : validatePath ,
8585 } ,
8686 override : {
8787 message :
88- colors . yellow ( 'The directory you specified already exists. It directory will be overridden!' ) +
89- '\nDo you wish to proceed (y)es|(n)o?' ,
88+ colors . yellow (
89+ "The directory you specified already exists. The directory will be overridden!"
90+ ) + "\nDo you wish to proceed (y)es|(n)o?" ,
9091 validator : / y [ e s ] * | n [ o ] ? / ,
91- warning : ' Must respond yes or no' ,
92- default : ' yes' ,
93- ask : function ( ) {
92+ warning : " Must respond yes or no" ,
93+ default : " yes" ,
94+ ask : function ( ) {
9495 let chkPath = defPath ;
95- if ( prompt . history ( ' path' ) ) {
96- chkPath = prompt . history ( ' path' ) . value ;
96+ if ( prompt . history ( " path" ) ) {
97+ chkPath = prompt . history ( " path" ) . value ;
9798 }
9899 return filepath . create ( chkPath ) . exists ( ) ;
99100 } ,
100101 } ,
101102 } ,
102103 } ;
103- return new Promise ( function ( resolve , reject ) {
104- prompt . get ( pathPromptSchema , function ( err , res ) {
104+ return new Promise ( function ( resolve , reject ) {
105+ prompt . get ( pathPromptSchema , function ( err , res ) {
105106 if ( err ) {
106107 return reject ( err ) ;
107108 }
@@ -111,33 +112,32 @@ function promptPath(promtedName) {
111112}
112113
113114/**
114- * Copy contants from the Scaffold Template to the specified folder
115- * @param {String } dstDir THe destination directory where files are to be copied
115+ * Copy contents from the Scaffold Template to the specified folder
116+ * @param {String } dstDir The destination directory where files are to be copied
116117 * @return {Promise } Promise resolved when the copy process is complete. Rejected
117118 * if there is some error in copying files.
118119 */
119120function copyContents ( dstDir ) {
120121 // console.log(colors.blue('Copying from:' + path.resolve(__dirname, './scaffoldTpl')));
121- const scaffoldFolder = path . resolve ( __dirname , ' ./scaffoldTpl' ) ;
122+ const scaffoldFolder = path . resolve ( __dirname , " ./scaffoldTpl" ) ;
122123 return fs . copy ( scaffoldFolder , dstDir ) ;
123124}
124125/**
125- * Repace the package.json file from copied fromplate to the new generated one.
126+ * Replace the package.json file from copied template to the new generated one.
126127 * @return {Promise } Promise resolved when the Package.json is successfully replaced.
127128 * @param {String } dstPath The path of the folder where the package.json needs to be replaced
128129 * @param {String } nameVal name value that should be replaced
129130 * Rejected if there is some error in creating new Package.json file.
130131 */
131132function replacePackageJSON ( dstPath , nameVal ) {
132133 // console.log("replacePackageJSON");
133- const newPackageJSON = Object . assign ( { } , packageJSON , { name : nameVal } ) ;
134+ const newPackageJSON = Object . assign ( { } , packageJSON , { name : nameVal } ) ;
134135 const curDir = path . resolve ( dstPath ) ;
135- const packagePath = path . resolve ( path . join ( curDir , 'package.json' ) ) ;
136- return fs . remove ( packagePath )
137- . then ( function ( data ) {
138- // console.log(colors.yellow('Writing json...'));
139- return fs . writeJson ( packagePath , newPackageJSON , { spaces : 2 } ) ;
140- } ) ;
136+ const packagePath = path . resolve ( path . join ( curDir , "package.json" ) ) ;
137+ return fs . remove ( packagePath ) . then ( function ( data ) {
138+ // console.log(colors.yellow('Writing json...'));
139+ return fs . writeJson ( packagePath , newPackageJSON , { spaces : 2 } ) ;
140+ } ) ;
141141}
142142/**
143143 * Installs the node modules in the folder where the template was created.
@@ -146,12 +146,12 @@ function replacePackageJSON(dstPath, nameVal) {
146146 * @return {Promise } Promise resolved when the packages are successfulyl installed.
147147 */
148148function installDeps ( dstPath ) {
149- console . log ( colors . italic ( ' \nInstalling dependencies...' ) ) ;
149+ console . log ( colors . italic ( " \nInstalling dependencies..." ) ) ;
150150 const opts = {
151151 cwd : path . resolve ( dstPath ) ,
152152 } ;
153- return new Promise ( ( resolve , reject ) => {
154- execFile ( ' npm' , [ ' install' ] , opts , ( err , stdout , stderr ) => {
153+ return new Promise ( ( resolve , reject ) => {
154+ execFile ( " npm" , [ " install" ] , opts , ( err , stdout , stderr ) => {
155155 // console.log(colors.red('Inside Child result', stderr, stdout, err));
156156 if ( err ) {
157157 // console.log(colors.red(err));
@@ -170,11 +170,10 @@ function installDeps(dstPath) {
170170function removeExistingFolder ( dstPath ) {
171171 const fp = filepath . create ( dstPath ) ;
172172 if ( fp . exists ( ) ) {
173- return fs . remove ( fp . toString ( ) )
174- . then ( function ( ) {
175- console . log ( colors . red ( 'Removing existing folder and its contents...' ) ) ;
176- return dstPath ;
177- } ) ;
173+ return fs . remove ( fp . toString ( ) ) . then ( function ( ) {
174+ console . log ( colors . red ( "Removing existing folder and its contents..." ) ) ;
175+ return dstPath ;
176+ } ) ;
178177 } else {
179178 return Promise . resolve ( dstPath ) ;
180179 }
@@ -183,56 +182,58 @@ function removeExistingFolder(dstPath) {
183182const promptRes = { } ;
184183// promot package name
185184promptName ( nameParam )
186- // prompt file path
187- . then ( function ( pathResp ) {
188- const nameRecv = pathResp . name ;
189- Object . assign ( promptRes , pathResp ) ;
190- return promptPath ( nameRecv ) ;
191- } )
192- // remove the folder if it exists
193- . then ( function ( pathResp ) {
194- if ( pathResp . override === 'n' || pathResp . override === 'no' ) {
195- return Promise . reject ( console . log ( colors . green ( 'Good Bye!' ) ) ) ;
196- }
197- Object . assign ( promptRes , pathResp ) ;
198- let pathRecv = pathResp . path ;
199- // if the entered path is relative, resolve to absolute
200- if ( isRelativePath ( pathRecv ) ) {
201- pathRecv = path . resolve ( path . join ( process . cwd ( ) , pathRecv ) ) ;
202- promptRes . path = pathRecv ;
203- }
204- return removeExistingFolder ( pathRecv ) ;
205- } )
206- // copy contents to folder
207- . then ( ( pathRecv ) => {
208- return ( copyContents ( pathRecv ) ) ;
209- } )
210- // replace package.json with new one
211- . then ( ( res ) => {
212- return replacePackageJSON ( promptRes . path , promptRes . name ) ;
213- } )
214- // install npm dependencies
215- . then ( ( res ) => {
216- return installDeps ( promptRes . path ) ;
217- } )
218- // output end results
219- . then ( function ( npmOutput ) {
220- console . log ( colors . yellow ( npmOutput ) ) ;
221- console . log ( colors . green ( `
185+ // prompt file path
186+ . then ( function ( pathResp ) {
187+ const nameRecv = pathResp . name ;
188+ Object . assign ( promptRes , pathResp ) ;
189+ return promptPath ( nameRecv ) ;
190+ } )
191+ // remove the folder if it exists
192+ . then ( function ( pathResp ) {
193+ if ( pathResp . override === "n" || pathResp . override === "no" ) {
194+ return Promise . reject ( console . log ( colors . green ( "Good Bye!" ) ) ) ;
195+ }
196+ Object . assign ( promptRes , pathResp ) ;
197+ let pathRecv = pathResp . path ;
198+ // if the entered path is relative, resolve to absolute
199+ if ( isRelativePath ( pathRecv ) ) {
200+ pathRecv = path . resolve ( path . join ( process . cwd ( ) , pathRecv ) ) ;
201+ promptRes . path = pathRecv ;
202+ }
203+ return removeExistingFolder ( pathRecv ) ;
204+ } )
205+ // copy contents to folder
206+ . then ( ( pathRecv ) => {
207+ return copyContents ( pathRecv ) ;
208+ } )
209+ // replace package.json with new one
210+ . then ( ( res ) => {
211+ return replacePackageJSON ( promptRes . path , promptRes . name ) ;
212+ } )
213+ // install npm dependencies
214+ . then ( ( res ) => {
215+ return installDeps ( promptRes . path ) ;
216+ } )
217+ // output end results
218+ . then ( function ( npmOutput ) {
219+ console . log ( colors . yellow ( npmOutput ) ) ;
220+ console . log (
221+ colors . green ( `
222222Your application setup is complete!
223223Please see the generated README.MD file to get more details about next steps.
224224You can find your application template in: ${ promptRes . path } .
225- ` ) ) ;
226- } )
227- // handle errors if any
228- . catch ( function ( err ) {
229- if ( err . message === 'canceled' ) {
230- return console . log ( colors . green ( '\nGood Bye!' ) ) ;
231- }
232- if ( err . message ) {
233- console . log ( 'An error occured.' , err ) ;
234- }
235- } ) ;
225+ ` )
226+ ) ;
227+ } )
228+ // handle errors if any
229+ . catch ( function ( err ) {
230+ if ( err . message === "canceled" ) {
231+ return console . log ( colors . green ( "\nGood Bye!" ) ) ;
232+ }
233+ if ( err . message ) {
234+ console . log ( "An error occurred." , err ) ;
235+ }
236+ } ) ;
236237
237238module . exports = {
238239 validatePath : validatePath ,
0 commit comments