CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-touch

Node.js implementation of Unix touch(1) command functionality for creating files and updating timestamps

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Touch

Touch is a Node.js implementation of the Unix touch(1) command functionality for creating empty files and updating access/modification timestamps. It provides both synchronous and asynchronous APIs with comprehensive options for controlling file timestamp behavior.

Package Information

  • Package Name: touch
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install touch

Core Imports

const touch = require("touch");

For ES modules:

import touch from "touch";

Basic Usage

const touch = require("touch");

// Create or update a file's timestamps to current time
await touch("path/to/file.txt");

// Create with specific options
await touch("path/to/file.txt", {
  atime: true,     // Update access time only
  nocreate: false, // Create file if it doesn't exist
  time: new Date("2023-01-01") // Set specific time
});

// Synchronous version
touch.sync("path/to/file.txt");

Capabilities

Main Touch Function

Creates or updates a file's access and modification timestamps asynchronously.

/**
 * Touch a file to update its access and modification times
 * @param {string} f - Path to the file to touch
 * @param {TouchOptions} [options] - Configuration options
 * @param {Function} [callback] - Optional callback function
 * @returns {Promise<undefined>} Promise that resolves to undefined when operation completes
 */
function touch(f, options, callback);

Synchronous Touch

Synchronous version of the main touch function.

/**
 * Synchronously touch a file to update its timestamps
 * @param {string} f - Path to the file to touch
 * @param {TouchOptions} [options] - Configuration options
 * @returns {undefined}
 */
function touchSync(f, options);

// Also available as:
touch.sync(f, options);

File Descriptor Touch

Touch a file using an existing file descriptor instead of a path.

/**
 * Touch a file using file descriptor
 * @param {number} fd - File descriptor
 * @param {TouchOptions} [options] - Configuration options
 * @param {Function} [callback] - Optional callback function
 * @returns {Promise<undefined>} Promise that resolves to undefined when operation completes
 */
function ftouch(fd, options, callback);

// Available as:
touch.ftouch(fd, options, callback);

Synchronous File Descriptor Touch

Synchronous version of file descriptor touch.

/**
 * Synchronously touch a file using file descriptor
 * @param {number} fd - File descriptor
 * @param {TouchOptions} [opt] - Configuration options
 * @returns {undefined}
 */
function ftouchSync(fd, opt);

// Available as:
touch.ftouchSync(fd, opt);

Command Line Interface

Touch provides a CLI binary that mimics Unix touch(1) behavior.

# Install globally to use CLI
npm install -g touch

# Basic usage
nodetouch file1 file2 file3

# Update access time only
nodetouch -a file.txt

# Update modification time only  
nodetouch -m file.txt

# Don't create file if it doesn't exist
nodetouch -c file.txt

# Force operation
nodetouch -f file.txt

# Use reference file's timestamps
nodetouch -r reference.txt target.txt

# Set specific time (format: [[CC]YY]MMDDhhmm[.SS])
nodetouch -t 202301011200.00 file.txt

Types and Options

/**
 * Configuration options for touch operations
 */
interface TouchOptions {
  /** Force operation like touch -f */
  force?: boolean;
  
  /** Set specific time - Date object, parseable string, or epoch ms */
  time?: Date | string | number;
  
  /** Update access time only (like touch -a) - boolean, Date, or epoch seconds */
  atime?: boolean | Date | number;
  
  /** Update modification time only (like touch -m) - boolean, Date, or epoch seconds */
  mtime?: boolean | Date | number;
  
  /** Reference file path to copy timestamps from (like touch -r) */
  ref?: string;
  
  /** Don't create file if it doesn't exist (like touch -c) */
  nocreate?: boolean;
  
  /** Close file descriptor after operation (only for ftouch/ftouchSync) */
  closeAfter?: boolean;
}

Usage Examples

Basic File Creation and Updates

const touch = require("touch");

// Create a new file or update existing file to current time
await touch("new-file.txt");

// Create multiple files
await Promise.all([
  touch("file1.txt"),
  touch("file2.txt"),
  touch("file3.txt")
]);

// Synchronous version
touch.sync("sync-file.txt");

Timestamp Control

// Set specific timestamps
await touch("file.txt", {
  time: new Date("2023-06-15T10:30:00Z")
});

// Update only access time
await touch("file.txt", {
  atime: true
});

// Update only modification time
await touch("file.txt", {
  mtime: true
});

// Set different times for access and modification
await touch("file.txt", {
  atime: new Date("2023-01-01"),
  mtime: new Date("2023-06-01")
});

Reference File Timestamps

// Copy timestamps from another file
await touch("new-file.txt", {
  ref: "reference-file.txt"
});

File Descriptor Operations

const fs = require("fs");

// Open file and touch using file descriptor
const fd = fs.openSync("file.txt", "w");
await touch.ftouch(fd, {
  time: new Date()
});
fs.closeSync(fd);

// Synchronous version
const fd2 = fs.openSync("file2.txt", "w");
touch.ftouchSync(fd2, {
  atime: true,
  mtime: true
});
fs.closeSync(fd2);

// Using closeAfter option to automatically close the file descriptor
const fd3 = fs.openSync("file3.txt", "w");
await touch.ftouch(fd3, {
  time: new Date(),
  closeAfter: true // Automatically closes fd3 after operation
});

Advanced Options

// Don't create file if it doesn't exist
await touch("maybe-exists.txt", {
  nocreate: true
});

// Force operation
await touch("readonly-file.txt", {
  force: true
});

// Combined options
await touch("complex-file.txt", {
  force: true,
  nocreate: false,
  atime: new Date("2023-01-01"),
  mtime: true  // Will use current time
});

Error Handling

// With callback
touch("file.txt", { time: new Date() }, (err) => {
  if (err) {
    console.error("Touch failed:", err.message);
  } else {
    console.log("File touched successfully");
  }
});

// With Promise
try {
  await touch("file.txt");
  console.log("Success");
} catch (err) {
  console.error("Touch failed:", err.message);
}

// Common error scenarios:
// - ENOENT: File doesn't exist and nocreate is true
// - EACCES: Permission denied
// - EISDIR: Target is a directory (handled gracefully)

CLI Usage Patterns

# Create empty files
nodetouch file1.txt file2.txt

# Update existing files to current time
nodetouch existing-file.txt

# Set specific timestamp
nodetouch -t 202312251200.00 christmas-file.txt

# Copy timestamps from reference file
nodetouch -r source.txt target1.txt target2.txt

# Update access time only, don't create if missing
nodetouch -ac important-file.txt

# Force update on read-only file
nodetouch -f readonly-file.txt

docs

index.md

tile.json