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