11#! /usr/bin/env node
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' ) ;
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
2121const 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 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:
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" ) . epilogue ( `for more information,\please see the README at:
2928 https://github.com/Staffbase/create-staffbase-plugin-nodejs/blob/main/README.MD` ) ;
3029// console.log('YARGS Parsed Data:\n', yargv.argv);
31- const packageJSON = fs . readJSONSync ( path . join ( scaffoldFolder , ' package.json' ) ) ;
30+ const packageJSON = fs . readJSONSync ( path . join ( scaffoldFolder , " package.json" ) ) ;
3231// Defaults package name to current folder name
3332const nameParam = yargv . argv . N || yargv . argv . name || defaultNPMName ;
3433prompt . override = {
@@ -44,25 +43,24 @@ function promptName(name) {
4443 const namePromptSchema = {
4544 properties : {
4645 name : {
47- description : ' What is the npm name of your plugin?' ,
48- type : ' string' ,
46+ description : " What is the npm name of your plugin?" ,
47+ type : " string" ,
4948 default : name ,
50- message : ' Name must be npm.js compatible' ,
49+ message : " Name must be npm.js compatible" ,
5150 required : true ,
52- conform : function ( value ) {
51+ conform : function ( value ) {
5352 return validateNPM ( value ) . validForNewPackages ;
5453 } ,
5554 } ,
5655 } ,
5756 } ;
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- } ) ;
57+ return new Promise ( function ( resolve , reject ) {
58+ prompt . start ( ) . get ( namePromptSchema , function ( err , res ) {
59+ if ( err ) {
60+ return reject ( err ) ;
61+ }
62+ return resolve ( res ) ;
63+ } ) ;
6664 } ) ;
6765}
6866/**
@@ -75,33 +73,34 @@ function promptPath(promtedName) {
7573 const pathPromptSchema = {
7674 properties : {
7775 path : {
78- description : ' Please enter the folder path for the App' ,
79- type : ' string' ,
76+ description : " Please enter the folder path for the new plugin project" ,
77+ type : " string" ,
8078 message :
81- ' Entered path is invalid or an already present file on the File System. Please enter a correct filepath,' ,
79+ " Entered path is invalid or an already present file on the File System. Please enter a correct filepath." ,
8280 default : defPath ,
8381 required : true ,
8482 conform : validatePath ,
8583 } ,
8684 override : {
8785 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?' ,
86+ colors . yellow (
87+ "The directory you specified already exists. It directory will be overridden!"
88+ ) + "\nDo you wish to proceed (y)es|(n)o?" ,
9089 validator : / y [ e s ] * | n [ o ] ? / ,
91- warning : ' Must respond yes or no' ,
92- default : ' yes' ,
93- ask : function ( ) {
90+ warning : " Must respond yes or no" ,
91+ default : " yes" ,
92+ ask : function ( ) {
9493 let chkPath = defPath ;
95- if ( prompt . history ( ' path' ) ) {
96- chkPath = prompt . history ( ' path' ) . value ;
94+ if ( prompt . history ( " path" ) ) {
95+ chkPath = prompt . history ( " path" ) . value ;
9796 }
9897 return filepath . create ( chkPath ) . exists ( ) ;
9998 } ,
10099 } ,
101100 } ,
102101 } ;
103- return new Promise ( function ( resolve , reject ) {
104- prompt . get ( pathPromptSchema , function ( err , res ) {
102+ return new Promise ( function ( resolve , reject ) {
103+ prompt . get ( pathPromptSchema , function ( err , res ) {
105104 if ( err ) {
106105 return reject ( err ) ;
107106 }
@@ -112,32 +111,31 @@ function promptPath(promtedName) {
112111
113112/**
114113 * Copy contants from the Scaffold Template to the specified folder
115- * @param {String } dstDir THe destination directory where files are to be copied
114+ * @param {String } dstDir The destination directory where files are to be copied
116115 * @return {Promise } Promise resolved when the copy process is complete. Rejected
117116 * if there is some error in copying files.
118117 */
119118function copyContents ( dstDir ) {
120119 // console.log(colors.blue('Copying from:' + path.resolve(__dirname, './scaffoldTpl')));
121- const scaffoldFolder = path . resolve ( __dirname , ' ./scaffoldTpl' ) ;
120+ const scaffoldFolder = path . resolve ( __dirname , " ./scaffoldTpl" ) ;
122121 return fs . copy ( scaffoldFolder , dstDir ) ;
123122}
124123/**
125- * Repace the package.json file from copied fromplate to the new generated one.
124+ * Replace the package.json file from copied template to the new generated one.
126125 * @return {Promise } Promise resolved when the Package.json is successfully replaced.
127126 * @param {String } dstPath The path of the folder where the package.json needs to be replaced
128127 * @param {String } nameVal name value that should be replaced
129128 * Rejected if there is some error in creating new Package.json file.
130129 */
131130function replacePackageJSON ( dstPath , nameVal ) {
132131 // console.log("replacePackageJSON");
133- const newPackageJSON = Object . assign ( { } , packageJSON , { name : nameVal } ) ;
132+ const newPackageJSON = Object . assign ( { } , packageJSON , { name : nameVal } ) ;
134133 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- } ) ;
134+ const packagePath = path . resolve ( path . join ( curDir , "package.json" ) ) ;
135+ return fs . remove ( packagePath ) . then ( function ( data ) {
136+ // console.log(colors.yellow('Writing json...'));
137+ return fs . writeJson ( packagePath , newPackageJSON , { spaces : 2 } ) ;
138+ } ) ;
141139}
142140/**
143141 * Installs the node modules in the folder where the template was created.
@@ -146,12 +144,12 @@ function replacePackageJSON(dstPath, nameVal) {
146144 * @return {Promise } Promise resolved when the packages are successfulyl installed.
147145 */
148146function installDeps ( dstPath ) {
149- console . log ( colors . italic ( ' \nInstalling dependencies...' ) ) ;
147+ console . log ( colors . italic ( " \nInstalling dependencies..." ) ) ;
150148 const opts = {
151149 cwd : path . resolve ( dstPath ) ,
152150 } ;
153- return new Promise ( ( resolve , reject ) => {
154- execFile ( ' npm' , [ ' install' ] , opts , ( err , stdout , stderr ) => {
151+ return new Promise ( ( resolve , reject ) => {
152+ execFile ( " npm" , [ " install" ] , opts , ( err , stdout , stderr ) => {
155153 // console.log(colors.red('Inside Child result', stderr, stdout, err));
156154 if ( err ) {
157155 // console.log(colors.red(err));
@@ -170,11 +168,10 @@ function installDeps(dstPath) {
170168function removeExistingFolder ( dstPath ) {
171169 const fp = filepath . create ( dstPath ) ;
172170 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- } ) ;
171+ return fs . remove ( fp . toString ( ) ) . then ( function ( ) {
172+ console . log ( colors . red ( "Removing existing folder and its contents..." ) ) ;
173+ return dstPath ;
174+ } ) ;
178175 } else {
179176 return Promise . resolve ( dstPath ) ;
180177 }
@@ -183,56 +180,58 @@ function removeExistingFolder(dstPath) {
183180const promptRes = { } ;
184181// promot package name
185182promptName ( 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 ( `
183+ // prompt file path
184+ . then ( function ( pathResp ) {
185+ const nameRecv = pathResp . name ;
186+ Object . assign ( promptRes , pathResp ) ;
187+ return promptPath ( nameRecv ) ;
188+ } )
189+ // remove the folder if it exists
190+ . then ( function ( pathResp ) {
191+ if ( pathResp . override === "n" || pathResp . override === "no" ) {
192+ return Promise . reject ( console . log ( colors . green ( "Good Bye!" ) ) ) ;
193+ }
194+ Object . assign ( promptRes , pathResp ) ;
195+ let pathRecv = pathResp . path ;
196+ // if the entered path is relative, resolve to absolute
197+ if ( isRelativePath ( pathRecv ) ) {
198+ pathRecv = path . resolve ( path . join ( process . cwd ( ) , pathRecv ) ) ;
199+ promptRes . path = pathRecv ;
200+ }
201+ return removeExistingFolder ( pathRecv ) ;
202+ } )
203+ // copy contents to folder
204+ . then ( ( pathRecv ) => {
205+ return copyContents ( pathRecv ) ;
206+ } )
207+ // replace package.json with new one
208+ . then ( ( res ) => {
209+ return replacePackageJSON ( promptRes . path , promptRes . name ) ;
210+ } )
211+ // install npm dependencies
212+ . then ( ( res ) => {
213+ return installDeps ( promptRes . path ) ;
214+ } )
215+ // output end results
216+ . then ( function ( npmOutput ) {
217+ console . log ( colors . yellow ( npmOutput ) ) ;
218+ console . log (
219+ colors . green ( `
222220Your application setup is complete!
223221Please see the generated README.MD file to get more details about next steps.
224222You 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- } ) ;
223+ ` )
224+ ) ;
225+ } )
226+ // handle errors if any
227+ . catch ( function ( err ) {
228+ if ( err . message === "canceled" ) {
229+ return console . log ( colors . green ( "\nGood Bye!" ) ) ;
230+ }
231+ if ( err . message ) {
232+ console . log ( "An error occured." , err ) ;
233+ }
234+ } ) ;
236235
237236module . exports = {
238237 validatePath : validatePath ,
0 commit comments