Node - 외부 명령어를 실행 해주는 모듈 [shelljs]
shelljs
외부 명령어를 쉽게 실행하기 위해 ShellJS 패키지 이다.
npm install shelljs --save
npm : https://www.npmjs.com/package/shelljs
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env node const shell = require('shelljs') shell.cd('~') if(shell.exec('ls -la').code !== 0) { shell.echo('Error: command failed') shell.exit(1) } | cs |
[설명]
shell.exec('')를 이용하여 외부(ex] window cmd)명령어를 실행 해준다.
shell.exit()는 스크립트를 종료시키는 메써드이다.
인수는 종료 코드(Exit Status)를 의미하는데 0을 전달하면 정상 종료를 의미하며
그 외의 숫자(1~255)는 오류로 인한 종료를 의미한다.
[응용 방법]
1 2 3 4 5 6 7 | if (shell.which('npm')) { versionRequired.push({ name: 'npm', currentVer: exec('npm --version'), versionRequired: package_config.engines.npm }) } | cs |
[ Shebang ]이란?
#!/usr/bin/env node
|
출처 : https://jsonobject.tistory.com/293
shelljs
外部コマンドを簡単に実行するためにShellJSパッケージである。
npm install shelljs --save
npm : https://www.npmjs.com/package/shelljs
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env node const shell = require('shelljs') shell.cd('~') if(shell.exec('ls -la').code !== 0) { shell.echo('Error: command failed') shell.exit(1) } | cs |
[説明]
shell.exec( '')を利用して、外部(ex] window cmd)コマンドを実行してくれる。
shell.exit()は、スクリプトを終了させるいるメソッドである。
引数は終了コード(Exit Status)を意味する0を渡すと、正常終了を意味し
その他の数字(1〜255)は、エラーに起因する終了を意味する。
[アプリケーションの方法]
[アプリケーションの方法]
1 2 3 4 5 6 7 | if (shell.which('npm')) { versionRequired.push({ name: 'npm', currentVer: exec('npm --version'), versionRequired: package_config.engines.npm }) } | cs |
【 Shebang ]とは?
#!/usr/bin/env node
|
出典: https://jsonobject.tistory.com/293
shelljs
It is a ShellJS package to easily execute external commands.
npm install shelljs --save
npm : https://www.npmjs.com/package/shelljs
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env node const shell = require('shelljs') shell.cd('~') if(shell.exec('ls -la').code !== 0) { shell.echo('Error: command failed') shell.exit(1) } | cs |
[Explanation]
Use shell.exec('') to execute external (ex) window cmd) commands.
shell.exit() is a method that terminates the script.
The argument means exit status, and passing 0 means normal exit.
Any other number (1~255) means termination due to error.
[Application method]
1 2 3 4 5 6 7 | if (shell.which('npm')) { versionRequired.push({ name: 'npm', currentVer: exec('npm --version'), versionRequired: package_config.engines.npm }) } | cs |
What is [ Shebang ]?
#!/usr/bin/env node
|
Source: https://jsonobject.tistory.com/293