Skip to content

Commit

Permalink
enhance(pods): use cli to export pod
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinslin committed Sep 9, 2020
1 parent 3fbf011 commit dd21f31
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 43 deletions.
8 changes: 6 additions & 2 deletions packages/common-server/src/logger.ts
Expand Up @@ -10,7 +10,8 @@ export class Logger {
this.name = opts.name;
this.level = opts.level;
}
_log(msg: any) {

private _log(msg: any) {
let ctx = "";
if (msg.ctx) {
ctx = msg.ctx;
Expand All @@ -33,7 +34,6 @@ function createLogger(name?: string, dest?: string): pino.Logger {
const level = env("LOG_LEVEL", { shouldThrow: false }) || "debug";
const nameClean = name || env("LOG_NAME");

//const logDst = env("LOG_DST", { shouldThrow: false }) || "stdout";
// if (logDst === "stdout") {
// // TODO: tmp disable pino logging on stdout
// const out = pino({ name: nameClean, level });
Expand All @@ -42,6 +42,10 @@ function createLogger(name?: string, dest?: string): pino.Logger {
if (dest) {
return pino(pino.destination(dest)).child({ name: nameClean, level });
} else {
// const logDst = env("LOG_DST", { shouldThrow: false })
// if (logDst) {
// return pino(pino.destination(logDst)).child({ name: nameClean, level });
// }
return pino({ name: nameClean, level });
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/common-server/src/testUtils.ts
Expand Up @@ -12,6 +12,12 @@ export { DirResult };
tmp.setGracefulCleanup();

export class FileTestUtils {
/**
* Compare files in root with expected
* @param root
* @param expected
* @param opts
*/
static cmpFiles = (
root: string,
expected: string[],
Expand Down
2 changes: 1 addition & 1 deletion packages/dendron-cli/.vscode/launch.json
Expand Up @@ -42,7 +42,7 @@
"--findRelatedTests",
"${relativeFile}"
],
"cwd": "${workspaceRoot}"
"cwd": "${workspaceRoot}","env": {"LOG_DST": "/tmp/dendron-cli.txt"}
}
]
}
3 changes: 2 additions & 1 deletion packages/dendron-cli/.vscode/tasks.json
Expand Up @@ -13,7 +13,8 @@
"label": "npm:test:watch",
"command": "npm run test:watch -- ${relativeFile} -u",
"type": "shell",
"problemMatcher": []
"problemMatcher": [],
"options": {"env": {"LOG_DST": "/tmp/denron-cli.txt"}}
},
{
"type": "npm",
Expand Down
2 changes: 1 addition & 1 deletion packages/dendron-cli/package.json
Expand Up @@ -30,7 +30,7 @@
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
"build": "npm run compile",
"compile": "tsc -p tsconfig.build.json",
"test": "env LOG_DST=/tmp/dendron-engine-server.log jest",
"test": "env LOG_DST=/tmp/dendron-cli.txt jest",
"test:unit:all": "cross-env LOG_LEVEL=error yarn test",
"coverage": "jest --coverage",
"watch": "npm run compile -- --watch",
Expand Down
90 changes: 90 additions & 0 deletions packages/dendron-cli/src/commands/__tests__/importPod.spec.ts
@@ -0,0 +1,90 @@
import {
FileTestUtils,
NodeTestUtils,
writeYAML,
} from "@dendronhq/common-server";
import { FileImportPod, getPodConfigPath } from "@dendronhq/pods-core";
import fs, { ensureDirSync } from "fs-extra";
import _ from "lodash";
import path from "path";
import { ImportPodCLICommand } from "../importPod";

type FileItem = {
path: string;
};

async function createFiles(root: string, files: FileItem[]) {
return Promise.all(
_.map(files, async (ent) => {
const fpath = path.join(root, ent.path);
return await fs.ensureFile(fpath);
})
);
}

describe("importPod", async () => {
let importSrc: string;
let podsDir: string;
let vault: string;

beforeEach(async function () {
importSrc = FileTestUtils.tmpDir().name;
podsDir = FileTestUtils.tmpDir().name;
vault = FileTestUtils.tmpDir().name;
await NodeTestUtils.createNotes(vault, []);

await createFiles(importSrc, [
{ path: "project/p2/n1.md" },
{ path: "project/p1/n1.md" },
{ path: "project/p1/n2.md" },
{ path: "project/p1/.DS_STORE_TEST" },
{ path: "project/p1/n3.pdf" },
{ path: "project/p1/n1.pdf" },
{ path: "project/p1/n1.pdf" },
{ path: "project/p.3/n1.md" },
]);
});

test("file import, no config", async () => {
try {
await ImportPodCLICommand.run({
podId: FileImportPod.id,
podsDir,
vault,
});
} catch (err) {
expect(err.message === "no config");
}
});

test("config present, default", async () => {
const configPath = getPodConfigPath(podsDir, FileImportPod);
ensureDirSync(path.dirname(configPath));
writeYAML(configPath, { src: importSrc });

const cmd = await ImportPodCLICommand.run({
podId: FileImportPod.id,
podsDir,
vault,
});
cmd.L.info({ msg: "in test file" });

let [expectedFiles, actualFiles] = FileTestUtils.cmpFiles(vault, [
"assets",
"project.p1.md",
"project.p1.n1.md",
"project.p1.n2.md",
"project.p2.n1.md",
"project.p-3.n1.md",
"root.md",
]);
expect(expectedFiles).toEqual(actualFiles);
const assetsDir = fs.readdirSync(path.join(vault, "assets"));
expect(assetsDir.length).toEqual(2);
const fileBody = fs.readFileSync(path.join(vault, "project.p1.md"), {
encoding: "utf8",
});
expect(fileBody.match("n1.pdf")).toBeTruthy();
expect(fileBody.match("n3.pdf")).toBeTruthy();
}, 5000000);
});
92 changes: 92 additions & 0 deletions packages/dendron-cli/src/commands/importPod.ts
@@ -0,0 +1,92 @@
import { DendronError, DEngine } from "@dendronhq/common-all";
import { DendronEngine } from "@dendronhq/engine-server";
import {
getAllImportPods,
getPodConfig,
podClassEntryToPodItem,
PodClassEntryV2,
PodItem,
} from "@dendronhq/pods-core";
import _ from "lodash";
import yargs from "yargs";
import { BaseCommand } from "./base";

type CommandOpts = {
engine: DEngine;
podClass: PodClassEntryV2;
config: any;
};

type CommandOutput = void;

type CommandCLIOpts = {
podId: string;
podsDir: string;
vault: string;
};

export { CommandCLIOpts as ImportPodCLIOpts };

export class ImportPodCLICommand extends BaseCommand<
CommandOpts,
CommandOutput
> {
static async buildArgs(args: yargs.Argv<CommandCLIOpts>) {
const podItems: PodItem[] = getAllImportPods().map((p) =>
podClassEntryToPodItem(p)
);
args.option("podId", {
describe: "pod to use",
choices: podItems.map((ent) => ent.id),
});
args.option("vault", {
describe: "location of vault",
});
}

async enrichArgs(args: CommandCLIOpts): Promise<CommandOpts> {
const ctx = "ImportPod:enrichArgs";
const { vault, podId, podsDir } = args;
const engine = DendronEngine.getOrCreateEngine({
root: vault,
forceNew: true,
});
const podClass = _.find(getAllImportPods(), {
id: podId,
}) as PodClassEntryV2;
const maybeConfig = getPodConfig(podsDir, podClass);
if (!maybeConfig) {
this.L.error({ msg: "no-config", ctx });
throw new DendronError({ msg: "no-config" });
}
return {
engine,
podClass,
config: maybeConfig,
};
}

async execute(opts: CommandOpts) {
const ctx = "ImportPod";
this.L.info({ ctx, msg: "enter" });
const { podClass, engine, config } = opts;
const root = engine.props.root;
const pod = new podClass({
roots: [root],
});
await pod.plant({ mode: "notes", config: config });
this.L.info({ ctx, msg: "exit" });
return;
}

static async run(args: CommandCLIOpts) {
const ctx = "ImportPod:run";
const cmd = new ImportPodCLICommand();
cmd.L.info({ ctx, msg: "enter", args });
const opts = await cmd.enrichArgs(args);
cmd.L.info({ ctx, msg: "enrichArgs:post", args });
await cmd.execute(opts);
cmd.L.info({ ctx, msg: "exit", args });
return cmd;
}
}
3 changes: 2 additions & 1 deletion packages/pods-core/.vscode/launch.json
Expand Up @@ -29,7 +29,8 @@
"name": "pods-core:debug test file",
"program": "${workspaceFolder:pods-core}/node_modules/jest/bin/jest.js",
"cwd": "${workspaceFolder:pods-core}",
"args": ["--runInBand", "${relativeFile}", "-u"]
"args": ["--runInBand", "${relativeFile}", "-u"],
"env": {"LOG_DST": "/tmp/pods-core.txt"}
}
]
}
Expand Down
56 changes: 49 additions & 7 deletions packages/pods-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd21f31

Please sign in to comment.