Joel Einbinder | 08b53b8 | 2018-05-15 15:29:11 +0000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This file generates jsconfig.json to improve VSCode autocomplete in the DevTools codebase. |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | const utils = require('./utils'); |
| 9 | |
| 10 | const FRONTEND_PATH = path.resolve(__dirname, '..', 'front_end'); |
| 11 | |
| 12 | const modulePaths = []; |
| 13 | for (let dir of fs.readdirSync(FRONTEND_PATH)) { |
| 14 | if (!utils.isDir(path.resolve(FRONTEND_PATH, dir))) |
| 15 | continue; |
| 16 | const modulePath = path.resolve(dir, 'module.json'); |
| 17 | if (utils.isFile(path.resolve(FRONTEND_PATH, dir, 'module.json'))) |
| 18 | modulePaths.push(dir); |
| 19 | } |
| 20 | const modules = new Map(); |
| 21 | for (const modulePath of modulePaths) { |
| 22 | const moduleObject = JSON.parse(fs.readFileSync(path.resolve(FRONTEND_PATH, modulePath, 'module.json'))); |
| 23 | modules.set(modulePath, moduleObject); |
| 24 | } |
| 25 | |
| 26 | for (const [name, moduleJSON] of modules) { |
| 27 | const jsconfig = { |
| 28 | compilerOptions: { |
| 29 | target: 'esnext', |
| 30 | lib: ['esnext', 'dom'] |
| 31 | }, |
| 32 | include: [ |
| 33 | '**/*', |
| 34 | '../Runtime.js', |
| 35 | '../externs.js' |
| 36 | ], |
| 37 | exclude: (moduleJSON.skip_compilation || []) |
| 38 | }; |
| 39 | for (const dependency of dependencyChain(name)) { |
| 40 | jsconfig.include.push('../' + dependency + '/**/*'); |
| 41 | for (const file of modules.get(dependency).skip_compilation || []) |
| 42 | jsconfig.exclude.push(path.posix.join('..',dependency, file)); |
| 43 | } |
| 44 | fs.writeFileSync(path.resolve(FRONTEND_PATH, name, 'jsconfig.json'), JSON.stringify(jsconfig, undefined, 2)); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param {string} moduleName |
| 49 | * @return {!Set<string>} |
| 50 | */ |
| 51 | function dependencyChain(moduleName) { |
| 52 | const dependencies = new Set(); |
| 53 | for (const dependency of modules.get(moduleName).dependencies || []){ |
| 54 | dependencies.add(dependency); |
| 55 | for (const innerDependency of dependencyChain(dependency)) |
| 56 | dependencies.add(innerDependency); |
| 57 | } |
| 58 | return dependencies; |
| 59 | } |