Skip to content

Commit fe27cfa

Browse files
authored
Merge pull request #2107 from nteract/export-notebook
fix: fix export notebook for new Atom versions
2 parents d8a1ee2 + 8c6e122 commit fe27cfa

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

lib/export-notebook.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
import * as path from "path";
2-
import { writeFile } from "fs";
2+
import { promises } from "fs";
3+
const { writeFile } = promises;
34
import { remote } from "electron";
45
const { dialog } = remote;
56

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

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

16-
const ext = path.extname(filename) === "" ? ".ipynb" : "";
17-
const fname = `${filename}${ext}`;
18-
writeFile(fname, stringifyNotebook(store.notebook), (err) => {
19-
if (err) {
20-
atom.notifications.addError("Error saving file", {
21-
detail: err.message,
22-
});
23-
} else {
24-
atom.notifications.addSuccess("Save successful", {
25-
detail: `Saved notebook as ${fname}`,
26-
});
27-
}
17+
const { canceled, filePath } = await dialog.showSaveDialog({
18+
title: editor.getTitle(),
19+
defaultPath: noteBookPath,
20+
});
21+
if (!canceled) {
22+
await saveNoteBook(filePath);
23+
}
24+
}
25+
26+
async function saveNoteBook(filePath: string) {
27+
if (filePath.length === 0) {
28+
return;
29+
}
30+
// add default extension
31+
const ext = path.extname(filePath) === "" ? ".ipynb" : "";
32+
const fname = `${filePath}${ext}`;
33+
34+
try {
35+
await writeFile(fname, stringifyNotebook(store.notebook));
36+
atom.notifications.addSuccess("Save successful", {
37+
detail: `Saved notebook as ${fname}`,
38+
});
39+
} catch (err) {
40+
atom.notifications.addError("Error saving file", {
41+
detail: err.message,
2842
});
29-
};
30-
// TODO this API is promisified -> should be fixed
31-
dialog.showSaveDialog(saveNotebook);
43+
}
3244
}

lib/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
openOrShowDock,
4242
kernelSpecProvidesGrammar,
4343
} from "./utils";
44-
import exportNotebook from "./export-notebook";
44+
import { exportNotebook } from "./export-notebook";
4545
import { importNotebook, ipynbOpener } from "./import-notebook";
4646
import type { KernelspecMetadata } from "@nteract/types";
4747

0 commit comments

Comments
 (0)