react-mfe-gen 1.0.8 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/constants.js CHANGED
@@ -15,9 +15,9 @@ export const QUESTION = {
15
15
  FORM_MANAGEMENT: "Select a form management library:",
16
16
  CONDITIONAL_MFE_NAME: "Enter your micro-frontend name:",
17
17
  CONTAINER_PATH:
18
- "Please enter a path to create container:\ne.g: G:\\workspace\\sample-container\n:",
18
+ "Please enter a path to create container:\ne.g:\n for windows os: G:\\workspace\\sample-mfe ,\n for macOS/Linux: /Users/Me/workspace/sample-mfe\n:",
19
19
  MFE_PATH:
20
- "Please enter a path to create microfront end:\ne.g: G:\\workspace\\sample-mfe\n:",
20
+ "Please enter a path to create microfront end:\ne.g:\n for windows os: G:\\workspace\\sample-mfe ,\n for macOS/Linux: /Users/Me/workspace/sample-mfe\n:",
21
21
  PATH: "Please enter a path to create ",
22
22
  };
23
23
  export const CHOICE_CONSTANTS = {
@@ -4,7 +4,7 @@ import utils from "../utility.js";
4
4
 
5
5
  const containerCreation = async (language) => {
6
6
  // To store different working dir
7
- const wrokingDirectories = [];
7
+ const workingDirectories = [];
8
8
  try {
9
9
  // Get typescript flag
10
10
  const isTypeScript = language === CHOICE_CONSTANTS.LANGUAGE.TYPE_SCRIPT;
@@ -41,9 +41,12 @@ const containerCreation = async (language) => {
41
41
  ]);
42
42
 
43
43
  // store working dir
44
- wrokingDirectories.push(
45
- `${commonInfo.containerPath}\\${containerName}`
44
+ // store working dir
45
+ const containerFullPath = path.join(
46
+ commonInfo.containerPath,
47
+ containerName
46
48
  );
49
+ workingDirectories.push(containerFullPath);
47
50
 
48
51
  // Go inside user specified dir
49
52
  process.chdir(commonInfo.containerPath);
@@ -70,7 +73,7 @@ const containerCreation = async (language) => {
70
73
  `${INFO_MESSAGE.SUCCESS.CONTAINER}\n${INFO_MESSAGE.HAPPY_CODING}`
71
74
  );
72
75
  } catch {
73
- utils.cleanupProject(wrokingDirectories);
76
+ utils.cleanupProject(workingDirectories);
74
77
  }
75
78
  };
76
79
 
@@ -1,3 +1,4 @@
1
+ import path from "path";
1
2
  import inquirer from "inquirer";
2
3
  import {
3
4
  PROMPT,
@@ -9,7 +10,7 @@ import utils from "../utility.js";
9
10
 
10
11
  const newProjectCreation = async (language) => {
11
12
  // To store different working dir
12
- const wrokingDirectories = [];
13
+ const workingDirectories = [];
13
14
  try {
14
15
  // Get typescript flag
15
16
  const isTypeScript = language === CHOICE_CONSTANTS.LANGUAGE.TYPE_SCRIPT;
@@ -47,9 +48,11 @@ const newProjectCreation = async (language) => {
47
48
  ]);
48
49
 
49
50
  // store working dir
50
- wrokingDirectories.push(
51
- `${commonInfo.containerPath}\\${projectInfo.projectName}`
51
+ const containerFullPath = path.join(
52
+ commonInfo.containerPath,
53
+ projectInfo.projectName
52
54
  );
55
+ workingDirectories.push(containerFullPath);
53
56
  // Go inside user specified dir
54
57
  process.chdir(commonInfo.containerPath);
55
58
 
@@ -76,7 +79,7 @@ const newProjectCreation = async (language) => {
76
79
 
77
80
  const mfeInfo = await inquirer.prompt([
78
81
  {
79
- message: `${QUESTION.PATH}${mfeName} as microfront end\ne.g: G:\\workspace\\sample-mfe\n:`,
82
+ message: `${QUESTION.PATH}${mfeName} as microfront end\ne.g:\n for windows os: G:/workspace/sample-mfe ,\n for macOS/Linux: /Users/Me/workspace/sample-mfe\n:`,
80
83
  type: "input",
81
84
  name: "path",
82
85
  },
@@ -84,10 +87,9 @@ const newProjectCreation = async (language) => {
84
87
  ...PROMPT.COMMON,
85
88
  ]);
86
89
  // store working dir
87
- wrokingDirectories.push(
88
- `${mfeInfo.path}\\${mfeName}`
89
- );
90
- // Go inside user specified mfe dir
90
+ const mfeFullPath = path.join(mfeInfo.path, mfeName);
91
+ workingDirectories.push(mfeFullPath);
92
+ // Go inside user specified mfe dir
91
93
  process.chdir(mfeInfo.path);
92
94
  const mfeAppCommand = utils.getLanguageTemplate(mfeName, isTypeScript);
93
95
 
@@ -106,7 +108,7 @@ const newProjectCreation = async (language) => {
106
108
  `${INFO_MESSAGE.SUCCESS.NEW_PRO}\n${INFO_MESSAGE.HAPPY_CODING}`
107
109
  );
108
110
  } catch {
109
- utils.cleanupProject(wrokingDirectories);
111
+ utils.cleanupProject(workingDirectories);
110
112
  }
111
113
  };
112
114
 
@@ -4,7 +4,7 @@ import utils from "../utility.js";
4
4
 
5
5
  const singleMfeCreation = async (language) => {
6
6
  // To store different working dir
7
- const wrokingDirectories = [];
7
+ const workingDirectories = [];
8
8
  try {
9
9
  // Get typescript flag
10
10
  const isTypeScript = language === CHOICE_CONSTANTS.LANGUAGE.TYPE_SCRIPT;
@@ -19,10 +19,9 @@ const singleMfeCreation = async (language) => {
19
19
  PROMPT.CONDITIONAL.FORM_MANAGEMENT,
20
20
  ]);
21
21
 
22
- // store working dir
23
- wrokingDirectories.push(
24
- `${mfeInfo.mfePath}\\${mfeName}`
25
- );
22
+ // store working dir
23
+ const mfepPath = path.join(mfeInfo.mfePath, mfeName);
24
+ workingDirectories.push(mfepPath);
26
25
 
27
26
  // Go inside user specified dir
28
27
  process.chdir(mfeInfo.mfePath);
@@ -35,7 +34,7 @@ const singleMfeCreation = async (language) => {
35
34
 
36
35
  // Make normal react app into MFE container
37
36
  await utils.configureMfe(
38
- { ...mfeInfo, projectName: mfeName, isTypeScript },
37
+ { ...mfeInfo, projectName: mfeName,mfeDescription, isTypeScript },
39
38
  mfeName,
40
39
  0
41
40
  );
@@ -44,7 +43,7 @@ const singleMfeCreation = async (language) => {
44
43
  `${INFO_MESSAGE.SUCCESS.ONE_MFE}\n${INFO_MESSAGE.HAPPY_CODING}`
45
44
  );
46
45
  } catch {
47
- utils.cleanupProject(wrokingDirectories)
46
+ utils.cleanupProject(workingDirectories);
48
47
  }
49
48
  };
50
49
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mfe-gen",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "Generate React micro-frontends, containers, and projects using a simple CLI with runtime integration support.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utility.js CHANGED
@@ -1,3 +1,4 @@
1
+ import path from "path";
1
2
  import { Spinner } from "cli-spinner";
2
3
  import { execa } from "execa";
3
4
  import {
@@ -19,6 +20,7 @@ import {
19
20
  getMfeAppContent,
20
21
  getmfeIndexContent,
21
22
  } from "./templates/mfe/index.js";
23
+
22
24
  class utils {
23
25
  static async runTask(logMessage, task) {
24
26
  const spinner = new Spinner(logMessage + "%s");
@@ -33,16 +35,18 @@ class utils {
33
35
  throw err;
34
36
  }
35
37
  }
38
+
36
39
  static async cleanupProject(dirs) {
37
- console.log(`Cleaning up the project directory: ${dirs}`);
38
40
  try {
39
41
  for (let i = 0; i < dirs.length; i++) {
42
+ console.log(`Cleaning up the project directory: ${dirs[i]}`);
40
43
  await rm(dirs[i], { recursive: true });
41
44
  }
42
45
  } catch (err) {
43
46
  console.log(`Failed to clean project directory\n Error:${err}`);
44
47
  }
45
48
  }
49
+
46
50
  static async createReactApp(appCommand) {
47
51
  try {
48
52
  await utils.runTask(INFO_MESSAGE.APP_CREATION, () =>
@@ -52,6 +56,7 @@ class utils {
52
56
  throw err;
53
57
  }
54
58
  }
59
+
55
60
  static async installPackages(packages, isDev = false) {
56
61
  try {
57
62
  let message = INFO_MESSAGE.i_DEPENDENCIES;
@@ -66,6 +71,7 @@ class utils {
66
71
  throw err;
67
72
  }
68
73
  }
74
+
69
75
  static getLanguageTemplate(appName, isTs) {
70
76
  return isTs ? [appName, "--template", "typescript"] : [appName];
71
77
  }
@@ -73,15 +79,18 @@ class utils {
73
79
  static withExt(name, isTs) {
74
80
  return isTs ? `${name}.tsx` : `${name}.jsx`;
75
81
  }
82
+
76
83
  static withScript(name, isTs) {
77
84
  return isTs ? `${name}.ts` : `${name}.js`;
78
85
  }
86
+
79
87
  static toCompName(AppName) {
80
88
  return AppName.replace(/[-\s]+/g, " ")
81
89
  .split(" ")
82
90
  .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
83
91
  .join("");
84
92
  }
93
+
85
94
  static async updateScripts(dir, newScripts) {
86
95
  const rawPackageJson = await readFile(dir);
87
96
  const packageJSON = JSON.parse(rawPackageJson, "utf8");
@@ -90,8 +99,8 @@ class utils {
90
99
 
91
100
  await writeFile(dir, JSON.stringify(packageJSON, null, 2), "utf8");
92
101
  }
102
+
93
103
  static async configureContainer(info, mfeNames) {
94
- // Destructure inputs
95
104
  const {
96
105
  projectName,
97
106
  projectDescription,
@@ -101,17 +110,15 @@ class utils {
101
110
  } = info;
102
111
 
103
112
  // Go inside src
104
- process.chdir(`${process.cwd()}\\${projectName}\\src`);
113
+ const srcPath = path.join(process.cwd(), projectName, "src");
114
+ process.chdir(srcPath);
105
115
 
106
- // Create heart of container
107
116
  await utils.runTask(INFO_MESSAGE.CONFIGURE_CONTAINER, () =>
108
117
  writeFile("MicroFrontend.js", getHeartContent(projectName))
109
118
  );
110
119
 
111
- // Convert project name into component name format
112
120
  const containerCompName = utils.toCompName(projectName);
113
121
 
114
- // Create container component
115
122
  await utils.runTask(INFO_MESSAGE.CONFIGURE_CONTAINER, () =>
116
123
  writeFile(
117
124
  utils.withExt(containerCompName, isTypeScript),
@@ -119,7 +126,6 @@ class utils {
119
126
  )
120
127
  );
121
128
 
122
- // Modify App.jsx or .tsx file
123
129
  await utils.runTask(INFO_MESSAGE.CONFIGURE_CONTAINER, () =>
124
130
  writeFile(
125
131
  utils.withExt("App", isTypeScript),
@@ -127,17 +133,14 @@ class utils {
127
133
  )
128
134
  );
129
135
 
130
- // Drop unnecessary files
131
136
  await unlink("App.css");
132
137
  await unlink("logo.svg");
133
138
 
134
- // Create MFE folders
135
139
  await mkdir("microfrontends");
136
140
 
137
- // Go inside MFE folder
138
- process.chdir(`${process.cwd()}\\microfrontends`);
141
+ const mfeFolderPath = path.join(process.cwd(), "microfrontends");
142
+ process.chdir(mfeFolderPath);
139
143
 
140
- // Run loop and create number of mfe components
141
144
  for (let i = 0; i < mfeNames.length; i++) {
142
145
  const mfeCompName = utils.toCompName(mfeNames[i]);
143
146
  await writeFile(
@@ -146,12 +149,10 @@ class utils {
146
149
  );
147
150
  }
148
151
 
149
- // Go to root
150
- process.chdir("../../");
151
- // Create env file with specified MFE default ports
152
+ process.chdir(path.resolve(srcPath, ".."));
153
+
152
154
  await writeFile(".env", getEnvContent(mfeNames));
153
155
 
154
- // Install common and user defined packages
155
156
  const packagesList = [
156
157
  ...DEFAULT_DEPENDENCIES,
157
158
  ...LIBRARY_PAIR.STYLING[styling],
@@ -159,44 +160,40 @@ class utils {
159
160
  ];
160
161
  await utils.installPackages(packagesList);
161
162
 
162
- // Create readme
163
163
  await writeFile("README.md", `# ${projectName}\n${projectDescription}`);
164
164
 
165
165
  console.log(`${projectName} created ✅\n`);
166
166
  }
167
+
167
168
  static async configureMfe(info, mfeName, index) {
168
- // Destructure inputs
169
169
  const {
170
170
  projectName,
171
171
  formManagement,
172
172
  styling,
173
173
  stateManagement,
174
+ mfeDescription = "",
174
175
  isTypeScript,
175
176
  } = info;
176
- // Go inside src
177
- process.chdir(`${process.cwd()}\\${mfeName}\\src`);
178
177
 
179
- // Modify App.jsx or .tsx file
178
+ const mfeSrcPath = path.join(process.cwd(), mfeName, "src");
179
+ process.chdir(mfeSrcPath);
180
+
180
181
  await writeFile(
181
182
  utils.withExt("App", isTypeScript),
182
183
  getMfeAppContent(utils.toCompName(mfeName), isTypeScript)
183
184
  );
184
185
 
185
- // Modify index.jsx or .tsx file
186
186
  await writeFile(
187
187
  utils.withExt("index", isTypeScript),
188
188
  getmfeIndexContent(utils.toCompName(mfeName), projectName, isTypeScript)
189
189
  );
190
190
 
191
- // Drop unnecessary files
192
191
  await unlink("App.css");
193
192
  await unlink("logo.svg");
194
193
  await unlink("index.css");
195
194
 
196
- // Go to root
197
- process.chdir("../");
195
+ process.chdir(path.resolve(mfeSrcPath, ".."));
198
196
 
199
- // add config override file
200
197
  await writeFile("config-overrides.js", getConfigOverridesContent());
201
198
 
202
199
  const updatedScript = {
@@ -206,10 +203,11 @@ class utils {
206
203
  eject: "react-app-rewired eject",
207
204
  };
208
205
 
209
- // modify package.json scripts
210
- utils.updateScripts(`${process.cwd()}\\package.json`, updatedScript);
206
+ await utils.updateScripts(
207
+ path.join(process.cwd(), "package.json"),
208
+ updatedScript
209
+ );
211
210
 
212
- // Install common and user defined packages
213
211
  const packagesList = [
214
212
  ...DEFAULT_DEPENDENCIES,
215
213
  ...LIBRARY_PAIR.STYLING[styling],
@@ -218,11 +216,9 @@ class utils {
218
216
  ];
219
217
 
220
218
  await utils.installPackages(packagesList);
221
-
222
219
  await utils.installPackages(DEFAULT_DEV_DEPENDENCIES, true);
223
220
 
224
- // Create readme
225
- await writeFile("README.md", `# ${mfeName}`);
221
+ await writeFile("README.md", `# ${mfeName}\n##${mfeDescription}`);
226
222
 
227
223
  console.log(`${mfeName} created ✅`);
228
224
  }