Skip to content

Commit

Permalink
Merge pull request #29 from electron/transforms
Browse files Browse the repository at this point in the history
feat: File transforms
  • Loading branch information
felixrieseberg authored Jul 9, 2018
2 parents b328798 + 3a928e2 commit b996446
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export type ElectronVersionState = 'downloading' | 'ready' | 'unknown';

export type WindowNames = 'main';

export type Files = Map<string, string>;

export type FileTransform = (files: Files) => Promise<Files>;

export interface GitHubVersion {
url: string;
assets_url: string;
Expand Down
71 changes: 46 additions & 25 deletions src/renderer/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';

import { ipcRendererManager } from './ipc';
import { IpcEvents } from '../ipc-events';
import { EditorValues } from '../interfaces';
import { EditorValues, Files, FileTransform } from '../interfaces';
import { INDEX_HTML_NAME, MAIN_JS_NAME, RENDERER_JS_NAME, PACKAGE_NAME } from '../constants';
import { appState } from './state';
import { getTitle } from '../utils/get-title';
Expand All @@ -13,18 +13,22 @@ export class FileManager {
this.openFiddle = this.openFiddle.bind(this);
this.saveFiddle = this.saveFiddle.bind(this);

ipcRendererManager.on(IpcEvents.FS_OPEN_FIDDLE, this.openFiddle);
ipcRendererManager.on(IpcEvents.FS_SAVE_FIDDLE, this.saveFiddle);
ipcRendererManager.on(IpcEvents.FS_OPEN_FIDDLE, (_event, filePath) => {
this.openFiddle(filePath);
});

ipcRendererManager.on(IpcEvents.FS_SAVE_FIDDLE, (_event, filePath) => {
this.saveFiddle(filePath);
});
}

/**
* Tries to open a fiddle
*
* @param {Electron.event} _event
* @param {string} filePath
* @memberof FileManager
*/
public async openFiddle(_event: Electron.Event, filePath: string) {
public async openFiddle(filePath: string) {
if (!filePath || typeof filePath !== 'string') return;

console.log(`FileManager: Asked to open`, filePath);
Expand All @@ -48,11 +52,10 @@ export class FileManager {
* Saves the current Fiddle to disk. If we never saved before,
* we'll first open the "Save" dialog.
*
* @param {Electron.event} _event
* @param {string} filePath
* @memberof FileManager
*/
public async saveFiddle(_event: Electron.Event, filePath: string) {
public async saveFiddle(filePath: string, ...transforms: Array<FileTransform>) {
const { localPath } = appState;
const pathToSave = filePath || localPath;

Expand All @@ -61,24 +64,14 @@ export class FileManager {
if (!pathToSave) {
ipcRendererManager.send(IpcEvents.FS_SAVE_FIDDLE_DIALOG);
} else {
const options = { includeDependencies: true, includeElectron: true };
const values = await window.ElectronFiddle.app.getValues(options);
const { html, main, package: packageJson, renderer } = values;

if (renderer) {
await this.saveFile(path.join(pathToSave, RENDERER_JS_NAME), renderer);
}

if (main) {
await this.saveFile(path.join(pathToSave, MAIN_JS_NAME), main);
}

if (html) {
await this.saveFile(path.join(pathToSave, INDEX_HTML_NAME), html);
}

if (packageJson) {
await this.saveFile(path.join(pathToSave, PACKAGE_NAME), packageJson);
const files = await this.getFiles(...transforms);

for (const [ fileName, content ] of files) {
try {
await this.saveFile(path.join(pathToSave, fileName), content);
} catch (error) {
console.warn(`FileManager: Failed to save file`, { fileName, error });
}
}

if (pathToSave !== localPath) {
Expand All @@ -87,6 +80,34 @@ export class FileManager {
}
}

/**
* Get files to save, but with a transform applied
*
* @param {...Array<FileTransform>} transforms
* @returns {Promise<Files>}
* @memberof FileManager
*/
public async getFiles(...transforms: Array<FileTransform>): Promise<Files> {
const options = { includeDependencies: true, includeElectron: true };
const values = await window.ElectronFiddle.app.getValues(options);
let output: Files = new Map();

output.set(RENDERER_JS_NAME, values.renderer);
output.set(MAIN_JS_NAME, values.main);
output.set(INDEX_HTML_NAME, values.html);
output.set(PACKAGE_NAME, values.package!);

for (const transform of transforms) {
try {
output = await transform(output);
} catch (error) {
console.warn(`getFiles: Failed to apply transform`, { transform, error });
}
}

return output;
}

/**
* Safely attempts to read a file, doesn't crash the app if
* it fails.
Expand Down

0 comments on commit b996446

Please sign in to comment.