Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(pods): use cli to export pod
- Loading branch information
Showing
16 changed files
with
550 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/dendron-cli/src/commands/__tests__/importPod.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.