utils.js 5.5 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. console.log(`Launching compiler at ${_projectPath}...`);
  76. cp.spawnSync(process.execPath, [path.join(__dirname, '../node_modules/typescript/lib/tsc.js'), '-p', projectPath], { stdio: 'inherit', stderr: 'inherit' });
  77. console.log(`Compiled ${_projectPath}`);
  78. }
  79. exports.tsc = tsc;
  80. /**
  81. * Launch prettier on a specific file.
  82. *
  83. * @param {string} _filePath
  84. */
  85. function prettier(_filePath) {
  86. const filePath = path.join(REPO_ROOT, _filePath);
  87. cp.spawnSync(process.execPath, [path.join(__dirname, '../node_modules/prettier/bin-prettier.js'), '--write', filePath], { stdio: 'inherit', stderr: 'inherit' });
  88. console.log(`Ran prettier over ${_filePath}`);
  89. }
  90. exports.prettier = prettier;
  91. /**
  92. * Transform an external .d.ts file to an internal .d.ts file
  93. *
  94. * @param {string} _source
  95. * @param {string} _destination
  96. * @param {string} namespace
  97. */
  98. function dts(_source, _destination, namespace) {
  99. const source = path.join(REPO_ROOT, _source);
  100. const destination = path.join(REPO_ROOT, _destination);
  101. const lines = fs
  102. .readFileSync(source)
  103. .toString()
  104. .split(/\r\n|\r|\n/);
  105. let result = [
  106. `/*---------------------------------------------------------------------------------------------`,
  107. ` * Copyright (c) Microsoft Corporation. All rights reserved.`,
  108. ` * Licensed under the MIT License. See License.txt in the project root for license information.`,
  109. ` *--------------------------------------------------------------------------------------------*/`,
  110. ``,
  111. `/// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />`,
  112. ``,
  113. `declare namespace ${namespace} {`
  114. ];
  115. for (let line of lines) {
  116. if (/^import/.test(line)) {
  117. continue;
  118. }
  119. line = line.replace(/ /g, '\t');
  120. line = line.replace(/declare /g, '');
  121. if (line.length > 0) {
  122. line = `\t${line}`;
  123. result.push(line);
  124. }
  125. }
  126. result.push(`}`);
  127. result.push(``);
  128. fs.writeFileSync(destination, result.join('\n'));
  129. prettier(_destination);
  130. }
  131. exports.dts = dts;
  132. function getGitVersion() {
  133. const git = path.join(REPO_ROOT, '.git');
  134. const headPath = path.join(git, 'HEAD');
  135. let head;
  136. try {
  137. head = fs.readFileSync(headPath, 'utf8').trim();
  138. } catch (e) {
  139. return void 0;
  140. }
  141. if (/^[0-9a-f]{40}$/i.test(head)) {
  142. return head;
  143. }
  144. const refMatch = /^ref: (.*)$/.exec(head);
  145. if (!refMatch) {
  146. return void 0;
  147. }
  148. const ref = refMatch[1];
  149. const refPath = path.join(git, ref);
  150. try {
  151. return fs.readFileSync(refPath, 'utf8').trim();
  152. } catch (e) {
  153. // noop
  154. }
  155. const packedRefsPath = path.join(git, 'packed-refs');
  156. let refsRaw;
  157. try {
  158. refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
  159. } catch (e) {
  160. return void 0;
  161. }
  162. const refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
  163. let refsMatch;
  164. const refs = {};
  165. while (refsMatch = refsRegex.exec(refsRaw)) {
  166. refs[refsMatch[2]] = refsMatch[1];
  167. }
  168. return refs[ref];
  169. }
  170. function getBundledFileHeader() {
  171. const sha1 = getGitVersion();
  172. const semver = require('../package.json').version;
  173. const headerVersion = semver + '(' + sha1 + ')';
  174. const BUNDLED_FILE_HEADER = [
  175. '/*!-----------------------------------------------------------------------------',
  176. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  177. ' * Version: ' + headerVersion,
  178. ' * Released under the MIT license',
  179. ' * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt',
  180. ' *-----------------------------------------------------------------------------*/',
  181. ''
  182. ].join('\n');
  183. return BUNDLED_FILE_HEADER;
  184. }
  185. exports.getBundledFileHeader = getBundledFileHeader;