(filePath: string)
| 77 | }; |
| 78 | |
| 79 | export const assertWritableProjectPath = (filePath: string): void => { |
| 80 | const projectRoot = getRealPath(process.cwd()); |
| 81 | const targetPath = path.resolve(process.cwd(), filePath); |
| 82 | |
| 83 | if (!isInsidePath(targetPath, projectRoot)) { |
| 84 | throw new Error(`Refusing to write outside project: ${filePath}`); |
| 85 | } |
| 86 | |
| 87 | const parentPath = path.dirname(targetPath); |
| 88 | const realParentPath = getRealPathOfNearestExistingAncestor(parentPath); |
| 89 | |
| 90 | if (!isInsidePath(realParentPath, projectRoot)) { |
| 91 | throw new Error( |
| 92 | `Refusing to write through directory outside project: ${filePath}` |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | try { |
| 97 | const targetStats = |
| 98 | typeof lstatSync === "function" ? lstatSync(targetPath) : undefined; |
| 99 | |
| 100 | if (targetStats?.isSymbolicLink()) { |
| 101 | throw new Error(`Refusing to write through symbolic link: ${filePath}`); |
| 102 | } |
| 103 | |
| 104 | const realTargetPath = getRealPath(targetPath); |
| 105 | if (!isInsidePath(realTargetPath, projectRoot)) { |
| 106 | throw new Error(`Refusing to write outside project: ${filePath}`); |
| 107 | } |
| 108 | } catch (error) { |
| 109 | if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | throw error; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | export const writeProjectFile = async ( |
| 118 | filePath: string, |
no test coverage detected
searching dependent graphs…