directory-helpers
A collection of helper methods for working with directories
Installing
directory-helpers
is available as an
npm package.
Usage
directory-helpers
is best used in an ES.next environment with async/await
enabled.
; async { const directory = './project'; await directory; const output = await project};
Methods
- constructor
- contains
- create
- exec
- execJs
- glob
- path
- read
- remove
- resolve
- spawn
- start
- stat
- stop
- symlink
- write
Constructor
; const directory = basePath
Initializes a directory adapter pointing to the given basePath
. The given
basePath
will be passed through path.resolve
. Note that the directory is
not created by the constructor.
Contains
; { const directory = '/tmp/project'; const fileInDirectory = await directory;}
Determines whether the given path is contained within the basePath
of the
directory. Note that #contains
does not determine whether a file actually
exists at the given path.
Create
; { const basePath = './project'; const directory = basePath; await directory;}
Creates the directory at the basePath
given to the constructor if it does not
already exist.
Exec
; { const directory = './project'; await directory; const output = await directory;}
Executes the given shell command from basePath
, returning the contents of
stdout
after the process exits. If the process exits unsuccessfully, the
contents of stderr
are thrown as an Error
.
ExecJs
; { const directory = './project'; await directory; const output = await directory;}
Executes the given JavaScript (ES.next supported) code from basePath
,
returning the output of stdout
. If execution fails, an error is thrown with
the contents of stderr
.
Glob
; { const directory = './project'; await directory; const files = await directory;}
Evaluates a glob using glob
,
exposing the same interface. Note that options.cwd
is set to the basePath
of the directory.
Path
; { const directory = './project'; directory;}
Resolves paths relative to the basePath
of the directory.
Read
; { const directory = './project'; await directory; const code = await directory; const data = await directory;}
Reads the contents of a file at the path relative to the basePath
of the
directory. The file contents are parsed using JSON.parse()
when possible.
Remove
; { const directory = './project'; await directory; await directory; await directory;}
Deletes a file at path relative to the basePath
of the directory. If no file
is given, deletes the directory at basePath
if it exists.
Resolve
; { const directory = './project'; await directory;}
Resolves the absolute path to a Node.js module using the require.resolve
algorithm.
Spawn
; { const directory = './project'; await directory; const server = directory; await server; serverprocess;}
Spawns a child process from basePath
and returns an
Observable
of text produced
by stdout
and stderr
. The ChildProcess
instance can be accessed from the
process
attribute. The command is spawned in its own process group using
setsid
so that when ChildProcess#kill
is called, it kills the spawned
process and all of its subprocesses.
Start
; { const directory = './project'; await directory; await directorystart/Listening/; // ... await directory;}
Spawns npm start
with basePath
as the working directory. If a regular
expression is given, #start
waits and resolves after reading a matching line
from STDOUT. The process is spawned in its own process group, making it easier
to kill the process and all of its descendent processes.
Stat
; { const directory = './project'; const stats = await directory;}
Reads the file status for the file at the given path relative to basePath
,
returning an instance of fs.Stats
.
Stop
; { const directory = './project'; await directory; await directorystart/Listening/; // ... await directory;}
Stops the process group spawned by #start
.
Symlink
; { const directory = './project'; await directory;}
Creates a symbolic link at destinationPath
pointing to the sourcePath
. Both
paths are relative to the basePath
. If any of the directories in the
destinationPath
do not exist, they are created.
Write
; { const directory = './project'; await directory; await directory;}
Creates or overwrites files at paths relative to the basePath
of the
directory. If the file contents are an object or array, it will be
converted to a string using JSON.stringify(contents, null, 2)
. If the file
contents are a string, that string will be re-indented so that there is no
leading space on the first line. Any missing directories will be created.
Development
Getting Started
The application requires the following external dependencies:
- Node.js
The rest of the dependencies are handled through:
npm install
Run tests with:
npm test