Parcourir la source

Linting CLI tool.

Also minor .eslintrc change to be able to use `process.exit()`,
instead of having to `throw new Error()` to exit the process.
tags/v2.2.0
Phillip Johnsen il y a 10 ans
Parent
révision
cbcd788b0c
3 fichiers modifiés avec 29 ajouts et 28 suppressions
  1. +1
    -0
      .eslintrc
  2. +27
    -27
      bin/mustache
  3. +1
    -1
      package.json

+ 1
- 0
.eslintrc Voir le fichier

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

+ 27
- 27
bin/mustache Voir le fichier

@@ -10,7 +10,7 @@ var partials = {};
var partialsPaths = [];
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]);
}

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

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

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

invoke(fns.shift());
}

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

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

function parseView(str) {
function parseView (str) {
try {
return JSON.parse(str);
} catch (ex) {
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);

process.exit(1);
}
}

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

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

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

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

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

stream.on('data', function(chunk) {
stream.on('data', function onData (chunk) {
data += chunk;
}).once('end', function() {
}).once('end', function onEnd () {
cb(data.toString());
}).on('error', function(err) {
}).on('error', function onError (err) {
if (wasNotFound(err)) {
console.error('Could not find file:', err.path);
} 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';
}

function hasVersionArg() {
return ['--version', '-v'].some(function(opt) {
function hasVersionArg () {
return ['--version', '-v'].some(function matchInArgs (opt) {
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 Voir le fichier

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


Chargement…
Annuler
Enregistrer