並行或串行運行命令
如果我樣要啓動兩個服務, 並且兩個服務並沒有相關性, 那麼這時候最好是使用並行.
一般情況會想到以下命令:
serve1 & serve2
但是這個命令在 widnows 上還是會順序執行, 必須 serve1 運行結束後纔會執行 serve2, 但是由於它是一個服務, 是不會結束的!
方案
concurrently
- 不能直接運行其他命令
- 不能支持串行
> node -v
v14.15.5
> npx concurrently "node -v"
6.2.1
> npx concurrently --raw "echo 123"
TypeError: commandInfo.command.match is not a function
tasksfile
- 需要創建配置文件
- 中文輸出會亂碼 #119
> chcp 936 && node tasksfile.js command
活動代碼頁: 936
ping baidu.com -t
���
���� Ping baidu.com [220.181.38.148] ���� 32 �ֽڵ����:
4�� 220.181.38.148 �Ļظ�: �ֽ�=32 ʱ��=48ms TTL=51
4�� 220.181.38.148 �Ļظ�: �ֽ�=32 ʱ��=47ms TTL=51
npm-run-all
- 只支持 npm 命令, 不能直接運行其他命令 #203
parallelshell
- 無法運行
> npx parallelshell "echo 1" "echo 2" "echo 3"
child_process.js:435
throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd);
結果
使用 qs 既可以運行 npm 命令, 也可以運行系統命令.
例如:
> qs -p "ping baidu.com" "ping qq.com"
-p 表示並行運行
如果需要把命令保存起來下次使用, 可以這樣:
> qs -n t1 -p "ping baidu.com" "ping qq.com"
以後就可以任何目錄使用 qs -s t1
來使用這個命令了.
參考
- npm並行&串行執行多個scripts命令
- vue項目 npm 同時執行多個命令
- 如何並行運行多個 npm 腳本?
- Consolidation of multiple similar libraries