NodeJS
grunt-cli
Gruntfile.js
規則grunt
模塊NodeJS
跟grunt-cli
包含進去?nodejs
,省去NodeJS安裝webkit
內核,方便編寫界面//切換運行環境到項目中 process.chdir("項目地址"); //執行grunt require("child_process").spawn("grunt");
這種方案最簡單,但須要依賴NodeJS
跟grunt-cli
前端
var gruntpath = "grunt.js的路徑"; var grunt = require(gruntpath); grunt.cli();
直接運行,提示找不到gruntfile.js,修改grunt文件夾中的task.js
,大概在430行左右node
var old_path = process.cwd();//獲取當前工做目錄 process.chdir('項目文件目錄');//修改到項目目錄 var gruntfile = allInit ? null : grunt.option('gruntfile') || grunt.file.findup('Gruntfile.{js,coffee}', {nocase: true});//查找gruntfile文件 process.chdir(old_path);//修改回程序目錄 此處省略n行代碼~~~ (grunt.option('npm') || []).forEach(task.loadNpmTasks);//加載npm的模塊 process.chdir('項目文件目錄');//切換到項目目錄
先切換到項目文件目錄查找gruntfile.js
文件,而後切換回程序目錄查找node模塊,運行完grunt後,程序自動退出了,囧,須要修改grunt下的exit.js
linux
process.exit(0);//這句須要註釋掉
var child = require("child_process").fork("child.js"); child.onmessage = function(data){ console.log(data); } child.on("exit",function(exitCode){ console.log(exitCode); });
--運行後提示:unzip the package xx/child.js,上網各類谷歌以後得出做者結論:web
child_process.fork is brokennpm
--囧,就是這個方法是不能用了?繼續谷歌以後得知json
child_process.fork是沒法運行js文件,它是直接運行執行命令
nw 文件夾
,因此修改一下,在child
文件夾中也放一個package.json
文件,執行後,果真成功了。 app
--而後又留下一個大問題,沒法使用fork的通訊接口,囧,最後只好用tcp
來實現進程通訊。tcp
--當我想打包成app測試的時候,發現,壓根就不會運行子進程child
grunt
由於child_process.fork是運行
nw
命令,因此,打包後是沒有nw命令 post
npm install node-webworker
以後引用worker.js
var Worker = require("worker.js"); var workker = new Worker("child.js"); worker.onmessage = function(data){ console.log(data); } worker.postmessage({msg:'hello'});
運行後,發現child.js中的任何信息都沒法傳遞迴給父進程,最後發如今worker.impl.buffer
中保存了相關信息。
var timeId = setInterval(function(){ var msg = worker.impl.buffer; if(!msg)return false; console.log(msg); worker.imple.buffer = ""; },1000);
雖然很挫,但仍是實現了功能了,打包測試,發現一切OK。發送到其它電腦測試,提示:
spawn node not found,就是須要執行的命令找不到
奇怪,我沒調用spawn
,哪來找不到?打開worker.js的源碼查看,囧,發現:
child_process.spawn("node xxx.js");
囧,原來它是執行了node
的命令來實現子進程,坑爹了。
var worker = new Worker("child.js"); worker.onmessage = function(data){}; worker.postmessage("hello");
測試後,發現成功運行,消息也正常接收,可...webworker
不支持運行nodejs
代碼,就是grunt沒法跑起來。