Skip to content

Commit 73f4bb4

Browse files
committed
cli
1 parent 45a69b6 commit 73f4bb4

File tree

9 files changed

+58
-44
lines changed

9 files changed

+58
-44
lines changed

.vscode/settings.json

-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
"activityBarBadge.background": "#25320e",
2121
"activityBarBadge.foreground": "#e7e7e7",
2222
"statusBar.background": "#832561",
23-
"statusBar.border": "#832561",
2423
"statusBar.foreground": "#e7e7e7",
2524
"statusBarItem.hoverBackground": "#ab307e",
2625
"titleBar.activeBackground": "#832561",
2726
"titleBar.activeForeground": "#e7e7e7",
28-
"titleBar.border": "#832561",
2927
"titleBar.inactiveBackground": "#83256199",
3028
"titleBar.inactiveForeground": "#e7e7e799"
3129
},

packages/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cmra/cli",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"main:src": "src/index.js",
55
"main": "index.js",
66
"author": "Matheus Martins",

packages/cli/scripts/create/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const create = async (types, name, opts) => {
3333
if (shouldCreateLib) {
3434
await createMicrofrontendWithLibrary(microfrontendName, pathToCreateMicro);
3535
} else {
36-
await createMicrofrontend(microfrontendName, pathToCreateMicro);
36+
await createMicrofrontend(microfrontendName, pathToCreateMicro, !shouldCreateApp);
3737
}
3838
return;
3939
}

packages/cli/scripts/create/microfrontend.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ ExportMicrofrontend({
1515
});
1616
`;
1717

18-
const createMicrofrontendWithTemplate = async (name, folder) => {
19-
await createModule(name, 'microfrontend', resolveApp(folder));
20-
await writeFile(resolvePackageSrc(folder, name, 'index.js'), indexJsFile(name));
18+
const createMicrofrontendWithTemplate = async (name, folder, isRootPath) => {
19+
await createModule(name, 'microfrontend', resolveApp(folder), isRootPath);
20+
if (isRootPath) {
21+
await writeFile(resolveApp(folder, name, 'src', 'index.js'), indexJsFile(name));
22+
} else {
23+
await writeFile(resolvePackageSrc(folder, name, 'index.js'), indexJsFile(name));
24+
}
2125
};
2226

23-
const createMicrofrontend = async (name, folder = '.') => {
24-
await explain('Creating microfrontend', () => createMicrofrontendWithTemplate(name, folder));
27+
const createMicrofrontend = async (name, folder = '.', isRootPath) => {
28+
await explain('Creating microfrontend', () => createMicrofrontendWithTemplate(name, folder, isRootPath));
2529
};
2630

2731
const createMicrofrontendWithLibrary = async (name, folder = '.') => {

packages/cli/scripts/create/module.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ const addScriptsToPackageJson = async (packageJsonPath, scripts) => {
1919
await writeJson(packageJsonPath, packageJson);
2020
};
2121

22-
const createModule = async (name, template, rootAppPath) => {
23-
const { execInPackages, execInApp } = createExecutionContext(rootAppPath, name);
22+
const createModule = async (name, template, rootAppPath, isRootPath = false) => {
23+
const { execInPackages, execInApp, execInRootApp, execInRoot } = createExecutionContext(rootAppPath, name);
2424

25-
await execInPackages(`npx create-react-app ${name}`);
26-
await appendFile(`${rootAppPath}/packages/${name}/.gitignore`, ['build-lib', 'public/meta.json'].join('\n'));
27-
await copyTemplateTo(template, `${rootAppPath}/packages/${name}`);
25+
if (isRootPath) {
26+
await execInRoot(`npx create-react-app ${name}`);
2827

29-
await execInApp('yarn add @cmra/cli');
30-
await execInApp('yarn add @cmra/react');
28+
await appendFile(`${rootAppPath}/.gitignore`, ['build-lib', 'public/meta.json'].join('\n'));
29+
await copyTemplateTo(template, rootAppPath);
3130

32-
await addScriptsToPackageJson(
33-
`${rootAppPath}/packages/${name}/package.json`,
34-
getModuleScripts(template === 'microfrontend')
35-
);
31+
await execInRootApp('yarn add @cmra/cli');
32+
await execInRootApp('yarn add @cmra/react');
33+
34+
await addScriptsToPackageJson(
35+
`${rootAppPath}/${name}/package.json`,
36+
getModuleScripts(template === 'microfrontend')
37+
);
38+
} else {
39+
await execInPackages(`npx create-react-app ${name}`);
40+
41+
await appendFile(`${rootAppPath}/packages/${name}/.gitignore`, ['build-lib', 'public/meta.json'].join('\n'));
42+
await copyTemplateTo(template, `${rootAppPath}/packages/${name}`);
43+
44+
await execInApp('yarn add @cmra/cli');
45+
await execInApp('yarn add @cmra/react');
46+
47+
await addScriptsToPackageJson(
48+
`${rootAppPath}/packages/${name}/package.json`,
49+
getModuleScripts(template === 'microfrontend')
50+
);
51+
}
3652
};
3753

3854
module.exports = {

packages/cli/scripts/utils/paths.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ const path = require('path');
22
const fs = require('fs');
33

44
const appDirectory = fs.realpathSync(process.cwd());
5-
const resolve = paths => path.resolve(...([appDirectory].concat(paths)));
6-
const resolveApp = relativePath => resolve([relativePath]);
7-
const resolvePackageSrc = (relativePath, packageName, file) => resolve([relativePath, 'packages', packageName, 'src', file]);
8-
const escapePackageName = packageName => packageName.replace(/@/g, '').replace(/\//g, '_');
5+
const resolve = (paths) => path.resolve(...[appDirectory].concat(paths));
6+
const resolveApp = (...relativePath) => resolve(relativePath);
7+
const resolvePackageSrc = (relativePath, packageName, file) =>
8+
resolve([relativePath, 'packages', packageName, 'src', file]);
9+
const escapePackageName = (packageName) => packageName.replace(/@/g, '').replace(/\//g, '_');
910

1011
module.exports = {
1112
resolveApp,

packages/cli/scripts/utils/process.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
21
const { spawn } = require('child_process');
32

4-
const exec = (command, {
5-
cwd,
6-
onStdout,
7-
onStderr,
8-
debug = true,
9-
} = {}) => new Promise((resolve, reject) => {
10-
const spawnProcess = spawn(command, [], { shell: true, cwd });
3+
const exec = (command, { cwd, onStdout, onStderr, debug = true } = {}) =>
4+
new Promise((resolve, reject) => {
5+
const spawnProcess = spawn(command, [], { shell: true, cwd });
116

12-
if (onStdout || debug) spawnProcess.stdout.on('data', onStdout || (data => process.stdout.write(data)));
13-
if (onStderr || debug) spawnProcess.stderr.on('data', onStderr || (data => process.stderr.write(data)));
7+
if (onStdout || debug) spawnProcess.stdout.on('data', onStdout || ((data) => process.stdout.write(data)));
8+
if (onStderr || debug) spawnProcess.stderr.on('data', onStderr || ((data) => process.stderr.write(data)));
149

15-
spawnProcess.on('exit', (code) => {
16-
if (code > 0) {
17-
reject(code);
18-
return;
19-
}
20-
resolve();
10+
spawnProcess.on('exit', (code) => {
11+
if (code > 0) {
12+
reject(code);
13+
return;
14+
}
15+
resolve();
16+
});
2117
});
22-
});
2318

2419
const createExecutionContext = (rootAppPath, appName, opts) => {
25-
const execInFolder = path => command => exec(command, { ...opts, cwd: `${rootAppPath}${path || ''}` });
20+
const execInFolder = (path) => (command) => exec(command, { ...opts, cwd: `${rootAppPath}${path || ''}` });
2621
return {
2722
execInRoot: execInFolder('/'),
23+
execInRootApp: execInFolder(`/${appName}`),
2824
execInPackages: execInFolder('/packages'),
2925
execInApp: execInFolder(`/packages/${appName}`),
3026
};

packages/react/src/microfrontend/communication.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Communication from './../base/communication';
1+
import Communication from '../base/communication';
22

33
class MicrofrontendClient extends Communication {
44
from: string;

packages/webapp/lib/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module.exports = {
2323
.join(' ');
2424

2525
const command = `${envVars} npm run build`;
26-
console.info(command);
2726
await exec(command, { cwd: `${__dirname}/../` });
2827
return `${__dirname}/../build`;
2928
},

0 commit comments

Comments
 (0)