Skip to content

Commit

Permalink
Merge pull request #243 from robin-thomas/robin_writable_path
Browse files Browse the repository at this point in the history
fix: check if provided path is writable
  • Loading branch information
PaulRBerg authored Sep 12, 2023
2 parents b2e345b + 601dfaf commit f6aeded
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/createEthApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Handlebars from "handlebars";
import path from "path";

import { FrameworkKey, TemplateKey } from "./helpers/constants";
import { isDirectoryEmpty } from "./helpers/directories";
import { isDirectoryEmpty, isDirectoryWriteable } from "./helpers/directories";
import { throwFrameworkNotFoundError, throwTemplateNotFoundError } from "./helpers/errors";
import { downloadAndExtractFrameworkHandlebars, hasFramework, hasFrameworkHandlebars } from "./helpers/frameworks";
import { tryGitInit } from "./helpers/git";
Expand Down Expand Up @@ -56,6 +56,11 @@ export async function createEthApp({
process.exit(1);
}

if (!(await isDirectoryWriteable(root))) {
console.error("Your application path is not writable. Please check your directory permissions and try again.");
process.exit(1);
}

shouldUseYarn();
shouldUseYarnWorkspaces();

Expand Down
9 changes: 9 additions & 0 deletions src/helpers/directories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ export function isDirectoryEmpty(directoryPath: string, appName: string): boolea

return true;
}

export async function isDirectoryWriteable(directory: string): Promise<boolean> {
try {
await fs.promises.access(directory, (fs.constants || fs).W_OK);
return true;
} catch (err) {
return false;
}
}
22 changes: 21 additions & 1 deletion test/unit/directories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fsExtra from "fs-extra";
import path from "path";
import tempy from "tempy";

import { isDirectoryEmpty } from "../../src/helpers/directories";
import { isDirectoryEmpty, isDirectoryWriteable } from "../../src/helpers/directories";

// This has to be in sync with the valid files defined "helpers/directories".
const filesDirsTable: string[][] = [
Expand Down Expand Up @@ -105,6 +105,26 @@ describe("directories", function () {
});
});

describe("when the directory is not writable", function () {
beforeEach(async function () {
await fsExtra.chmod(testDirPath, 0o444);
});

test("returns false", async function () {
await expect(isDirectoryWriteable(testDirPath)).resolves.toBe(false);
});
});

describe("when the directory is writable", function () {
beforeEach(async function () {
await fsExtra.chmod(testDirPath, 0o777);
});

test("returns true", async function () {
await expect(isDirectoryWriteable(testDirPath)).resolves.toBe(true);
});
});

afterEach(function () {
fsExtra.removeSync(testDirPath);
});
Expand Down

0 comments on commit f6aeded

Please sign in to comment.