I've been working on a shell script to automatically set up the directory system for a basic web document, as an attempt to keep my projects a bit more consistently organized.
#!/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
The shell script works when I run it natively (ex: ./webapp.sh myapp lodash
). It will create the directories and, if prompted, initialize a git repo and install dependencies, open VSCode and launch live-server. That's great and all.
But then I had a thought, if I refined this, maybe others would want to use it and it might be useful as an NPM package (granted, it's nowhere near ready for an NPM publication). Instead of rewriting the whole thing in Node, I decided to just use shelljs
:
const shelljs = require('shelljs');
const args = process.argv.slice(2).reduce((accum, current)=> {
return accum + current + ' ';
}, ' ').trim();
shelljs.exec(`./webapp.sh ${args}`);
Using Javascript it would be called node ./webapp.js myapp lodash express
(as an example.) In this case, shelljs should execute ./webapp.sh myapp lodash express
(where myapp is the name of the app and any following arguments are libraries to be installed). But what happens is after setting up the initial directories and echoing Generating myap ...
it just hangs. It doesn't run the read -p 'Do you want a git repo'
line.
Is this a problem with my version of bash (3.2) not matching that of Shelljs maybe? I mean, it works fine if I just run it as a shell script, but if I try to run it through shelljs, it hangs. Is there a solution to this? Why would it hang on that read -p . . .
read -p . . .
line only if called through shelljs?
The repo is here: https://github.com/jckuhl/Web-App-Generator
As per the ShellJS FAQ :
Running interactive programs with
exec()
We don't currently support running commands in
exec
which require interactive input. The correct workaround is to use the nativechild_process
module:child_process.execFileSync(commandName, [arg1, arg2, ...], {stdio: 'inherit'});
Using
npm init
as an example:child_process.execFileSync('npm', ['init'], {stdio: 'inherit'});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.