Debian Codegraph @

build/bin/linking.js

raw
1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5 'use strict';
6 //@ts-check
7
8 const path = require('path');
9 const shell = require('shelljs');
10
11 const fs = require('fs');
12 const promisify = require('util').promisify;
13 const stat = promisify(fs.stat);
14 const readdir = promisify(fs.readdir);
15 const mkdir = promisify(fs.mkdir);
16 const exists = promisify(fs.exists);
17 const unlink = promisify(fs.unlink);
18
19 /**
20 * @param {string} source
21 * @param {string} dest
22 */
23 const hardLink = exports.hardLink = async function(source, dest) {
24 const sourceStat = await stat(source);
25 if (sourceStat.isFile()) {
26 shell.ln('-f', source, dest);
27 } else {
28 await mkdir(dest);
29 const files = await readdir(source);
30 for (const file of files) {
31 if (file === '.' || file === '..') {
32 continue;
33 }
34 await hardLink(path.join(source, file), path.join(dest, file));
35 }
36 }
37 };
38
39 const tryHardLink = exports.tryHardLink = async function(source, dest) {
40 console.log(`Linking recusively ${source} -> ${dest}`);
41 if (await exists(dest)) {
42 shell.rm('-rf', dest);
43 }
44 await hardLink(source, dest);
45 };
46
47 exports.softLink = async function(source, dest) {
48 if (await exists(dest)) {
49 shell.rm('-rf', dest);
50 }
51 const parent = path.dirname(dest);
52 if (!await exists(parent)) {
53 await mkdir(parent, { recursive: true });
54 }
55 shell.ln('-s', source, dest);
56 };
57
58 /**
59 * @param {string} module
60 * @param {string} name
61 * @param {string} source
62 */
63 const tryLink = exports.tryLink = async function(module, name, source) {
64 const current = process.cwd();
65 try {
66 const nodeModules = path.join(module, 'node_modules');
67 if (!await exists(nodeModules)) {
68 await mkdir(nodeModules);
69 }
70 process.chdir(nodeModules);
71 if (await exists(name)) {
72 shell.rm('-rf' , name);
73 }
74 shell.ln('-s', source, name);
75 } finally {
76 process.chdir(current);
77 }
78 };
79
80 exports.tryLinkTextDocuments = async function(module) {
81 return tryLink(module, 'vscode-languageserver-textdocument', path.join('..', '..', 'textDocument'));
82 };
83
84 exports.tryLinkTypes = async function(module) {
85 return tryLink(module, 'vscode-languageserver-types', path.join('..', '..', 'types'));
86 };
87
88 exports.tryLinkJsonRpc = async function(module) {
89 return tryLink(module, 'vscode-jsonrpc', path.join('..', '..', 'jsonrpc'));
90 };
91
92 exports.tryLinkProtocol = async function(module) {
93 return tryLink(module, 'vscode-languageserver-protocol', path.join('..', '..', 'protocol'));
94 };