computeState.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. //@ts-check
  6. const fs = require('fs');
  7. const cp = require('child_process');
  8. const packageJson = require('../../../package.json');
  9. if (process.argv.length !== 4) {
  10. console.error(`usage: node computeState.js <"workflow_dispatch"|"schedule"> <"true"|"false">`);
  11. process.exit(1);
  12. }
  13. const EVENT_NAME = /** @type {'workflow_dispatch'|'schedule'} */ (process.argv[2]);
  14. const STR_NIGHTLY = /** @type {'true'|'false'|''} */ (process.argv[3]);
  15. if (!/^((workflow_dispatch)|(schedule))$/.test(EVENT_NAME)) {
  16. console.error(`usage: node computeState.js <"workflow_dispatch"|"schedule"> <"true"|"false">`);
  17. process.exit(2);
  18. }
  19. if (!/^((true)|(false)|())$/.test(STR_NIGHTLY)) {
  20. console.error(`usage: node computeState.js <"workflow_dispatch"|"schedule"> <"true"|"false">`);
  21. process.exit(3);
  22. }
  23. const NIGHTLY = EVENT_NAME === 'schedule' || STR_NIGHTLY === 'true';
  24. const latestMonacoEditorVersion = npmGetLatestVersion('monaco-editor');
  25. const version = (() => {
  26. if (NIGHTLY) {
  27. const pieces = latestMonacoEditorVersion.split('.');
  28. const minor = parseInt(pieces[1], 10);
  29. const date = new Date();
  30. const yyyy = date.getUTCFullYear();
  31. const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
  32. const dd = String(date.getUTCDate()).padStart(2, '0');
  33. return `0.${minor + 1}.0-dev.${yyyy}${mm}${dd}`;
  34. } else {
  35. return packageJson.version;
  36. }
  37. })();
  38. const vscodeBranch = (() => {
  39. if (NIGHTLY) {
  40. return 'main';
  41. } else {
  42. return packageJson.vscode;
  43. }
  44. })();
  45. const skipMonacoEditorCore = (() => {
  46. return /** @type {'true'|'false'} */ (String(npmExists('monaco-editor-core', version)));
  47. })();
  48. const skipMonacoEditor = (() => {
  49. return /** @type {'true'|'false'} */ (String(npmExists('monaco-editor-core', version)));
  50. })();
  51. console.log(`
  52. ::set-output name=version::${version}
  53. ::set-output name=vscode_branch::${vscodeBranch}
  54. ::set-output name=skip_monaco_editor_core::${skipMonacoEditorCore}
  55. ::set-output name=skip_monaco_editor::${skipMonacoEditor}
  56. `);
  57. /**
  58. * @param {string} packageName
  59. * @returns {string}
  60. */
  61. function npmGetLatestVersion(packageName) {
  62. const output = cp.execSync(`npm show ${packageName} version`).toString();
  63. const version = output.split(/\r\n|\r|\n/g)[0];
  64. if (!/^0\.(\d+)\.(\d+)$/.test(version)) {
  65. console.error(`version ${version} does not match 0.x.y`);
  66. process.exit(1);
  67. }
  68. return version;
  69. }
  70. /**
  71. * @param {string} packageName
  72. * @param {string} version
  73. * @returns {boolean}
  74. */
  75. function npmExists(packageName, version) {
  76. const output = cp.execSync(`npm show ${packageName}@${version} version`).toString();
  77. const result = output.split(/\r\n|\r|\n/g)[0];
  78. if (result.trim().length === 0) {
  79. return false;
  80. }
  81. return true;
  82. }