-
Notifications
You must be signed in to change notification settings - Fork 55
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
base: main
Are you sure you want to change the base?
Changes from all commits
7f70acf
3ae97aa
fc441af
9393daa
1a04c2b
8dc2f5e
79a66d5
c6478d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
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: update this constant when the static fileserver is set up |
||
|
||
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)) { | ||
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( | ||
|
Uh oh!
There was an error while loading. Please reload this page.