Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-40] Expand output path with tilde #357

Merged
merged 6 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ OPTIONS
-h, --help show CLI help
-p, --port=port (required) [default: 3010] Port on which to run the mock server
--pathPrefix=pathPrefix Prefix to prepend to each endpoint path
--proxyBaseUrl=proxyBaseUrl If set, the server would act as a proxy and fetch data from the given remote server

--proxyBaseUrl=proxyBaseUrl If set, the server will act as a proxy and fetch data from the given remote server
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems unrelated?

instead of mocking it

EXAMPLE
Expand Down
3 changes: 2 additions & 1 deletion lib/src/io/output.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fs from "fs-extra";
import path from "path";
import { expandPathWithTilde } from "../utilities/expand-path-with-tilde";

export function outputFile(
outDir: string,
relativePath: string,
content: string,
override = true
): boolean {
const destinationPath = path.join(outDir, relativePath);
const destinationPath = path.join(expandPathWithTilde(outDir), relativePath);
fs.mkdirpSync(path.dirname(destinationPath));
if (!override && fs.existsSync(destinationPath)) {
// Skip.
Expand Down
16 changes: 16 additions & 0 deletions lib/src/utilities/expand-path-with-tilde.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os from "os";
import { expandPathWithTilde } from "./expand-path-with-tilde";

describe("Expand path with tilde", () => {
it("expands path with tilde to home directory", () => {
expect(expandPathWithTilde("~/test/dir")).toBe(`${os.homedir()}/test/dir`);
});

it("does not expand path with tilde if not prefixed properly", () => {
expect(expandPathWithTilde("~test/dir")).toBe(`~test/dir`);
});

it("does not expand path with no tilde", () => {
expect(expandPathWithTilde("./test/dir")).toBe(`./test/dir`);
});
});
11 changes: 11 additions & 0 deletions lib/src/utilities/expand-path-with-tilde.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os from "os";

export function expandPathWithTilde(path: string) {
const homeDir = os.homedir();

if (!homeDir) {
return path;
}

return path.replace(/^~(?=\/|\\)/, homeDir);
}