繁体   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