Comparing version 2.2.0-beta.1 to 2.2.0-beta.2
@@ -5,3 +5,3 @@ import { Option, program } from "commander"; | ||
import getCurl from "./lib/get-curl"; | ||
import run from "./lib/run"; | ||
import runStories from "./lib/run-stories"; | ||
import { createProject, initProject } from "./lib/project"; | ||
@@ -37,5 +37,3 @@ import picocolors from "picocolors"; | ||
.action(async (storyFilePath, options) => | ||
visit(await findStories(storyFilePath, options), (story) => | ||
run(story, options), | ||
), | ||
runStories(await findStories(storyFilePath, options), options), | ||
); | ||
@@ -42,0 +40,0 @@ |
import { FetchStory } from "fetchbook"; | ||
import path from "path"; | ||
import { glob } from "glob"; | ||
import selectStory from "./select-story"; | ||
import { glob } from "glob"; | ||
import spaceCase from "to-space-case"; | ||
import titleize from "titleize"; | ||
export type FileFetchStory = FetchStory & { file: string }; | ||
const getStory = (storyFilePath: string) => | ||
import(storyFilePath).then((mod) => mod.default as FetchStory); | ||
const packageRoot = path.join(__dirname, ".."); | ||
const isFetchbookFile = (file: string) => { | ||
export const packageRoot = path.join(import.meta.dir, ".."); | ||
export const isFetchbookFile = (file: string) => { | ||
const relative = path.relative(packageRoot, file); | ||
@@ -28,6 +28,8 @@ return relative && !relative.startsWith("..") && !path.isAbsolute(relative); | ||
options?: { demo?: boolean; all?: boolean }, | ||
) { | ||
): Promise<FileFetchStory[]> { | ||
storyFilePath = getStoryFilePath(storyFilePath, options); | ||
if (storyFilePath?.endsWith(".ts")) { | ||
return [await getStory(path.resolve(storyFilePath))]; | ||
const file = path.resolve(storyFilePath); | ||
const story = await getStory(file); | ||
return [{ ...story, file }]; | ||
} else { | ||
@@ -38,3 +40,2 @@ const pattern = "**/*.fetch.ts"; | ||
: process.cwd(); | ||
const storyToFile = new Map<FetchStory, string>(); | ||
const stories = ( | ||
@@ -50,7 +51,3 @@ await Promise.all( | ||
.sort() | ||
.map(async (file) => { | ||
const story = await getStory(file); | ||
storyToFile.set(story, file); | ||
return story; | ||
}), | ||
.map(async (file) => ({ ...(await getStory(file)), file })), | ||
) | ||
@@ -64,18 +61,4 @@ ).sort((a, b) => a.name.localeCompare(b.name)); | ||
} | ||
return [ | ||
await selectStory(stories, (story) => | ||
path | ||
.dirname( | ||
path.relative( | ||
isFetchbookFile(storyToFile.get(story) ?? "") ? packageRoot : cwd, | ||
storyToFile.get(story) ?? "", | ||
), | ||
) | ||
.split(path.sep) | ||
.map(spaceCase) | ||
.map(titleize) | ||
.join(" / "), | ||
), | ||
]; | ||
return [await selectStory(stories)]; | ||
} | ||
} |
@@ -35,5 +35,5 @@ import { mkdir, writeFile, copy, exists } from "fs-extra"; | ||
await copy( | ||
path.join(__dirname, "..", "examples"), | ||
path.join(import.meta.dir, "..", "examples"), | ||
path.join(cwd, "fetchbook", "samples"), | ||
); | ||
} |
@@ -1,11 +0,8 @@ | ||
import { FetchStory } from "fetchbook"; | ||
import picocolors from "picocolors"; | ||
import autocomplete, { Separator } from "inquirer-autocomplete-standalone"; | ||
import Fuse from "fuse.js"; | ||
import groupBy from "lodash.groupby"; | ||
import groupStories from "./group-stories"; | ||
import { FileFetchStory } from "./find-stories"; | ||
export default function selectStory( | ||
stories: FetchStory[], | ||
groupByKey: (story: FetchStory) => string, | ||
) { | ||
export default function selectStory(stories: FileFetchStory[]) { | ||
const fuse = new Fuse(stories, { | ||
@@ -21,11 +18,8 @@ keys: [ | ||
message: "Select a fetch story", | ||
source: async (input) => { | ||
return Object.entries( | ||
groupBy( | ||
input ? fuse.search(input).map((result) => result.item) : stories, | ||
groupByKey, | ||
), | ||
source: async (input) => | ||
groupStories( | ||
input ? fuse.search(input).map((result) => result.item) : stories, | ||
) | ||
.map(([group, stories]) => [ | ||
new Separator(picocolors.gray(group)), | ||
.map(({ name, stories }) => [ | ||
new Separator(name), | ||
...stories.map((story) => ({ | ||
@@ -37,5 +31,4 @@ name: story.name, | ||
]) | ||
.flat(); | ||
}, | ||
.flat(), | ||
}); | ||
} |
import jsonServer from "json-server"; | ||
import { Server } from "http"; | ||
import http from "http"; | ||
let server: Server | undefined; | ||
type TestServer = { | ||
start: () => Promise<void>; | ||
reset: () => Promise<void>; | ||
stop: () => Promise<void>; | ||
}; | ||
export default { | ||
start: async () => | ||
new Promise<void>((resolve) => { | ||
const app = jsonServer.create(); | ||
app.use(jsonServer.defaults({ logger: false })); | ||
app.use( | ||
jsonServer.router({ | ||
posts: [ | ||
{ | ||
id: 1, | ||
it: "works!", | ||
}, | ||
], | ||
}), | ||
); | ||
server = app.listen(3000, resolve); | ||
}), | ||
stop: () => server?.close(), | ||
}; | ||
let testServer: TestServer | undefined = undefined; | ||
if (process.env.FETCHBOOK_TEST) { | ||
const app = jsonServer.create(); | ||
app.disable("etag"); | ||
app.use(jsonServer.defaults({ logger: false })); | ||
const router = jsonServer.router<{ posts: { id: number; it?: string }[] }>({ | ||
posts: [], | ||
}); | ||
app.use(router); | ||
const server = http.createServer(app); | ||
testServer = { | ||
start: () => | ||
new Promise<void>((resolve) => { | ||
process.env.FETCHBOOK_TEST ? server.listen(3000, resolve) : resolve(); | ||
}), | ||
reset: async () => { | ||
router.db.setState({ | ||
posts: [ | ||
{ | ||
id: 1, | ||
it: "works!", | ||
}, | ||
], | ||
}); | ||
}, | ||
stop: () => | ||
new Promise<void>((resolve) => { | ||
process.env.FETCHBOOK_TEST ? server.close(() => resolve()) : resolve(); | ||
}), | ||
}; | ||
} | ||
export default testServer; |
{ | ||
"name": "fetchbook", | ||
"version": "2.2.0-beta.1", | ||
"version": "2.2.0-beta.2", | ||
"description": "Manage your HTTP requests", | ||
@@ -20,2 +20,3 @@ "author": "Alejandro Tardín <alejandro@tardin.com>", | ||
"json-server": "^0.17.4", | ||
"listr2": "^7.0.1", | ||
"lodash.groupby": "^4.6.0", | ||
@@ -22,0 +23,0 @@ "picocolors": "^1.0.0", |
@@ -212,3 +212,2 @@ # Fetchbook [](https://www.npmjs.com/package/fetchbook) | ||
- [ ] Print response body by default to mimic a standard cURL request. | ||
- [ ] Run stories with a runner like [listr2](https://github.com/listr2/listr2). | ||
- [ ] Add command to create a story: `fetchbook create`. | ||
@@ -215,0 +214,0 @@ - [ ] Add command to import a story from other formats: `fetchbook import`. |
@@ -8,3 +8,3 @@ import { expect, test } from "bun:test"; | ||
expect(exitCode).toMatchSnapshot(); | ||
expect(stdout).toMatchSnapshot(); | ||
expect(stdout.replaceAll(/\s*"date": ".+",?/g, "")).toMatchSnapshot(); | ||
expect(stderr).toMatchSnapshot(); | ||
@@ -19,2 +19,3 @@ }); | ||
$test`fetchbook run test/pass --all`; | ||
$test`fetchbook run test/pass --all -v`; | ||
$test`fetchbook run test/fail --all`; | ||
@@ -21,0 +22,0 @@ $test`fetchbook run test --all`; |
@@ -32,6 +32,2 @@ import { FetchStory } from "fetchbook"; | ||
}, | ||
{ | ||
id: 5, | ||
it: "works!", | ||
}, | ||
], | ||
@@ -38,0 +34,0 @@ }, |
Sorry, the diff of this file is not supported yet
41257
34
691
17
223
+ Addedlistr2@^7.0.1
+ Addedansi-escapes@5.0.0(transitive)
+ Addedcli-cursor@4.0.0(transitive)
+ Addedcli-truncate@3.1.0(transitive)
+ Addedcolorette@2.0.20(transitive)
+ Addedeventemitter3@5.0.1(transitive)
+ Addedis-fullwidth-code-point@4.0.0(transitive)
+ Addedlistr2@7.0.2(transitive)
+ Addedlog-update@5.0.1(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedrestore-cursor@4.0.0(transitive)
+ Addedrfdc@1.4.1(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedslice-ansi@5.0.0(transitive)
+ Addedtype-fest@1.4.0(transitive)