-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathstart-application.mjs
123 lines (114 loc) · 3.29 KB
/
start-application.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { existsSync, readdirSync, readFileSync } from 'fs';
import { basename } from 'path';
import concurrently from 'concurrently';
import execa from 'execa';
import inquirer from 'inquirer';
import inquirerSearchList from 'inquirer-search-list';
/**
* Workspace location for all applications.
* @type {string}
*/
const applicationsWorkspace = 'packages/manager/apps';
/**
* Container application package name
* @type {string}
*/
const containerPackageName = '@ovh-ux/manager-container-app';
/**
* List all applications available for a given workspace.
* @return {Array} Applications' list.
*/
const getApplications = () =>
readdirSync(applicationsWorkspace, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map(({ name }) => ({ application: name }))
.filter(({ application }) =>
existsSync(`${applicationsWorkspace}/${application}/package.json`),
)
.map(({ application }) => {
const data = readFileSync(
`${applicationsWorkspace}/${application}/package.json`,
'utf8',
);
const { name, regions } = JSON.parse(data);
// Skip scoped package name.
// `@ovh-ux/foo` => `foo`.
const [, formatedName] = name.split('/');
return {
name: formatedName,
value: name,
regions,
};
});
/**
* Ask for both packageName and region to start the corresponding application.
*/
const questions = [
{
type: 'search-list',
name: 'packageName',
message: 'Which application do you want to start?',
choices: getApplications,
},
{
type: 'list',
name: 'region',
message: 'Please specify the region:',
choices({ packageName }) {
const { regions } = getApplications().find(
({ value }) => value === packageName,
);
if (!regions) {
throw new Error(`No regions found on ${packageName} package.json`);
}
return regions;
},
},
{
type: 'confirm',
name: 'container',
message: 'Start the application inside the container?',
default: true,
// Skip for container
when: ({ packageName }) => packageName !== containerPackageName,
},
];
async function getApplicationId(packageName) {
return basename(
JSON.parse((await execa('yarn', ['workspaces', 'info'])).stdout)[
packageName
].location,
);
}
inquirer.registerPrompt('search-list', inquirerSearchList);
inquirer
.prompt(questions)
.then(async ({ packageName, region = 'EU', container = false }) => {
/**
* Region is defined as an environment variable which is used by the webpack
* development server.
*
* {@link https://github.com/ovh/manager/tree/master/packages/manager/tools/webpack-dev-server#env | webpack dev server }
*/
process.env.REGION = region;
try {
if (container) {
const appId = await getApplicationId(packageName);
await concurrently(
[
`VITE_CONTAINER_APP=${appId} yarn workspace ${containerPackageName} run start:dev`,
`CONTAINER=1 yarn workspace ${packageName} run start:dev`,
],
{
raw: true,
},
);
} else {
await execa('yarn', ['workspace', packageName, 'run', 'start:dev'], {
stdio: 'inherit',
});
}
} catch (error) {
console.log(error);
}
});