@equinor/roma-nx-plugin 1.1.0 → 2.0.0-BETA.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/migrations.json +80 -0
- package/package.json +7 -6
- package/src/executors/build/build.d.ts +4 -0
- package/src/executors/build/build.js +76 -0
- package/src/executors/build/build.js.map +1 -0
- package/src/executors/build/executor.d.ts +5 -0
- package/src/executors/build/executor.js +8 -0
- package/src/executors/build/executor.js.map +1 -0
- package/src/executors/build/minify.d.ts +2 -0
- package/src/executors/build/minify.js +22 -0
- package/src/executors/build/minify.js.map +1 -0
- package/src/executors/component-test/executor.d.ts +4 -0
- package/src/executors/component-test/executor.js +48 -0
- package/src/executors/component-test/executor.js.map +1 -0
- package/src/executors/create-vite-config.d.ts +3 -0
- package/src/executors/create-vite-config.js +106 -0
- package/src/executors/create-vite-config.js.map +1 -0
- package/src/executors/executor-context.d.ts +2 -0
- package/src/executors/executor-context.js +101 -0
- package/src/executors/executor-context.js.map +1 -0
- package/src/executors/publish/executor.d.ts +5 -0
- package/src/executors/publish/executor.js +24 -0
- package/src/executors/publish/executor.js.map +1 -0
- package/src/executors/publish/manifest-model.d.ts +29 -0
- package/src/executors/publish/manifest-model.js +3 -0
- package/src/executors/publish/manifest-model.js.map +1 -0
- package/src/executors/publish/publish-script.d.ts +8 -0
- package/src/executors/publish/publish-script.js +193 -0
- package/src/executors/publish/publish-script.js.map +1 -0
- package/src/executors/serve/executor.d.ts +3 -0
- package/src/executors/serve/executor.js +17 -0
- package/src/executors/serve/executor.js.map +1 -0
- package/src/executors/serve/get-manifest.d.ts +4 -0
- package/src/executors/serve/get-manifest.js +20 -0
- package/src/executors/serve/get-manifest.js.map +1 -0
- package/src/executors/serve/server.d.ts +3 -0
- package/src/executors/serve/server.js +179 -0
- package/src/executors/serve/server.js.map +1 -0
- package/src/executors/utils/get-vite-config.d.ts +1 -0
- package/src/executors/utils/get-vite-config.js +13 -0
- package/src/executors/utils/get-vite-config.js.map +1 -0
- package/src/executors/utils/logger.d.ts +24 -0
- package/src/executors/utils/logger.js +69 -0
- package/src/executors/utils/logger.js.map +1 -0
- package/src/generators/app/data/modules.d.ts +19 -0
- package/src/generators/app/data/modules.js +23 -0
- package/src/generators/app/data/modules.js.map +1 -0
- package/src/generators/app/generator.d.ts +5 -0
- package/src/generators/app/generator.js +101 -0
- package/src/generators/app/generator.js.map +1 -0
- package/src/generators/component-test/files/cypress/support/commands.d.ts +0 -0
- package/src/generators/component-test/files/cypress/support/{commands.ts → commands.js} +2 -1
- package/src/generators/component-test/files/cypress/support/commands.js.map +1 -0
- package/src/generators/component-test/files/cypress/support/component.d.ts +9 -0
- package/src/generators/component-test/files/cypress/support/{component.ts → component.js} +7 -21
- package/src/generators/component-test/files/cypress/support/component.js.map +1 -0
- package/src/generators/component-test/generator.d.ts +4 -0
- package/src/generators/component-test/generator.js +36 -0
- package/src/generators/component-test/generator.js.map +1 -0
- package/src/generators/preset/generator.d.ts +4 -0
- package/src/generators/preset/generator.js +13 -0
- package/src/generators/preset/generator.js.map +1 -0
- package/src/index.js +5 -0
- package/src/index.js.map +1 -0
- package/src/migrations/add-publish-target/add-publish-target.d.ts +2 -0
- package/src/migrations/add-publish-target/add-publish-target.js +32 -0
- package/src/migrations/add-publish-target/add-publish-target.js.map +1 -0
- package/src/migrations/migrate-fusion-manifest/migrate-fusion-manifest.d.ts +2 -0
- package/src/migrations/migrate-fusion-manifest/migrate-fusion-manifest.js +15 -0
- package/src/migrations/migrate-fusion-manifest/migrate-fusion-manifest.js.map +1 -0
- package/src/migrations/migrate-fusion-manifest/old-format.json +21 -0
- package/src/migrations/migrate-fusion-manifest/update-manifest.d.ts +6 -0
- package/src/migrations/migrate-fusion-manifest/update-manifest.js +26 -0
- package/src/migrations/migrate-fusion-manifest/update-manifest.js.map +1 -0
- package/src/migrations/move-fusion-manifest/move-fusion-manifest.d.ts +2 -0
- package/src/migrations/move-fusion-manifest/move-fusion-manifest.js +30 -0
- package/src/migrations/move-fusion-manifest/move-fusion-manifest.js.map +1 -0
- package/src/migrations/remove-fusion-cli/remove-fusion-cli.d.ts +2 -0
- package/src/migrations/remove-fusion-cli/remove-fusion-cli.js +48 -0
- package/src/migrations/remove-fusion-cli/remove-fusion-cli.js.map +1 -0
- package/.eslintrc.json +0 -37
- package/jest.config.ts +0 -13
- package/manifest-schema.json +0 -80
- package/project.json +0 -65
- package/src/executors/build/build.ts +0 -100
- package/src/executors/build/executor.spec.ts +0 -13
- package/src/executors/build/executor.ts +0 -10
- package/src/executors/build/minify.ts +0 -20
- package/src/executors/component-test/executor.spec.ts +0 -18
- package/src/executors/component-test/executor.ts +0 -57
- package/src/executors/create-vite-config.ts +0 -133
- package/src/executors/executor-context.ts +0 -102
- package/src/executors/publish/executor.ts +0 -29
- package/src/executors/publish/manifest-model.ts +0 -33
- package/src/executors/publish/publish-script.spec.ts +0 -118
- package/src/executors/publish/publish-script.ts +0 -210
- package/src/executors/serve/executor.ts +0 -19
- package/src/executors/serve/get-manifest.ts +0 -20
- package/src/executors/serve/server.ts +0 -250
- package/src/executors/utils/get-vite-config.ts +0 -9
- package/src/executors/utils/logger.ts +0 -90
- package/src/generators/app/data/modules.ts +0 -19
- package/src/generators/app/generator.spec.ts +0 -86
- package/src/generators/app/generator.ts +0 -141
- package/src/generators/component-test/generator.spec.ts +0 -79
- package/src/generators/component-test/generator.ts +0 -67
- package/src/generators/preset/generator.spec.ts +0 -38
- package/src/generators/preset/generator.ts +0 -18
- package/src/migrations/add-publish-target/add-publish-target.spec.ts +0 -60
- package/src/migrations/add-publish-target/add-publish-target.ts +0 -28
- package/src/migrations/move-fusion-manifest/move-fusion-manifest.ts +0 -35
- package/src/migrations/remove-fusion-cli/remove-fusion-cli.ts +0 -70
- package/tsconfig.json +0 -17
- package/tsconfig.lib.json +0 -20
- package/tsconfig.spec.json +0 -26
- package/vite.config.ts +0 -31
- /package/src/{index.ts → index.d.ts} +0 -0
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { publishApplication } from './publish-script';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
|
-
import { AppManifest } from '@equinor/fusion-framework-module-app';
|
|
5
|
-
|
|
6
|
-
// Mock external dependencies
|
|
7
|
-
vi.mock('@azure/identity', () => ({
|
|
8
|
-
DefaultAzureCredential: vi.fn().mockImplementation(() => ({
|
|
9
|
-
getToken: vi.fn().mockResolvedValue({ token: 'mock-token' }),
|
|
10
|
-
})),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
vi.mock('node:zlib', () => ({
|
|
14
|
-
brotliCompress: vi.fn((input, callback) =>
|
|
15
|
-
callback(null, Buffer.from('mock-brotli'))
|
|
16
|
-
),
|
|
17
|
-
}));
|
|
18
|
-
|
|
19
|
-
const readFileMocks = vi.hoisted(() => {
|
|
20
|
-
return {
|
|
21
|
-
readFile: vi.fn(),
|
|
22
|
-
};
|
|
23
|
-
});
|
|
24
|
-
vi.mock('node:fs/promises', () => ({
|
|
25
|
-
readFile: readFileMocks.readFile,
|
|
26
|
-
}));
|
|
27
|
-
|
|
28
|
-
// Override fetch
|
|
29
|
-
// @ts-expect-error Only needs a partial implementation
|
|
30
|
-
global.fetch = vi.fn((url: string, req) => {
|
|
31
|
-
if (url.includes('/api/apps/categories')) {
|
|
32
|
-
return Promise.resolve({
|
|
33
|
-
status: 200,
|
|
34
|
-
json: () => Promise.resolve([{ id: 'category1', name: 'Category 1' }]),
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Mock response for the publish manifest URL
|
|
39
|
-
if (url.includes('/api/apps') && req?.method === 'POST') {
|
|
40
|
-
return Promise.resolve({
|
|
41
|
-
status: 200,
|
|
42
|
-
json: () => Promise.resolve({ success: true }),
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Mock response for the upload bundle URL
|
|
47
|
-
if (url.includes('/api/apps/key/')) {
|
|
48
|
-
return Promise.resolve({
|
|
49
|
-
status: 200,
|
|
50
|
-
json: () => Promise.resolve({ success: true }),
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Default to a network error if the URL is not recognized
|
|
55
|
-
return Promise.reject(new Error('Network error'));
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('publishApplication', () => {
|
|
59
|
-
let manifest: string;
|
|
60
|
-
beforeEach(() => {
|
|
61
|
-
vi.clearAllMocks();
|
|
62
|
-
manifest = JSON.stringify({
|
|
63
|
-
categoryId: 'category1',
|
|
64
|
-
key: 'test',
|
|
65
|
-
name: 'Test',
|
|
66
|
-
description: 'Test',
|
|
67
|
-
entry:
|
|
68
|
-
'https://romadev.equinor.com/cdn/applications/bundle/test/app-bundle.js?version=1.0.0',
|
|
69
|
-
version: {
|
|
70
|
-
major: 1,
|
|
71
|
-
minor: 0,
|
|
72
|
-
patch: 0,
|
|
73
|
-
},
|
|
74
|
-
} as unknown as AppManifest);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should publish application successfully', async () => {
|
|
78
|
-
// Define your input parameters
|
|
79
|
-
readFileMocks.readFile.mockResolvedValue(Buffer.from(manifest));
|
|
80
|
-
const pathToOutput = '/path/to/output';
|
|
81
|
-
const options = { environment: 'dev', randomVersion: false } as const;
|
|
82
|
-
|
|
83
|
-
// Call the function to test
|
|
84
|
-
await expect(
|
|
85
|
-
publishApplication(pathToOutput, options)
|
|
86
|
-
).resolves.not.toThrow();
|
|
87
|
-
|
|
88
|
-
// Check if readFile was called with the correct path
|
|
89
|
-
expect(readFile).toHaveBeenCalledWith('/path/to/output/app-bundle.js');
|
|
90
|
-
// Check if fetch was called with the correct parameters
|
|
91
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
92
|
-
expect.stringContaining('api/apps/categories'),
|
|
93
|
-
expect.anything()
|
|
94
|
-
);
|
|
95
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
96
|
-
expect.stringMatching(/api\/apps$/),
|
|
97
|
-
expect.objectContaining({
|
|
98
|
-
body: manifest,
|
|
99
|
-
})
|
|
100
|
-
);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should throw if manifest is missing', async () => {
|
|
104
|
-
readFileMocks.readFile.mockResolvedValue(Buffer.from(''));
|
|
105
|
-
// Define your input parameters
|
|
106
|
-
const pathToOutput = '/path/to/output';
|
|
107
|
-
const options = { environment: 'dev', randomVersion: true } as const;
|
|
108
|
-
|
|
109
|
-
// Call the function to test
|
|
110
|
-
await expect(publishApplication(pathToOutput, options)).rejects.toThrow();
|
|
111
|
-
|
|
112
|
-
// Check if readFile was called with the correct path
|
|
113
|
-
expect(readFile).toHaveBeenCalledWith('/path/to/output/app-bundle.js');
|
|
114
|
-
expect(readFile).toHaveBeenCalledWith('/path/to/output/manifest.json');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// Add more test cases as needed
|
|
118
|
-
});
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { DefaultAzureCredential } from '@azure/identity';
|
|
4
|
-
import { Manifest } from './manifest-model';
|
|
5
|
-
import type { PublishExecutorSchema } from './schema';
|
|
6
|
-
import { brotliCompress } from 'node:zlib';
|
|
7
|
-
import { promisify } from 'node:util';
|
|
8
|
-
|
|
9
|
-
const brotli = promisify(brotliCompress);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* An enum representing the status of the publish operation.
|
|
13
|
-
* @enum {number}
|
|
14
|
-
*/
|
|
15
|
-
enum Status {
|
|
16
|
-
SUCCESS,
|
|
17
|
-
ERROR,
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* An object representing the configuration for the publish script.
|
|
22
|
-
*
|
|
23
|
-
* @property apiScopes - An array of API scopes.
|
|
24
|
-
* @property romaUri - The URI of the API.
|
|
25
|
-
* @property randomVersion - Whether we should generate a random version number.
|
|
26
|
-
*/
|
|
27
|
-
type Config = {
|
|
28
|
-
romaUri: string;
|
|
29
|
-
apiScopes: Array<string>;
|
|
30
|
-
randomVersion: boolean;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A map of environment names to their respective configurations.
|
|
35
|
-
*/
|
|
36
|
-
const configMap: Record<string, Config> = {
|
|
37
|
-
dev: {
|
|
38
|
-
romaUri: 'https://romadev.equinor.com',
|
|
39
|
-
randomVersion: true,
|
|
40
|
-
apiScopes: ['api://RomaWorkbenchApiNonProd/.default'],
|
|
41
|
-
},
|
|
42
|
-
qa: {
|
|
43
|
-
romaUri: 'https://romaqa.equinor.com',
|
|
44
|
-
randomVersion: true,
|
|
45
|
-
apiScopes: ['api://RomaWorkbenchApiNonProd/.default'],
|
|
46
|
-
},
|
|
47
|
-
production: {
|
|
48
|
-
romaUri: 'https://roma.equinor.com',
|
|
49
|
-
randomVersion: false,
|
|
50
|
-
apiScopes: ['api://RomaWorkbenchApi/.default'],
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Auth token to publish the application
|
|
56
|
-
*/
|
|
57
|
-
let token: string;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Publishes the application to the specified environment.
|
|
61
|
-
*
|
|
62
|
-
* @param pathToOutput - The path to the output directory.
|
|
63
|
-
* @param options - The options for the publish operation.
|
|
64
|
-
* @param version - The version of the application.
|
|
65
|
-
*/
|
|
66
|
-
export const publishApplication = async (
|
|
67
|
-
pathToOutput: string,
|
|
68
|
-
options: PublishExecutorSchema
|
|
69
|
-
) => {
|
|
70
|
-
let env = options.environment;
|
|
71
|
-
// @ts-expect-error Need to rewrite this as nx expects production, not prod.
|
|
72
|
-
if (env === 'prod') env = 'production';
|
|
73
|
-
const config = configMap[env];
|
|
74
|
-
config.randomVersion = options.randomVersion ?? config.randomVersion;
|
|
75
|
-
const bundle = await readFile(join(pathToOutput, 'app-bundle.js'));
|
|
76
|
-
const manifest = await readFile(join(pathToOutput, 'manifest.json'));
|
|
77
|
-
const manifestJsonString = manifest.toString('utf-8');
|
|
78
|
-
const parsedManifest = JSON.parse(manifestJsonString) as Manifest;
|
|
79
|
-
const appName = parsedManifest.key;
|
|
80
|
-
const categories = await getCategories(config);
|
|
81
|
-
const categoryIds = categories.map((c) => c.id);
|
|
82
|
-
if (!categoryIds.includes(parsedManifest.categoryId)) {
|
|
83
|
-
console.log(
|
|
84
|
-
`Manifest validation failed: categoryId ${parsedManifest.categoryId} is not in list of categories`
|
|
85
|
-
);
|
|
86
|
-
console.log(`Valid categories:`);
|
|
87
|
-
console.table(categories, ['id', 'name']);
|
|
88
|
-
console.log(`Publish will now exit.`);
|
|
89
|
-
throw new Error(`ManifestValidationError`);
|
|
90
|
-
}
|
|
91
|
-
console.log(`Reading & uploading bundle for ${appName}`);
|
|
92
|
-
const bundleResult = await uploadBundle(appName, bundle, config);
|
|
93
|
-
if (bundleResult === Status.ERROR) {
|
|
94
|
-
throw new Error('Failed to upload application');
|
|
95
|
-
}
|
|
96
|
-
console.log(`Reading & uploading application manifest to ${config.romaUri}`);
|
|
97
|
-
|
|
98
|
-
const status = await publishManifest(parsedManifest, config);
|
|
99
|
-
if (status === Status.ERROR) {
|
|
100
|
-
throw new Error(`Failed to publish application ${appName}`);
|
|
101
|
-
} else {
|
|
102
|
-
console.log(`Successfully published application ${appName}`);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Authenticates the user and returns the authentication token.
|
|
108
|
-
*
|
|
109
|
-
* @returns The authentication token.
|
|
110
|
-
*/
|
|
111
|
-
async function authenticate(config: Config) {
|
|
112
|
-
if (token) return token;
|
|
113
|
-
const auth = new DefaultAzureCredential();
|
|
114
|
-
const tokenHolder = await auth.getToken(config.apiScopes);
|
|
115
|
-
token = tokenHolder.token;
|
|
116
|
-
|
|
117
|
-
return token;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Gets a list of valid roma categories
|
|
122
|
-
*
|
|
123
|
-
* @param config - The configuration for the environment.
|
|
124
|
-
* @returns The categories for roma.
|
|
125
|
-
*/
|
|
126
|
-
async function getCategories(
|
|
127
|
-
config: Config
|
|
128
|
-
): Promise<Array<{ id: string; name: string }>> {
|
|
129
|
-
const token = await authenticate(config);
|
|
130
|
-
const req = await fetch(`${config.romaUri}/api/apps/categories`, {
|
|
131
|
-
headers: {
|
|
132
|
-
Authorization: `Bearer ${token}`,
|
|
133
|
-
'Content-Type': 'application/json',
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
if (req.status >= 400) {
|
|
137
|
-
console.error('Failure', req.status);
|
|
138
|
-
return [];
|
|
139
|
-
} else {
|
|
140
|
-
return await req.json();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Publishes the manifest for the application.
|
|
146
|
-
*
|
|
147
|
-
* @param manifest - The manifest for the application.
|
|
148
|
-
* @param config - The configuration for the application.
|
|
149
|
-
* @returns The status of the publish operation.
|
|
150
|
-
*/
|
|
151
|
-
async function publishManifest(manifest: Manifest, config: Config) {
|
|
152
|
-
manifest.description = manifest.description || manifest.name;
|
|
153
|
-
const { major, minor, patch } = manifest.version;
|
|
154
|
-
const version = config.randomVersion
|
|
155
|
-
? Date.now()
|
|
156
|
-
: `${major}.${minor}.${patch}`;
|
|
157
|
-
manifest.entry = `${config.romaUri}/cdn/applications/bundle/${manifest.key}/app-bundle.js?version=${version}`;
|
|
158
|
-
const token = await authenticate(config);
|
|
159
|
-
const req = await fetch(`${config.romaUri}/api/apps`, {
|
|
160
|
-
method: 'POST',
|
|
161
|
-
headers: {
|
|
162
|
-
Authorization: `Bearer ${token}`,
|
|
163
|
-
'Content-Type': 'application/json',
|
|
164
|
-
},
|
|
165
|
-
body: JSON.stringify(manifest),
|
|
166
|
-
});
|
|
167
|
-
if (req.status < 400) {
|
|
168
|
-
return Status.SUCCESS;
|
|
169
|
-
} else {
|
|
170
|
-
console.error(
|
|
171
|
-
`Encountered a problem when publishing application: Received statuscode ${req.status}`
|
|
172
|
-
);
|
|
173
|
-
const err = await req.json();
|
|
174
|
-
console.error(err);
|
|
175
|
-
return Status.ERROR;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Uploads the bundle for the application to Azure Blob Storage.
|
|
181
|
-
*
|
|
182
|
-
* @param appName - The name of the application.
|
|
183
|
-
* @param bundle - The bundle for the application.
|
|
184
|
-
* @param config - The active config
|
|
185
|
-
*/
|
|
186
|
-
async function uploadBundle(appName: string, bundle: Buffer, config: Config) {
|
|
187
|
-
const fd = new FormData();
|
|
188
|
-
const br = await brotli(bundle);
|
|
189
|
-
fd.set('bundleFile', new Blob([br.buffer]));
|
|
190
|
-
|
|
191
|
-
const token = await authenticate(config);
|
|
192
|
-
const req = await fetch(`${config.romaUri}/api/apps/key/${appName}/bundle`, {
|
|
193
|
-
method: 'PUT',
|
|
194
|
-
headers: {
|
|
195
|
-
Authorization: `Bearer ${token}`,
|
|
196
|
-
},
|
|
197
|
-
body: fd,
|
|
198
|
-
});
|
|
199
|
-
if (req.status < 400) {
|
|
200
|
-
console.log(`Successfully uploaded bundle to ${appName}/app-bundle.js`);
|
|
201
|
-
return Status.SUCCESS;
|
|
202
|
-
} else {
|
|
203
|
-
console.error(
|
|
204
|
-
`Encountered a problem when publishing application: Received statuscode ${req.status}`
|
|
205
|
-
);
|
|
206
|
-
const err = await req.json();
|
|
207
|
-
console.error(err);
|
|
208
|
-
return Status.ERROR;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ServeExecutorSchema } from './schema';
|
|
2
|
-
import { ExecutorContext } from '@nx/devkit';
|
|
3
|
-
import { createServer } from './server';
|
|
4
|
-
|
|
5
|
-
export default async function runExecutor(
|
|
6
|
-
options: ServeExecutorSchema,
|
|
7
|
-
context: ExecutorContext
|
|
8
|
-
) {
|
|
9
|
-
const configs = [];
|
|
10
|
-
if (options.additionalProjects) {
|
|
11
|
-
options.additionalProjects.forEach((project) => {
|
|
12
|
-
const config = context.workspace.projects[project].targets.serve
|
|
13
|
-
.options as ServeExecutorSchema;
|
|
14
|
-
config.name = project;
|
|
15
|
-
configs.push(config);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
return createServer(options, context, configs);
|
|
19
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { join } from 'path';
|
|
2
|
-
import { AppManifest } from '@equinor/fusion-framework-module-app';
|
|
3
|
-
import { readJsonFile } from '@nx/devkit';
|
|
4
|
-
|
|
5
|
-
export function getManifest(root: string) {
|
|
6
|
-
let manifest: AppManifest;
|
|
7
|
-
try {
|
|
8
|
-
const packageJson = readJsonFile(join(root, 'package.json'));
|
|
9
|
-
const pkgManifest = packageJson.roma;
|
|
10
|
-
if (pkgManifest) {
|
|
11
|
-
manifest = {
|
|
12
|
-
...pkgManifest,
|
|
13
|
-
version: packageJson.version,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
} catch {
|
|
17
|
-
manifest = readJsonFile(join(root, 'fusion-manifest.json')) as AppManifest;
|
|
18
|
-
}
|
|
19
|
-
return manifest;
|
|
20
|
-
}
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
import { ServeExecutorSchema } from './schema';
|
|
3
|
-
|
|
4
|
-
const openBrowser = require('open');
|
|
5
|
-
const express = require('express');
|
|
6
|
-
const chalk = require('chalk');
|
|
7
|
-
const getPort = require('get-port-cjs');
|
|
8
|
-
import {
|
|
9
|
-
createServer as createViteServer,
|
|
10
|
-
LibraryOptions,
|
|
11
|
-
loadConfigFromFile,
|
|
12
|
-
} from 'vite';
|
|
13
|
-
import { resolve, join } from 'path';
|
|
14
|
-
import { readFileSync } from 'node:fs';
|
|
15
|
-
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
16
|
-
import { EnvironmentResponse } from '@equinor/fusion-framework-module-service-discovery';
|
|
17
|
-
import { AppManifest } from '@equinor/fusion-framework-module-app';
|
|
18
|
-
import { createRomaViteConfig } from '../create-vite-config';
|
|
19
|
-
import type { ExecutorContext } from '@nx/devkit';
|
|
20
|
-
import { getViteConfig } from '../utils/get-vite-config';
|
|
21
|
-
import { getManifest } from './get-manifest';
|
|
22
|
-
|
|
23
|
-
async function loadServices(portalTarget: string) {
|
|
24
|
-
try {
|
|
25
|
-
const req = await fetch(`${portalTarget}/_discovery/environments/current`);
|
|
26
|
-
const res = (await req.json()) as EnvironmentResponse;
|
|
27
|
-
return res.services
|
|
28
|
-
.filter((s) => s.uri.startsWith(portalTarget))
|
|
29
|
-
.map((s) => new URL(s.uri).pathname)
|
|
30
|
-
.filter((path) => path.length >= 3 && !path.startsWith('/apim/'));
|
|
31
|
-
} catch (ex) {
|
|
32
|
-
console.error(ex);
|
|
33
|
-
process.exit();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export async function createServer(
|
|
38
|
-
{
|
|
39
|
-
port: requestedPort = 3000,
|
|
40
|
-
devPortal = 'node_modules/@equinor/roma-framework/dev-portal',
|
|
41
|
-
portal: portalTarget = 'https://romadev.equinor.com',
|
|
42
|
-
open,
|
|
43
|
-
}: ServeExecutorSchema,
|
|
44
|
-
context: ExecutorContext,
|
|
45
|
-
additionalProjects?: Array<ServeExecutorSchema>
|
|
46
|
-
) {
|
|
47
|
-
// Get path to dev-portal, this would be a flag from executor.
|
|
48
|
-
const devPortalPath = resolve(process.cwd(), devPortal);
|
|
49
|
-
|
|
50
|
-
// Get available port, prefers requestedPort but falls back to another free port
|
|
51
|
-
const ports = Array.from(
|
|
52
|
-
new Set([requestedPort, ...getPort.portNumbers(3000, 30009)])
|
|
53
|
-
);
|
|
54
|
-
const port = (await getPort({ port: ports })) as number;
|
|
55
|
-
if (!ports.includes(port)) {
|
|
56
|
-
throw new Error(`All valid ports (3000-3009) are closed`);
|
|
57
|
-
}
|
|
58
|
-
console.clear();
|
|
59
|
-
|
|
60
|
-
if (port !== requestedPort) {
|
|
61
|
-
console.log(chalk.yellow(`Port ${requestedPort} is taken, using ${port}`));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const projectName = context.projectName;
|
|
65
|
-
const project = context.workspace.projects[projectName];
|
|
66
|
-
const base = resolve(context.root, project.root);
|
|
67
|
-
|
|
68
|
-
// Load user-defined viteConfig
|
|
69
|
-
const viteConfigPath = getViteConfig(base);
|
|
70
|
-
const userConfig = await loadConfigFromFile(
|
|
71
|
-
{
|
|
72
|
-
command: 'build',
|
|
73
|
-
mode: 'production',
|
|
74
|
-
},
|
|
75
|
-
viteConfigPath
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
// Create express application
|
|
79
|
-
const app = express();
|
|
80
|
-
app.set('x-powered-by', false);
|
|
81
|
-
app.use(express.static(devPortalPath));
|
|
82
|
-
|
|
83
|
-
// Load Roma services
|
|
84
|
-
const services = await loadServices(portalTarget);
|
|
85
|
-
// Add proxy middleware for all roma-services
|
|
86
|
-
services.forEach((p) =>
|
|
87
|
-
app.use(
|
|
88
|
-
p,
|
|
89
|
-
createProxyMiddleware(p, { target: portalTarget, changeOrigin: true })
|
|
90
|
-
)
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
// Range of port numbers for hmr, otherwise HMR won't work for multiple apps at the same time
|
|
94
|
-
const hmrPorts = getPort.portNumbers(40000, 45000);
|
|
95
|
-
const hmrPort = await getPort(hmrPorts);
|
|
96
|
-
|
|
97
|
-
const viteConfig = createRomaViteConfig(
|
|
98
|
-
userConfig.config,
|
|
99
|
-
context,
|
|
100
|
-
false,
|
|
101
|
-
hmrPort
|
|
102
|
-
);
|
|
103
|
-
// Create Vite server in middleware mode
|
|
104
|
-
const vite = await createViteServer(viteConfig);
|
|
105
|
-
|
|
106
|
-
// Apply vite-middleware to the express app
|
|
107
|
-
app.use(vite.middlewares);
|
|
108
|
-
|
|
109
|
-
app.use(
|
|
110
|
-
'/apim*',
|
|
111
|
-
createProxyMiddleware('/apim', { target: portalTarget, changeOrigin: true })
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
app.use(
|
|
115
|
-
// Custom proxy-setup for service discovery endpoint
|
|
116
|
-
'/_discovery*',
|
|
117
|
-
createProxyMiddleware('/_discovery', {
|
|
118
|
-
target: portalTarget,
|
|
119
|
-
changeOrigin: true, // Avoid API errors due to origin-conflicts
|
|
120
|
-
secure: false,
|
|
121
|
-
selfHandleResponse: true, // Tell the proxy we want to override the response
|
|
122
|
-
onProxyRes: (proxyRes, req, res) => {
|
|
123
|
-
const body: Array<Uint8Array> = [];
|
|
124
|
-
proxyRes.on('data', function (chunk: Uint8Array) {
|
|
125
|
-
body.push(chunk);
|
|
126
|
-
});
|
|
127
|
-
proxyRes.on('end', function () {
|
|
128
|
-
const bodyStr = Buffer.concat(body).toString();
|
|
129
|
-
const environment = JSON.parse(bodyStr) as EnvironmentResponse;
|
|
130
|
-
// Override the services, and direct all api's from <target> to go via this proxy.
|
|
131
|
-
environment.services.forEach((s) => {
|
|
132
|
-
if (s.uri.includes(portalTarget)) {
|
|
133
|
-
s.uri = s.uri.replace(portalTarget, `http://localhost:${port}`);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
res.end(JSON.stringify(environment));
|
|
137
|
-
});
|
|
138
|
-
},
|
|
139
|
-
})
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
// Extract entrypoint-url
|
|
143
|
-
const libConfig = vite.config.build.lib as LibraryOptions;
|
|
144
|
-
const entry = libConfig.entry as Record<string, string>;
|
|
145
|
-
|
|
146
|
-
// Custom proxy for app-list to add the current application
|
|
147
|
-
app.get(
|
|
148
|
-
'/api/apps',
|
|
149
|
-
createProxyMiddleware('/api/apps', {
|
|
150
|
-
target: portalTarget,
|
|
151
|
-
changeOrigin: true,
|
|
152
|
-
selfHandleResponse: true,
|
|
153
|
-
onProxyRes: (proxyRes, req, res) => {
|
|
154
|
-
const body: Array<Uint8Array> = [];
|
|
155
|
-
proxyRes.on('data', function (chunk: Uint8Array) {
|
|
156
|
-
body.push(chunk);
|
|
157
|
-
});
|
|
158
|
-
proxyRes.on('end', function () {
|
|
159
|
-
const bodyStr = Buffer.concat(body).toString();
|
|
160
|
-
const data = JSON.parse(bodyStr) as Array<AppManifest>;
|
|
161
|
-
const atIndex =
|
|
162
|
-
data?.findIndex((manifest) => manifest.key === projectName) ?? -1;
|
|
163
|
-
// If existing app, we need to change the entry-point.
|
|
164
|
-
if (atIndex > -1) {
|
|
165
|
-
data[atIndex].entry = `/${entry.self}`;
|
|
166
|
-
} else {
|
|
167
|
-
// New app, so adding it to the app-list
|
|
168
|
-
try {
|
|
169
|
-
// Read and parse fusion-manifest
|
|
170
|
-
const manifest = getManifest(vite.config.root);
|
|
171
|
-
manifest.entry = `/${entry.self}`;
|
|
172
|
-
data.push(manifest);
|
|
173
|
-
} catch (ex) {
|
|
174
|
-
throw new Error(
|
|
175
|
-
chalk.red(
|
|
176
|
-
`Fatal error: Failed to read fusion-manifest.json in ${vite.config.root}
|
|
177
|
-
Application will now exit.`
|
|
178
|
-
)
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (additionalProjects) {
|
|
183
|
-
additionalProjects.forEach((project) => {
|
|
184
|
-
const atIndex =
|
|
185
|
-
data?.findIndex((manifest) => manifest.key === project.name) ??
|
|
186
|
-
-1;
|
|
187
|
-
if (atIndex >= 0) {
|
|
188
|
-
data[atIndex] = {
|
|
189
|
-
...data[atIndex],
|
|
190
|
-
entry: `/${entry[project.name]}`,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
res.json(data);
|
|
196
|
-
});
|
|
197
|
-
},
|
|
198
|
-
})
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
// Roma portal api
|
|
202
|
-
app.use(
|
|
203
|
-
'/api/*',
|
|
204
|
-
createProxyMiddleware('/api', {
|
|
205
|
-
target: portalTarget,
|
|
206
|
-
changeOrigin: true,
|
|
207
|
-
})
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
// Roma SSE api
|
|
211
|
-
app.use(
|
|
212
|
-
'/eventstream/*',
|
|
213
|
-
createProxyMiddleware('/eventstream', {
|
|
214
|
-
target: portalTarget,
|
|
215
|
-
changeOrigin: true,
|
|
216
|
-
proxyTimeout: 1000 * 60 * 30,
|
|
217
|
-
})
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
// SPA-style routing
|
|
221
|
-
app.use('*', async (req, res) => {
|
|
222
|
-
const htmlRaw = readFileSync(join(devPortalPath + '/index.html'), 'utf-8');
|
|
223
|
-
const html = await vite.transformIndexHtml(req.url, htmlRaw);
|
|
224
|
-
res.send(html);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
return new Promise((resolve) => {
|
|
228
|
-
// listen to requested port
|
|
229
|
-
app.listen(port, () => {
|
|
230
|
-
console.log(chalk.green('Roma dev server listening on port ' + port));
|
|
231
|
-
if (open) {
|
|
232
|
-
console.log(
|
|
233
|
-
chalk.green(
|
|
234
|
-
`Opening the application at http://localhost:${port}/apps/${projectName}`
|
|
235
|
-
)
|
|
236
|
-
);
|
|
237
|
-
openBrowser(`http://localhost:${port}/apps/${projectName}`);
|
|
238
|
-
} else {
|
|
239
|
-
console.log(
|
|
240
|
-
chalk.green(
|
|
241
|
-
`Open the application at http://localhost:${port}/apps/${projectName}`
|
|
242
|
-
)
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
app.on('close', () => {
|
|
247
|
-
resolve({ success: true });
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { readdirSync } from 'node:fs';
|
|
2
|
-
import { resolve } from 'path';
|
|
3
|
-
|
|
4
|
-
export function getViteConfig(root: string) {
|
|
5
|
-
const files = readdirSync(root);
|
|
6
|
-
const file = files.find((f) => f.startsWith('vite.config'));
|
|
7
|
-
if (!file) throw new Error(`No vite config found!`);
|
|
8
|
-
return resolve(root, file);
|
|
9
|
-
}
|