Skip to content

Commit

Permalink
Merge pull request #7 from electron/type-definitions
Browse files Browse the repository at this point in the history
Add TypeScript definitions for Electron
  • Loading branch information
felixrieseberg authored Jun 7, 2018
2 parents b52ee70 + 80993f2 commit dcbc9ef
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { render } from 'react-dom';
import * as loader from 'monaco-loader';
import { observable } from 'mobx';
import * as MonacoType from 'monaco-editor';

import { mainTheme } from './themes';
import { getContent } from './content';
Expand Down Expand Up @@ -31,8 +32,9 @@ class App {
renderer: null,
html: null
};
public monaco: any = null;
public monaco: typeof MonacoType | null = null;
public name = 'test';
public typeDefDisposable = null;

constructor() {
this.getValues = this.getValues.bind(this);
Expand All @@ -52,7 +54,9 @@ class App {
}

private createThemes() {
this.monaco.editor.defineTheme('main', mainTheme);
if (!this.monaco) return;

this.monaco.editor.defineTheme('main', mainTheme as any);
}

private createEditor(id: string) {
Expand All @@ -72,7 +76,7 @@ class App {
value
};

return this.monaco.editor.create(element, options);
return this.monaco.editor.create(element!, options);
}

private getValues() {
Expand All @@ -93,4 +97,5 @@ class App {
}
}

(window as any).electronFiddle = new App();
// tslint:disable-next-line:no-string-literal
window['electronFiddle'] = new App();
1 change: 0 additions & 1 deletion src/renderer/components/output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class Output extends React.Component<CommandsProps, {}> {
const timestamp = <span className='timestamp'>{ts}</span>;
const lines = entry.text.split(/\r?\n/);


return lines.map((text) => (
<p>{timestamp}{text}</p>
));
Expand Down
10 changes: 9 additions & 1 deletion src/renderer/components/version-chooser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { observer } from 'mobx-react';

import { normalizeVersion } from '../../utils/normalize-version';
import { AppState } from '../app';
import { updateEditorTypeDefinitions } from '../fetch-types';

export interface VersionChooserState {
value: string;
Expand All @@ -19,7 +20,7 @@ export class VersionChooser extends React.Component<VersionChooserProps, Version
super(props);

this.handleChange = this.handleChange.bind(this);
this.updateDownloadedVersionState();
this.handleVersionChange(this.props.appState.version);
}

public async updateDownloadedVersionState() {
Expand All @@ -39,10 +40,17 @@ export class VersionChooser extends React.Component<VersionChooserProps, Version

public handleChange(event: React.ChangeEvent<HTMLSelectElement>) {
const version = normalizeVersion(event.target.value);
this.handleVersionChange(version);
}

public handleVersionChange(version: string) {
console.log(`Version Chooser: Switching to v${version}`);

this.props.appState.version = version;

// Update TypeScript definitions
updateEditorTypeDefinitions(version);

// Fetch new binaries, maybe?
if ((this.props.appState.versions[version] || { state: '' }).state === 'ready') return;

Expand Down
103 changes: 103 additions & 0 deletions src/renderer/fetch-types.ts
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} 😢`);
}
}

0 comments on commit dcbc9ef

Please sign in to comment.