utils.js 5.5 KB

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