1
0

release.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. import path = require('path');
  6. import fs = require('fs');
  7. import { REPO_ROOT, readFiles, writeFiles, IFile } from '../build/utils';
  8. import { removeDir } from '../build/fs';
  9. import ts = require('typescript');
  10. import { generateMetadata } from './releaseMetadata';
  11. removeDir(`release`);
  12. // dev folder
  13. AMD_releaseOne('dev');
  14. // min folder
  15. AMD_releaseOne('min');
  16. // esm folder
  17. ESM_release();
  18. // monaco.d.ts, editor.api.d.ts
  19. releaseDTS();
  20. // ThirdPartyNotices.txt
  21. releaseThirdPartyNotices();
  22. // esm/metadata.d.ts, esm/metadata.js
  23. generateMetadata();
  24. // package.json
  25. (() => {
  26. const packageJSON = readFiles('package.json', { base: '' })[0];
  27. const json = JSON.parse(packageJSON.contents.toString());
  28. json.private = false;
  29. delete json.scripts['postinstall'];
  30. packageJSON.contents = Buffer.from(JSON.stringify(json, null, ' '));
  31. writeFiles([packageJSON], `release`);
  32. })();
  33. (() => {
  34. /** @type {IFile[]} */
  35. let otherFiles = [];
  36. otherFiles = otherFiles.concat(readFiles('README.md', { base: '' }));
  37. otherFiles = otherFiles.concat(readFiles('CHANGELOG.md', { base: '' }));
  38. otherFiles = otherFiles.concat(
  39. readFiles('node_modules/monaco-editor-core/min-maps/**/*', {
  40. base: 'node_modules/monaco-editor-core/'
  41. })
  42. );
  43. otherFiles = otherFiles.concat(
  44. readFiles('node_modules/monaco-editor-core/LICENSE', {
  45. base: 'node_modules/monaco-editor-core/'
  46. })
  47. );
  48. writeFiles(otherFiles, `release`);
  49. })();
  50. /**
  51. * Release to `dev` or `min`.
  52. * @param {'dev'|'min'} type
  53. */
  54. function AMD_releaseOne(type) {
  55. const coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, {
  56. base: `node_modules/monaco-editor-core/${type}`
  57. });
  58. AMD_addPluginContribs(type, coreFiles);
  59. writeFiles(coreFiles, `release/${type}`);
  60. const pluginFiles = readFiles(`out/release/${type}/**/*`, {
  61. base: `out/release/${type}`,
  62. ignore: ['**/monaco.contribution.js']
  63. });
  64. writeFiles(pluginFiles, `release/${type}`);
  65. }
  66. /**
  67. * Edit editor.main.js:
  68. * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  69. * - append monaco.contribution modules from plugins
  70. * - append new AMD module 'vs/editor/editor.main' that stiches things together
  71. */
  72. function AMD_addPluginContribs(type: 'dev' | 'min', files: IFile[]) {
  73. for (const file of files) {
  74. if (!/editor\.main\.js$/.test(file.path)) {
  75. continue;
  76. }
  77. let contents = file.contents.toString();
  78. // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  79. contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
  80. const pluginFiles = readFiles(`out/release/${type}/**/monaco.contribution.js`, {
  81. base: `out/release/${type}`
  82. });
  83. const extraContent = pluginFiles.map((file) => {
  84. return file.contents
  85. .toString()
  86. .replace(
  87. /define\((['"][a-z\/\-]+\/fillers\/monaco-editor-core['"]),\[\],/,
  88. "define($1,['vs/editor/editor.api'],"
  89. );
  90. });
  91. const allPluginsModuleIds = pluginFiles.map((file) => {
  92. return file.path.replace(/\.js$/, '');
  93. });
  94. extraContent.push(
  95. `define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join(
  96. '","'
  97. )}"], function(api) { return api; });`
  98. );
  99. let insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
  100. if (insertIndex === -1) {
  101. insertIndex = contents.length;
  102. }
  103. contents =
  104. contents.substring(0, insertIndex) +
  105. '\n' +
  106. extraContent.join('\n') +
  107. '\n' +
  108. contents.substring(insertIndex);
  109. file.contents = Buffer.from(contents);
  110. }
  111. }
  112. function ESM_release() {
  113. const coreFiles = readFiles(`node_modules/monaco-editor-core/esm/**/*`, {
  114. base: 'node_modules/monaco-editor-core/esm',
  115. // we will create our own editor.api.d.ts which also contains the plugins API
  116. ignore: ['node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts']
  117. });
  118. ESM_addImportSuffix(coreFiles);
  119. ESM_addPluginContribs(coreFiles);
  120. writeFiles(coreFiles, `release/esm`);
  121. ESM_releasePlugins();
  122. }
  123. /**
  124. * Release a plugin to `esm`.
  125. * Adds a dependency to 'vs/editor/editor.api' in contrib files in order for `monaco` to be defined.
  126. * Rewrites imports for 'monaco-editor-core/**'
  127. */
  128. function ESM_releasePlugins() {
  129. const files = readFiles(`out/release/esm/**/*`, { base: 'out/release/esm/' });
  130. for (const file of files) {
  131. if (!/(\.js$)|(\.ts$)/.test(file.path)) {
  132. continue;
  133. }
  134. let contents = file.contents.toString();
  135. const info = ts.preProcessFile(contents);
  136. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  137. let importText = info.importedFiles[i].fileName;
  138. const pos = info.importedFiles[i].pos;
  139. const end = info.importedFiles[i].end;
  140. if (!/(^\.\/)|(^\.\.\/)/.test(importText)) {
  141. // non-relative import
  142. if (!/^monaco-editor-core/.test(importText)) {
  143. console.error(`Non-relative import for unknown module: ${importText} in ${file.path}`);
  144. process.exit(1);
  145. }
  146. if (importText === 'monaco-editor-core') {
  147. importText = 'monaco-editor-core/esm/vs/editor/editor.api';
  148. }
  149. const importFilePath = importText.substring('monaco-editor-core/esm/'.length);
  150. let relativePath = path
  151. .relative(path.dirname(file.path), importFilePath)
  152. .replace(/\\/g, '/');
  153. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  154. relativePath = './' + relativePath;
  155. }
  156. contents = contents.substring(0, pos + 1) + relativePath + contents.substring(end + 1);
  157. }
  158. }
  159. file.contents = Buffer.from(contents);
  160. }
  161. for (const file of files) {
  162. if (!/monaco\.contribution\.js$/.test(file.path)) {
  163. continue;
  164. }
  165. const apiFilePath = 'vs/editor/editor.api';
  166. let relativePath = path.relative(path.dirname(file.path), apiFilePath).replace(/\\/g, '/');
  167. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  168. relativePath = './' + relativePath;
  169. }
  170. let contents = file.contents.toString();
  171. contents = `import '${relativePath}';\n` + contents;
  172. file.contents = Buffer.from(contents);
  173. }
  174. ESM_addImportSuffix(files);
  175. writeFiles(files, `release/esm`);
  176. }
  177. /**
  178. * Adds `.js` to all import statements.
  179. */
  180. function ESM_addImportSuffix(files: IFile[]) {
  181. for (const file of files) {
  182. if (!/\.js$/.test(file.path)) {
  183. continue;
  184. }
  185. let contents = file.contents.toString();
  186. const info = ts.preProcessFile(contents);
  187. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  188. const importText = info.importedFiles[i].fileName;
  189. const pos = info.importedFiles[i].pos;
  190. const end = info.importedFiles[i].end;
  191. if (/(\.css)|(\.js)$/.test(importText)) {
  192. // A CSS import or an import already using .js
  193. continue;
  194. }
  195. contents = contents.substring(0, pos + 1) + importText + '.js' + contents.substring(end + 1);
  196. }
  197. file.contents = Buffer.from(contents);
  198. }
  199. }
  200. /**
  201. * - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
  202. * - Create esm/vs/editor/editor.main.js that that stiches things together
  203. */
  204. function ESM_addPluginContribs(files: IFile[]) {
  205. for (const file of files) {
  206. if (!/editor\.main\.js$/.test(file.path)) {
  207. continue;
  208. }
  209. file.path = file.path.replace(/editor\.main/, 'edcore.main');
  210. }
  211. const mainFileDestPath = 'vs/editor/editor.main.js';
  212. const mainFileImports = readFiles(`out/release/esm/**/monaco.contribution.js`, {
  213. base: `out/release/esm`
  214. }).map((file) => {
  215. let relativePath = path
  216. .relative(path.dirname(mainFileDestPath), file.path)
  217. .replace(/\\/g, '/')
  218. .replace(/\.js$/, '');
  219. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  220. relativePath = './' + relativePath;
  221. }
  222. return relativePath;
  223. });
  224. const mainFileContents =
  225. mainFileImports.map((name) => `import '${name}';`).join('\n') +
  226. `\n\nexport * from './edcore.main';`;
  227. files.push({
  228. path: mainFileDestPath,
  229. contents: Buffer.from(mainFileContents)
  230. });
  231. }
  232. /**
  233. * Edit monaco.d.ts:
  234. * - append monaco.d.ts from plugins
  235. */
  236. function releaseDTS() {
  237. const monacodts = readFiles('node_modules/monaco-editor-core/monaco.d.ts', {
  238. base: 'node_modules/monaco-editor-core'
  239. })[0];
  240. let contents = monacodts.contents.toString();
  241. const extraContent = readFiles('out/release/*.d.ts', {
  242. base: 'out/release/'
  243. }).map((file) => {
  244. return file.contents.toString().replace(/\/\/\/ <reference.*\n/m, '');
  245. });
  246. contents =
  247. [
  248. '/*!-----------------------------------------------------------',
  249. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  250. ' * Type definitions for monaco-editor',
  251. ' * Released under the MIT license',
  252. '*-----------------------------------------------------------*/'
  253. ].join('\n') +
  254. '\n' +
  255. contents +
  256. '\n' +
  257. extraContent.join('\n');
  258. // Ensure consistent indentation and line endings
  259. contents = cleanFile(contents);
  260. monacodts.contents = Buffer.from(contents);
  261. const editorapidts = {
  262. path: 'esm/vs/editor/editor.api.d.ts',
  263. contents: Buffer.from(toExternalDTS(contents))
  264. };
  265. writeFiles([monacodts, editorapidts], `release`);
  266. fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
  267. fs.writeFileSync('website/typedoc/monaco.d.ts', contents);
  268. }
  269. /**
  270. * Transforms a .d.ts which uses internal modules (namespaces) to one which is usable with external modules
  271. * This function is duplicated in the `vscode` repo.
  272. */
  273. function toExternalDTS(contents: string): string {
  274. let lines = contents.split(/\r\n|\r|\n/);
  275. let killNextCloseCurlyBrace = false;
  276. for (let i = 0; i < lines.length; i++) {
  277. let line = lines[i];
  278. if (killNextCloseCurlyBrace) {
  279. if ('}' === line) {
  280. lines[i] = '';
  281. killNextCloseCurlyBrace = false;
  282. continue;
  283. }
  284. if (line.indexOf(' ') === 0) {
  285. lines[i] = line.substr(4);
  286. } else if (line.charAt(0) === '\t') {
  287. lines[i] = line.substr(1);
  288. }
  289. continue;
  290. }
  291. if ('declare namespace monaco {' === line) {
  292. lines[i] = '';
  293. killNextCloseCurlyBrace = true;
  294. continue;
  295. }
  296. if (line.indexOf('declare namespace monaco.') === 0) {
  297. lines[i] = line.replace('declare namespace monaco.', 'export namespace ');
  298. }
  299. if (line.indexOf('declare let MonacoEnvironment') === 0) {
  300. lines[i] = `declare global {\n let MonacoEnvironment: Environment | undefined;\n}`;
  301. }
  302. if (line.indexOf(' MonacoEnvironment?') === 0) {
  303. lines[i] = ` MonacoEnvironment?: Environment | undefined;`;
  304. }
  305. }
  306. return lines.join('\n').replace(/\n\n\n+/g, '\n\n');
  307. }
  308. /**
  309. * Normalize line endings and ensure consistent 4 spaces indentation
  310. */
  311. function cleanFile(contents: string): string {
  312. return contents
  313. .split(/\r\n|\r|\n/)
  314. .map(function (line) {
  315. const m = line.match(/^(\t+)/);
  316. if (!m) {
  317. return line;
  318. }
  319. const tabsCount = m[1].length;
  320. let newIndent = '';
  321. for (let i = 0; i < 4 * tabsCount; i++) {
  322. newIndent += ' ';
  323. }
  324. return newIndent + line.substring(tabsCount);
  325. })
  326. .join('\n');
  327. }
  328. /**
  329. * Edit ThirdPartyNotices.txt:
  330. * - append ThirdPartyNotices.txt from plugins
  331. */
  332. function releaseThirdPartyNotices() {
  333. const tpn = readFiles('node_modules/monaco-editor-core/ThirdPartyNotices.txt', {
  334. base: 'node_modules/monaco-editor-core'
  335. })[0];
  336. let contents = tpn.contents.toString();
  337. console.log('ADDING ThirdPartyNotices from ./ThirdPartyNotices.txt');
  338. let thirdPartyNoticeContent = fs
  339. .readFileSync(path.join(REPO_ROOT, 'ThirdPartyNotices.txt'))
  340. .toString();
  341. thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
  342. contents += '\n' + thirdPartyNoticeContent;
  343. tpn.contents = Buffer.from(contents);
  344. writeFiles([tpn], `release`);
  345. }