-
-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from electron/type-definitions
Add TypeScript definitions for Electron
- Loading branch information
Showing
4 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import * as MonacoType from 'monaco-editor'; | ||
|
||
import { userData } from './constants'; | ||
|
||
const definitionPath = path.join(userData, 'electron-typedef'); | ||
|
||
|
||
/** | ||
* Fetch TypeScript definitions for the current version of Electron (online) | ||
* | ||
* @param {string} version | ||
* @returns {Promise<string>} | ||
*/ | ||
export function fetchTypeDefinitions(version: string): Promise<string> { | ||
const url = `https://unpkg.com/electron@${version}/electron.d.ts`; | ||
|
||
return window.fetch(url) | ||
.then((response) => response.text()) | ||
.catch((error) => { | ||
console.warn(`Fetch Types: Could not fetch definitions`, error); | ||
return ''; | ||
}); | ||
} | ||
|
||
/** | ||
* Get the path for offline TypeScript definitions | ||
* | ||
* @param {string} version | ||
* @returns {string} | ||
*/ | ||
export function getOfflineTypeDefinitionPath(version: string): string { | ||
return path.join(definitionPath, 'electron-typedef', version, 'electron.d.ts'); | ||
} | ||
|
||
/** | ||
* Do TypeScript definitions for the current version of Electron exist on disk? | ||
* | ||
* @param {string} version | ||
* @returns {boolean} | ||
*/ | ||
export function getOfflineTypeDefinitions(version: string): boolean { | ||
return fs.existsSync(getOfflineTypeDefinitionPath(version)); | ||
} | ||
|
||
/** | ||
* Get TypeScript defintions for a version of Electron. If none can't be | ||
* found, returns null. | ||
* | ||
* @param {string} version | ||
* @returns {void} | ||
*/ | ||
export async function getTypeDefinitions(version: string): Promise<string | null> { | ||
await fs.mkdirp(definitionPath); | ||
|
||
const offlinePath = getOfflineTypeDefinitionPath(version); | ||
|
||
if (getOfflineTypeDefinitions(version)) { | ||
try { | ||
return fs.readFile(offlinePath, 'utf-8'); | ||
} catch (error) { | ||
return null; | ||
} | ||
} else { | ||
const typeDefs = await fetchTypeDefinitions(version); | ||
|
||
if (typeDefs && typeDefs.length > 0) { | ||
try { | ||
await fs.outputFile(offlinePath, typeDefs); | ||
|
||
return typeDefs; | ||
} catch (error) { | ||
console.warn(`Fetch Types: Could not write to disk`, error); | ||
return null; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Tries to update the editor with type definitions. | ||
* | ||
* @param {string} version | ||
*/ | ||
export async function updateEditorTypeDefinitions(version: string) { | ||
const monaco: typeof MonacoType = (window as any).electronFiddle.monaco; | ||
const typeDefs = await getTypeDefinitions(version); | ||
|
||
if ((window as any).electronFiddle.typeDefDisposable) { | ||
(window as any).electronFiddle.typeDefDisposable.dispose(); | ||
} | ||
|
||
if (typeDefs) { | ||
console.log(`Fetch Types: Updating Monaco types with electron.d.ts@${version}`); | ||
const disposable = monaco.languages.typescript.javascriptDefaults.addExtraLib(typeDefs); | ||
(window as any).electronFiddle.typeDefDisposable = disposable; | ||
} else { | ||
console.log(`Fetch Types: No type definitons for ${version} 😢`); | ||
} | ||
} |