Path: blob/main/extensions/copilot/script/setup/copySources.ts
13388 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as fs from 'fs';6import * as path from 'path';7import { join } from 'path';8import * as ts from 'typescript';9const VS_ROOT = join(__dirname, '../../../vscode/src');10const TARGET = join(__dirname, '../../src/util/vs');1112/**13* Returns the absolute file path where the given file should be placed.14*/15function determineTargetPath(absoluteVSCodeFilePath: string): string {1617const vsRelative = path.relative(VS_ROOT, absoluteVSCodeFilePath);1819const segements = vsRelative.split(path.sep);2021if (segements[0] === 'typings' || segements[0] === 'vs') {22segements.shift();23}2425return join(TARGET, segements.join(path.sep));26}2728/**29* Returns the relative path of `importedFilePath` to `currentFilePath` in a format suitable for import statements.30*/31function createRelativeImportPath(currentFilePath: string, importedFilePath: string): string {32const relativePath = path.relative(path.dirname(currentFilePath), importedFilePath).replaceAll('\\', '/');33const result = relativePath.startsWith('.') ? relativePath : './' + relativePath;34return result.replace(/\.ts$/, '');35}3637async function doIt(filepaths: string[]) {38try {39await fs.promises.access(VS_ROOT);40} catch {41console.error(`❌ VS Code root not found at ${VS_ROOT}`);42process.exit(1);43}4445try {46await fs.promises.rm(join(TARGET), { recursive: true });47} catch {48// ignore49}5051type Edit = ts.TextRange & { newText: string };52type File = { sourceFilePath: string; targetFilePath: string; contents: string };53type StackElement = { filepath: string; importTrajectory: string[] };5455const seen = new Map<string, File>(); // indexed by sourceFilePath56const stack: StackElement[] = [...filepaths.map(p => ({ filepath: join(VS_ROOT, p), importTrajectory: [] }))];5758while (stack.length > 0) {59const stackElement = stack.pop()!;60const importTrajectory = stackElement.importTrajectory.slice(0);61importTrajectory.push(stackElement.filepath);6263let filepath = stackElement.filepath;64if (seen.has(filepath)) {65continue;66}6768const edits: Edit[] = [];69let source: string = '';70try {71source = String(await fs.promises.readFile(filepath));72} catch (e) {73try {74// .ts doesn't exist, try, .d.ts75filepath = filepath.replace(/\.ts$/, '.d.ts');76source = String(await fs.promises.readFile(filepath));77} catch (e) {78console.error(`❌ Error reading file ${filepath}. Trajectory:\n${stackElement.importTrajectory.reverse().map(el => `- ${el}`).join('\n')}:`);79throw e;80}81}8283const destinationFilePath = determineTargetPath(filepath);84const info = ts.preProcessFile(source, true, true);85for (const importedFile of info.importedFiles) {8687let absolutePath: string | undefined;88if (importedFile.fileName.startsWith('.')) {89absolutePath = join(filepath, '..', importedFile.fileName.replace(/\.js$/, '.ts'));90} else if (importedFile.fileName.includes('/')) {91absolutePath = join(VS_ROOT, importedFile.fileName.replace(/\.js$/, '.ts'));92}9394if (absolutePath) {95stack.push({ filepath: absolutePath, importTrajectory });9697edits.push({98...importedFile,99newText: createRelativeImportPath(destinationFilePath, determineTargetPath(absolutePath)),100});101}102103// console.log(`${filepath} <<<imports<<< ${absolutePath}`);104}105106let newSource = source;107108for (const edit of edits.sort((a, b) => b.pos - a.pos)) {109newSource = newSource.slice(0, edit.pos + 1) + edit.newText + newSource.slice(edit.end + 1);110}111112newSource = '//!!! DO NOT modify, this file was COPIED from \'microsoft/vscode\'\n\n' + newSource;113114seen.set(filepath, {115sourceFilePath: filepath,116targetFilePath: destinationFilePath,117contents: newSource118});119}120121for (const [_, file] of seen) {122123const targetFilepath = file.targetFilePath;124125await fs.promises.mkdir(join(targetFilepath, '..'), { recursive: true });126await fs.promises.writeFile(targetFilepath, file.contents);127}128129console.log(`✅ done, copied ${filepaths.length} files and ${seen.size - filepaths.length} dependencies`);130}131132(async function () {133try {134await doIt([135// ********************************************136// add modules from `base` here and137// run `npx tsx script/setup/copySources.ts`138// ********************************************139'vs/base/common/arrays.ts',140'vs/base/common/async.ts',141'vs/base/common/cache.ts',142'vs/base/common/cancellation.ts',143'vs/base/common/charCode.ts',144'vs/base/common/date.ts',145'vs/base/common/errors.ts',146'vs/base/common/event.ts',147'vs/base/common/functional.ts',148'vs/base/common/glob.ts',149'vs/base/common/htmlContent.ts',150'vs/base/common/iconLabels.ts',151'vs/base/common/iterator.ts',152'vs/base/common/lifecycle.ts',153'vs/base/common/linkedList.ts',154'vs/base/common/map.ts',155'vs/base/common/numbers.ts',156'vs/base/common/objects.ts',157'vs/base/common/resources.ts',158'vs/base/common/strings.ts',159'vs/base/common/ternarySearchTree.ts',160'vs/base/common/themables.ts',161'vs/base/common/uri.ts',162'vs/base/common/uuid.ts',163'vs/base/common/yaml.ts',164'vs/editor/common/core/ranges/offsetRange.ts',165'vs/editor/common/core/wordHelper.ts',166'vs/editor/common/model/prefixSumComputer.ts',167168'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.ts',169170'vs/base/node/ports.ts',171172'vs/platform/instantiation/common/instantiationService.ts',173'vs/editor/common/core/edits/lineEdit.ts',174'vs/editor/common/core/edits/lengthEdit.ts',175'vs/editor/common/core/edits/arrayEdit.ts',176'vs/editor/common/core/text/positionToOffset.ts',177'vs/editor/common/model/mirrorTextModel.ts',178179'vs/workbench/api/common/extHostTypes/diagnostic.ts',180'vs/workbench/api/common/extHostTypes/location.ts',181'vs/workbench/api/common/extHostTypes/markdownString.ts',182'vs/workbench/api/common/extHostTypes/notebooks.ts',183'vs/workbench/api/common/extHostTypes/position.ts',184'vs/workbench/api/common/extHostTypes/range.ts',185'vs/workbench/api/common/extHostTypes/selection.ts',186'vs/workbench/api/common/extHostTypes/snippetString.ts',187'vs/workbench/api/common/extHostTypes/snippetTextEdit.ts',188'vs/workbench/api/common/extHostTypes/textEdit.ts',189'vs/workbench/api/common/extHostTypes/symbolInformation.ts',190'vs/workbench/api/common/extHostDocumentData.ts',191'vs/workbench/contrib/chat/common/promptSyntax/promptFileParser.ts',192193'vs/base/common/sseParser.ts',194195// SPECIAL IMPLICIT DEPENDENCIES196'typings/vscode-globals-nls.d.ts',197'typings/vscode-globals-product.d.ts',198'typings/base-common.d.ts',199'typings/crypto.d.ts',200]);201} catch (error) {202console.error(error);203}204})();205206207