npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

justo-plugin-fs

v0.4.1

Published

Simple tasks to run file system commands.

Downloads

24

Readme

NPM version Build Status Dependency Status devDependency Status

Simple tasks to run file system commands:

  • Copy files and directories.
  • Clean files and directories.
  • Create files and directories.
  • Change owner and group.
  • Change mode.

Proudly made with ♥ in Valencia, Spain, EU.

Install

npm install justo-plugin-fs

Use

const fs = require("justo-plugin-fs");

create task

The create task creates files and/or directories:

create(justoOpts, opts : object) : number
create(justoOpts, src : string) : number
create(justoOpts, src : string[]) : number

Returns the number of entries created.

The opts parameter can have the following properties:

  • src (string or string[]). The entries to create. The directories must end with a / character.
  • file (string or object) or files (string[] or object[]). The files to create.
  • dir (string) dirs (string[]). The directories to create.

The file or files can have:

  • path (string). The file path.
  • content (string or object). The file content. If object, the task transforms it to JSON.
  • overwrite (boolean). Overwrite if existing? true, yep; false, nope. Default: false.

Examples for creating files:

const create = require("justo-plugin-fs").create;

//empty file
create("Create file.txt", "file.txt")
create("Create file.txt", {src: "file.txt"});
create("Create file.txt", {file: "file.txt"});

//file with content
create("Create file.txt", {file: {path: "file.txt", content: "ABC."}});

//several empty files
create("Create a.txt and b.txt", "a.txt", "b.txt");
create("Create a.txt and b.txt", {src: ["a.txt", "b.txt"]});
create("Create a.txt and b.txt", {files: ["a.txt", "b.txt"]});
create("Create a.txt and b.txt", {files: [{path: "a.txt"}, {path: "b.txt"}]});

//several files with content
create("Create a.txt and b.txt", {files: [{path: "a.txt", content: "ABC."}, {path: "b.txt", content: "CBA."}]});

The dir and dirs are used for creating directories. Examples:

const create = require("justo-plugin-fs").create;

create("Create dir", "dir/");
create("Create dir", {src: "dir/"});
create("Create dir", {src: ["dir1/", "dir2/"]});
create("Create dir", {dir: "dir"});
create("Create dir", {dirs: ["dir"]});
create("Create dir1 and dir2", {dirs: ["dir1", "dir2"]});

The src option can be used for creating files and directories. In this case, the directories must end with the / character. If we use the dir or dirs options, it is not needed.

clean task

The clean task removes files and directories. This must be called as follows:

clean(justoOpts, opts : object)
clean(justoOpts, src : string|string[])

To delete files, use the files property as string[]. If some file doesn't exist, clean() doesn't throw any error. If some file is a directory, the directory isn't removed.

To delete directories, we must use the dirs property as string[]. If some directory doesn't exist, clean() doesn't throw any error. If some directory is a file, the file isn't removed.

When we don't want to indicate the entry type, we can use the src property (string[]).

Example:

const clean = require("justo-plugin-fs").clean;

clean("Example", {
  files: [
    "a.txt",
    "b.txt"
  ],
  dirs: [
    "dir1",
    "dir2"
  ]
});

clean("Example", {
  src: [
    "a.txt",
    "b.txt",
    "dir1",
    "dir2"
  ]
});

clean("Example", [
  "a.txt",
  "b.txt",
  "dir1",
  "dir2"
])

remove task

The remove task is an alias of clean.

copy task

The copy task must be called as follows:

copy(justoOpts, {src, dst, opts})
copy(justoOpts, [{src, dst, ignore, force}, {src, dst, ignore, force}, {src, dst, ignore, force}...])

When we use the parameters src and dst, the first one indicates the entry to copy to the second one. If dst ends with /, the source will be copied to the directory with the same name as the source has. Example:

copy("title...", {src: "entry", dst: "dir/"});      //copy entry to dir/entry
copy("title...", {src: "entry", dst: "entry.old"}); //copy entry to entry.old

The copy options are:

  • ignore (string or string[]). To exclude entries.
  • force (boolean). Don't throw error when nonexistent entries. Default value: false.

Example:

const copy = require("justo-plugin-fs").copy;

//copy one.txt to dir/one.txt
//copy two.txt to dir/two.txt
copy("title...", {
  src: ["one.txt", "two.txt"],
  dst: "dir"
});

//copy dir1/one.txt to dst/one.txt
//copy dir2/two.txt to dst/two.txt
copy("title...", {
  src: ["dir1/one.txt", "dir2/two.txt"],
  dst: "dst"
});

//copy src/one.txt to dst1/one.txt
//copy src/two.txt to dst2/two.txt
copy("title...", [
  {src: "src/one.txt", dst: "dst1"},
  {src: "src/two.txt", dst: "dst2"}
]);

exists task

Check whether an entry exists:

exists(justoOpts, opts : object) : true

The opts parameter:

  • src (string). The entry to check.

Example:

if (!fs.exists("Check whether /etc/apt/sources.list.d/rethinkdb.list exists", {src: "/etc/apt/sources.list.d/rethinkdb.list"})) {
  ...
}

chown task

Change owner and group:

chown(justoOpts, opts : object)

The opts parameter:

  • src (string or string[]). The source dirs and files.
  • user (number). The new owner.
  • group (number). The new group.
  • recurse (boolean). Recurse? Default: false.

Example:

chown("Set couchdb as owner", {
  src: ["/usr/lib/couchdb", "/usr/share/couchdb", "/etc/couchdb", "/usr/bin/couchdb"],
  user: 123,
  group: 321,
  recurse: true
});

chmod task

Change mode:

chmod(justoOpts, opts : object)

The opts parameter:

  • src (string or string[]). The source dirs and files.
  • mode (number|string). The new mode.
  • recurse (boolean). Recurse? Default: false.

Example:

chmod("Set permissions", {
  src: ["/usr/lib/couchdb", "/usr/share/couchdb", "/etc/couchdb", "/usr/bin/couchdb"],
  mode: "777",
  recurse: true
});