Kaynağa Gözat

Merge pull request #503 from janl/lint-cli

Linting CLI tool & fixing its current style issues
tags/v2.2.0
David da Silva 10 yıl önce
ebeveyn
işleme
1e87dd679c
3 değiştirilmiş dosya ile 29 ekleme ve 28 silme
  1. +1
    -0
      .eslintrc
  2. +27
    -27
      bin/mustache
  3. +1
    -1
      package.json

+ 1
- 0
.eslintrc Dosyayı Görüntüle

@@ -14,6 +14,7 @@
"curly": 0, "curly": 0,
"consistent-return": 0, "consistent-return": 0,
"no-use-before-define": 0, "no-use-before-define": 0,
"no-process-exit": 0,
"strict": 0 "strict": 0
} }
} }

+ 27
- 27
bin/mustache Dosyayı Görüntüle

@@ -10,7 +10,7 @@ var partials = {};
var partialsPaths = []; var partialsPaths = [];
var partialArgIndex = -1; var partialArgIndex = -1;


while((partialArgIndex = process.argv.indexOf("-p")) > -1){
while ((partialArgIndex = process.argv.indexOf('-p')) > -1) {
partialsPaths.push(process.argv.splice(partialArgIndex, 2)[1]); partialsPaths.push(process.argv.splice(partialArgIndex, 2)[1]);
} }


@@ -34,75 +34,75 @@ run(readPartials, readView, readTemplate, render, toStdout);
* function the returned values of all the previously invoked functions. * function the returned values of all the previously invoked functions.
* Each function is expected to exit the process if an error occurs. * Each function is expected to exit the process if an error occurs.
*/ */
function run(/*args*/) {
function run (/*args*/) {
var values = []; var values = [];
var fns = Array.prototype.slice.call(arguments); var fns = Array.prototype.slice.call(arguments);


function invokeNextFn(val) {
function invokeNextFn (val) {
values.unshift(val); values.unshift(val);
if (fns.length == 0) return;
if (fns.length === 0) return;
invoke(fns.shift()); invoke(fns.shift());
} }


function invoke(fn) {
function invoke (fn) {
fn.apply(null, [invokeNextFn].concat(values)); fn.apply(null, [invokeNextFn].concat(values));
} }


invoke(fns.shift()); invoke(fns.shift());
} }


function readView(cb) {
function readView (cb) {
var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg); var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg);


streamToStr(view, function(str) {
streamToStr(view, function onDone (str) {
cb(parseView(str)); cb(parseView(str));
}); });
} }


function parseView(str) {
function parseView (str) {
try { try {
return JSON.parse(str); return JSON.parse(str);
} catch (ex) { } catch (ex) {
console.error( console.error(
'Shooot, could not parse view as JSON.\n'+
'Tips: functions are not valid JSON and keys / values must be surround with double quotes.\n\n'+
'Shooot, could not parse view as JSON.\n' +
'Tips: functions are not valid JSON and keys / values must be surround with double quotes.\n\n' +
ex.stack); ex.stack);


process.exit(1); process.exit(1);
} }
} }


function readPartials(cb) {
if(!partialsPaths.length) return cb();
function readPartials (cb) {
if (!partialsPaths.length) return cb();
var partialPath = partialsPaths.pop(); var partialPath = partialsPaths.pop();
var partial = fs.createReadStream(partialPath); var partial = fs.createReadStream(partialPath);
streamToStr(partial, function(str) {
streamToStr(partial, function onDone (str) {
partials[getPartialName(partialPath)] = str; partials[getPartialName(partialPath)] = str;
readPartials(cb); readPartials(cb);
}); });
} }


function readTemplate(cb) {
function readTemplate (cb) {
var template = fs.createReadStream(templateArg); var template = fs.createReadStream(templateArg);
streamToStr(template, cb); streamToStr(template, cb);
} }


function render(cb, templateStr, jsonView) {
function render (cb, templateStr, jsonView) {
cb(Mustache.render(templateStr, jsonView, partials)); cb(Mustache.render(templateStr, jsonView, partials));
} }


function toStdout(cb, str) {
function toStdout (cb, str) {
cb(process.stdout.write(str)); cb(process.stdout.write(str));
} }


function streamToStr(stream, cb) {
function streamToStr (stream, cb) {
var data = ''; var data = '';


stream.on('data', function(chunk) {
stream.on('data', function onData (chunk) {
data += chunk; data += chunk;
}).once('end', function() {
}).once('end', function onEnd () {
cb(data.toString()); cb(data.toString());
}).on('error', function(err) {
}).on('error', function onError (err) {
if (wasNotFound(err)) { if (wasNotFound(err)) {
console.error('Could not find file:', err.path); console.error('Could not find file:', err.path);
} else { } else {
@@ -112,20 +112,20 @@ function streamToStr(stream, cb) {
}); });
} }


function isStdin(viewArg) {
return viewArg === '-';
function isStdin (view) {
return view === '-';
} }


function wasNotFound(err) {
function wasNotFound (err) {
return err.code && err.code === 'ENOENT'; return err.code && err.code === 'ENOENT';
} }


function hasVersionArg() {
return ['--version', '-v'].some(function(opt) {
function hasVersionArg () {
return ['--version', '-v'].some(function matchInArgs (opt) {
return process.argv.indexOf(opt) > -1; return process.argv.indexOf(opt) > -1;
}); });
} }


function getPartialName(filename) {
return path.basename(filename, '.mustache')
function getPartialName (filename) {
return path.basename(filename, '.mustache');
} }

+ 1
- 1
package.json Dosyayı Görüntüle

@@ -32,7 +32,7 @@
"npm": ">=1.4.0" "npm": ">=1.4.0"
}, },
"scripts": { "scripts": {
"pretest": "eslint mustache.js",
"pretest": "eslint mustache.js bin/mustache",
"test": "mocha --reporter spec test/*-test.js", "test": "mocha --reporter spec test/*-test.js",
"test-render": "mocha --reporter spec test/render-test", "test-render": "mocha --reporter spec test/render-test",
"pre-test-browser": "node test/create-browser-suite.js", "pre-test-browser": "node test/create-browser-suite.js",


Yükleniyor…
İptal
Kaydet