Skip to content

Commit 49e9b36

Browse files
committed
Fix: Separate dev from production environment, clean building blocks & comments
1 parent 7f982bb commit 49e9b36

File tree

6 files changed

+68
-103
lines changed

6 files changed

+68
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test.js
1111

1212
# compiled output
1313
/dist
14+
/build
1415
/tmp
1516
/out-tsc
1617
# Only exists if Bazel was run

bin/create-collabo-app

Lines changed: 50 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const { warning, error } = require('../lib/helpers');
99

1010
const { argv, env } = process;
1111
const fs_access = promisify(access);
12-
const dist_folder = join('./', 'dist');
1312

1413
const 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

4041
const 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

5855
const 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+
});

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
},
1111
"scripts": {
1212
"predev": "node -r esm dev/customize/startMessage.js",
13-
"dev": "npm-run-all --parallel lint:watch ts:watch",
13+
"dev": "npm-run-all --parallel lint:watch tsc:dev:watch",
14+
"production:build": "npm-run-all --parallel lint tsc:prod",
1415
"cleanup": "node -r esm dev/cleanup.js",
1516
"lint": "esw src --ext .ts --color",
1617
"lint:watch": "npm run lint -- --watch",
17-
"ts:watch": "tsc -w",
18+
"tsc:dev:watch": "tsc -p tsconfig.dev.json -w",
19+
"tsc:prod": "tsc -p tsconfig.prod.json",
1820
"test": "npm-run-all --parallel test-message dev-test",
1921
"dev-test": "nodemon --exec babel-node spec/run.js",
2022
"CI-test": "babel-node spec/run.js",
@@ -88,7 +90,7 @@
8890
},
8991
"files": [
9092
"bin/",
91-
"dist/src/",
93+
"build/src/",
9294
"templates/"
9395
]
9496
}

tsconfig.dev.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
}
6+
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"_version": "20.1.0",
44
"compilerOptions": {
55
"lib": ["es2023"],
6-
"outDir": "dist",
76
"module": "ES2022",
87
"target": "ES5",
98
"strict": true,

tsconfig.prod.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "build",
5+
}
6+
}

0 commit comments

Comments
 (0)