A Node.js command-line script that asynchronously reads files from a directory and creates an annotated copy of each file (renamed with a timestamp) in a given target directory.
Each file is annotated with four pieces of information:
- The name of the source file (e.g.
file.json
) - The source file’s relative path (e.g.
files/original
) - The name of the annotated target file (e.g.
file_EDITED_2015-1-1_12-44.json
) - The target file’s relative path (e.g.
files/moved
)
The script can be run with npm start
and outputs the results of the script, as
follows:
% npm start
renamed 5 files, with 0 errors
or, for example,
% npm start
ENOENT: no such file or directory, open './files/moved/random-json-list_EDITED_2017-09-07_21-26.json'
ENOENT: no such file or directory, open './files/moved/random-json-file-two_EDITED_2017-09-07_21-26.json'
renamed 3 files, with 2 errors (random-json-list.json, random-json-file-two.json)
% npm start
./files/original/anothertext-file.json: Unexpected token h in JSON at position 1
renamed 4 files, with 1 error (anothertext-file.json)
The script currently handles .txt
and .json
files.
To specify source and target directories, pass the --source
or --target
flags. Defaults are as follows:
% npm start -- --source files/original --target files/moved % npm start -- -s files/original -t files/moved
To install dependencies, run yarn install
.
- Node 8.4
- yarn 0.27.5
- babel-cli
- babel-core
- babel-register
- fs-extra
- pluralize
Tests are written using the following libraries:
Run tests from the project root by issuing npm test
.
% npm test
completionMessage
✓ given no errors, pluralizes correctly
✓ given one error, pluralizes correctly
✓ given multiple errors, pluralizes correctly
✓ given no successes, pluralizes correctly
✓ given one success, pluralizes correctly
✓ given multiple successes, pluralizes correctly
FileInfo
✓ parses original file info
✓ parses target file info
✓ has an object prop with data to be appended to source files
MoveAnnotated
.fileWithInfo
✓ invokes txt file handler if given txt extension
✓ invokes json file handler if given json extension
✓ raises if given an unrecognized type
.txt
✓ writes text from source file, annotated, to target file
.json
✓ writes json from source file, annotated, to target file
Results
#success
✓ pushes onto a success stack
#failure
✓ pushes onto a failure stack
#timestamp
✓ returns an appropriately formatted timestamp string in UTC
17 passing (81ms)