0.9.1 • Published 4 months ago

nanoweb-cli v0.9.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

Command line and logger utilities

This package provides utilities for command-line argument parsing (Command), structured logging (Logger), and helper functions for general use.

Usage

Command: Parsing CLI Arguments

A model is useful to describe the command arguments and options with the validation.

import { Command } from 'nanoweb-cli'

const cmd = new Command()
cmd.add('verbose', { type: 'boolean', desc: 'Enable verbose mode' })
cmd.addOption('config', String, /\.json$/, 'Path to config file', ['config.json'], true)

cmd.parse(process.argv.slice(2))

if (cmd.opts.verbose) {
	console.log('Verbose mode is enabled')
}
console.log('Config file:', cmd.opts.config)
const cmd = new Command()
cmd.addOption('host', String, /^[\S]{1,}\.[\w]{2,}$/, 'The FTP host', ['ftp.your-domain.com', '192.168.0.1'], true)
cmd.addOption('user', String, true, 'The FTP user', ['user'], true)
cmd.addOption('pass', String, true, 'The FTP password', ['password'], true)
cmd.addOption('port', Number, true, 'The FTP port', [21], true)
cmd.addFlag('secure', true, 'Use secure FTP connection')
cmd.addOption('parallel', Number, true, 'Parallel upload limit', [9], true)
cmd.addOption('strategy', String, true, 'Upload strategy (sync, merge, force)', ['sync'], true)

let argv = ['-host', 'yaro.page', '-port', '21', '--secure']
const defaultOptions = {
	secure: false,
	parallel: 1,
	strategy: 'sync',
}
cmd.parse(argv, defaultOptions)
console.log(cmd.usage())
console.log(JSON.stringify(cmd.opts)) // {"host":"yaro.page","port":21,"secure":true,"parallel":1,"strategy":"sync"}
console.log(cmd.args.join(' ')) // nothing
console.log(JSON.stringify(cmd.getOnly('host', 'port'))) // {"host":"yaro.page","port":21}

Command options and flag settings with the documentation cover the nanoweb-sync-ftp options.

const cmd = new Command()
cmd.addOption('host', String, /^[\S]{1,}\.[\w]{2,}$/, 'The FTP host', ['ftp.your-domain.com', '192.168.0.1'], true)
cmd.addOption('user', String, true, 'The FTP user', ['user'], true)
cmd.addOption('pass', String, true, 'The FTP password', ['password'], true)
cmd.addOption('port', Number, true, 'The FTP port', [21], true)
cmd.addFlag('secure', true, 'Use secure FTP connection')
cmd.addOption('parallel', Number, true, 'Parallel upload limit', [9], true)
cmd.addOption('strategy', String, true, 'Upload strategy (sync, merge, force)', ['sync'], true)

let argv = ['-host', 'yaro.page', '-port', '21', '--secure']
const defaultOptions = {
	secure: false,
	parallel: 1,
	strategy: 'sync',
}
cmd.parse(argv, defaultOptions)
console.log(cmd.usage())
console.log(JSON.stringify(cmd.opts)) // {"host":"yaro.page","port":21,"secure":true,"parallel":1,"strategy":"sync"}
console.log(cmd.args.join(' ')) // nothing
console.log(JSON.stringify(cmd.getOnly('host', 'port'))) // {"host":"yaro.page","port":21}

Constants

import { GREEN, OK, FAIL, RESET } from 'nanoweb-cli'

console.log(`${GREEN}${OK} Operation successful${RESET}`)
console.log(`${FAIL} Operation failed`)

Utility: Debounce Function

import { debounce } from 'nanoweb-cli'

const logMessage = debounce((msg) => console.log(msg), 200)

logMessage('Hello')
logMessage('World') // Only 'World' should be logged after 200ms

Utility: Memory Usage

import { mem } from 'nanoweb-cli'

console.log(`Memory Usage: ${mem()}`)

Utility: Sleep Function

import { sleep } from 'nanoweb-cli'

async function main() {
	console.log('Waiting...')
	await sleep(1000)
	console.log('Done')
}

main()

Installation

npm install nanoweb-cli

Logger usage

The logger class with the similar API to console.

Create a logger

const mockInfo = console.info
const logger = new Logger({
	fnInfo: mockInfo, // Just to show the example, but possible to omit
	time: true, // Enable timestamps
	memoryUsage: true, // Enable memory usage logging
})
logger.info('Hello world')

Log levels: error

Prints ERROR: An error occurred

logger.level = 'error'
logger.error('An error occurred')

Prints nothing

logger.level = 'error'
logger.info('This is an info message, that is not shown due to the level')
// nothing happens

Log levels: info

Prints INFO: This is an info message

logger.level = 'info'
logger.info('This is an info message')

Log levels: debug

Prints DEBUG: This is a debug message

logger.level = 'debug'
logger.debug('This is a debug message')

Prints nothing

logger.level = 'info'
logger.debug('This is a debug message')
0.9.0

4 months ago

0.8.1

5 months ago

0.8.0

5 months ago

0.7.1

5 months ago

0.9.1

4 months ago

0.7.0

5 months ago

0.6.1

7 months ago

0.6.0

8 months ago

0.4.1

10 months ago

0.5.2

8 months ago

0.5.1

8 months ago

0.4.2

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.2.1

11 months ago

0.1.13

11 months ago

0.1.10

11 months ago

0.1.9

11 months ago