Home | History | Annotate | Download | only in ui
      1 // Copyright (C) 2018 The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 /**
     16  * Configuration file for lite-server. Contains configuration for auto rerunning
     17  * ninja on file change.
     18  */
     19 'use strict';
     20 
     21 const { spawn } = require('child_process');
     22 const path = require('path');
     23 
     24 // Print without added new line.
     25 const print = data => process.stdout.write(data);
     26 const printErr = data => process.stderr.write(data);
     27 
     28 const ninjaOutDir = process.env.OUT_DIR;
     29 const uiOutDir = path.join(ninjaOutDir, 'ui');
     30 const perfettoRoot = process.env.ROOT_DIR;
     31 const ninjaPath = path.join(perfettoRoot, 'tools', 'ninja');
     32 let ninjaRunning = false;
     33 
     34 function rebasePath(relative_path) {
     35   return path.join(perfettoRoot, relative_path);
     36 }
     37 
     38 module.exports = function(bs) {
     39   return {
     40     files: [
     41       {
     42         match: [
     43           "ui/**",
     44           "src/trace_processor/**",
     45           "protos/**",
     46         ].map(rebasePath),
     47         fn: function(event, file) {
     48           console.log(`Change detected on ${file}`);
     49           if (ninjaRunning) {
     50             console.log("Already have a ninja build running. Doing nothing.");
     51             return;
     52           }
     53 
     54           ninjaRunning = true;
     55 
     56           console.log(`Executing: ninja -C ${ninjaOutDir} ui`);
     57           const ninja = spawn(ninjaPath, ['-C', ninjaOutDir, 'ui']);
     58           ninja.stdout.on('data', data => print(data.toString()));
     59           ninja.stderr.on('data', data => printErr(data.toString()));
     60 
     61           // We can be smarter and load just the file we need. Need to
     62           // resolve to the dist/location of the file in that case.
     63           // For now, we're reloading the whole page.
     64           ninja.on('exit', () => {
     65             ninjaRunning = false;
     66             bs.reload();
     67           });
     68         },
     69         options: {
     70           ignored: [
     71             "ui/dist/",
     72             "ui/.git/",
     73             "ui/node_modules/",
     74           ].map(rebasePath),
     75           ignoreInitial: true,
     76         }
     77       }
     78     ],
     79     server: {
     80       baseDir: uiOutDir,
     81     },
     82   };
     83 };
     84