Hector Dearman | 3d26fdb | 2018-07-09 13:54:06 +0100 | [diff] [blame] | 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. |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 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'); |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 22 | const path = require('path'); |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 23 | |
| 24 | // Print without added new line. |
| 25 | const print = data => process.stdout.write(data); |
| 26 | const printErr = data => process.stderr.write(data); |
| 27 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 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'); |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 32 | let ninjaRunning = false; |
| 33 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 34 | function rebasePath(relative_path) { |
| 35 | return path.join(perfettoRoot, relative_path); |
| 36 | } |
| 37 | |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 38 | module.exports = function(bs) { |
| 39 | return { |
| 40 | files: [ |
| 41 | { |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 42 | match: [ |
| 43 | "ui/**", |
| 44 | "src/trace_processor/**", |
| 45 | "protos/**", |
| 46 | ].map(rebasePath), |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 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`); |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 57 | const ninja = spawn(ninjaPath, ['-C', ninjaOutDir, 'ui']); |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 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: { |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 70 | ignored: [ |
| 71 | "ui/dist/", |
| 72 | "ui/.git/", |
| 73 | "ui/node_modules/", |
| 74 | ].map(rebasePath), |
| 75 | ignoreInitial: true, |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | ], |
| 79 | server: { |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 80 | baseDir: uiOutDir, |
Deepanjan Roy | d7b03b2 | 2018-05-17 09:05:13 -0400 | [diff] [blame] | 81 | }, |
| 82 | }; |
| 83 | }; |