computeState.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 distTag = NIGHTLY ? 'next' : 'latest';
  25. const latestMonacoEditorVersion = npmGetLatestVersion('monaco-editor');
  26. const version = (() => {
  27. if (NIGHTLY) {
  28. const pieces = latestMonacoEditorVersion.split('.');
  29. const minor = parseInt(pieces[1], 10);
  30. const date = new Date();
  31. const yyyy = date.getUTCFullYear();
  32. const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
  33. const dd = String(date.getUTCDate()).padStart(2, '0');
  34. return `0.${minor + 1}.0-dev.${yyyy}${mm}${dd}`;
  35. } else {
  36. return packageJson.version;
  37. }
  38. })();
  39. const vscodeBranch = (() => {
  40. if (NIGHTLY) {
  41. return 'main';
  42. } else {
  43. return packageJson.vscode;
  44. }
  45. })();
  46. const skipMonacoEditorCore = (() => {
  47. return /** @type {'true'|'false'} */ (String(npmExists('monaco-editor-core', version)));
  48. })();
  49. const skipMonacoEditor = (() => {
  50. return /** @type {'true'|'false'} */ (String(npmExists('monaco-editor', version)));
  51. })();
  52. console.log(`
  53. ::set-output name=dist_tag::${distTag}
  54. ::set-output name=version::${version}
  55. ::set-output name=vscode_branch::${vscodeBranch}
  56. ::set-output name=skip_monaco_editor_core::${skipMonacoEditorCore}
  57. ::set-output name=skip_monaco_editor::${skipMonacoEditor}
  58. `);
  59. /**
  60. * @param {string} packageName
  61. * @returns {string}
  62. */
  63. function npmGetLatestVersion(packageName) {
  64. const output = cp.execSync(`npm show ${packageName} version`).toString();
  65. const version = output.split(/\r\n|\r|\n/g)[0];
  66. if (!/^0\.(\d+)\.(\d+)$/.test(version)) {
  67. console.error(`version ${version} does not match 0.x.y`);
  68. process.exit(1);
  69. }
  70. return version;
  71. }
  72. /**
  73. * @param {string} packageName
  74. * @param {string} version
  75. * @returns {boolean}
  76. */
  77. function npmExists(packageName, version) {
  78. try {
  79. const output = cp.execSync(`npm show ${packageName}@${version} version`).toString();
  80. const result = output.split(/\r\n|\r|\n/g)[0];
  81. if (result.trim().length === 0) {
  82. return false;
  83. }
  84. return true;
  85. } catch (err) {
  86. return false;
  87. }
  88. }