Skip to content

Commit

Permalink
enhance(pods): additional options on export
Browse files Browse the repository at this point in the history
- support  including stubs (default: false)
- support including body (default: true)
  • Loading branch information
kevinslin committed Sep 8, 2020
1 parent e50fe35 commit 500a908
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 29 deletions.
9 changes: 8 additions & 1 deletion packages/common-all/src/node.ts
Expand Up @@ -406,7 +406,14 @@ export abstract class DNode<T = DNodeData> implements IDNode<T>, QuickPickItem {
toNoteProps(): NoteProps {
const node = this;
const body = this.body;
const props = _.pick(node, ["id", "title", "desc", "updated", "created"]);
const props = _.pick(node, [
"id",
"title",
"desc",
"updated",
"created",
"stub",
]);
const { custom } = node;
const meta = { ...props, ...custom };
return {
Expand Down
10 changes: 8 additions & 2 deletions packages/common-server/src/testUtils.ts
Expand Up @@ -76,13 +76,19 @@ export class FileTestUtils {
}

export class NodeTestUtils {
static createNotes = (vaultPath: string, notes: Partial<NoteRawProps>[]) => {
static createNotes = (
vaultPath: string,
notes: Partial<NoteRawProps>[],
opts?: { withBody: boolean }
) => {
const cleanOpts = _.defaults(opts, { withBody: true });
node2MdFile(new Note({ fname: "root", id: "root", title: "root" }), {
root: vaultPath,
});
notes.map((n) => {
const body = cleanOpts.withBody ? n.fname + " body" : "";
// @ts-ignore
node2MdFile(new Note(n), {
node2MdFile(new Note({ ...n, body }), {
root: vaultPath,
});
});
Expand Down
@@ -1,8 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`note refs note refs 1`] = `
"# Foo Content
# Bar Content # I am bar
"foo body
"
`;
Expand Up @@ -7,6 +7,7 @@ Object {
"data": Object {
"desc": "",
"id": "baz",
"stub": false,
"title": "Baz",
},
"excerpt": "",
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-server/src/__tests__/engine.spec.ts
Expand Up @@ -340,6 +340,7 @@ describe("engine:exact", () => {
desc: "",
id: "foo.one",
title: "foo.one",
stub: false,
});

[expectedFiles, actualFiles] = FileTestUtils.cmpFiles(
Expand Down Expand Up @@ -369,6 +370,7 @@ describe("engine:exact", () => {
desc: "",
id: "foo",
title: "foo",
stub: false,
});

[expectedFiles, actualFiles] = FileTestUtils.cmpFiles(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-core/src/commands/ExportPod.ts
Expand Up @@ -62,7 +62,7 @@ export class ExportPodCommand extends BaseCommand<CommandOpts, CommandOutput> {
roots: [root],
podsDir: ws.podsDir,
});
await pod.plant({ mode: "notes", metaOnly: true, config: opts.config });
await pod.plant({ mode: "notes", config: opts.config });
const dest = opts.config.dest;
window.showInformationMessage(`done exporting. destination: ${dest}`);
}
Expand Down
93 changes: 89 additions & 4 deletions packages/plugin-core/src/test/suite-integ/extension.test.ts
Expand Up @@ -75,6 +75,7 @@ import {
getPodPath,
} from "@dendronhq/pods-core";
import { podClassEntryToPodItem } from "../../utils/pods";
import { ExportConfig } from "packages/pods-core/src/base";

const expectedSettings = (opts?: { folders?: any; settings?: any }): any => {
const settings = {
Expand Down Expand Up @@ -1740,7 +1741,7 @@ suite("ExportPod", function () {
});
});

test("config present", function (done) {
test("config present, default", function (done) {
onWSInit(async () => {
podsDir = DendronWorkspace.instance().podsDir;
const pods = getAllExportPods();
Expand All @@ -1752,14 +1753,98 @@ suite("ExportPod", function () {
"export.json"
);
ensureDirSync(path.dirname(configPath));
writeYAML(configPath, { dest: exportDest });
writeYAML(configPath, { dest: exportDest } as ExportConfig);
cmd.gatherInputs = async () => ({
podChoice: podClassEntryToPodItem(podClassEntry),
});
await cmd.run();
const payload = fs.readJSONSync(exportDest);
assert.deepEqual(
NodeTestUtils.cleanNodeMeta({ payload, fields: ["fname"] }),
NodeTestUtils.cleanNodeMeta({ payload, fields: ["fname", "body"] }),
[
{ fname: "root", body: "\n" },
{ fname: "bar", body: "bar body\n" },
]
);
done();
});

setupDendronWorkspace(root.name, ctx, {
useCb: async () => {
NodeTestUtils.createNotes(path.join(root.name, "vault"), [
{ fname: "foo", stub: true },
{ fname: "bar" },
]);
},
});
});

test("config present, default, include stubs", function (done) {
onWSInit(async () => {
podsDir = DendronWorkspace.instance().podsDir;
const pods = getAllExportPods();
const podClassEntry = pods[0];
const cmd = new ExportPodCommand();
const configPath = getPodConfigPath(podsDir, podClassEntry);
const exportDest = path.join(
getPodPath(podsDir, podClassEntry),
"export.json"
);
ensureDirSync(path.dirname(configPath));
writeYAML(configPath, {
dest: exportDest,
includeStubs: true,
} as ExportConfig);
cmd.gatherInputs = async () => ({
podChoice: podClassEntryToPodItem(podClassEntry),
});
await cmd.run();
const payload = fs.readJSONSync(exportDest);
assert.deepEqual(
NodeTestUtils.cleanNodeMeta({ payload, fields: ["fname", "body"] }),
[
{ fname: "root", body: "\n" },
{ fname: "bar", body: "bar body\n" },
{ fname: "foo", body: "foo body\n" },
]
);
done();
});

setupDendronWorkspace(root.name, ctx, {
useCb: async () => {
NodeTestUtils.createNotes(path.join(root.name, "vault"), [
{ fname: "foo", stub: true },
{ fname: "bar" },
]);
},
});
});

test("config present, default, include stubs, no body", function (done) {
onWSInit(async () => {
podsDir = DendronWorkspace.instance().podsDir;
const pods = getAllExportPods();
const podClassEntry = pods[0];
const cmd = new ExportPodCommand();
const configPath = getPodConfigPath(podsDir, podClassEntry);
const exportDest = path.join(
getPodPath(podsDir, podClassEntry),
"export.json"
);
ensureDirSync(path.dirname(configPath));
writeYAML(configPath, {
dest: exportDest,
includeStubs: true,
includeBody: false,
} as ExportConfig);
cmd.gatherInputs = async () => ({
podChoice: podClassEntryToPodItem(podClassEntry),
});
await cmd.run();
const payload = fs.readJSONSync(exportDest);
assert.deepEqual(
NodeTestUtils.cleanNodeMeta({ payload, fields: ["fname", "body"] }),
[{ fname: "root" }, { fname: "bar" }, { fname: "foo" }]
);
done();
Expand All @@ -1768,7 +1853,7 @@ suite("ExportPod", function () {
setupDendronWorkspace(root.name, ctx, {
useCb: async () => {
NodeTestUtils.createNotes(path.join(root.name, "vault"), [
{ fname: "foo" },
{ fname: "foo", stub: true },
{ fname: "bar" },
]);
},
Expand Down
24 changes: 21 additions & 3 deletions packages/pods-core/src/base.ts
@@ -1,6 +1,7 @@
import { DEngine } from "@dendronhq/common-all";
import { DendronEngine } from "@dendronhq/engine-server";
import _ from "lodash";
import { URI } from "vscode-uri";

export type PodOptsV2 = {
roots: string[];
Expand Down Expand Up @@ -31,10 +32,20 @@ export abstract class PodBaseV2<TExportPodOpts extends ExportConfig = any> {
await this.engine.init();
}

cleanConfig(config: ExportConfig) {
return {
..._.defaults(config, { includeStubs: false, includeBody: true }),
dest: URI.file(config.dest),
};
}

prepareForExport(opts: ExportPodOpts<TExportPodOpts>) {
this.initEngine();
const nodes = this.engine[opts.mode];
const hideBody = opts.metaOnly ? true : false;
let nodes = _.values(this.engine[opts.mode]);
const hideBody = opts.config.includeBody ? false : true;
if (!opts.config.includeStubs) {
nodes = _.reject(nodes, { stub: true });
}
const payload: any = _.map(nodes, (ent) => {
return ent.toRawProps(hideBody);
});
Expand All @@ -44,6 +55,14 @@ export abstract class PodBaseV2<TExportPodOpts extends ExportConfig = any> {

export type ExportConfig = {
dest: string;
/**
* Default: false
*/
includeStubs?: boolean;
/**
* Default: true
*/
includeBody?: boolean;
};

export type ExportPodOpts<TConfig extends ExportConfig> = {
Expand All @@ -54,7 +73,6 @@ export type ExportPodOpts<TConfig extends ExportConfig> = {
/**
* Only export metadata?
*/
metaOnly: boolean;
config: TConfig;
};

Expand Down
20 changes: 8 additions & 12 deletions packages/pods-core/src/builtin/JSONPod.ts
@@ -1,6 +1,13 @@
import fs from "fs-extra";
import { URI } from "vscode-uri";
import { ExportPod, ExportPodOpts, PodBaseV2, PodOptsV2 } from "../base";
import {
ExportPod,
ExportPodOpts,
PodBaseV2,
PodOptsV2,
ExportConfig,
} from "../base";
import _ from "lodash";

class JSONPod extends PodBaseV2 {
public opts: PodOptsV2;
Expand All @@ -11,10 +18,6 @@ class JSONPod extends PodBaseV2 {
}
}

type ExportConfig = {
dest: string;
};

export type PodConfigEntry = {
key: string;
description: string;
Expand All @@ -36,13 +39,6 @@ export class JSONExportPod extends JSONPod implements ExportPod<ExportConfig> {
];
};

cleanConfig(config: ExportConfig) {
return {
...config,
dest: URI.file(config.dest),
};
}

async plant(opts: ExportPodOpts<ExportConfig>): Promise<void> {
return new Promise(async (resolve) => {
await this.initEngine();
Expand Down
6 changes: 3 additions & 3 deletions packages/pods-core/src/builtin/__tests__/JSONPod.spec.ts
Expand Up @@ -9,6 +9,7 @@ import _ from "lodash";
import path from "path";
import { genPodConfig, getPodConfigPath } from "../..";
import { JSONExportPod } from "../JSONPod";
import { ExportConfig } from "packages/pods-core/lib/base";

const createNotes = (vaultPath: string, notes: Partial<NoteRawProps>[]) => {
node2MdFile(new Note({ fname: "root", id: "root", title: "root" }), {
Expand Down Expand Up @@ -115,11 +116,10 @@ describe("JSONExportPod", () => {
test("basic no body", async () => {
const pod = new JSONExportPod({ roots: [storeDir], podsDir });
const mode = "notes";
const metaOnly = true;
const destDir = FileTestUtils.tmpDir().name;
const destPath = path.join(destDir, "export.json");
const config = { dest: destPath };
await pod.plant({ mode, metaOnly, config });
const config: ExportConfig = { dest: destPath, includeBody: false };
await pod.plant({ mode, config });
const payload = fs.readJSONSync(destPath) as NoteRawProps[];
assertNodeMeta({
expect,
Expand Down

0 comments on commit 500a908

Please sign in to comment.