@@ -9,14 +9,14 @@ const { warning, error } = require('../lib/helpers');
99
1010const { argv, env } = process ;
1111const fs_access = promisify ( access ) ;
12- const dist_folder = join ( './' , 'dist' ) ;
1312
1413const folder = {
1514 content : {
1615 /* ---------------------------------------------------
17- Lists the files and/or folders in the targetFolder
18- of your choice (in an array)
19- --------------------------------------------------- */
16+ Returns: Array of strings
17+ Description: Lists the files and/or folders in the
18+ folder supplied
19+ ----------------------------------------------------*/
2020 list : ( { targetFolder } ) => {
2121 const folderContent = readdirSync ( targetFolder , ( err , filesAndFolders ) => {
2222 if ( err ) {
@@ -26,10 +26,11 @@ const folder = {
2626 } ) ;
2727 return folderContent ;
2828 } ,
29- /* ---------------------------------------------------
30- Checks the files and/or folders in the targetFolder
31- of your choice (in an array)
32- --------------------------------------------------- */
29+ /* -----------------------------------------------------
30+ Returns: boolean
31+ Description: Confirms the existence of a single file
32+ or folder, inside the folder supplied
33+ -------------------------------------------------------*/
3334 exists : ( { searchFolder, searchFor } ) => {
3435 const folderContent = folder . content . list ( { targetFolder : searchFolder } ) ;
3536 return folderContent . some ( folder => folder === searchFor ) ;
@@ -39,104 +40,54 @@ const folder = {
3940
4041const rootFolder = './' ;
4142
42- const test = async ( { success, error } ) => {
43- const devMode = folder . content . exists ( { searchFolder : rootFolder , searchFor : 'dev' } ) ;
44- const prodMode = ! devMode && folder . content . exists ( { searchFolder : rootFolder , searchFor : 'dist' } ) ;
45- //--- set node environment
46- if ( devMode ) env . NODE_ENV = 'development' ;
47- if ( prodMode ) env . NODE_ENV = 'production' ;
43+ const devMode = folder . content . exists ( { searchFolder : rootFolder , searchFor : 'dev' } ) ;
44+ const prodMode = ! devMode ;
4845
49- if ( env . NODE_ENV === 'development' ) {
50- try {
51- await success ( ) ;
52- } catch ( err ) {
53- error ( { err } ) ;
54- }
55- }
56- }
46+ if ( devMode ) env . NODE_ENV = 'development' ;
47+ if ( prodMode ) env . NODE_ENV = 'production' ;
48+
49+ let outDirName = '' ;
50+ if ( env . NODE_ENV === 'development' ) outDirName = 'dist' ;
51+ if ( env . NODE_ENV === 'production' ) outDirName = 'build' ;
52+
53+ const outDir = join ( './' , outDirName ) ;
5754
5855const collabo_app = {
5956 cli : {
60- start : ( ) => require ( '../dist/src/cli' ) . cli ( argv ) ,
57+ start : ( ) => {
58+ require ( `../${ outDirName } /src/cli` ) . cli ( argv ) ;
59+ } ,
6160 error : ( { message } ) => {
62- const distFolderExists = folder . content . exists ( { searchFolder : rootFolder , searchFor : 'dist' } ) ;
63- const dynamicErrorDetails = distFolderExists ? 'existing \'dist\' does not have required code to run CLI' : `No 'dist' folder detected - ${ message } ` ;
64- const dynamicWarningText = distFolderExists ? 'correct \'dist\' folder content' : '\'dist\' folder with required content' ;
65- error ( `ERROR: ${ dynamicErrorDetails } ` ) ;
66- warning ( `ℹ Follow these steps to generate ${ dynamicWarningText } : \n- Open up another terminal, run the "npm run dev" script command there (keep this new terminal open always)\n- Go back to running the 'collabo-be' command in the previously open terminal, the CLI should now run successfully` ) ;
61+ const outDirExists = folder . content . exists ( { searchFolder : rootFolder , searchFor : outDirName } ) ;
62+ if ( env . NODE_ENV === 'development' ) {
63+ const dynamicErrorDetails = outDirExists ? `existing '${ outDirName } ' folder does not have required code to run CLI` : `No '${ outDirName } ' folder detected - ${ message } ` ;
64+ error ( `ERROR: ${ dynamicErrorDetails } ` ) ;
65+ const dynamicWarningText = outDirExists ? `correct '${ outDirName } ' folder content` : `'${ outDirName } ' folder with required content` ;
66+ warning ( `ℹ Follow these steps to generate ${ dynamicWarningText } : \n- Open up another terminal, run the 'npm run dev' script command there (keep this new terminal open always)\n- Go back to running the 'collabo-be' command in the previously open terminal, the CLI should now run successfully` ) ;
67+ // TODO: (if it still crashes after following the help steps above) Report issue
68+ }
69+ if ( env . NODE_ENV === 'production' ) {
70+ const dynamicErrorDetails = outDirExists ? `Cannot run '${ outDirName } ' folder content` : `'${ outDirName } ' folder not found` ;
71+ error ( `ERROR: ${ dynamicErrorDetails } ` ) ;
72+ // TODO: Report issue
73+ }
6774 } ,
6875 } ,
69- // run: async ({ success, error }) => {
70- // const devMode = folder.content.exists({ searchFolder: rootFolder, searchFor: 'dev' });
71- // const prodMode = !devMode && folder.content.exists({ searchFolder: rootFolder, searchFor: 'dist' });
72- // //--- set node environment
73- // if (devMode) env.NODE_ENV = 'development';
74- // if (prodMode) env.NODE_ENV = 'production';
75-
76- // if (env.NODE_ENV === 'development') {
77- // try {
78- // await success();
79- // } catch(err) {
80- // error({ err });
81- // }
82- // }
83- // },
84- run : async ( ) => {
85- test ( {
86- success : async ( ) => {
87- await fs_access ( dist_folder , constants . F_OK ) ;
88- collabo_app . cli . start ( ) ;
89- } ,
90- error : ( { err } ) => {
91- collabo_app . cli . error ( { message : err . message } ) ;
92- }
93- } ) ;
76+ run : async ( { success, error } ) => {
77+ try {
78+ await fs_access ( outDir , constants . F_OK ) ;
79+ success ( ) ;
80+ } catch ( err ) {
81+ error ( { err } ) ;
82+ }
9483 } ,
95- // cli: {
96- // start: () => require('../dist/src/cli').cli(argv),
97- // error: ({ dynamicErrorDetails, dynamicWarningText }) => {
98- // error(`ERROR: ${dynamicErrorDetails}`);
99- // warning(`ℹ Follow these steps to generate ${dynamicWarningText}: \n- Open up another terminal, run the "npm run dev" script command there (keep this new terminal open always)\n- Go back to running the 'collabo-be' command in the previously open terminal, the CLI should now run successfully`);
100- // },
101- // },
102- // run: async () => {
103- // const repoContent = readdirSync('./', (err, filesAndFolders) => {
104- // if (err) {
105- // throw err;
106- // }
107-
108- // return filesAndFolders;
109- // });
110- // const folderExists = ({ name }) => repoContent.some(folder => folder === name);
111- // const devMode = folderExists({ name: 'dev' });
112- // env.NODE_ENV = devMode ? 'development' : 'production';
113- // if (env.NODE_ENV === 'development') {
114- // try {
115- // await fs_access(dist_folder, constants.F_OK);
116- // collabo_app.cli.start();
117- // } catch(err) {
118- // const dynamicErrorDetails = folderExists({ name: 'dist' }) ? 'existing \'dist\' does not have required code to run CLI' : `No 'dist' folder detected - ${err.message}`;
119- // const dynamicWarningText = folderExists({ name: 'dist' }) ? 'correct \'dist\' folder content' : '\'dist\' folder with required content';
120- // collabo_app.cli.error({
121- // dynamicErrorDetails,
122- // dynamicWarningText,
123- // });
124- // }
125- // }
126- // if (env.NODE_ENV === 'production') {
127- // collabo_app.cli.start();
128- // }
129- // },
13084} ;
13185
132- collabo_app . run ( ) ;
133-
134- // collabo_app.run({
135- // success: async () => {
136- // await fs_access(dist_folder, constants.F_OK);
137- // collabo_app.cli.start();
138- // },
139- // error: ({ err }) => {
140- // collabo_app.cli.error({ message: err.message });
141- // }
142- // });
86+ collabo_app . run ( {
87+ success : ( ) => {
88+ collabo_app . cli . start ( ) ;
89+ } ,
90+ error : ( { err } ) => {
91+ collabo_app . cli . error ( { message : err . message } ) ;
92+ }
93+ } ) ;
0 commit comments