NodeJS子进程中的电子快速入门

时间:2018-03-24 10:58:59

标签: javascript node.js electron child-process

我正在尝试将Gulp设置与调试电子快速启动相结合,并且当我使用child_process.spawn实现的源文件发生更改并启动时,我正尝试关闭并重新打开Electron它工作正常,关闭似乎很好,但电子应用程序保持活动状态并开启一个新的 - 即使它在自己的主进程中运行,使用SIGINT发送cmd+C将关闭应用程序贝壳也是。我已将我的代码简化为以下代码段,并添加了倒计时,以确保它不是时间问题。此外,electron-quick-start的整个存储库实际上位于./app/文件夹中,因为我的Gulp将位于其外部,它只是输出到应用程序资源文件夹内。

function destroyElectron(){
	
  return new Promise(function( resolve, reject ){
	
    if( electron ){
		
      console.log( 'Closing Electron...' );
	
      electron.on( 'close', () => {
				
        console.log( 'Electron Event: Close' );
				
        resolve();
				
      });
      electron.on( 'exit', () => {
				
        console.log( 'Electron Event: Exit' );
				
        resolve();
				
      });
			
      electron.kill( 'SIGINT' );
      electron = null;
			
    } else {
		
      resolve();
		
    }
	
  });
	
}
function startElectron(){
	
  destroyElectron().then(() => {
	
    console.log( 'Starting Electron...' );
	
    electron = spawn( 'npm', [ 'start' ], { stdio: 'inherit', cwd: './app' } );
    electron.on( 'close', destroyElectron );
	
  });
	
}

var spawn = require( 'child_process' ).spawn;
var electron;
var count = 0;

startElectron();

var interval = setInterval(function(){

  if( ++count == 10 ){
		
    clearInterval( interval );
    destroyElectron().then(() => {
			
      console.log( 'Electron is destroyed' );
			
    });
	
  } else {
		
    console.log( 10 - count );
		
  }
	
}, 1000 );

有理由在命令行中手动结束它与手动发送信号有不同的效果吗?我可以关闭子进程中运行的完整Electron Quick Start应用程序吗?

1 个答案:

答案 0 :(得分:0)

我对你的问题感到困惑,但我想与你分享我用来观看我的项目文件并重新加载app的gulp文件:)

class TrainerCreateView(BrandOwnersPermissionMixin, CreateView):
    model = User
    form_class = UserCreationForm
    second_form_class = TrainerForm
    template_name = 'panel/accounts/trainer_form.html'

    def get_context_data(self, **kwargs):
        context = super(TrainerCreateView, self).get_context_data(**kwargs)
        return context

    def form_valid(self, form):
        try:
            trainer_form = TrainerForm(self.request.POST, self.request.FILES)
            with transaction.atomic():
                if form.is_valid() and trainer_form.is_valid():
                    user = form.save()
                    trainer = trainer_form.save(commit=False)
                    trainer.user_id = user.id
                    trainer_group = Group.objects.get(name='trainers')
                    trainer_group.user_set.add(user)
                    trainer.save()
        except Exception as e:
            print(e)
        return HttpResponseRedirect(self.get_success_url(self))

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))