Skip to content

Commit b80952b

Browse files
committed
multiple fixes
1 parent 9369ed8 commit b80952b

File tree

16 files changed

+160
-95
lines changed

16 files changed

+160
-95
lines changed

packages/cli/bin/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ program
2121
2222
File example "${chalk.italic('my-config.json')}":
2323
${chalk.bold(`
24+
{
25+
"proxyUrl": "https://my-aplication-url.com",
2426
"microfrontends": {
2527
"my-microfrontend1": "/path/to/microfrontend1/root",
2628
"my-microfrontend2": "/path/to/microfrontend2/root"
2729
},
2830
"app": {
2931
"my-webapp": "/path/to/app/root"
3032
}
33+
}
3134
`)}
3235
`
3336
)
@@ -49,6 +52,12 @@ program
4952
- a devserver running just with your current microfrontend (where you started this command) in development
5053
`
5154
)
55+
.option(
56+
'-w, --webapp',
57+
`
58+
Combined with --proxy, put this container as proxy root.
59+
`
60+
)
5261
.option(
5362
'-a, --all <webapp_package_name>',
5463
`
@@ -73,6 +82,7 @@ program
7382
opts.configurationFile = options.configurationFile;
7483
} else if (options.proxy) {
7584
type = start.TYPE.PROXY;
85+
opts.isContainer = options.webapp;
7686
opts.url = options.proxy;
7787
} else if (options.all) {
7888
type = start.TYPE.LOCAL;

packages/cli/scripts/start/all.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
1-
21
const { microfrontendFolderName, getPackagesFromConfig } = require('../utils/config');
32
const { writeJson } = require('../utils/fs');
43
const { escapePackageName } = require('../utils/paths');
54
const startSingleApp = require('./single');
6-
5+
const startProxyServer = require('./proxy-server');
6+
const { getMetaFromUrl } = require('./proxy');
77

88
const startMultipleLocations = async (configurationFilePath, opts = {}) => {
9-
const {
10-
microfrontends,
11-
app,
12-
} = await getPackagesFromConfig(configurationFilePath, opts);
9+
const { microfrontends, app, proxyUrl } = await getPackagesFromConfig(configurationFilePath, opts);
1310

1411
const INITIAL_PORT = 3001;
1512

16-
const metaJson = Object.keys(microfrontends).reduce((agg, packageName, i) => Object.assign(agg, {
17-
[escapePackageName(packageName)]: {
18-
host: `http://localhost:${INITIAL_PORT + i}`,
19-
},
20-
}), {});
13+
const metaJson = Object.keys(microfrontends).reduce(
14+
(agg, packageName, i) =>
15+
Object.assign(agg, {
16+
[escapePackageName(packageName)]: {
17+
host: `http://localhost:${INITIAL_PORT + i}`,
18+
},
19+
}),
20+
{}
21+
);
2122

2223
const pathToAppPackage = Object.values(app)[0];
2324

24-
await writeJson(`${pathToAppPackage}/public/${microfrontendFolderName}/meta.json`, metaJson);
25+
const envJson = await getMetaFromUrl(proxyUrl);
26+
await writeJson(`${pathToAppPackage}/public/${microfrontendFolderName}/meta.json`, {
27+
...envJson,
28+
...metaJson,
29+
});
2530

26-
Object.values(microfrontends)
27-
.forEach((packagePath, i) => startSingleApp({
31+
Object.values(microfrontends).forEach((packagePath, i) =>
32+
startSingleApp({
2833
pathToPackage: packagePath,
2934
port: INITIAL_PORT + i,
3035
isMicro: true,
3136
isRunningAll: true,
32-
}));
37+
})
38+
);
3339

3440
startSingleApp({
3541
pathToPackage: pathToAppPackage,

packages/cli/scripts/start/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const startProxyServer = require('./proxy-server');
22
const startLocalAll = require('./all');
33
const startSingle = require('./single');
4+
const { writeJson } = require('../utils/fs');
5+
const { resolveApp } = require('../utils/paths');
6+
const { getMetaFromUrl } = require('./proxy');
47

58
const TYPE = {
69
SINGLE: 'SINGLE',
@@ -15,10 +18,15 @@ const start = (type, opts) => {
1518
const { configurationFile } = opts;
1619
startLocalAll(configurationFile, opts);
1720
},
18-
[TYPE.PROXY]: () => {
19-
const { url } = opts;
20-
startProxyServer(url);
21-
startSingle({ port: 3001, isMicro: true });
21+
[TYPE.PROXY]: async () => {
22+
const { url, isContainer } = opts;
23+
if (!isContainer) {
24+
startProxyServer(url);
25+
} else {
26+
const envJson = await getMetaFromUrl(url);
27+
await writeJson(resolveApp('public/microfrontends/meta.json'), envJson);
28+
}
29+
startSingle({ port: isContainer ? 3000 : 3001, isMicro: !isContainer });
2230
},
2331
}[type]());
2432
};

packages/cli/scripts/start/proxy-server.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const axios = require('axios');
44
const { readJson } = require('../utils/fs');
55
const { escapePackageName, appPackageJson } = require('../utils/paths');
66

7-
const startProxyServer = async (proxyUrl) => {
7+
const startProxyServer = async (proxyUrl, opts = {}) => {
88
const packageJson = await readJson(appPackageJson);
99
const app = express();
1010
const PORT = 3000;
@@ -14,21 +14,29 @@ const startProxyServer = async (proxyUrl) => {
1414
const namespace = url.pathname;
1515

1616
app.get(`${namespace}microfrontends/meta.json`, (_, res) => {
17-
axios.get(`${proxyUrl}microfrontends/meta.json`).then(response => response.data).then((json) => {
18-
res.json({
19-
...json,
20-
[escapedPackageName]: { host: 'http://localhost:3001' },
17+
axios
18+
.get(`${proxyUrl}microfrontends/meta.json`)
19+
.then((response) => response.data)
20+
.then((json) => {
21+
res.json({
22+
...json,
23+
[escapedPackageName]: { host: 'http://localhost:3001' },
24+
});
2125
});
22-
});
2326
});
2427

25-
app.use(namespace, proxy(proxyUrl, {
26-
proxyReqPathResolver: req => `${namespace}${req.url}`,
27-
}));
28+
app.use(
29+
namespace,
30+
proxy(proxyUrl, {
31+
proxyReqPathResolver: (req) => `${namespace}${req.url}`,
32+
})
33+
);
2834

2935
app.use('/', proxy(url.origin));
3036

31-
app.listen(PORT, () => console.log(`Example app listening on PORT ${PORT}! Access localhost:${PORT}${namespace} to see your app`));
37+
app.listen(PORT, () =>
38+
console.log(`Example app listening on PORT ${PORT}! Access localhost:${PORT}${namespace} to see your app`)
39+
);
3240
};
3341

3442
module.exports = startProxyServer;

packages/cli/scripts/start/proxy.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const axios = require('axios');
2+
3+
const getMetaFromUrl = async (url) => {
4+
const envJson = await new Promise((resolve) => {
5+
axios
6+
.get(`${url}microfrontends/meta.json`)
7+
.then((response) => response.data)
8+
.then((json) => {
9+
resolve(json);
10+
});
11+
});
12+
Object.values(envJson).forEach((envJson) => {
13+
envJson.js = envJson.js.map((pathToFile) => pathToFile.replace('./', url));
14+
});
15+
16+
return envJson;
17+
};
18+
19+
module.exports = {
20+
getMetaFromUrl,
21+
};

packages/cli/scripts/utils/fs.js

+8-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { promises: fs, existsSync, rmdirSync } = require('fs');
22
const path = require('path');
3+
const reactAppRewired = require('react-app-rewired');
34

4-
const mkdir = (dir) => fs.mkdir(dir, { recursive: true });
5+
const mkdir = dir => fs.mkdir(dir, { recursive: true });
56
const isDirectory = async (source) => {
67
const stat = await fs.stat(source);
78
return stat.isDirectory();
@@ -31,7 +32,7 @@ const copyFolder = async (src, dest) => {
3132
} else {
3233
await copyFile(srcFile, destFile);
3334
}
34-
})
35+
}),
3536
);
3637
};
3738

@@ -58,31 +59,10 @@ const copyTemplateTo = async (template, pathToFolder) => {
5859

5960
const getDirsFrom = async (source) => {
6061
const paths = await fs.readdir(source);
61-
return paths.map((name) => path.join(source, name)).filter(isDirectory);
62+
return paths.map(name => path.join(source, name)).filter(isDirectory);
6263
};
6364

64-
const getReactAppRewiredPath = async () => {
65-
const options = [
66-
`${__dirname}/../../../../react-app-rewired/bin/index.js`,
67-
`${__dirname}/../../../node_modules/react-app-rewired/bin/index.js`,
68-
];
69-
70-
const libInfos = await Promise.all(
71-
options.map(async (option) => ({
72-
option,
73-
exists: await (async () => {
74-
try {
75-
await fs.access(option);
76-
return true;
77-
} catch (e) {
78-
return false;
79-
}
80-
})(),
81-
}))
82-
);
83-
84-
return libInfos.find((lib) => lib.exists).option;
85-
};
65+
const getReactAppRewiredPath = async () => `${reactAppRewired.paths.ownPath}/../react-app-rewired/bin/index.js`;
8666

8767
const rm = async (pathTo) => {
8868
if (!existsSync(pathTo)) return Promise.resolve();
@@ -99,10 +79,10 @@ const rm = async (pathTo) => {
9979
const getDirectories = async (source) => {
10080
const all = await fs.readdir(source);
10181
const mappedFolders = await Promise.all(
102-
all.map(async (fileOrFolder) => ({
82+
all.map(async fileOrFolder => ({
10383
fileOrFolder,
10484
isDirectory: await isDirectory(path.join(source, fileOrFolder)),
105-
}))
85+
})),
10686
);
10787

10888
return mappedFolders.filter(({ isDirectory: isDir }) => isDir).map(({ fileOrFolder: folder }) => folder);
@@ -120,7 +100,7 @@ const getAllFilesFromDir = async (dir, allFiles = []) => {
120100
} else {
121101
allFiles.push(fullDir);
122102
}
123-
})
103+
}),
124104
);
125105
return allFiles;
126106
};

packages/doc/docs/backoffice/developing.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ If you are using aws s3 as your artifact's host, login in into aws cli and then
1111

1212
Create a .env.development.local with some envs:
1313

14-
```
14+
```bash
1515
# Core configs
1616

1717
database_config='{"host":"my.host", "port": 5432, "username": "my-user", "password": "my-password", "database": "my-db}'
@@ -22,10 +22,38 @@ firebase_config='FIREBASE_CONFIG_JSON' # https://firebase.google.com/docs/web/se
2222
aws_profile='my-profile'
2323
AWS_SDK_LOAD_CONFIG=1 # needed to use a profile
2424
AWS_ARTIFACTS_BUCKET='some-bucket-with-all-artifacts'
25+
AWS_REGION='your-amazon-region'
2526
```
2627

2728
Run a script:
2829

2930
`./scripts/start.js`
3031

3132
Access your backoffice: http://localhost:3333/
33+
34+
## Generating db migration
35+
36+
Create a file at `/packages/server/ormconfig.json` with this content:
37+
38+
```json
39+
{
40+
"host": "my.host",
41+
"port": 5432,
42+
"username": "my-user",
43+
"password": "my-password",
44+
"database": "my-db",
45+
"type": "postgres",
46+
"migrations": ["migration/*.js"],
47+
"entities": ["./dist/src/entity/*.js"],
48+
"cli": {
49+
"migrationsDir": "migration"
50+
}
51+
}
52+
```
53+
54+
Make sure you have `typeorm` installed globally:
55+
56+
```bash
57+
npm i -g typeorm
58+
typeorm migration:generate -n <FEATURE_NAME>
59+
```

packages/doc/docs/cli/future.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
id: future
3+
title: Future
4+
---
5+
6+
## Just brainstorming with ideas
7+
8+
Cli should become 3 different packages:
9+
10+
- `scripts` to be used by apps and microfrontends to build/start (`cmra start` and `cmra build`)
11+
- `create` to create modules (`cmra create`)
12+
- `cli` to access backofffice API (does not exist today)
13+
- should `cmra publish` and `cmra deploy` be on cli or scripts?

packages/server/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ npm-debug.log*
2121
yarn-debug.log*
2222
yarn-error.log*
2323

24+
ormconfig.json
25+
2426
firebase-admin-sdk-key.json

packages/server/migration/1598384168452-PostRefactorin.ts

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class ProjectLink1598386312520 implements MigrationInterface {
4+
name = 'ProjectLink1598386312520';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "microfrontend" ADD "projectLink" character varying`);
8+
}
9+
10+
public async down(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(`ALTER TABLE "microfrontend" DROP COLUMN "projectLink"`);
12+
}
13+
}

packages/server/src/entity/integration/aws-s3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface AwsS3Config {
1111
token: string;
1212
}
1313

14-
const s3 = new AWS.S3({ region: 'sa-east-1' });
14+
const s3 = new AWS.S3({ region: process.env.AWS_REGION });
1515

1616
class AwsS3Integration extends Integration {
1717
static async downloadArtifact(artifactPath: string, opts: { path: MicrofrontendDeployPath }) {

packages/server/src/entity/microfrontend.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Microfrontend extends BaseEntity {
4343
@Column()
4444
public packageName: string = '';
4545

46-
@Column()
46+
@Column({ nullable: true })
4747
public projectLink: string = '';
4848

4949
@Column({ nullable: true })

0 commit comments

Comments
 (0)