簡體   English   中英

為什么從Shell.js執行時,有效的Shell腳本停止執行?

[英]Why does a working shell script stop executing when executed from shelljs?

我一直在研究一個shell腳本,以自動為基本的Web文檔設置目錄系統,以使我的項目更加一致。

#!/bin/bash

if [ -e $1 ]; then
    echo $1 'already exists!'
    exit
else
    echo 'Generating' $1 '. . .' 
    mkdir $1
    cd $1
    mkdir 'html'
    mkdir 'scripts'
    touch 'scripts/app.js'
    mkdir 'style'
    touch 'style/style.css'
    touch 'index.html'
    echo '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

</body>
</html>' > 'index.html'
    read -p 'Do you want a git repo? [Y/N]' response
    if [ $response = 'Y' ] || [ $response = 'y' ]; then
        echo 'Generating git repo . . .'
        git init
    fi
fi

if [ $# -eq 1 ]; then
    read -p 'Do you want a package.json? [Y/N] ' response
    if [ $response = 'Y' ] || [ $response = 'y' ]; then
        npm init
    fi
else 
    npm init
    shift
    echo 'installing' $*
    npm install $*
    touch '.gitignore'
    echo '/node_modules' > '.gitignore'
fi

echo 'Your web app has been generated!'
echo 'You can find it at'
pwd

read -p 'Would you like to push your first commit now? [Y/N]'

if [ $response = 'Y' ] || [ $response = 'y' ]; then
    git add .
    git commit -m 'initial commit'
    read -p 'Please enter the url to your git (.git):' url
    git remote add origin $url
    git push -u origin master
fi

read -p 'Would you like to open VSCode? [Y/N] ' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
    code .
fi

read -p 'Would you like to start live-server? [Y/N] ' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
    live-server
fi

exit

當我在本地運行時,shell腳本有效(例如: ./webapp.sh myapp lodash )。 它將創建目錄,並在出現提示時初始化git repo並安裝依賴項,打開VSCode並啟動實時服務器。 太好了,所有。

但是后來我想到,如果我對此進行完善,也許其他人會想使用它,並且它可能作為NPM軟件包很有用(當然,它距離NPM出版物還差得很遠)。 我決定只使用shelljs ,而不是重寫Node的全部shelljs

const shelljs = require('shelljs');

const args = process.argv.slice(2).reduce((accum, current)=> {
    return accum + current + ' ';
}, ' ').trim();

shelljs.exec(`./webapp.sh ${args}`);

使用Javascript將其稱為node ./webapp.js myapp lodash express (作為示例。)在這種情況下,shelljs應該執行./webapp.sh myapp lodash express (其中myapp是應用程序的名稱,以下任何參數是庫)。 但是,在設置初始目錄並回顯Generating myap ...之后,會發生什么Generating myap ...它只是掛起。 它不會運行read -p 'Do you want a git repo'行。

這是我的bash(3.2)版本與Shelljs版本不匹配的問題嗎? 我的意思是,如果我僅將其作為shell腳本運行,則效果很好,但如果嘗試通過shelljs運行,則將掛起。 有針對這個的解決方法嗎? 為什么它將掛在該read -p . . . read -p . . . 僅通過shelljs調用才行嗎?

倉庫在這里: https : //github.com/jckuhl/Web-App-Generator

根據ShellJS常見問題解答

使用exec()運行交互式程序

我們目前不支持在exec中運行需要交互輸入的命令。 正確的解決方法是使用本機child_process模塊:

 child_process.execFileSync(commandName, [arg1, arg2, ...], {stdio: 'inherit'}); 

npm init為例:

 child_process.execFileSync('npm', ['init'], {stdio: 'inherit'}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM