CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tmp-promise

The tmp package with promises support and disposers.

93

1.78x
Overview
Eval results
Files

task.mdevals/scenario-4/

Safe Configuration File Updater

A utility that safely updates configuration files using atomic write operations to prevent data corruption during writes.

Problem Description

You need to implement a configuration file update utility that ensures data integrity by using atomic write operations. The utility should:

  1. Generate a temporary filename in the same directory as the target file
  2. Write new content to the temporary file
  3. Atomically rename the temporary file to replace the original file
  4. Handle errors gracefully and clean up temporary files on failure

This pattern (write to temp, then rename) is commonly used for atomic file operations to ensure that readers never see partial writes and the original file is preserved if the write operation fails.

Requirements

Implement a function updateConfigFile(filePath, newContent) that:

  • Generates a unique temporary filename in the same directory as the target file
  • Writes the new content to the temporary file
  • Performs an atomic rename to replace the original file
  • Cleans up the temporary file if any errors occur during the process
  • Returns a promise that resolves when the update is complete

The function should handle the following scenarios:

  • Creating the temporary file with a .tmp extension
  • Writing content to the temporary file
  • Atomically renaming the temporary file to the target filename
  • Cleaning up the temporary file if writing fails

Implementation

@generates

Test Cases

  • Writing valid content to a config file succeeds and the file contains the new content @test
  • When a write error occurs, the temporary file is cleaned up @test
  • Multiple concurrent updates to different files work correctly @test

API

/**
 * Atomically updates a configuration file with new content.
 *
 * @param {string} filePath - The path to the configuration file to update
 * @param {string} newContent - The new content to write to the file
 * @returns {Promise<void>} A promise that resolves when the update is complete
 * @throws {Error} If the file cannot be written or renamed
 */
async function updateConfigFile(filePath, newContent) {
  // Implementation here
}

module.exports = { updateConfigFile };

Dependencies { .dependencies }

tmp-promise { .dependency }

Provides temporary file management with promise support.

Install with Tessl CLI

npx tessl i tessl/npm-tmp-promise

tile.json