Поиск по этому блогу

понедельник, 17 октября 2016 г.

Знакомимся с gulp-shell и с lodash

Можно ли вызывать с Gulp системные (консольные) команды? Здесь фрагменты кода из документации.

monsterlessons.com В этом уроке мы разберем как использовать методы some, groupBy и sortBy в lodash.
lodash A modern JavaScript utility library delivering modularity, performance & extras.
FP-Guide The lodash/fp module promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods
lodash REPL Sometimes we use the Node.js REPL interface to experiment with code. Wouldn’t it be great to have that interface with lodash required by default?
vinyl
gulp-shell

Начнем с gulp-shell

Что это за подозрительный код в примере из документации gulp-shell

In [ ]:
var gulp  = require('gulp')
var shell = require('gulp-shell')
 
gulp.task('example', function () {
  return gulp.src('*.js', {read: false})
    .pipe(shell([
      'echo <%= f(file.path) %>',
      'ls -l <%= file.path %>'
    ], {
      templateData: {
        f: function (s) {
          return s.replace(/$/, '.bak')
        }
      }
    }))
})

A command can be a template) which can be interpolated by some file info

А lodash включает три вида "знакомых" разделителей

In [ ]:
<%=      -"interpolate"
<%-      -"escape" 
<%       -"evaluate"
In [ ]:
 _.template([string=''], [options={}])
In [ ]:
// Use the "interpolate" delimiter to create a compiled template.
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'
 
// Use the HTML "escape" delimiter to escape data property values.
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b>&lt;script&gt;</b>'
 
// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
 
// Use the internal `print` function in "evaluate" delimiters.
var compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'
 
// Use the ES template literal delimiter as an "interpolate" delimiter.
// Disable support by replacing the "interpolate" delimiter.
var compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'
 
// Use backslashes to treat delimiters as plain text.
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// => '<%- value %>'
 
// Use the `imports` option to import `jQuery` as `jq`.
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
 
// Use the `sourceURL` option to specify a custom sourceURL for the template.
var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);
// => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
 
// Use the `variable` option to ensure a with-statement isn't used in the compiled template.
var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;
// => function(data) {
//   var __t, __p = '';
//   __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
//   return __p;
// }
 
// Use custom template delimiters.
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'
 
// Use the `source` property to inline compiled templates for meaningful
// line numbers in error messages and stack traces.
fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
  var JST = {\
    "main": ' + _.template(mainText).source + '\
  };\
');

Приложение. Вот зависимости из package.json gulp-shell

In [ ]:
"dependencies": {
    "async": "^1.5.0",
    "gulp-util": "^3.0.7",
    "lodash": "^4.0.0",
    "through2": "^2.0.0"
  },
  "description": "A handy command line interface for gulp",
  "devDependencies": {
    "chai": "~3.4.0",
    "coveralls": "~2.11.4",
    "eslint": "~1.10.3",
    "gulp": "~3.9.0",
    "istanbul": "~0.4.0",
    "mocha": "~2.3.3",
    "mocha-lcov-reporter": "~1.0.0"
  },


Посты чуть ниже также могут вас заинтересовать

Комментариев нет: