makhulu
Simple and parallel Node.js task runner
Last updated 3 months ago by mgenware .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install makhulu 
SYNC missed versions from official npm registry.

makhulu

Build Status MEAN Module npm version Node.js Version

ky

???? Simple and parallel Node.js task runner

  • Parallel, all functions are written in async
  • Simple, no need to write plugins/wrappers, do everything in plain TypeScript
  • Strongly typed, supports TypeScript out of the box

Installation

yarn add makhulu

Getting started

Examples

More examples at https://github.com/mgenware/makhulu-examples

Use the latest uglifyjs to uglify all JS files in ./test_files/, then merge the results into one single file merge.js and save it to ./dist_files:

/**
 * Assuming you have installed the following packages:
 * makhulu, uglify-js, @types/uglify-js
 */
import * as mk from 'makhulu';
import { minify } from 'uglify-js';
import * as nodepath from 'path';

(async () => {
  const srcDir = './test_files/';
  // Select all js files as the initial data list
  const files = await mk.fs.src(srcDir, '**/*.js');
  /**
   * Now the data list is like:
   * [
   *   {
   *      SrcDir: './test_files/',
   *      FilePath: 'a.js',
   *    },
   *    {
   *      SrcDir: './test_files/',
   *      FilePath: 'sub/b/js',
   *    },
   *    ...
   * ]
   */

  // Prints src file paths using printsRelativeFile
  await files.forEach('Source files', mk.fs.printsRelativeFile);

  // Read file paths to string contents, now data list contains file content data
  await files.map('Read files', mk.fs.readToString);
  /**
   * Now the data list is like (note that this only adds attributes to the target data map, all previous attributes are preserved):
   * [
   *   {
   *      SrcDir: './test_files/',
   *      FilePath: 'a.js',
   *      Content: 'blabla',
   *    },
   *    {
   *      SrcDir: './test_files/',
   *      FilePath: 'sub/b/js',
   *      Content: 'blabla',
   *    },
   *    ...
   * ]
   */

  // You can modify the content to whatever you want, e.g. uglify the content
  await files.map('Uglify', async data => {
    const content = data[mk.fs.FileContent] as string;
    const uglifyRes = minify(content);
    if (uglifyRes.error) {
      throw uglifyRes.error;
    }
    data[mk.fs.FileContent] = uglifyRes.code;
    return data;
  });
  /**
   * Now the data list is like:
   * [
   *   {
   *      SrcDir: './test_files/',
   *      FilePath: 'a.js',
   *      Content: 'Uglified content ...',
   *    },
   *    {
   *      SrcDir: './test_files/',
   *      FilePath: 'sub/b/js',
   *      Content: 'Uglified content ...',
   *    },
   *    ...
   * ]
   */

  // Another example of modify the content, we merge all the content of previous files into one, and manually creates the DataObject
  await files.reset('Merge into one file', async dataList => {
    // set merged file as "bundle.js"
    const destPath = 'bundle.js';
    // merge contents of all files into a single string
    let contents = '';
    dataList.forEach(d => {
      contents += d[mk.fs.FileContent] as string;
    });
    // create a new DataObject
    const bundleFileObject = {
      [mk.fs.SrcDir]: srcDir,
      [mk.fs.RelativeFile]: destPath,
      [mk.fs.FileContent]: contents,
    };
    return [bundleFileObject];
  });
  /**
   * Now the data list is like:
   * [
   *   {
   *      SrcDir: './test_files/',
   *      FilePath: 'merged.js',
   *      Content: 'Merged content',
   *    },
   * ]
   */

  // Call writeToDirectory to save all files to a directory, in this case, only one file called `merged.js` which we created
  await files.map(
    'Write files',
    mk.fs.writeToDirectory(`./dist_files/${nodepath.basename(__dirname)}`),
  );
  await files.forEach('Dest files', mk.fs.printsDestFile);
  /**
   * Now the data list is like:
   * [
   *   {
   *      SrcDir: './test_files/',
   *      FilePath: 'merged.js',
   *      Content: 'Merged content',
   *      DestFilePath: './dist_files/uglifyjs-and-merge/merged.js',
   *    },
   * ]
   */
})();

Sample output:

???? Job started
> 3
> Done in 1ms
???? Source files
a.js
b.js
subdir/c.js
> Done in 3ms
???? Read files
> Done in 51ms
???? Uglify
> Done in 19ms
???? Merge into one file
> 3 >> 1
> Done in 1ms
???? Write files
> Done in 4ms
???? Dest files
dist_files/uglifyjs-and-merge/bundle.js
> Done in 1ms

Common Errors

File content not found on data object

This happens when you call writeToDirectory and DataObject.get(FS.FileContent) returns null or undefined, possible reasons:

  • You forgot to call readToString, or called readToString without the await keyword before a call to writeToDirectory.
  • You accidentally set this value to null or undefined, if you want to write to an empty file, set it to an empty string (''), or if you want to remove this file, use DataList.filter or DataList.reset instead.

Relative path not found on data object

writeToDirectory cannot locate the source path of a file, you forgot to call fs.src before writeToDirectory?

Current Tags

  • 1.2.3                                ...           latest (3 months ago)

23 Versions

  • 1.2.3                                ...           3 months ago
  • 1.2.2                                ...           6 months ago
  • 1.2.1                                ...           9 months ago
  • 1.2.0                                ...           10 months ago
  • 1.1.3                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.4.3                                ...           2 years ago
  • 0.4.2                                ...           2 years ago
  • 0.4.1                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (7)
Dev Dependencies (12)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |