Skip to content

Commit

Permalink
refactor: remove $.exists/existsSync(...) - Use `$.path(...).exists…
Browse files Browse the repository at this point in the history
…Sync()` (#99)
  • Loading branch information
dsherret authored Feb 12, 2023
1 parent 230ecb5 commit d03fc71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,20 +522,12 @@ $.cd("someDir");
console.log(Deno.cwd()); // will be in someDir directory
```

Checking if a path exists:

```ts
// Note: beware of "time of check to time of use" race conditions when using this
await $.exists("./file.txt");
$.existsSync("./file.txt");
```

Sleeping asynchronously for a specified amount of time:

```ts
await $.sleep(100); // ms
await $.sleep("1.5s");
await $.sleep("100ms");
await $.sleep("1m30s");
```

Getting path to an executable based on a command name:
Expand Down
18 changes: 9 additions & 9 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ Deno.test("test remove", async () => {
Deno.test("test mkdir", async () => {
await withTempDir(async (dir) => {
await $`mkdir ${dir}/a`;
await $.exists(dir + "/a");
assert(dir.join("a").existsSync());

{
const error = await $`mkdir ${dir}/a`.noThrow().stderr("piped").spawn()
Expand All @@ -1154,7 +1154,7 @@ Deno.test("test mkdir", async () => {
}

await $`mkdir -p ${dir}/b/c`;
assert(await $.exists(dir + "/b/c"));
assert(await dir.join("b/c").exists());
});
});

Expand Down Expand Up @@ -1216,12 +1216,12 @@ Deno.test("copy test", async () => {
});

Deno.test("cp test2", async () => {
await withTempDir(async () => {
await withTempDir(async (dir) => {
await $`mkdir -p a/d1`;
await $`mkdir -p a/d2`;
Deno.createSync("a/d1/f").close();
await $`cp a/d1/f a/d2`;
assert($.existsSync("a/d2/f"));
assert(dir.join("a/d2/f").existsSync());
});
});

Expand Down Expand Up @@ -1339,15 +1339,15 @@ Empty lines (like the one above) will not affect the common indentation.`.trim()
});

Deno.test("touch test", async () => {
await withTempDir(async () => {
await withTempDir(async (dir) => {
await $`touch a`;
assert($.existsSync("a"));
assert(dir.join("a").existsSync());
await $`touch a`;
assert($.existsSync("a"));
assert(dir.join("a").existsSync());

await $`touch b c`;
assert($.existsSync("b"));
assert($.existsSync("c"));
assert(dir.join("b").existsSync());
assert(dir.join("c").existsSync());

assertEquals(await getStdErr($`touch`), "touch: missing file operand\n");

Expand Down
17 changes: 0 additions & 17 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,6 @@ export interface $BuiltInProperties<TExtras extends ExtrasObject = {}> {
* so that only the content in between remains.
*/
dedent: typeof outdent;
/**
* Gets if the provided path exists asynchronously.
*
* Although there is a potential for a race condition between the
* time this check is made and the time some code is used, it may
* not be a big deal to use this in some scenarios and simplify
* the code a lot.
*/
exists(path: string): Promise<boolean>;
/** Gets if the provided path exists synchronously. */
existsSync(path: string): boolean;
/**
* Determines if the provided command exists resolving to `true` if the command
* will be resolved by the shell of the current `$` or false otherwise.
Expand Down Expand Up @@ -548,12 +537,6 @@ const helperObject = {
return wasmInstance.strip_ansi_codes(text);
},
dedent: outdent,
exists(path: string) {
return fs.exists(path);
},
existsSync(path: string) {
return fs.existsSync(path);
},
sleep,
which(commandName: string) {
if (commandName.toUpperCase() === "DENO") {
Expand Down

0 comments on commit d03fc71

Please sign in to comment.