Skip to content

Commit 2e235ab

Browse files
authored
Bundled web command (#2853)
1 parent db947f4 commit 2e235ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+24323
-25
lines changed

projects/optic/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
"files": [
99
"/build",
1010
"/src",
11-
"/ci"
11+
"/ci",
12+
"/web"
1213
],
1314
"bin": "build/index.js",
1415
"repository": {
1516
"type": "git",
1617
"url": "https://github.com/opticdev/optic/edit/main/projects/optic"
1718
},
1819
"scripts": {
19-
"build": "yarn tsc --build --verbose && yarn babel ./src --out-dir ./build --extensions \".ts\",\".tsx\"",
20+
"build": "yarn tsc --build --verbose && yarn babel ./src --out-dir ./build --extensions \".ts\",\".tsx\" && yarn run build:web",
21+
"build:web": "cd web && npm install && npm run build",
2022
"clean": "rm -rf build",
2123
"test": "ENVIRONMENT=test jest --colors",
2224
"local:run": "yarn ts-node src/index.ts"
@@ -130,7 +132,8 @@
130132
"build/commands/ruleset/upload.js"
131133
],
132134
"assets": [
133-
"ci/configs/*"
135+
"ci/configs/*",
136+
"web/build/*"
134137
],
135138
"targets": [
136139
"node18-macos-arm64",

projects/optic/src/commands/diff/diff-all.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ async function openWebpage(
581581
changelogData
582582
);
583583
analyticsData.compressedDataLength = compressedData.length;
584-
url = `${config.client.getWebBase()}/cli/diff#${compressedData}`;
584+
const baseHtml = path.resolve(__dirname, '../../../web/build/index.html');
585+
url = `${baseHtml}#${compressedData}`;
585586
}
586587
trackEvent('optic.diff_all.view_web', analyticsData);
587588

projects/optic/src/commands/diff/diff.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,11 @@ const getDiffAction =
496496
);
497497
analyticsData.compressedDataLength = compressedData.length;
498498
logger.info('Opening up diff in web view');
499-
maybeChangelogUrl = `${config.client.getWebBase()}/cli/diff#${compressedData}`;
499+
const baseHtml = path.resolve(
500+
__dirname,
501+
'../../../web/build/index.html'
502+
);
503+
maybeChangelogUrl = `${baseHtml}#${compressedData}`;
500504
await flushEvents();
501505
}
502506
trackEvent('optic.diff.view_web', analyticsData);

projects/optic/src/commands/lint/lint.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Command, Option } from 'commander';
2+
import p from 'path';
23

34
import { compute } from '../diff/compute';
45
import { loadSpec, ParseResult } from '../../utils/spec-loaders';
@@ -123,9 +124,8 @@ const getLintAction =
123124

124125
trackEvent('optic.lint.view_web', analyticsData);
125126
await flushEvents();
126-
await openUrl(
127-
`${config.client.getWebBase()}/cli/diff#${compressedData}`
128-
);
127+
const baseHtml = p.resolve(__dirname, '../../../web/build/index.html');
128+
await openUrl(`${baseHtml}#${compressedData}`);
129129
}
130130

131131
if (failuresForSeverity > 0) {

projects/optic/src/utils/open-url.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@ import path from 'path';
66
const tmpDirectory = os.tmpdir();
77

88
export const openUrl = async (url: string) => {
9-
try {
10-
await open(url, {
11-
wait: false,
12-
});
13-
} catch (e) {
14-
if (e instanceof Error && /ENAMETOOLONG/i.test(e.message)) {
15-
const tmpHtmlPath = path.join(tmpDirectory, 'optic', 'tmp-web.html');
16-
await fs.mkdir(path.dirname(tmpHtmlPath), { recursive: true });
9+
const tmpHtmlPath = path.join(tmpDirectory, 'optic', 'tmp-web.html');
10+
await fs.mkdir(path.dirname(tmpHtmlPath), { recursive: true });
1711

18-
await fs.writeFile(
19-
tmpHtmlPath,
20-
`<!DOCTYPE html><html><body><script type="text/javascript">window.location.replace("${url}")</script></body></html>`
21-
);
12+
await fs.writeFile(
13+
tmpHtmlPath,
14+
`<!DOCTYPE html><html><body><script type="text/javascript">window.location.replace("${url}")</script></body></html>`
15+
);
2216

23-
await open(tmpHtmlPath);
24-
} else {
25-
throw e;
26-
}
27-
}
17+
await open(tmpHtmlPath);
2818
};

projects/optic/web/.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

projects/optic/web/.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.test.ts
2+
*.test.ts.snap

projects/optic/web/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.\
15+
You will also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35+
36+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39+
40+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).

projects/optic/web/craco.config.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = {
4+
webpack: {
5+
alias: {
6+
fs: false,
7+
path: false,
8+
// Alias fallbacks for node files with `node:` prefix removed
9+
// In the future, we should split out our dependencies into node + browser
10+
'fs/promises': false,
11+
zlib: false,
12+
os: false,
13+
url: false,
14+
assert: false,
15+
'graceful-fs': false,
16+
child_process: false,
17+
'@stoplight/spectral-ruleset-bundler/with-loader': false,
18+
},
19+
resolve: {
20+
fallback: {
21+
buffer: require.resolve('buffer/'),
22+
'@stoplight/spectral-ruleset-bundler/with-loader': false,
23+
},
24+
},
25+
plugins: {
26+
add: [
27+
// https://github.com/webpack/webpack/issues/13290#issuecomment-1188760779
28+
// in combination with the fallbacks above
29+
new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => {
30+
resource.request = resource.request.replace(/^node:/, '');
31+
}),
32+
],
33+
},
34+
},
35+
};

0 commit comments

Comments
 (0)