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

Infer file from source #1

Merged
merged 3 commits into from Feb 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions README.md
Expand Up @@ -18,13 +18,12 @@ Use the following code.

```ts
import { scaffold } from "https://deno.land/x/skaffe@v1.0.0/mod.ts";
import { join } from "https://deno.land/std@0.123.0/path/mod.ts";

const thisFile = import.meta.url;
const targetFile = join(Deno.cwd(), "target-file.ts");
const targetDirectory = Deno.cwd(); // using a dir will infer the file name

// Will copy this very source file into the working directory
await scaffold(thisFile, targetFile);
await scaffold(thisFile, targetDirectory);
```

## Node Example
Expand All @@ -39,13 +38,12 @@ and use the following code.

```ts
const { scaffold } = require("skaffe");
const { join } = require("path");

const thisFile = __filename;
const targetFile = join(process.cwd(), "target-file.js");
const targetDirectory = process.cwd(); // using a dir will infer the file name

// Will copy this very source file into the working directory
scaffold(thisFile, targetFile);
scaffold(thisFile, targetDirectory);
```

## Why?
Expand All @@ -66,5 +64,5 @@ and Node, and let `skaffe` do the nasty part.

## What's in a Name?

The word _skaffe_ is Norwegian and means _provide_.
It also looks similar to the prefix of _scaffolding_.
The word _skaffe_ is Norwegian and means _provide_. It also looks similar to the
prefix of _scaffolding_.
5 changes: 2 additions & 3 deletions example.ts
@@ -1,8 +1,7 @@
import { scaffold } from "https://deno.land/x/skaffe@v1.0.0/mod.ts";
import { join } from "https://deno.land/std@0.123.0/path/mod.ts";

const thisFile = import.meta.url;
const targetFile = join(Deno.cwd(), "example.ts");
const targetDirectory = Deno.cwd(); // using a dir will infer the file name

// Will copy this very source file into the working directory
await scaffold(thisFile, targetFile);
await scaffold(thisFile, targetDirectory);
4 changes: 4 additions & 0 deletions src/platform.deno.ts
Expand Up @@ -3,6 +3,10 @@ import {
readerFromIterable,
} from "https://deno.land/std@0.123.0/streams/mod.ts";

export { basename, join } from "https://deno.land/std@0.123.0/path/mod.ts";
export const isDirectory = (path: string) =>
Deno.stat(path).then((info) => info.isDirectory);

export async function writeData(
target: string,
data: AsyncIterable<Uint8Array>,
Expand Down
5 changes: 5 additions & 0 deletions src/platform.node.ts
@@ -1,5 +1,10 @@
import { createWriteStream } from "fs";
import { Readable } from "stream";
import { lstat } from "fs/promises";

export { basename, join } from "path";
export const isDirectory = (path: string) =>
lstat(path).then((info) => info.isDirectory());

export async function writeData(
target: string,
Expand Down
25 changes: 23 additions & 2 deletions src/scaffold.ts
@@ -1,8 +1,9 @@
import { writeData } from "./platform.deno.ts";
import { basename, isDirectory, join, writeData } from "./platform.deno.ts";

export async function scaffold(source: string | URL, target: string) {
const file = await toFile(source, target);
const data: AsyncIterable<Uint8Array> = await getFile(source);
await writeData(target, data);
await writeData(file, data);
}

export async function getFile(
Expand All @@ -15,3 +16,23 @@ export async function getFile(
}
return data;
}

export async function toFile(
sourceFile: string | URL,
targetDirectoryOrFile: string,
): Promise<string> {
let result = targetDirectoryOrFile;
try {
// If the given target is an existing directory ...
if (await isDirectory(targetDirectoryOrFile)) {
// ... use the source file name in the directory.
const path = typeof sourceFile === "string"
? sourceFile
: sourceFile.pathname;
result = join(targetDirectoryOrFile, basename(path));
}
} catch {
// Failed to stat, do not infer filename
}
return result;
}
2 changes: 2 additions & 0 deletions src/shim.node.ts
@@ -1,4 +1,6 @@
import { createReadStream } from "fs";
import { URL } from "url";
export { URL };

interface FetchResult {
body: AsyncIterable<Uint8Array>;
Expand Down