How to automate the build from the following confi

2019-05-10 15:27发布

问题:

Backdrop

I have a loopback and Angular app, Loopback gives use the server models and api's and using its sdk we are able to get client services.

Now i am planning to automate the following build process using gulp. If any changes in the model is made then the sdk command is run and also the server is restarted/ and secondly when any changes to the angular files the sdk files are run and files are fetched from angular dist folder and server is restarted and best possible we can use live reload of browser.

Here is what i have tried and this never seems to work have been working on this for days.

Update

I was able to automate most of the stuff the one place where it fails is

gulp.task('browser-sync', function() {
  browserSync.init(null, {
    proxy: 'http://localhost:3000/home',
    browser: 'google chrome',
    port: 7000,
  });
  gulp.watch(['client/src/app/*.ts'], browserSync.reload);
  let watcher = gulp.watch(['./common/models/**.js', './server/**.js', 'gulpfile.js'], ['sdk', 'server']);
  watcher.on('change', function(event) {
    console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); // this watcher
  });
});

gulp.task('sdk', function() {
  spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'});
});

This watcher restarts the server and runs the sdk but it is failing in the sdk

The stack trace please help

via remoting. The Angular code for this scope won't be generated.
[19:29:37] Starting 'sdk'...
[19:29:37] Finished 'sdk' after 11 ms
[19:29:37] Starting 'server'...
[19:29:37] Finished 'server' after 17 ms
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: spawn ./node_modules/.bin/lb-sdk ENOENT
    at exports._errnoException (util.js:1050:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:367:16)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Update

I have multiple gulp task and one such is ng build -w which happens in a new directory for the same i change the process.chdir to change path and i also keep tab of this sdk so do i need to check the path again her . How can i check or give absolute paths in my spawn . if this is one of the probable causes of failure

回答1:

Taking into account your update

What might be happening is that once you change your directory using process.chdir for a sepearate task and also you have kept watch on all the tasks . The path is set to the previous path and the gulp task is not able to find the sdk i:e spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'}); in that respective path .

To fix this you can add the following check in the sdk task

gulp.task('sdk', function() {
  if (process.cwd() != __dirname) { // this checks for the current path 
    process.chdir(<change path>); // if it dosent match your base path change it here
  }
  spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'});
});


回答2:

  1. Are you sure you've run npm install --save-dev @mean-expert/loopback-sdk-builder?
  2. Are you sure that gulpfile.js is in the same dir as package.json?
  3. Are you certain ./node_modules/.bin/lb-sdk exists?
  4. Have you tried reinstalling everything?

The answer to the error is simply that your spawn function can't find ./node_modules/.bin/lb-sdk. This is either because the file doesn't exist, or because the it can't be found relatively to your gulpfile.js

The ENOENT error in the console means "error: no entity". It comes from UNIX, rather than Node itself, and basically just translates to "file not found", but applies to a variety of generic things, not just files/directories.

Check that the file ./node_modules/.bin/lb-sdk definitely exists. Then check that your gulpfile is in the root relative to that directory.