How to use the touch function from shelljs

Find comprehensive JavaScript shelljs.touch code examples handpicked from public code repositorys.

In the ShellJS library for Node.js, the touch function is used to create an empty file, or update the timestamp of an existing file.

2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
    entries.forEach(entry => {
        assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`);
    });

    // this should result in a changed entry
    shell.touch(goodFile);
    fileCache = fCache.createFromFile(cacheLocation);
    assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`);
    assert(fileCache.getFileDescriptor(goodFile).changed === true, `the entry for ${goodFile} is changed`);
});
fork icon0
star icon0
watch icon1

+ 59 other calls in file

How does shelljs.touch work?

shelljs.touch is a function in the ShellJS library for Node.js that creates an empty file, or updates the timestamp of an existing file.

When shelljs.touch is called with a file path as input, it performs the following operations:

  • If the file does not exist, it creates an empty file with that name.
  • If the file already exists, it updates its timestamp to the current time.

By using shelljs.touch, developers can create empty files or update the timestamps of existing files in their Node.js applications. This can be useful in situations where files need to be created or updated as part of a build process or other automated task. Note that shelljs.touch may not work as expected in certain environments or with certain file systems, so it is important to test thoroughly before using it in production code.

Ai Example

1
2
3
4
5
6
7
const shell = require("shelljs");

// Creating an empty file
shell.touch("example.txt");

// Updating the timestamp of an existing file
shell.touch("example.txt");

In this example, we're using shelljs.touch to create an empty file named "example.txt" in the current directory. If the file already exists, calling shelljs.touch again will update its timestamp to the current time. Note that shelljs.touch returns an object with information about the file operation, including the path of the file that was created or updated and a boolean flag indicating whether the operation was successful. This information can be useful for error handling or for logging the results of the operation to a file or console.