-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as fs from "node:fs/promises"; | ||
import * as os from "node:os"; | ||
import * as path from "node:path"; | ||
|
||
async function makeDirectory(dirPath: string): Promise<void> { | ||
try { | ||
await fs.mkdir(dirPath, { recursive: true }); | ||
} catch (error) { | ||
if (error instanceof Error && "code" in error && error.code !== "EEXIST") { | ||
throw new Error( | ||
`Failed to create directory ${dirPath}: ${error.message}`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
async function touchFile(filePath: string): Promise<void> { | ||
try { | ||
const parentDir = path.dirname(filePath); | ||
await makeDirectory(parentDir); | ||
|
||
try { | ||
const handle = await fs.open(filePath, "wx"); | ||
await handle.close(); | ||
} catch (error) { | ||
if ( | ||
error instanceof Error && | ||
"code" in error && | ||
error.code === "EEXIST" | ||
) { | ||
return; | ||
} | ||
throw error; | ||
} | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to create file ${filePath}: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
} | ||
|
||
async function setFilePermissions(filePath: string): Promise<void> { | ||
try { | ||
if (os.platform() === "win32") { | ||
// On Windows, TODO? | ||
return; | ||
} | ||
|
||
const mode = 0o755; // -rwxr-xr-x | ||
await fs.chmod(filePath, mode); | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to set permissions on ${filePath}: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
} | ||
|
||
export async function setupTaskFile(taskFilePath: string) { | ||
try { | ||
const normalizedDir = path.normalize(path.dirname(taskFilePath)); | ||
const normalizedPath = path.normalize(taskFilePath); | ||
|
||
if (!path.isAbsolute(normalizedDir)) { | ||
throw new Error("Task directory must be an absolute path"); | ||
} | ||
|
||
if (!normalizedPath.startsWith(normalizedDir)) { | ||
throw new Error("Task file must be within the task directory"); | ||
} | ||
|
||
await makeDirectory(normalizedDir); | ||
await touchFile(normalizedPath); | ||
await setFilePermissions(normalizedPath); | ||
} catch (error) { | ||
console.error("Error setting up task file:", error); | ||
throw error; | ||
} | ||
} |