{"id":18870354,"url":"https://github.com/streetstrider/command-promise","last_synced_at":"2025-04-14T15:21:12.629Z","repository":{"id":18189026,"uuid":"21307017","full_name":"StreetStrider/command-promise","owner":"StreetStrider","description":"Promise \u0026 stream wrapper around child_process.exec","archived":false,"fork":false,"pushed_at":"2016-11-22T19:19:34.000Z","size":70,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T13:52:47.853Z","etag":null,"topics":["child-process","node","pipes","promise","shell","streams"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/command-promise","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StreetStrider.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-28T16:50:23.000Z","updated_at":"2019-08-01T11:50:42.000Z","dependencies_parsed_at":"2022-09-01T12:01:23.692Z","dependency_job_id":null,"html_url":"https://github.com/StreetStrider/command-promise","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StreetStrider%2Fcommand-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StreetStrider%2Fcommand-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StreetStrider%2Fcommand-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StreetStrider%2Fcommand-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StreetStrider","download_url":"https://codeload.github.com/StreetStrider/command-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248904622,"owners_count":21180836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["child-process","node","pipes","promise","shell","streams"],"created_at":"2024-11-08T05:19:50.744Z","updated_at":"2025-04-14T15:21:12.604Z","avatar_url":"https://github.com/StreetStrider.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Command [![license|mit](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](README.md#license) [![npm|command-promise](http://img.shields.io/badge/npm-command--promise-CB3837.svg?style=flat-square)](https://www.npmjs.org/package/command-promise) [![npm test|with mocha](http://img.shields.io/badge/npm%20test-with%20mocha-9E785A.svg?style=flat-square)](http://mochajs.org/)\nPromise \u0026 stream wrapper around `child_process.exec`. Uses [promise](https://www.npmjs.org/package/promise) for result, but if there is a [Q](https://www.npmjs.org/package/q) or [Bluebird](https://www.npmjs.org/package/bluebird) will switch to it. This lib also handles arrays smartly, so not need in manual constructing any `apply`-ing them.\n\nModule can be bundled: all deps melted into, without `node_modules/` **size** is reduced to ~80kB.\n\n## usage\n```javascript\n// Command return promise\nCommand('ls -1').then(console.log, console.error)\n```\n\n```javascript\n// Process return duplex stream with writable as stdin,\n// and readable as stdout for spawned subprocess\nProcess('ls -1').pipe(process.stdout)\nProcess('ls -1').pipe(Process('xargs file -b')).pipe(process.stdout)\n```\n\n## signature\n```javascript\n// Command(…) → Promise\n// promise's value is `[ String(stdout), String(stderr) ]`\nCommand(chunk)\nCommand(chunk, options)\nCommand(chunk, chunk, ...)\nCommand(chunk, chunk, ..., options)\nCommand(chunk, chunk, ..., options, options)\nCommand(chunk, chunk, ..., options, chunk, options)\nCommand(\u003cany sequence of chunks and options\u003e)\n\n// Process(…) → Stream (is duplex)\n// duplex input  is subprocess stdin\n// duplex output is subprocess stdout\nProcess(chunk)\nProcess(chunk, options)\nProcess(chunk, chunk, ...)\nProcess(chunk, chunk, ..., options)\nProcess(chunk, chunk, ..., options, options)\nProcess(chunk, chunk, ..., options, chunk, options)\nProcess(\u003cany sequence of chunks and options\u003e)\n```\n**options** is a object of options for `child_process.exec`.\n\n**chunk** is a *string* or *array of strings* or *arguments* or *array of strings and options*.\n\nAll chunks are concatenated in one flat array, options objects are merged in one as well.\n\nIf you have hardcoded data just pass strings. If you have variative data then pass arrays, no need in joining elements or manipulating with `.apply`. If all of your data is hardcoded, look at [Command.Simple](#simple-command).\n\nThe executed command can be complex, contain pipes and shell stream redirections.\n\n## Command utils\nThere're some utils to transform Command's result:\n\n**only stdout**: If you want command to return only stdout, use `util.stdout`:\n```javascript\nCommand('ls -l').then(Command.util.stdout)\n```\nThis will return not pair, but `stdout` string only.\n\n**stderr as error**: By default, result promise will be rejected only if `child_process.exec` returns error. It happens when return code is non-zero. If you want to reject also if there is something in `stderr`, use `util.stderr`. If no error, this will return `stdout` string only.\n\n**trim content**: The majority of shell commands return streams with newline at the end. You can use `util.trim` to trim both `stdout` and `stderr`. It also works with string only, if promise was converted by `util.stdout` earlier.\n\n## examples\n```javascript\nCommand('ls', '-lA', { cwd: '/tmp' }).then(...)\nCommand('ls', [ '-l', '-A' ], { cwd: '/tmp' }).then(...)\nCommand('ls', [ '-l', '-A', { cwd: '/tmp' } ]).then(...)\nCommand([ 'ls', '-1' ], { cwd: '/tmp' }).then(...)\n\nfunction Echo () { return Command('echo', '-', arguments); }\nEcho('-n', '-a', '-b', { encoding: 'ascii' }).then(...)\n\nProcess('find src -name \"*.js\"').pipe(Process('xargs cat')).pipe(process.stdout)\n// or just\nProcess('find src -name \"*.js\"', '|', 'xargs cat').pipe(process.stdout)\n```\n\n## simple command\nIf you don't need any advanced arguments features, you can use `Command.Simple` \u0026 `Process.Simple`.\n```javascript\nCommand.Simple(str)\nCommand.Simple(str, options)\n\nProcess.Simple(str)\nProcess.Simple(str, options)\n```\n\n## using in the mid of the promise flow\nUse `Command.so` it creates function which will self invoke command.\nIt does not append any arguments to command, so it can be used to order operations.\n```javascript\nCommand('mkdir -p build')\n.then(Command.so('cp package.json build/'))\n.then(Command.so('cp -r src/'))\n.then(...);\n```\n\n## using in partials\n```javascript\nvar gitLog = partial(Command, 'git', 'log', { cwd: '/opt/repo' });\nvar gitLogOneline = partial(gitLog, '--oneline');\n\ngitLogOneline().then(console.log, console.error);\ngitLogOneline('-15').then(console.log, console.error);\n```\n\n## using in pipes\nUse `Process` in pipes, as a cheap and clear analogue of Gulp/Vinyl streams.\n```javascript\nvar proc = require('command-promise/Process')\nvar write = require('fs').createWriteStream\n\ngulp.task('less', function ()\n{\n\treturn proc('lessc', './web/css/index.less')\n\t.pipe (proc('postcss --use autoprefixer'))\n\t.pipe(write('index.css'))\n})\n```\n`Process` produces regular (not `vinyl-fs`) duplex streams. So you can use any commands in your pipes (even if they have no adapters for Gulp). Since Gulp can work with regular streams, you can use `Process` inside Gulp tasks.\n\n## license\nMIT. © StreetStrider, 2014 — 2015.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetstrider%2Fcommand-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstreetstrider%2Fcommand-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetstrider%2Fcommand-promise/lists"}