Skip to content

Commit

Permalink
Bundled web command (#2853)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Aug 7, 2024
1 parent db947f4 commit 2e235ab
Show file tree
Hide file tree
Showing 51 changed files with 24,323 additions and 25 deletions.
9 changes: 6 additions & 3 deletions projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
"files": [
"/build",
"/src",
"/ci"
"/ci",
"/web"
],
"bin": "build/index.js",
"repository": {
"type": "git",
"url": "https://github.com/opticdev/optic/edit/main/projects/optic"
},
"scripts": {
"build": "yarn tsc --build --verbose && yarn babel ./src --out-dir ./build --extensions \".ts\",\".tsx\"",
"build": "yarn tsc --build --verbose && yarn babel ./src --out-dir ./build --extensions \".ts\",\".tsx\" && yarn run build:web",
"build:web": "cd web && npm install && npm run build",
"clean": "rm -rf build",
"test": "ENVIRONMENT=test jest --colors",
"local:run": "yarn ts-node src/index.ts"
Expand Down Expand Up @@ -130,7 +132,8 @@
"build/commands/ruleset/upload.js"
],
"assets": [
"ci/configs/*"
"ci/configs/*",
"web/build/*"
],
"targets": [
"node18-macos-arm64",
Expand Down
3 changes: 2 additions & 1 deletion projects/optic/src/commands/diff/diff-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ async function openWebpage(
changelogData
);
analyticsData.compressedDataLength = compressedData.length;
url = `${config.client.getWebBase()}/cli/diff#${compressedData}`;
const baseHtml = path.resolve(__dirname, '../../../web/build/index.html');
url = `${baseHtml}#${compressedData}`;
}
trackEvent('optic.diff_all.view_web', analyticsData);

Expand Down
6 changes: 5 additions & 1 deletion projects/optic/src/commands/diff/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ const getDiffAction =
);
analyticsData.compressedDataLength = compressedData.length;
logger.info('Opening up diff in web view');
maybeChangelogUrl = `${config.client.getWebBase()}/cli/diff#${compressedData}`;
const baseHtml = path.resolve(
__dirname,
'../../../web/build/index.html'
);
maybeChangelogUrl = `${baseHtml}#${compressedData}`;
await flushEvents();
}
trackEvent('optic.diff.view_web', analyticsData);
Expand Down
6 changes: 3 additions & 3 deletions projects/optic/src/commands/lint/lint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command, Option } from 'commander';
import p from 'path';

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

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

if (failuresForSeverity > 0) {
Expand Down
24 changes: 7 additions & 17 deletions projects/optic/src/utils/open-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import path from 'path';
const tmpDirectory = os.tmpdir();

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

await fs.writeFile(
tmpHtmlPath,
`<!DOCTYPE html><html><body><script type="text/javascript">window.location.replace("${url}")</script></body></html>`
);
await fs.writeFile(
tmpHtmlPath,
`<!DOCTYPE html><html><body><script type="text/javascript">window.location.replace("${url}")</script></body></html>`
);

await open(tmpHtmlPath);
} else {
throw e;
}
}
await open(tmpHtmlPath);
};
23 changes: 23 additions & 0 deletions projects/optic/web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2 changes: 2 additions & 0 deletions projects/optic/web/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.test.ts
*.test.ts.snap
46 changes: 46 additions & 0 deletions projects/optic/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

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.

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.

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.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
35 changes: 35 additions & 0 deletions projects/optic/web/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const webpack = require('webpack');

module.exports = {
webpack: {
alias: {
fs: false,
path: false,
// Alias fallbacks for node files with `node:` prefix removed
// In the future, we should split out our dependencies into node + browser
'fs/promises': false,
zlib: false,
os: false,
url: false,
assert: false,
'graceful-fs': false,
child_process: false,
'@stoplight/spectral-ruleset-bundler/with-loader': false,
},
resolve: {
fallback: {
buffer: require.resolve('buffer/'),
'@stoplight/spectral-ruleset-bundler/with-loader': false,
},
},
plugins: {
add: [
// https://github.com/webpack/webpack/issues/13290#issuecomment-1188760779
// in combination with the fallbacks above
new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => {
resource.request = resource.request.replace(/^node:/, '');
}),
],
},
},
};
Loading

0 comments on commit 2e235ab

Please sign in to comment.