2014-05-23 17 views
0

我想弄清楚,如果它可能從一個用戶從shell運行一個node.js腳本暫停腳本,輸入一些輸入如冗長級別,然後讓腳本繼續執行。基本上我想在腳本實際運行時更改變量名稱(詳細程度)。我唯一的想法是用setInterval每隔幾秒解析一個配置文件並重新載入執行參數。是否可以暫停node.js腳本,提示用戶輸入,然後重新啓動腳本?

+0

更改變量*名稱*或變量*值*? – mscdex

+0

冗長的變量值,正如我所見。 –

+0

「var limit = 100」之類的值;或'var verbosity = 1'; – chovy

回答

1

使用fs.watch()來檢測配置文件變更http://nodejs.org/api/fs.html#fs_class_fs_fswatcher

例如

// Require the file system 
fs = require("fs"); 
// Watch the sim directory 
fs.watch("text.txt", { persistent: true }, function (event, fileName) { 
    console.log("Event: " + event); 
    console.log(fileName + "\n"); 
}); 
0

您可以詢問用戶,只要你想顯示終端問題(像提示)寫你的變量名。 stdio模塊可以幫助你做很多事情。例如,當您需要用戶輸入時,您可以編寫以下內容:

var stdio = require('stdio'); 
stdio.question('What is your variable name?', function (err, varname) { 
    console.log('Your var name now is "%s"', varname); 
    // Do here whatever you want with varname 
}); 

我希望這有助於。我是stdio創建者。 :-)

NPM

相關問題