|
| 1 | +// Copyright 2021 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 | +import path from 'path'; |
| 6 | +import fs from 'fs'; |
| 7 | +import childProcess from 'child_process'; |
| 8 | + |
| 9 | +// JSON files under .vscode/ synced by this script |
| 10 | +const VSCODE_SETTINGS_TO_MERGE = [ |
| 11 | + { settingsFile: 'settings.json' }, |
| 12 | + { settingsFile: 'tasks.json', mergeField: 'tasks', byField: 'label' }, |
| 13 | + { |
| 14 | + settingsFile: 'launch.json', |
| 15 | + mergeField: 'configurations', |
| 16 | + byField: 'name', |
| 17 | + }, |
| 18 | +]; |
| 19 | + |
| 20 | +// If the user has opted out of updates, return and do nothing. |
| 21 | +if (Boolean(process.env['SKIP_VSCODE_SETTINGS_SYNC'])) { |
| 22 | + process.exit(0); |
| 23 | +} |
| 24 | + |
| 25 | +for (const { settingsFile, mergeField, byField } of VSCODE_SETTINGS_TO_MERGE) { |
| 26 | + const vscodeSettingsLocation = path.join( |
| 27 | + process.cwd(), |
| 28 | + '.vscode', |
| 29 | + settingsFile, |
| 30 | + ); |
| 31 | + const devtoolsSettingsLocation = path.join( |
| 32 | + process.cwd(), |
| 33 | + '.vscode', |
| 34 | + 'devtools-workspace-' + settingsFile, |
| 35 | + ); |
| 36 | + |
| 37 | + // If there are no settings to copy and paste, skip. |
| 38 | + if (!fs.existsSync(devtoolsSettingsLocation)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + const devtoolsSettings = ( |
| 44 | + await import(devtoolsSettingsLocation, { |
| 45 | + with: { type: 'json' }, |
| 46 | + }) |
| 47 | + ).default; |
| 48 | + let preExistingSettings = {}; |
| 49 | + if (fs.existsSync(vscodeSettingsLocation)) { |
| 50 | + preExistingSettings = ( |
| 51 | + await import(vscodeSettingsLocation, { |
| 52 | + with: { type: 'json' }, |
| 53 | + }) |
| 54 | + ).default; |
| 55 | + } |
| 56 | + |
| 57 | + const updatedSettings = { |
| 58 | + ...devtoolsSettings, |
| 59 | + // Any setting specified by the engineer will always take precedence over the defaults |
| 60 | + ...preExistingSettings, |
| 61 | + }; |
| 62 | + |
| 63 | + // TODO: We found that this never work so patch it now |
| 64 | + // so gclient sync updates it. |
| 65 | + // Later we should remove and allow user to keep their |
| 66 | + // preference. |
| 67 | + if (updatedSettings['eslint.runtime']) { |
| 68 | + const nodePath = childProcess |
| 69 | + .execSync( |
| 70 | + path.join( |
| 71 | + import.meta.dirname, |
| 72 | + '..', |
| 73 | + '..', |
| 74 | + 'third_party', |
| 75 | + 'node', |
| 76 | + 'node_path.py', |
| 77 | + ), |
| 78 | + ) |
| 79 | + .toString('utf-8') |
| 80 | + .trim(); |
| 81 | + |
| 82 | + updatedSettings['eslint.runtime'] = nodePath; |
| 83 | + } |
| 84 | + |
| 85 | + if ( |
| 86 | + mergeField && |
| 87 | + byField && |
| 88 | + preExistingSettings[mergeField] && |
| 89 | + devtoolsSettings[mergeField] |
| 90 | + ) { |
| 91 | + // We need to merge two lists in a field. The list items have a unique |
| 92 | + // id specified by byField. If an entry is found in devtoolsSettings we |
| 93 | + // assume it should be updated when the devtools settings file is updated, |
| 94 | + // otherwise we keep the preexisting setting. |
| 95 | + const mergedList = [...devtoolsSettings[mergeField]]; |
| 96 | + const doNotDuplicate = new Set(mergedList.map(item => item[byField])); |
| 97 | + for (const item of preExistingSettings[mergeField]) { |
| 98 | + if (!doNotDuplicate.has(item[byField])) { |
| 99 | + mergedList.push(item); |
| 100 | + } |
| 101 | + } |
| 102 | + updatedSettings[mergeField] = mergedList; |
| 103 | + } |
| 104 | + fs.writeFileSync( |
| 105 | + vscodeSettingsLocation, |
| 106 | + JSON.stringify(updatedSettings, null, 2), |
| 107 | + ); |
| 108 | + } catch (err) { |
| 109 | + console.warn('Unable to update VSCode settings - skipping'); |
| 110 | + console.warn(err.stack); |
| 111 | + } |
| 112 | +} |
0 commit comments