utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. const fs = require('fs');
  6. const path = require('path');
  7. const cp = require('child_process');
  8. const REPO_ROOT = path.join(__dirname, '..');
  9. /**
  10. * Copy a file.
  11. *
  12. * @param {string} _source
  13. * @param {string} _destination
  14. */
  15. function copyFile(_source, _destination) {
  16. const source = path.join(REPO_ROOT, _source);
  17. const destination = path.join(REPO_ROOT, _destination);
  18. // ensure target dir
  19. (function () {
  20. /** @type {string[]} */
  21. const dirs = [];
  22. /** @type {string} */
  23. let dirname = path.dirname(destination);
  24. while (dirname.length > REPO_ROOT.length) {
  25. dirs.push(dirname);
  26. dirname = path.dirname(dirname);
  27. }
  28. dirs.reverse();
  29. dirs.forEach(function (dir) {
  30. try {
  31. fs.mkdirSync(dir);
  32. } catch (err) {}
  33. });
  34. })();
  35. fs.writeFileSync(destination, fs.readFileSync(source));
  36. console.log(`Copied ${_source} to ${_destination}`);
  37. }
  38. exports.copyFile = copyFile;
  39. /**
  40. * Remove a directory and all its contents.
  41. *
  42. * @param {string} _dirPath
  43. */
  44. function removeDir(_dirPath) {
  45. const dirPath = path.join(REPO_ROOT, _dirPath);
  46. if (!fs.existsSync(dirPath)) {
  47. return;
  48. }
  49. rmDir(dirPath);
  50. console.log(`Deleted ${_dirPath}`);
  51. /**
  52. * @param {string} dirPath
  53. */
  54. function rmDir(dirPath) {
  55. const entries = fs.readdirSync(dirPath);
  56. for (const entry of entries) {
  57. const filePath = path.join(dirPath, entry);
  58. if (fs.statSync(filePath).isFile()) {
  59. fs.unlinkSync(filePath);
  60. } else {
  61. rmDir(filePath);
  62. }
  63. }
  64. fs.rmdirSync(dirPath);
  65. }
  66. }
  67. exports.removeDir = removeDir;
  68. /**
  69. * Launch the typescript compiler synchronously over a project.
  70. *
  71. * @param {string} _projectPath
  72. */
  73. function tsc(_projectPath) {
  74. const projectPath = path.join(REPO_ROOT, _projectPath);
  75. cp.spawnSync(process.execPath, [path.join(__dirname, '../node_modules/typescript/lib/tsc.js'), '-p', projectPath], { stdio: 'inherit', stderr: 'inherit' });
  76. console.log(`Compiled ${_projectPath}`);
  77. }
  78. exports.tsc = tsc;
  79. /**
  80. * Launch prettier on a specific file.
  81. *
  82. * @param {string} _filePath
  83. */
  84. function prettier(_filePath) {
  85. const filePath = path.join(REPO_ROOT, _filePath);
  86. cp.spawnSync(process.execPath, [path.join(__dirname, '../node_modules/prettier/bin-prettier.js'), '--write', filePath], { stdio: 'inherit', stderr: 'inherit' });
  87. console.log(`Ran prettier over ${_filePath}`);
  88. }
  89. exports.prettier = prettier;
  90. /**
  91. * Transform an external .d.ts file to an internal .d.ts file
  92. *
  93. * @param {string} _source
  94. * @param {string} _destination
  95. * @param {string} namespace
  96. */
  97. function dts(_source, _destination, namespace) {
  98. const source = path.join(REPO_ROOT, _source);
  99. const destination = path.join(REPO_ROOT, _destination);
  100. const lines = fs
  101. .readFileSync(source)
  102. .toString()
  103. .split(/\r\n|\r|\n/);
  104. let result = [
  105. `/*---------------------------------------------------------------------------------------------`,
  106. ` * Copyright (c) Microsoft Corporation. All rights reserved.`,
  107. ` * Licensed under the MIT License. See License.txt in the project root for license information.`,
  108. ` *--------------------------------------------------------------------------------------------*/`,
  109. ``,
  110. `/// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />`,
  111. ``,
  112. `declare namespace ${namespace} {`
  113. ];
  114. for (let line of lines) {
  115. if (/^import/.test(line)) {
  116. continue;
  117. }
  118. line = line.replace(/ /g, '\t');
  119. line = line.replace(/declare /g, '');
  120. if (line.length > 0) {
  121. line = `\t${line}`;
  122. result.push(line);
  123. }
  124. }
  125. result.push(`}`);
  126. result.push(``);
  127. fs.writeFileSync(destination, result.join('\n'));
  128. prettier(_destination);
  129. }
  130. exports.dts = dts;
  131. function getGitVersion() {
  132. const git = path.join(REPO_ROOT, '.git');
  133. const headPath = path.join(git, 'HEAD');
  134. let head;
  135. try {
  136. head = fs.readFileSync(headPath, 'utf8').trim();
  137. } catch (e) {
  138. return void 0;
  139. }
  140. if (/^[0-9a-f]{40}$/i.test(head)) {
  141. return head;
  142. }
  143. const refMatch = /^ref: (.*)$/.exec(head);
  144. if (!refMatch) {
  145. return void 0;
  146. }
  147. const ref = refMatch[1];
  148. const refPath = path.join(git, ref);
  149. try {
  150. return fs.readFileSync(refPath, 'utf8').trim();
  151. } catch (e) {
  152. // noop
  153. }
  154. const packedRefsPath = path.join(git, 'packed-refs');
  155. let refsRaw;
  156. try {
  157. refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
  158. } catch (e) {
  159. return void 0;
  160. }
  161. const refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
  162. let refsMatch;
  163. const refs = {};
  164. while (refsMatch = refsRegex.exec(refsRaw)) {
  165. refs[refsMatch[2]] = refsMatch[1];
  166. }
  167. return refs[ref];
  168. }
  169. function getBundledFileHeader() {
  170. const sha1 = getGitVersion();
  171. const semver = require('../package.json').version;
  172. const headerVersion = semver + '(' + sha1 + ')';
  173. const BUNDLED_FILE_HEADER = [
  174. '/*!-----------------------------------------------------------------------------',
  175. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  176. ' * Version: ' + headerVersion,
  177. ' * Released under the MIT license',
  178. ' * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt',
  179. ' *-----------------------------------------------------------------------------*/',
  180. ''
  181. ].join('\n');
  182. return BUNDLED_FILE_HEADER;
  183. }
  184. exports.getBundledFileHeader = getBundledFileHeader;