Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/@power-doctest/markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
"@power-doctest/javascript": "workspace:^",
"@power-doctest/types": "workspace:^",
"@types/unist": "^3.0.3",
"remark": "^11.0.2",
"remark": "^15.0.1",
"unist-util-find-all-between": "^2.1.0",
"unist-util-find-before": "^2.0.5",
"unist-util-parents": "^1.0.3",
"unist-util-select": "^2.0.2"
"unist-util-find-before": "^4.0.1",
"unist-util-parents": "^3.0.0",
"unist-util-select": "^5.1.0"
}
}
28 changes: 13 additions & 15 deletions packages/@power-doctest/markdown/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { ParserArgs, ParsedResults } from "@power-doctest/types";
import { DocTestController } from "./DocTestController.js";
import { remark } from "remark";
import { selectAll } from "unist-util-select";
import { parents as attachParents } from "unist-util-parents";
import findAllBetween from "unist-util-find-all-between";
import { findBefore } from "unist-util-find-before";

type UnistParentNode = import("unist").Parent;
// unist-util-parents
type UnistNode = import("unist").Node & {
parent: UnistParentNode;
};

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const remark = require("remark")();
const select = require("unist-util-select");
const attachParents = require("unist-util-parents");
const findAllBetween = require("unist-util-find-all-between");
const findBefore = require("unist-util-find-before");
const getComments = (parentNode: UnistParentNode, codeNode: UnistNode) => {
const nonHtmlNode = findBefore(parentNode, codeNode, (node: UnistNode) => {
const nonHtmlNode = findBefore(parentNode, codeNode, (node: any) => {
return node.type !== "html";
});
const startNode = nonHtmlNode ? nonHtmlNode : parentNode.children[0];
Expand All @@ -29,12 +26,13 @@ const getComments = (parentNode: UnistParentNode, codeNode: UnistNode) => {
* Parse Markdown code and return ParseResult object.
*/
export const parse = ({ content, filePath }: ParserArgs): ParsedResults => {
const markdownAST = attachParents(remark.parse(content));
const codeBlocks = [].concat(
select.selectAll(`code[lang="js"]`, markdownAST),
select.selectAll(`code[lang="javascript"]`, markdownAST),
);
return codeBlocks.map((codeBlock: UnistNode & { value: string | undefined }) => {
const processor = remark();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move instancination to out of function.

const markdownAST = attachParents(processor.parse(content));
const codeBlocks = [
...selectAll(`code[lang="js"]`, markdownAST),
...selectAll(`code[lang="javascript"]`, markdownAST),
];
return codeBlocks.map((codeBlock: any) => {
const codeValue: string = codeBlock.value || "";
const comments = getComments(codeBlock.parent, codeBlock);
const docTestController = new DocTestController(comments);
Expand Down
2 changes: 1 addition & 1 deletion packages/power-doctest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@power-doctest/javascript": "workspace:^",
"@power-doctest/markdown": "workspace:^",
"@power-doctest/tester": "workspace:^",
"meow": "^5.0.0"
"meow": "^13.2.0"
},
"engines": {
"node": ">=16.17.0"
Expand Down
57 changes: 31 additions & 26 deletions packages/power-doctest/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import * as fs from "fs";
import * as path from "path";
import { parseArgs } from "node:util";
import meow from "meow";
import { runPowerDoctest, RunPowerDoctestOption } from "./power-doctest.js";

const USAGE = `Usage:
$ power-doctest /path/to/file.{js,md,adoc}

Options:
--packageDir Current Working directory. Should put package.json in the directory.
--disableRunning Disable running test case that has not state.

Examples:
$ power-doctest ./README.md
$ power-doctest ./README.adoc
$ power-doctest ./src/main.js`;

export async function run() {
const options = {
packageDir: { type: "string" },
defaultRunning: { type: "boolean" },
disableRunning: { type: "boolean", default: false },
} as const;
const cli = meow(
`
Usage:
$ power-doctest /path/to/file.{js,md,adoc}

const { values: flags, positionals } = parseArgs({
options,
allowPositionals: true,
});
Options:
--packageDir Current Working directory. Should put package.json in the directory.
--disableRunning Disable running test case that has not state.

Examples:
$ power-doctest ./README.md
$ power-doctest ./README.adoc
$ power-doctest ./src/main.js
`,
{
importMeta: import.meta,
flags: {
packageDir: {
type: "string",
},
disableRunning: {
type: "boolean",
default: false,
},
},
},
);

const disableRunning = flags.disableRunning;
const input = positionals[0];
const disableRunning = cli.flags.disableRunning;
const input = cli.input[0];
if (!input) {
console.log(USAGE);
cli.showHelp();
throw new Error("No input file specified. Please provide a file path as an argument.");
}
const content = fs.readFileSync(input, "utf-8");
Expand All @@ -53,7 +58,7 @@ export async function run() {
if (!contentType) {
throw new Error("Not supported file type" + input);
}
const cwd = flags.packageDir || process.cwd();
const cwd = cli.flags.packageDir || process.cwd();
const pkgFilePath = path.join(cwd, "package.json");
const pkg = await (async () => {
try {
Expand Down
Loading