Skip to content

Commit

Permalink
Merge pull request #2107 from nteract/export-notebook
Browse files Browse the repository at this point in the history
fix: fix export notebook for new Atom versions
  • Loading branch information
aminya committed May 26, 2021
2 parents d8a1ee2 + 8c6e122 commit fe27cfa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
56 changes: 34 additions & 22 deletions lib/export-notebook.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import * as path from "path";
import { writeFile } from "fs";
import { promises } from "fs";
const { writeFile } = promises;
import { remote } from "electron";
const { dialog } = remote;

import { stringifyNotebook } from "@nteract/commutable";

import store from "./store";
export default function exportNotebook() {
// TODO: Refactor to use promises, this is a bit "nested".
const saveNotebook = function (filename) {
if (!filename) {
return;
}
export async function exportNotebook() {
const editor = atom.workspace.getActiveTextEditor();
const editorPath = editor.getPath();
const directory = path.dirname(editorPath);
const rawFileName = path.basename(editorPath, path.extname(editorPath));
const noteBookPath = path.join(directory, `${rawFileName}.ipynb`);

const ext = path.extname(filename) === "" ? ".ipynb" : "";
const fname = `${filename}${ext}`;
writeFile(fname, stringifyNotebook(store.notebook), (err) => {
if (err) {
atom.notifications.addError("Error saving file", {
detail: err.message,
});
} else {
atom.notifications.addSuccess("Save successful", {
detail: `Saved notebook as ${fname}`,
});
}
const { canceled, filePath } = await dialog.showSaveDialog({
title: editor.getTitle(),
defaultPath: noteBookPath,
});
if (!canceled) {
await saveNoteBook(filePath);
}
}

async function saveNoteBook(filePath: string) {
if (filePath.length === 0) {
return;
}
// add default extension
const ext = path.extname(filePath) === "" ? ".ipynb" : "";
const fname = `${filePath}${ext}`;

try {
await writeFile(fname, stringifyNotebook(store.notebook));
atom.notifications.addSuccess("Save successful", {
detail: `Saved notebook as ${fname}`,
});
} catch (err) {
atom.notifications.addError("Error saving file", {
detail: err.message,
});
};
// TODO this API is promisified -> should be fixed
dialog.showSaveDialog(saveNotebook);
}
}
2 changes: 1 addition & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
openOrShowDock,
kernelSpecProvidesGrammar,
} from "./utils";
import exportNotebook from "./export-notebook";
import { exportNotebook } from "./export-notebook";
import { importNotebook, ipynbOpener } from "./import-notebook";
import type { KernelspecMetadata } from "@nteract/types";

Expand Down

0 comments on commit fe27cfa

Please sign in to comment.