Skip to content

Commit

Permalink
Check directory existence
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetsu committed Mar 23, 2024
1 parent 9f09399 commit 22460ae
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const newfile = (extension: string) => {

const actualNewFilePath = extension ? replaceExtension(newFilePath, extension) : newFilePath;

if (isDirectory(actualNewFilePath)) {
throw new Error(`The path ${actualNewFilePath} is directory`);
}

if (!fs.existsSync(actualNewFilePath)) {
makeTempFile(actualNewFilePath, initialContent);
} else {
Expand All @@ -77,6 +81,13 @@ const newfile = (extension: string) => {
openByTab(actualNewFilePath, append);
};

const isDirectory = (path: string): boolean => {
if (!fs.existsSync(path)) {
return false;
}
return fs.statSync(path).isDirectory();
};

const makeTempFile = async (newFilePath: string, content: string) => {
const dirPath = path.dirname(newFilePath);

Expand Down

0 comments on commit 22460ae

Please sign in to comment.