( filePath: string, secrets: Record<string, string>, )
| 135 | ); |
| 136 | |
| 137 | const writeAll = ( |
| 138 | filePath: string, |
| 139 | secrets: Record<string, string>, |
| 140 | ): Effect.Effect<void, StorageError> => { |
| 141 | const dir = path.dirname(filePath); |
| 142 | const tmp = `${filePath}.${process.pid}.${randomUUID()}.tmp`; |
| 143 | return Effect.gen(function* () { |
| 144 | if (!fs.existsSync(dir)) { |
| 145 | yield* Effect.try({ |
| 146 | try: () => fs.mkdirSync(dir, { recursive: true, mode: 0o700 }), |
| 147 | catch: toStorageError("Failed to create auth directory"), |
| 148 | }); |
| 149 | } |
| 150 | yield* Effect.try({ |
| 151 | try: () => { |
| 152 | fs.writeFileSync(tmp, JSON.stringify(secrets, null, 2), { mode: 0o600 }); |
| 153 | // `mode` only applies when the file is created; chmod afterwards covers |
| 154 | // the case where a temporary file with broader permissions already exists. |
| 155 | fs.chmodSync(tmp, 0o600); |
| 156 | }, |
| 157 | catch: toStorageError("Failed to write temporary auth file"), |
| 158 | }); |
| 159 | yield* Effect.try({ |
| 160 | try: () => fs.renameSync(tmp, filePath), |
| 161 | catch: toStorageError("Failed to replace auth file"), |
| 162 | }); |
| 163 | }); |
| 164 | }; |
| 165 | |
| 166 | const migrateLegacyAuthFile = ({ |
| 167 | filePath, |
no test coverage detected