A general-purpose module for preprocessing using Docker.
npm install docker-preprocessorA general-purpose module for preprocessing using [Docker][docker].
[docker]: https://www.docker.com/
``bash`
npm install --save-dev docker-preprocessor
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
`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
`
* options [
* dockerOptions [
* image [][mdn docs type string] Image and tag string to'ubuntu'
create a Docker container with. Defaults to (which defaultslatest
to the tag).
* command [][mdn docs type function] A function that takes apath
string and returns an [exec array][docker cmd]. The pathdockerPreprocessorRunner
string is provided by . Defaults to() => ['bash']
.
* streams , [][mdn docs type array] AWritable
stream or an array of Writable streams to pipe to from adockerode
Docker container. See [][dockerode equivalent example] for aprocess.stdout
more in-depth explanation. Defaults to .
* [createOptions] [
* [startOptions] [
* paths [
* main [][mdn docs type string] A path to the main content
to be retrieved.
* [emittedFiles] [][mdn docs type array] An optional array
of paths as strings, to be emitted to the final build directory.
* [sourceMap] [][mdn docs type string] Optional path tomain
the source mapping file, usually for the content.
[dockerode getting started]: https://github.com/apocas/dockerode#getting-started
[docker cmd]: https://docs.docker.com/engine/reference/builder/#cmd
[dockerode equivalent example]: https://github.com/apocas/dockerode#equivalent-of-docker-run-in-dockerode
Returns a function, dockerPreprocessorRunner.
#### dockerPreprocessorRunner(filePath)
* filePath [][mdn docs type string] Path to a source file. Thiscommand
string is given to the function property given todockerPreprocessor
in its option object.
Returns a Promise which resolves to a result object.
* result [
* container [
* error [][mdn docs type null], [null
Returns if the exit code is 0 or returns an error.
* content
* sourceMap Defaults to null.
* emittedFiles [][mdn docs type array] Default to []`.
[dockerode]: https://github.com/apocas/dockerode
[mdn docs type object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[mdn docs type function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
[mdn docs type array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[mdn docs type string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
[mdn docs type null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
[mdn docs type error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error