Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/youngjuning/child_process
Nodejs child_process 模块使用案例
https://github.com/youngjuning/child_process
Last synced: about 2 months ago
JSON representation
Nodejs child_process 模块使用案例
- Host: GitHub
- URL: https://github.com/youngjuning/child_process
- Owner: youngjuning
- Created: 2021-09-09T07:31:43.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-12T06:24:01.000Z (over 3 years ago)
- Last Synced: 2024-04-16T05:16:04.087Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
child_process
Nodejs 子进程使用案例
## child_process
### 执行 `yarn install`
```js
const { spawnSync } = require('child_process');spawnSync("yarn install",{
shell: true,
stdio: 'inherit'
});
```![image](https://user-images.githubusercontent.com/13204332/132640641-5cf686eb-1f62-48d5-99ef-93e76324f101.png)
### 获取 `git config` 信息
#### spawnSync
- [How to read child_process.spawnSync stdout with stdio option 'inherit'](https://stackoverflow.com/questions/35689080/how-to-read-child-process-spawnsync-stdout-with-stdio-option-inherit)
```js
const { spawnSync } = require('child_process');
const gitConfig = spawnSync("git config --get user.name", {
stdio: 'pipe',
encoding: 'utf-8',
shell: true,
})
console.log(gitConfig.stdout.trim());
```#### execSync
- [vue-cli git-user.js](https://unpkg.com/browse/[email protected]/lib/git-user.js)
```js
const { execSync } = require('child_process');
console.log(gitConfig.stdout.trim());const name = execSync('git config --get user.name')
console.log(name.toString().trim());
```## execa
### 执行 `yarn install`
> 一个[小问题](https://github.com/sindresorhus/execa/issues/473)坑我很久。
```js
const execa = require("execa");execa.commandSync('yarn install',{
stdout: "inherit",
stderr: "inherit",
shell: true,
});
```![image](https://user-images.githubusercontent.com/13204332/132640641-5cf686eb-1f62-48d5-99ef-93e76324f101.png)
### 获取 `git config` 信息
```js
const execa = require("execa");const {stdout: username} = execa.commandSync('git config --get user.name');
console.log(username);
```## exec-sh
### 执行 `yarn install`
```js
const execSh = require('exec-sh').promise;(async () => {
await execSh('yarn install',{
shell: true
})
})()
```![image](https://user-images.githubusercontent.com/13204332/132640641-5cf686eb-1f62-48d5-99ef-93e76324f101.png)
## shelljs
### 执行 `yarn install`
> 这是唯一一个不能满足需求的库,但是这个库的核心功能其实是封装了很多 shell 方法,但是也有点脱裤子放屁,我直接用 execa 也可以直接执行任何命令呀。
```js
const shell = require("shelljs");
shell.exec("yarn install");
```![image](https://user-images.githubusercontent.com/13204332/132643279-58606264-f7f3-4fe1-a8b7-ca6b620fe1b6.png)