docker-preprocessor

0.1.0 • Public • Published

docker-preprocessor

A general-purpose module for preprocessing using Docker.

Install

npm install --save-dev docker-preprocessor

Usage

docker-preprocessor can be used for extremely complex (Emscripten, for C++ to WebAssembly compilation) to very simple tasks (checking output of a Node.js version). It is meant to be unopinionated and easy to fit into any preprocessor toolchain.

file.txt

Hello, World!

preprocessor.js

import dockerPreprocessor from 'docker-preprocessor';
 
const options = {
  image: 'node',
  createOptions: {
    Binds: ['/:/host'],
    WorkingDir: '/src',
  },
  command: path => [
    'sh',
    '-c',
    `
      node \
        -e \
        " \
          const { readFileSync, writeFileSync } = require('fs'); \
          const content = readFileSync('/host${path}', { encoding: 'utf8' }).split('').reverse().join(''); \
          writeFileSync('./result', content); \
        " \
      ;
    `,
  ],
  paths: {
    main: '/src/result',
  },
};
 
const { content } = await dockerPreprocessor(options)('file.txt');
 
console.log(bufferOfContentsReversedByDockerContainer.toString('utf8')); // !dlroW ,olleH

Documentation

dockerPreprocessor(options)

  • options <Object>

    • dockerOptions <Object> Options passed to dockerode's instantiation of a Docker object.

    • image <string> Image and tag string to create a Docker container with. Defaults to 'ubuntu' (which defaults to the latest tag).

    • command <Function> A function that takes a path string and returns an exec array. The path string is provided by dockerPreprocessorRunner. Defaults to () => ['bash'].

    • streams <Writable>, <Array> A Writable stream or an array of Writable streams to pipe to from a Docker container. See dockerode for a more in-depth explanation. Defaults to process.stdout.

    • [createOptions] <Object> Optional options used for container creation. See dockerode for a more in-depth explanation.

    • [startOptions] <Object> Optional options used for container start. See dockerode for a more in-depth explanation.

    • paths <Object> An object with paths to file locations within the Docker container to retrieve and return as Buffers.

      • main <string> A path to the main content to be retrieved.

      • [emittedFiles] <Array> An optional array of paths as strings, to be emitted to the final build directory.

      • [sourceMap] <string> Optional path to the source mapping file, usually for the main content.

Returns a function, dockerPreprocessorRunner.

dockerPreprocessorRunner(filePath)

  • filePath <string> Path to a source file. This string is given to the command function property given to dockerPreprocessor in its option object.

Returns a Promise which resolves to a result object.

  • result <Object>

    • container <Object> The container object created by dockerode.

    • error <null>, Returns null if the exit code is 0 or returns an error.

    • content <Buffer>

    • sourceMap <Buffer> Defaults to null.

    • emittedFiles <Array> Default to [].

Package Sidebar

Install

npm i docker-preprocessor

Weekly Downloads

3

Version

0.1.0

License

MIT

Last publish

Collaborators

  • dfrankland