diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cca1794 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: node_js + +node_js: + - "4.4.3" + - "5.11.0" + - "6.1" + +before_script: + - npm install + +script: + - npm run test \ No newline at end of file diff --git a/cli.js b/cli.js index 7314957..06385a2 100755 --- a/cli.js +++ b/cli.js @@ -1,4 +1,3 @@ -#!/usr/bin/env node 'use strict'; const glob = require('glob'); @@ -9,15 +8,6 @@ const sh = require('shelljs'); const mkdirp = require('mkdirp'); const convert = require('./convert'); -const ARGS = yargs - .describe('output', 'Output file') - .describe('version', 'Print version number and exit') - .help('help') - .alias('o', 'output') - .alias('h', 'help') - .alias('v', 'version') - .argv; - const cwd = sh.pwd().toString(); const convertAndWrite = (file, outputPath) => { @@ -27,49 +17,67 @@ const convertAndWrite = (file, outputPath) => { output = convert(output); } catch ( e ) { console.error('ERROR: convert error. Input code is too complex to convert.'); - process.exit(1); + return 1; } const outputDir = path.join(cwd, '/' + outputPath); mkdirp(outputPath); fs.writeFileSync(outputDir + path.basename(file), output); + return 0; }; -if (ARGS.version || ARGS.v) { - const json = require('./package.json'); - console.log(json.name + ' ' + json.version); - process.exit(0); -} - - -if (ARGS._[0]) { - const input = ARGS._[0]; - let output = undefined; - if (ARGS.output || ARGS.o) { - output = ARGS.output || ARGS.o; - } else { - console.error('ERROR: Invalid output'); - process.exit(1); +const cli = (args) => { + if (typeof args === 'undefined') { + return 1; } - if (fs.existsSync(input)) { - convertAndWrite(input, output); - process.exit(0); + if (typeof args !== 'object') { + return 1; } - glob(input, (err, files) => { - if (err) { - console.error('ERROR: Invalid glob definition'); - process.exit(1); + yargs + .describe('output', 'Output file') + .describe('version', 'Print version number and exit') + .help('help') + .alias('o', 'output') + .alias('h', 'help') + .alias('v', 'version'); + + if (args.version || args.v) { + const json = require('./package.json'); + console.log(json.name + ' ' + json.version); + return 0; + } + + + if (args._[0]) { + const input = args._[0]; + let output = undefined; + if (args.output || args.o) { + output = args.output || args.o; + } else { + console.error('ERROR: Invalid output'); + return 1; } - for (let file of files) { - convertAndWrite(file, output); + if (fs.existsSync(input)) { + return convertAndWrite(input, output); } - }); - process.exit(0); -} + glob(input, (err, files) => { + if (err) { + console.error('ERROR: Invalid glob definition'); + return 1; + } + for (let file of files) { + convertAndWrite(file, output); + } + }); + return 0; + } -if ((ARGS.output || ARGS.o) && !ARGS._[0]) { - console.error('ERROR: Invalid input'); - process.exit(1); -} + if ((args.output || args.o) && !args._[0]) { + console.error('ERROR: Invalid input'); + return 1; + } + + console.log('Usage: kaomojify -o '); + return 0; +}; -console.log('Usage: kaomojify -o '); -process.exit(0); \ No newline at end of file +module.exports = cli; \ No newline at end of file diff --git a/cli.test.js b/cli.test.js new file mode 100644 index 0000000..5ae99db --- /dev/null +++ b/cli.test.js @@ -0,0 +1,30 @@ +'use strict'; + +const assert = require('power-assert'); +const cli = require('./cli'); + +const buildArgs = (str) => { + let args = new Object(); + args._ = []; + args[str] = true; + return args; +}; + +describe('cli', () => { + + describe('options', () => { + + it('throw error of run `kaomojify` (empty)', () => { + assert.strictEqual(cli(), 1); + }); + + it('no error of run `kaomojify -v`', () => { + assert.strictEqual(cli(buildArgs('-v')), 0); + }); + + it('no error of run `kotori -h`', () => { + assert.strictEqual(cli(buildArgs('-h')), 0); + }); + + }); +}); diff --git a/convert.js b/convert.js index 9d9b702..5f10da4 100644 --- a/convert.js +++ b/convert.js @@ -1,6 +1,6 @@ -'use strict'; - module.exports = (text) => { + 'use strict'; + let t; const b = [ "(c^_^o)", diff --git a/kaomojify.js b/kaomojify.js new file mode 100755 index 0000000..a32c94b --- /dev/null +++ b/kaomojify.js @@ -0,0 +1,11 @@ +#!/usr/bin/env node +'use strict'; + +const yargs = require('yargs'); +const cli = require('./cli'); + +const exitCode = cli(yargs.argv); + +process.on('exit', () => { + process.exit(exitCode); +}); \ No newline at end of file diff --git a/package.json b/package.json index 616dda5..293fe9f 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,10 @@ { "name": "kaomojify", - "version": "0.1.4", + "version": "0.2.0", "description": "Kaomojify Javascript code", "main": "", "bin": { - "kaomojify": "./cli.js" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "lint": "eslint ./" + "kaomojify": "./kaomojify.js" }, "repository": { "type": "git", @@ -23,14 +19,20 @@ "url": "https://github.com/kokororin/kaomojify/issues" }, "homepage": "https://github.com/kokororin/kaomojify#readme", - "devDependencies": { - "eslint": "^3.5.0", - "eslint-config-crocodile": "^1.0.0" - }, "dependencies": { "glob": "^7.0.6", "mkdirp": "^0.5.1", "shelljs": "^0.7.4", "yargs": "^5.0.0" + }, + "devDependencies": { + "eslint": "^3.5.0", + "eslint-config-crocodile": "^1.0.0", + "mocha": "^3.0.2", + "power-assert": "^1.4.1" + }, + "scripts": { + "test": "mocha ./*.test.js", + "lint": "eslint ./" } }