1. #!/usr/bin/env node
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. const {exec, spawn} = require('child-process-promise');
    
  6. const {join} = require('path');
    
  7. const {readFileSync} = require('fs');
    
  8. const theme = require('./theme');
    
  9. const {getDateStringForCommit, logPromise, printDiff} = require('./utils');
    
  10. 
    
  11. const cwd = join(__dirname, '..', '..');
    
  12. 
    
  13. const CIRCLE_CI_BUILD = 12707;
    
  14. const COMMIT = 'b3d1a81a9';
    
  15. const VERSION = '1.2.3';
    
  16. 
    
  17. const run = async () => {
    
  18.   const defaultOptions = {
    
  19.     cwd,
    
  20.     env: process.env,
    
  21.   };
    
  22. 
    
  23.   try {
    
  24.     // Start with a known build/revision:
    
  25.     // https://circleci.com/gh/facebook/react/12707
    
  26.     let promise = spawn(
    
  27.       'node',
    
  28.       [
    
  29.         './scripts/release/prepare-release-from-ci.js',
    
  30.         `--build=${CIRCLE_CI_BUILD}`,
    
  31.       ],
    
  32.       defaultOptions
    
  33.     );
    
  34.     logPromise(
    
  35.       promise,
    
  36.       theme`Checking out "next" build {version ${CIRCLE_CI_BUILD}}`
    
  37.     );
    
  38.     await promise;
    
  39. 
    
  40.     const dateString = await getDateStringForCommit(COMMIT);
    
  41. 
    
  42.     // Upgrade the above build top a known React version.
    
  43.     // Note that using the --local flag skips NPM checkout.
    
  44.     // This isn't totally necessary but is useful if we want to test an unpublished "next" build.
    
  45.     promise = spawn(
    
  46.       'node',
    
  47.       [
    
  48.         './scripts/release/prepare-release-from-npm.js',
    
  49.         `--version=0.0.0-${COMMIT}-${dateString}`,
    
  50.         '--local',
    
  51.       ],
    
  52.       defaultOptions
    
  53.     );
    
  54.     promise.childProcess.stdin.setEncoding('utf-8');
    
  55.     promise.childProcess.stdout.setEncoding('utf-8');
    
  56.     promise.childProcess.stdout.on('data', data => {
    
  57.       if (data.includes('✓ Version for')) {
    
  58.         // Update all packages to a stable version
    
  59.         promise.childProcess.stdin.write(VERSION);
    
  60.       } else if (data.includes('(y/N)')) {
    
  61.         // Accept all of the confirmation prompts
    
  62.         promise.childProcess.stdin.write('y');
    
  63.       }
    
  64.     });
    
  65.     logPromise(promise, theme`Preparing stable release {version ${VERSION}}`);
    
  66.     await promise;
    
  67. 
    
  68.     const beforeContents = readFileSync(
    
  69.       join(cwd, 'scripts/release/snapshot-test.snapshot'),
    
  70.       'utf-8'
    
  71.     );
    
  72.     await exec('cp build/temp.diff scripts/release/snapshot-test.snapshot', {
    
  73.       cwd,
    
  74.     });
    
  75.     const afterContents = readFileSync(
    
  76.       join(cwd, 'scripts/release/snapshot-test.snapshot'),
    
  77.       'utf-8'
    
  78.     );
    
  79. 
    
  80.     if (beforeContents === afterContents) {
    
  81.       console.log(theme.header`Snapshot test passed.`);
    
  82.     } else {
    
  83.       printDiff(
    
  84.         'scripts/release/snapshot-test.snapshot',
    
  85.         beforeContents,
    
  86.         afterContents
    
  87.       );
    
  88.       console.log();
    
  89.       console.error(theme.error('Snapshot test failed!'));
    
  90.       console.log();
    
  91.       console.log(
    
  92.         'If this failure was expected, please update the contents of the snapshot file:'
    
  93.       );
    
  94.       console.log(
    
  95.         theme`  {command git add} {path scripts/release/snapshot-test.snapshot}`
    
  96.       );
    
  97.       console.log(
    
  98.         theme`  {command git commit -m "Updating release script snapshot file."}`
    
  99.       );
    
  100.       process.exit(1);
    
  101.     }
    
  102.   } catch (error) {
    
  103.     console.error(theme.error(error));
    
  104.     process.exit(1);
    
  105.   }
    
  106. };
    
  107. 
    
  108. run();