Skip to content

Commit

Permalink
#44 Using the JavaScript Standard Style
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Feb 16, 2017
1 parent b0b1422 commit b00f648
Show file tree
Hide file tree
Showing 21 changed files with 1,052 additions and 1,032 deletions.
10 changes: 10 additions & 0 deletions .eslintrc
@@ -0,0 +1,10 @@
{
"extends": "standard",
"env": {
"mocha": true
},
"rules": {
"no-multi-spaces": 0,
"yoda": 0
}
}
33 changes: 17 additions & 16 deletions README.md
Expand Up @@ -4,6 +4,7 @@
[![npm version](https://badge.fury.io/js/wpxml2md.svg)](https://badge.fury.io/js/wpxml2md)
[![Build Status](https://travis-ci.org/akabekobeko/npm-wpxml2md.svg?branch=master)](https://travis-ci.org/akabekobeko/npm-wpxml2md)
[![Document](https://doc.esdoc.org/github.com/akabekobeko/npm-wpxml2md/badge.svg?t=0)](https://doc.esdoc.org/github.com/akabekobeko/npm-wpxml2md)
[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](http://standardjs.com/)

Convert the WordPress XML file to Markdown files.

Expand All @@ -29,29 +30,29 @@ Can export the **WordPress XML** in the following way.
`wpxml2md` is promisify function.

```js
const wpxml2md = require( 'wpxml2md' );
const wpxml2md = require('wpxml2md');

wpxml2md( 'wordpress.xml', 'dest', { report: true } )
.then( () => {
console.log( 'Completed!!' );
wpxml2md('wordpress.xml', 'dest', {report: true})
.then(() => {
console.log('Completed!!');
} )
.catch( ( err ) => {
console.error( err );
} );
.catch((err) => {
console.error(err);
});
```

Add modes:

```js
const wpxml2md = require( 'wpxml2md' );

wpxml2md( 'wordpress.xml', 'dest', { noGFM: true, noMELink: true } )
.then( () => {
console.log( 'Completed!!' );
} )
.catch( ( err ) => {
console.error( err );
} );
const wpxml2md = require('wpxml2md');

wpxml2md('wordpress.xml', 'dest', {noGFM: true, noMELink: true})
.then(() => {
console.log('Completed!!');
})
.catch((err) => {
console.error( err);
});
```

`wpxml2md( src, dest, [OPTIONS] )`
Expand Down
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

14 changes: 9 additions & 5 deletions package.json
Expand Up @@ -8,12 +8,11 @@
"engines": {
"node": ">= 4"
},
"main": "index.js",
"bin": "src/bin/main.js",
"main": "src/lib/index.js",
"bin": "src/bin/index.js",
"files": [
"src/bin",
"src/lib",
"index.js"
"src/lib"
],
"keywords": [
"Convert",
Expand Down Expand Up @@ -52,7 +51,8 @@
},
"scripts": {
"test": "mocha --timeout 50000 --compilers js:babel-register test/**/*.test.js",
"esdoc": "esdoc"
"esdoc": "esdoc",
"eslint": "eslint ./src"
},
"dependencies": {
"collapse-whitespace": "^1.1.4",
Expand All @@ -64,6 +64,10 @@
"babel-register": "^6.22.0",
"esdoc": "^0.5.2",
"esdoc-node": "^1.0.0",
"eslint": "^3.15.0",
"eslint-config-standard": "^6.2.1",
"eslint-plugin-promise": "^3.4.1",
"eslint-plugin-standard": "^2.0.1",
"mocha": "^3.2.0",
"power-assert": "^1.4.2"
}
Expand Down
183 changes: 95 additions & 88 deletions src/bin/cli.js
@@ -1,13 +1,12 @@
'use strict';
'use strict'

const Path = require( 'path' );
const Path = require('path')

/**
* Help text.
* @type {[type]}
*/
const HelpText =
`
const HelpText = `
Usage: wpxml2md [OPTIONS]
Convert the WordPress XML file to Markdown files.
Expand All @@ -34,20 +33,20 @@ Usage: wpxml2md [OPTIONS]
See also:
https://github.com/akabekobeko/npm-wpxml2md
`;
`

/**
* CLI options.
* @type {Object}
*/
const Options = {
help: [ '-h', '--help' ],
help: [ '-h', '--help' ],
version: [ '-v', '--version' ],
input: [ '-i', '--input' ],
output: [ '-o', '--output' ],
modes: [ '-m', '--modes' ],
report: [ '-r', '--report' ]
};
input: [ '-i', '--input' ],
output: [ '-o', '--output' ],
modes: [ '-m', '--modes' ],
report: [ '-r', '--report' ]
}

/**
* Output modes.
Expand All @@ -56,7 +55,7 @@ const Options = {
const Modes = {
noGFM: 'no-gfm',
noMELink: 'no-melink'
};
}

/**
* Provides a command line interface.
Expand All @@ -69,22 +68,22 @@ class CLI {
*
* @return {CLIOptions} Parse results.
*/
static parseArgv( argv ) {
if( !( argv && 0 < argv.length ) ) {
return { help: true };
static parseArgv (argv) {
if (!(argv && 0 < argv.length)) {
return { help: true }
}

switch( argv[ 0 ] ) {
case Options.help[ 0 ]:
case Options.help[ 1 ]:
return { help: true };
switch (argv[ 0 ]) {
case Options.help[0]:
case Options.help[1]:
return {help: true}

case Options.version[ 0 ]:
case Options.version[ 1 ]:
return { version: true };
case Options.version[0]:
case Options.version[1]:
return {version: true}

default:
return CLI._parseArgv( argv );
return CLI._parseArgv(argv)
}
}

Expand All @@ -93,26 +92,26 @@ class CLI {
*
* @param {WritableStream} stdout Standard output.
*/
static printHelp( stdout ) {
stdout.write( HelpText );
static printHelp (stdout) {
stdout.write(HelpText)
}

/**
* Print a version number.
*
* @param {WritableStream} stdout Standard output.
*/
static printVersion( stdout ) {
const read = ( path ) => {
static printVersion (stdout) {
const read = (path) => {
try {
return require( path ).version;
} catch( err ) {
return null;
return require(path).version
} catch (err) {
return null
}
};
}

const version = read( '../package.json' ) || read( '../../package.json' );
stdout.write( 'v' + version + '\n' );
const version = read('../package.json') || read('../../package.json')
stdout.write('v' + version + '\n')
}

/**
Expand All @@ -122,9 +121,9 @@ class CLI {
*
* @return {Boolean} If the option of the value "true".
*/
static _isValue( value ) {
const keys = Object.keys( Options );
return !( keys.some( ( key ) => value === Options[ key ][ 0 ] || value === Options[ key ][ 1 ] ) );
static _isValue (value) {
const keys = Object.keys(Options)
return !(keys.some((key) => value === Options[key][0] || value === Options[key][1]))
}

/**
Expand All @@ -134,45 +133,49 @@ class CLI {
*
* @return {CLIOptions} Parse results.
*/
static _parseArgv( argv ) {
const options = {};
let value = null;

argv.forEach( ( arg, index ) => {
switch( arg ) {
case Options.input[ 0 ]:
case Options.input[ 1 ]:
value = CLI._parseArgValue( argv, index );
if( value ) { options.input = Path.resolve( value ); }
break;

case Options.output[ 0 ]:
case Options.output[ 1 ]:
value = CLI._parseArgValue( argv, index );
if( value ) { options.output = Path.resolve( value ); }
break;

case Options.report[ 0 ]:
case Options.report[ 1 ]:
options.report = true;
break;

case Options.modes[ 0 ]:
case Options.modes[ 1 ]:
value = CLI._parseArgValue( argv, index );
if( value ) {
const modes = CLI._parseModes( value );
options.noGFM = modes.noGFM;
options.noMELink = modes.noMELink;
static _parseArgv (argv) {
const options = {}
let value = null

argv.forEach((arg, index) => {
switch (arg) {
case Options.input[0]:
case Options.input[1]:
value = CLI._parseArgValue(argv, index)
if (value) {
options.input = Path.resolve(value)
}
break;
break

case Options.output[0]:
case Options.output[1]:
value = CLI._parseArgValue(argv, index)
if (value) {
options.output = Path.resolve(value)
}
break

case Options.report[0]:
case Options.report[1]:
options.report = true
break

case Options.modes[0]:
case Options.modes[1]:
value = CLI._parseArgValue(argv, index)
if (value) {
const modes = CLI._parseModes(value)
options.noGFM = modes.noGFM
options.noMELink = modes.noMELink
}
break

default:
break;
break
}
} );
})

return options;
return options
}

/**
Expand All @@ -183,11 +186,13 @@ class CLI {
*
* @return {String} Its contents if the option value, otherwise null.
*/
static _parseArgValue( argv, index ) {
if( !( index + 1 < argv.length ) ) { return null; }
static _parseArgValue (argv, index) {
if (!(index + 1 < argv.length)) {
return null
}

const value = argv[ index + 1 ];
return ( CLI._isValue( value ) ? value : null );
const value = argv[index + 1]
return (CLI._isValue(value) ? value : null)
}

/**
Expand All @@ -197,27 +202,29 @@ class CLI {
*
* @return {Object} Modes.
*/
static _parseModes( arg ) {
const result = {};
if( typeof arg !== 'string' ) { return result; }
static _parseModes (arg) {
const result = {}
if (typeof arg !== 'string') {
return result
}

const units = arg.split( ',' );
units.forEach( ( unit ) => {
switch( unit ) {
const units = arg.split(',')
units.forEach((unit) => {
switch (unit) {
case Modes.noGFM:
result.noGFM = true;
break;
result.noGFM = true
break

case Modes.noMELink:
result.noMELink = true;
break;
result.noMELink = true
break

default:
break;
break
}
} );
})

return result;
return result
}
}

Expand All @@ -226,4 +233,4 @@ module.exports = {
Options: Options,
Modes: Modes,
CLI: CLI
};
}

0 comments on commit b00f648

Please sign in to comment.