utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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;