-
Notifications
You must be signed in to change notification settings - Fork 66
Handle local type definitions in Monaco #2883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
josephjclark
wants to merge
8
commits into
main
Choose a base branch
from
local-adaptor-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7f70acf
editor: handle @local typedefs
josephjclark 3ae97aa
tidy
josephjclark fc441af
editor: fix type tracking issue
josephjclark 9393daa
remove log line
josephjclark 1a04c2b
standardise regex
josephjclark 8dc2f5e
slightly improve URL handling
josephjclark 79a66d5
changelog
josephjclark c6478d8
revert lib stuff
josephjclark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,39 @@ import type { editor } from 'monaco-editor'; | |
| import type { EditorProps as MonacoProps } from '@monaco-editor/react'; | ||
|
|
||
| import { MonacoEditor, type Monaco } from '../monaco'; | ||
| import { fetchDTSListing, fetchFile } from '@openfn/describe-package'; | ||
| import * as describe from '@openfn/describe-package'; | ||
| import createCompletionProvider from './magic-completion'; | ||
| import { initiateSaveAndRun } from '../common'; | ||
|
|
||
| const LOCAL_ADAPTORS_ROOT = 'http://localhost:5000'; | ||
josephjclark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async function* fetchDTSListing(specifier: string) { | ||
| if (specifier.endsWith('@local')) { | ||
| const lang = specifier | ||
| .replace('@local', '') | ||
| .replace('@openfn/language-', ''); | ||
| const url = `${LOCAL_ADAPTORS_ROOT}/packages/${lang}/types`; | ||
| const r = await fetch(url, { headers: { Accept: 'application/json' } }); | ||
| const json = await r.json(); | ||
| for (const f of json.files) { | ||
| yield `/types/${f.base}`; | ||
| } | ||
| } else { | ||
| return describe.fetchDTSListing(specifier); | ||
| } | ||
| } | ||
| const fetchFile = async (path: string) => { | ||
| if (path.includes('@local')) { | ||
| path = path.replace('@openfn/language-', '').replace('@local', ''); | ||
| const url = `${LOCAL_ADAPTORS_ROOT}/packages/${path}`; | ||
| return fetch(url, { headers: { Accept: 'text/plain' } }).then(r => | ||
| r.text() | ||
| ); | ||
| } else { | ||
| return describe.fetchFile(path); | ||
| } | ||
| }; | ||
|
|
||
| // static imports for core lib | ||
| import dts_es5 from './lib/es5.min.dts'; | ||
|
|
||
|
|
@@ -95,6 +124,8 @@ type Lib = { | |
| }; | ||
|
|
||
| async function loadDTS(specifier: string): Promise<Lib[]> { | ||
| const useLocal = specifier.endsWith('@local'); | ||
|
|
||
| // Work out the module name from the specifier | ||
| // (his gets a bit tricky with @openfn/ module names) | ||
| const nameParts = specifier.split('@'); | ||
|
|
@@ -107,16 +138,18 @@ async function loadDTS(specifier: string): Promise<Lib[]> { | |
| // TODO maybe we need other dependencies too? collections? | ||
| if (name !== '@openfn/language-common') { | ||
| const pkg = await fetchFile(`${specifier}/package.json`); | ||
| const commonVersion = JSON.parse(pkg || '{}').dependencies?.[ | ||
| '@openfn/language-common' | ||
| ]; | ||
|
|
||
| // jsDeliver doesn't appear to support semver range syntax (^1.0.0, 1.x, ~1.1.0) | ||
| const commonVersionMatch = commonVersion?.match(/^\d+\.\d+\.\d+/); | ||
| if (!commonVersionMatch) { | ||
| console.warn( | ||
| `@openfn/language-common@${commonVersion} contains semver range syntax.` | ||
| ); | ||
| const commonVersion = useLocal | ||
| ? 'local' | ||
| : JSON.parse(pkg || '{}').dependencies?.['@openfn/language-common']; | ||
|
|
||
| if (!useLocal) { | ||
| // jsDeliver doesn't appear to support semver range syntax (^1.0.0, 1.x, ~1.1.0) | ||
| const commonVersionMatch = commonVersion?.match(/^\d+\.\d+\.\d+/); | ||
| if (!commonVersionMatch) { | ||
| console.warn( | ||
| `@openfn/language-common@${commonVersion} contains semver range syntax.` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const commonSpecifier = `@openfn/language-common@${commonVersion.replace( | ||
|
|
@@ -127,10 +160,12 @@ async function loadDTS(specifier: string): Promise<Lib[]> { | |
| if (!filePath.startsWith('node_modules')) { | ||
| // Load every common typedef into the common module | ||
| let content = await fetchFile(`${commonSpecifier}${filePath}`); | ||
| content = content.replace(/\* +@(.+?)\*\//gs, '*/'); | ||
| results.push({ | ||
| content: `declare module '@openfn/language-common' { ${content} }`, | ||
| }); | ||
| if (!content.match(/<!doctype html>/i)) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO check this doctype stuff - this is from |
||
| content = content.replace(/\* +@(.+?)\*\//gs, '*/'); | ||
| results.push({ | ||
| content: `declare module '@openfn/language-common' { ${content} }`, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -144,6 +179,13 @@ async function loadDTS(specifier: string): Promise<Lib[]> { | |
| for await (const filePath of fetchDTSListing(specifier)) { | ||
| if (!filePath.startsWith('node_modules')) { | ||
| let content = await fetchFile(`${specifier}${filePath}`); | ||
| if (content.match(/<!doctype html>/i)) { | ||
| continue; | ||
| } | ||
| // Convert relative paths | ||
| content = content | ||
| .replace(/from '\.\//g, `from '${name}/`) | ||
| .replace(/import '\.\//g, `import '${name}/`); | ||
|
|
||
| // Remove js doc annotations | ||
| // this regex means: find a * then an @ (with 1+ space in between), then match everything up to a closing comment */ | ||
|
|
@@ -154,9 +196,6 @@ async function loadDTS(specifier: string): Promise<Lib[]> { | |
|
|
||
| // Import the index as the global namespace - but take care to convert all paths to absolute | ||
| if (fileName === 'index' || fileName === 'Adaptor') { | ||
| content = content.replace(/from '\.\//g, `from '${name}/`); | ||
| content = content.replace(/import '\.\//g, `import '${name}/`); | ||
|
|
||
| // It turns out that "export * as " seems to straight up not work in Monaco | ||
| // So this little hack will refactor import statements in a way that works | ||
| content = content.replace( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.