Comparing version 0.0.11 to 0.0.12
@@ -10,10 +10,10 @@ #!/usr/bin/env node | ||
.flags({ | ||
n: { alias: 'name', demand: true } | ||
n: { alias: 'name', demand: true } | ||
}) | ||
.run(function($) { | ||
$.assert.argsLen(2); // exit if there isn't two words | ||
var word0 = $(0), word1 = $(1); // pull from positional arguments | ||
var name = $('name'); // pull from flags. You can also use $('n') here | ||
var sentence = exports.sentence($('name'), word0, word1); | ||
$.cout(sentence).exit(); // prints the sentence, exits 0 | ||
// Check that there is two positional arguments, then call the function | ||
// exports.sentence with the value of flag "name", and the first two | ||
// positional arguments. Finally, exit the script (optional, read why | ||
// in the documentation for exit). | ||
$.assert.argsLen(2).cout(exports.sentence($('name'), $(0), $(1))).exit(); | ||
}); |
{ | ||
"name": "main", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"main": "index.js", | ||
@@ -13,3 +13,4 @@ "repository": { | ||
"dependencies": { | ||
"optimist": "~0.6.0" | ||
"optimist": "~0.6.0", | ||
"temp": "~0.6.0" | ||
}, | ||
@@ -16,0 +17,0 @@ "author": "Trevor Senior <trevor@tsenior.com> (http://tsenior.com/)", |
147
README.md
@@ -34,3 +34,3 @@ node-main | ||
### `.usage(message)` | ||
#### `.usage(message)` | ||
@@ -43,3 +43,3 @@ An optional message to append to the top of flags that can describe how the script should be invoked, e.g. | ||
### `.flags(options)` | ||
#### `.flags(options)` | ||
@@ -57,3 +57,3 @@ `options` follows the [optimist format for options](https://github.com/substack/node-optimist#optionskey-opt), but groups them together, e.g.: | ||
### `.run(fn)` | ||
#### `.run(fn)` | ||
@@ -74,43 +74,54 @@ `fn` is the callback that will be invoked when the script is ran directly from a terminal. It can take the following parameters: | ||
### `$(positionOrFlag)` | ||
### General | ||
Allows easy access to positional arguments or flag values. | ||
##### `$.exit([exitCode])` | ||
```bash | ||
./script --name Bill /path/to/file | ||
Exits the running program with the specified exit code. If no code is specified, it will exit with code 0 (success). The script doesn't have to explicitly be exited with this command, e.g. | ||
```javascript | ||
// both of these are valid and will exit with a status of 0 (success) | ||
require('main')(module).run(function($) { $.cout('Hello World').exit(); }); | ||
require('main')(module).run(function($) { $.cout('Hello World'); }); | ||
``` | ||
##### `$.stdin(callback)` | ||
Read from stdin in it's entirety, and return a string once finished in a callback. Do not use this if you are dealing with large amounts of data from stdin - look towards streaming solutions. | ||
```javascript | ||
$(0); // returns the argument in position 0, or /path/to/file | ||
$('name'); // returns 'Bill' | ||
// read from stdin, write to stdout | ||
$.stdin(function(input) { $.out(input); }); | ||
``` | ||
### `$.help` | ||
##### `$.stringify(thing)` | ||
A string holding the help message that was generated from the usage and any flags provided. | ||
Converts some "thing" that is passed in to a string. If it's an object, it will attempt to call `JSON.stringify` on it. If it's an instance of `Error` it will attempt to get the message of said error. | ||
### `$.args` | ||
##### `$.help` | ||
An array holding the positional arguments that this script was invoked with. | ||
A string holding the help message that was generated from the usage (see above) and any flags provided. | ||
### `$.exit([exitCode])` | ||
Exits the running program with the specified exit code. | ||
### Flags and Arguments | ||
### `$.stringify(thing)` | ||
##### `$(positionOrFlag)` | ||
Converts some "thing" that is passed in to a string. If it's an object, it will attempt to call `JSON.stringify` on it. If it's an instance of `Error` it will attempt to get the message of said error. | ||
Allows easy access to positional arguments or flag values. | ||
### `$.stdin(callback)` | ||
```sh | ||
./script --name Bill /path/to/file | ||
``` | ||
Read from stdin in it's entirety, and return a string once finished in a callback. Do not use this if you are dealing with large amounts of data from stdin - look towards streaming solutions. | ||
```javascript | ||
// read from stdin, write to stdout | ||
$.stdin(function(input) { $.out(input); }); | ||
$(0); // returns the argument in position 0, or '/path/to/file' | ||
$('name'); // returns 'Bill' | ||
``` | ||
##### `$.args` | ||
An array holding the positional arguments that this script was invoked with. | ||
### Output Helpers | ||
Output helpers print text to the window, and can be chained, e.g. | ||
Output helpers print text to the window and can be chained, e.g. | ||
@@ -121,13 +132,71 @@ ```javascript | ||
#### `$.out(something)` and `$.cout(something, [arg1, arg2, ...])` | ||
##### `$.out(string)` | ||
Write something to standard output. `$.out` uses `process.stdout.write` and `cout` uses `console.log`. | ||
Write something to standard output using [`process.stdout.write`](http://nodejs.org/api/process.html#process_process_stdout). | ||
#### `$.err(something)` and `$.cerr(something, [arg1, arg2, ...])` | ||
##### `$.cout([data], [...])` | ||
Write something to standard output. `$.err` uses `process.stderr.write` and `$.cerr` uses `console.error`. | ||
Write something to standard output using [`console.log`](http://nodejs.org/api/stdio.html#stdio_console_log_data). | ||
##### `$.err(something)` | ||
Write something to standard error using [`process.stderr.write`](http://nodejs.org/api/process.html#process_process_stderr). | ||
##### `$.cerr([data], [...])` | ||
Write something to standard error using [`console.error`](http://nodejs.org/api/stdio.html#stdio_console_error_data). | ||
### File Operations | ||
All file operations are sync, and some can be chained. Take a look at the examples/fileOps.js file for some use cases. | ||
##### `$.mktemp([options])` | ||
Returns a temporary file path - it does not create the file. | ||
The options allow you to specify a prefix, suffix, and a directory. All of these are optional: | ||
```javascript | ||
// returns a path to a temporary file ending in .txt in the system tmp folder. | ||
$.mktemp({ suffix: '.txt' }); | ||
// same as above, but we've specified a custom folder to create this temp file. | ||
$.mktemp({ suffix: '.txt' dir: '/home/nolan/myapp' }) | ||
``` | ||
##### `$.read(fileName)` | ||
Reads the contents of a given file. Assumes that the encoding is utf8. | ||
##### `$.write(fileName, data)` | ||
Writes data to a given file. | ||
##### `$.append(fileName, data)` | ||
Appends data to a given file. | ||
##### `$.rm(fileName)` | ||
Removes a given file or directory. | ||
##### `$.walk(directory)` | ||
Returns back an array of absolute file paths that exist beneith this directory. e.g. | ||
```javascript | ||
$.walk('/home/nolan/myapp'); | ||
/* | ||
Example return values: | ||
[ '/home/nolan/myapp/FILE1.txt', | ||
'/home/nolan/myapp/FILE2.txt' | ||
'/home.nolan/myapp/subfolder/foobar.png' ] | ||
*/ | ||
``` | ||
If something went wrong fetching the files in the directory, the script will exit and print the error message. | ||
### Assert Statements | ||
Assert statements help with the checking basic things. If an assertion fails it will print the usage information followed by the reason the script failed and then exit with a status of 1. All assertions can be chained, e.g. | ||
Assert statements help with the checking of basic things. If an assertion fails it will print the usage information followed by the reason the script failed and then exit with a status of 1. All assertions can be chained, e.g. | ||
@@ -138,3 +207,3 @@ ```javascript | ||
#### `$.assert.argsLen` | ||
##### `$.assert.argsLen` | ||
@@ -149,2 +218,10 @@ Check for positional arguments length. There is a family of checks available for use: | ||
##### `$assert.fileExists(fileName)` | ||
Checks if a file exists. Useful if you want to ensure a file exists before reading from it, e.g. | ||
```javascript | ||
var fileContents = $.assert.fileExists('foo.txt').read('foo.txt'); | ||
``` | ||
## Example | ||
@@ -166,10 +243,10 @@ | ||
.flags({ | ||
n: { alias: 'name', demand: true } // create flag "n", must be present | ||
n: { alias: 'name', demand: true } | ||
}) | ||
.run(function($) { | ||
$.assert.argsLen(2); // exit if there aren't two words | ||
var word0 = $(0), word1 = $(1); // pull from positional arguments | ||
var name = $('name'); // pull from flags. You can also use $('n') here | ||
var sentence = exports.sentence($('name'), word0, word1); | ||
$.cout(sentence).exit(); // prints the sentence, exits 0 | ||
// Check that there is two positional arguments, then call the function | ||
// exports.sentence with the value of flag "name", and the first two | ||
// positional arguments. Finally, exit the script (optional, read why | ||
// in the documentation for exit). | ||
$.assert.argsLen(2).cout(exports.sentence($('name'), $(0), $(1))).exit(); | ||
}); | ||
@@ -176,0 +253,0 @@ ``` |
@@ -115,3 +115,84 @@ var extend = require('util')._extend; | ||
/* | ||
Path to a temporary file, that is it. | ||
@param {string} optional suffix to append to this temporary file name. | ||
*/ | ||
self.mktemp = function(options) { | ||
return require('temp').path(options); | ||
}; | ||
/* | ||
Makes a directory, that is it. | ||
*/ | ||
self.mkdir = function(path) { | ||
require('fs').mkdirSync(path); | ||
return self; | ||
}; | ||
/* | ||
Write some data to a file | ||
@param {string} fileName - The file to write to. | ||
@param {string} data - Data to write. | ||
*/ | ||
self.write = function(fileName, data) { | ||
require('fs').writeFileSync(fileName, data); | ||
return self; | ||
}; | ||
/* | ||
Append some data to a file | ||
@param {string} fileName - The file to write to. | ||
@param {string} data - Data to append to the file. | ||
*/ | ||
self.append = function(fileName, data) { | ||
require('fs').appendFileSync(fileName, data); | ||
return self; | ||
}; | ||
/* | ||
Reads some data from a file. Assumes utf8 encoding. | ||
@param {string} fileName - The file to read from. | ||
*/ | ||
self.read = function(fileName) { | ||
return require('fs').readFileSync(fileName, { encoding: 'utf8' }); | ||
}; | ||
/* | ||
Removes the file. Careful as this will remove directories as well. | ||
@param {string} fileName - The file to remove. | ||
*/ | ||
self.rm = function(fileName) { | ||
var fs = require('fs'); | ||
if (fs.statSync(fileName).isDirectory()) { | ||
fs.rmdirSync(fileName); | ||
} else { | ||
fs.unlinkSync(fileName); | ||
} | ||
return self; | ||
}; | ||
self.walk = function(directory) { | ||
console.log(directory); | ||
// Modified from http://stackoverflow.com/a/16684530/586621 | ||
var fs = require('fs'); | ||
var walk = function(dir) { | ||
var results = []; | ||
var list = fs.readdirSync(dir); | ||
list.forEach(function(file) { | ||
file = dir + '/' + file; | ||
var stat = fs.statSync(file); | ||
if (stat && stat.isDirectory()) { | ||
results = results.concat(walk(file)); | ||
} | ||
else { results.push(file); } | ||
}); | ||
return results; | ||
}; | ||
return walk(directory); | ||
}; | ||
self.assert = { | ||
// Positional Argument Asserts | ||
argsLen: function(length) { | ||
@@ -156,2 +237,9 @@ if (self.args.length !== length) { | ||
return self; | ||
}, | ||
// File Asserts | ||
fileExists: function(fileName) { | ||
if (!require('fs').existsSync(fileName)) { | ||
self.cerr('Expected file ' + fileName + ' to exist').exit(1); | ||
} | ||
return self; | ||
} | ||
@@ -158,0 +246,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20087
12
317
270
0
2
7
+ Addedtemp@~0.6.0
+ Addedgraceful-fs@1.2.3(transitive)
+ Addedosenv@0.0.3(transitive)
+ Addedrimraf@2.1.4(transitive)
+ Addedtemp@0.6.0(transitive)