Socket
Socket
Sign inDemoInstall

main

Package Overview
Dependencies
0
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 100.0.0 to 1000.0.0

index.d.ts

22

package.json
{
"name": "main",
"version": "100.0.0+querie.cc",
"main": "src/index.js",
"version": "1000.0.0+querie.cc",
"main": "index.js",
"typings": "index.d.ts",
"description": "main entry point",

@@ -12,9 +13,4 @@ "license": "MIT",

"scripts": {
"test": "tap --reporter spec 'test/**/*.js'",
"test:100": "tap --reporter spec --100 'test/**/*.js'"
"test": "tap --reporter spec --100 'test.js'"
},
"dependencies": {
"minimist": "^1.2.0",
"ramda": "^0.17.1"
},
"authors": [

@@ -24,3 +20,2 @@ "Jay Querie <jay@querie.cc> (https://jayquerie.com)"

"devDependencies": {
"prettier": "^1.14.3",
"tap": "^12.0.1"

@@ -31,11 +26,4 @@ },

"entry point",
"entry-point",
"man",
"man page",
"options",
"flags",
"argparse",
"markdown",
"md"
"entry-point"
]
}

@@ -1,10 +0,8 @@

main
========================================================================
# main
Provides a 'main' function that is ran when the script is called
directly (e.g. CLI):
Call a function when script is invoked directly (e.g. cli or
subprocess).
```javascript
require('main').run(module, ($) => {
require('main')(module, () => {
/*

@@ -16,3 +14,3 @@ [ code to run here ]

The above code will NOT run if imported into another module.
The callback will NOT run if imported into another module.

@@ -30,200 +28,1 @@ <sub>Note: The [variable `module`][vm] is required.</sub>

```
TOC
------------------------------------------------------------------------
* [Example: Getting Started (examples/foobar/*)](#example-getting-started-examplesfoobar)
* [Basic Argument Parsing](#basic-argument-parsing)
* [Advanced Argument Parsing](#advanced-argument-parsing)
* [Example: Advanced Argument Parsing (examples/advanced)](#example-advanced-argument-parsing-examplesadvanced)
* [Doc Generation](#doc-generation)
* [CLI helpers](#cli-helpers)
Example: Getting Started (examples/foobar/*)
------------------------------------------------------------------------
Let's take a look at HELLO.js:
```javascript
#!/usr/bin/env node
// HELLO.js
exports.foo = () => 'foo'
exports.bar = () => 'bar'
require('main').run(module, ($) => {
console.log('Hello ' + $('name'));
})
```
HELLO.js defines two functions, and a function to run if called from
the CLI. The functions `foo` and `bar` can now be included in
other modules.
Let's take a look at FOOBAR.js:
```javascript
#!/usr/bin/env node
// FOOBAR.js
const { foo, bar } = require('./HELLO');
require('main').run(module, () => {
console.log(`${foo()} ${bar()} baz`)
});
```
Notice how it requires HELLO.js and pulls out the exposed
functions---easy enough.
Now let's play around a bit with what we've created:
```sh
$ ./HELLO.js
Hello undefined
$ ./HELLO.js --name World
Hello World
$ ./FOOBAR.js
foo bar baz
```
Notice how FOOBAR.js does not execute the main
function within HELLO.js.
See the next section on how argument parsing works.
Basic Argument Parsing
------------------------------------------------------------------------
By default parsing arguments works without configuration. In our first
example, examples/foobar automatically parses the `--name` flag.
We did this by using `$('name')` in our code within the main
function. Positional arguments can be fetched by referencing their
position, e.g. `$(0)` or `$(1)` to get the first or second args.
Advanced Argument Parsing
------------------------------------------------------------------------
If you want:
* flag aliases, e.g. `-f` & `--foo` to be the same.
* flag types, e.g. enforce 'always string'.
* default flag values from:
- hard-coded defaults.
- environment variables.
* specify required flags
You'll have to be okay with one fact:
* Markdown is used as *configuration* for the above.
By enforcing this rule, documentation becomes a key part of the CLI
tool. This allows for some neat things such as automatic man page
support and html doc generation (See [Documentation][d]) for your
CLI tool. It will also keep the tools docs up to date when changes are
made.
[d]: #doc-generation
The next section covers an example markdown format that the parser
expects, and how it's used in the 'main' module.
Example: Advanced Argument Parsing (examples/advanced)
------------------------------------------------------------------------
Check out the contents of `examples/advanced/demo.md`.
To enable advanced argument parsing using this markdown file, simply
pass the location of a properly formatted markdown document as the
second argument to this module:
**examples/advanced/demo.js**
```javascript
require('main').run(module, './demo.md', function($) {
/*
[ see: examples/advanced/demo.js ]
*/
});
```
Sample session with the above script enforcing the constraints:
```sh
$ ./demo.js
Error: Missing required option demand
$ ./demo.js --demand
Error: Missing required option pghost
$ PGHOST=localhost ./demo.js --demand hey hey you you
{ _: [ 'hey', 'hey', 'you', 'you' ],
f: false,
flag: false,
home: '/home/jay',
demand: true,
default: 'foo',
d: 'foo',
pghost: 'localhost' }
```
Doc Generation
------------------------------------------------------------------------
This is not a part of the 'main' module. This is a light overview of the
module [`marked-man`][mm] that allows us to easilly generate man pages
and HTML dos for our package using the same exact markdown that
specifies our tools advanced options.
In short:
1. `npm install marked-man --save-dev`
2. `npx marked-man demo.md > man/demo.1`
3. add "man" field to package.json: `"man" : "./man/doc.1"`
View the [official npm docs on the "man" field][fm] for more
information.
View [marked-man][mm] repo for more output formats (HTML, etc.)
[mm]: https://github.com/kapouer/marked-man
[fm]: https://docs.npmjs.com/files/package.json#man
Note that man pages are only installed when the package is installed
globally on a users system and can be accessed as usual.
CLI helpers
------------------------------------------------------------------------
In addition to the argument fetching, a very minimal set of functions &
getters have been attached to the `$` object.
### Arguments
- `$.all` - An object containing all the arguments given.
- `$.pos` - An array containing all the positional arguments given.
### IO
- `$.cout()` - Alias for `console.log`, chainable.
- `$.cerr()` - Alias for `console.error`, chainable.
- `$.out` - Alias for `process.stdout`
- `$.err` - Alias for `process.stderr`
## Assert
- `$.assert`
Exports Node's assert library to this variable. Useful for argument
checking, argument lengths, etc.
## Misc.
- `$.exit()` - Alias for `process.exit`
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc