publish.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import commander from 'commander';
  6. import * as path from 'path';
  7. import * as os from 'os';
  8. import { handlePackage } from './update-dist-tag';
  9. import * as utils from './utils';
  10. /**
  11. * Sleep for a specified period.
  12. *
  13. * @param wait The time in milliseconds to wait.
  14. */
  15. async function sleep(wait: number): Promise<void> {
  16. return new Promise(resolve => setTimeout(resolve, wait));
  17. }
  18. // Specify the program signature.
  19. commander
  20. .description('Publish the JS packages')
  21. .option(
  22. '--skip-build',
  23. 'Skip the build step (if there was a network error during a JS publish'
  24. )
  25. .option('--skip-publish', 'Skip publish and only handle tags')
  26. .option('--skip-tags', 'publish assets but do not handle tags')
  27. .option('--yes', 'Publish without confirmation')
  28. .option('--dry-run', 'Do not actually push any assets')
  29. .action(async (options: any) => {
  30. utils.exitOnUuncaughtException();
  31. // No-op if we're in release helper dry run
  32. if (process.env.RH_DRY_RUN === 'true') {
  33. return;
  34. }
  35. if (!options.skipPublish) {
  36. if (!options.skipBuild) {
  37. utils.run('jlpm run build:all');
  38. }
  39. if (!options.dryRun) {
  40. // Make sure we are logged in.
  41. if (utils.checkStatus('npm whoami') !== 0) {
  42. console.error('Please run `npm login`');
  43. process.exit(1);
  44. }
  45. }
  46. // Ensure a clean git environment
  47. try {
  48. utils.run('git commit -am "[ci skip] bump version"');
  49. } catch (e) {
  50. // do nothing
  51. }
  52. // Publish JS to the appropriate tag.
  53. const curr = utils.getPythonVersion();
  54. let cmd = 'lerna publish from-package ';
  55. if (options.dryRun) {
  56. cmd += '--no-git-tag-version --no-push ';
  57. }
  58. if (options.yes) {
  59. cmd += ' --yes ';
  60. }
  61. let tag = 'latest';
  62. if (!/\d+\.\d+\.\d+$/.test(curr)) {
  63. tag = 'next';
  64. }
  65. utils.run(`${cmd} --dist-tag=${tag} -m "Publish"`);
  66. }
  67. // Fix up any tagging issues.
  68. if (!options.skipTags && !options.dryRun) {
  69. const basePath = path.resolve('.');
  70. const paths = utils.getLernaPaths(basePath).sort();
  71. const cmds = await Promise.all(paths.map(handlePackage));
  72. cmds.forEach(cmdList => {
  73. cmdList.forEach(cmd => {
  74. if (!options.dryRun) {
  75. utils.run(cmd);
  76. } else {
  77. throw new Error(`Tag is out of sync: ${cmd}`);
  78. }
  79. });
  80. });
  81. }
  82. // Make sure all current JS packages are published.
  83. // Try and install them into a temporary local npm package.
  84. console.log('Checking for published packages...');
  85. const installDir = os.tmpdir();
  86. utils.run('npm init -y', { cwd: installDir, stdio: 'pipe' }, true);
  87. const specifiers: string[] = [];
  88. utils.getCorePaths().forEach(async pkgPath => {
  89. const pkgJson = path.join(pkgPath, 'package.json');
  90. const pkgData = utils.readJSONFile(pkgJson);
  91. specifiers.push(`${pkgData.name}@${pkgData.version}`);
  92. });
  93. let attempt = 0;
  94. while (attempt < 10) {
  95. try {
  96. utils.run(`npm install ${specifiers.join(' ')}`, { cwd: installDir });
  97. break;
  98. } catch (e) {
  99. console.error(e);
  100. console.log('Sleeping for one minute...');
  101. await sleep(1 * 60 * 1000);
  102. attempt += 1;
  103. }
  104. }
  105. if (attempt == 10) {
  106. console.error('Could not install packages');
  107. process.exit(1);
  108. }
  109. // Emit a system beep.
  110. process.stdout.write('\x07');
  111. });
  112. commander.parse(process.argv);