1. #!/usr/bin/env node
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. const {exec} = require('child-process-promise');
    
  6. const {existsSync} = require('fs');
    
  7. const {join} = require('path');
    
  8. const {getArtifactsList, logPromise} = require('../utils');
    
  9. const theme = require('../theme');
    
  10. 
    
  11. const run = async ({build, cwd, releaseChannel}) => {
    
  12.   const artifacts = await getArtifactsList(build);
    
  13.   const buildArtifacts = artifacts.find(entry =>
    
  14.     entry.path.endsWith('build.tgz')
    
  15.   );
    
  16. 
    
  17.   if (!buildArtifacts) {
    
  18.     console.log(
    
  19.       theme`{error The specified build (${build}) does not contain any build artifacts.}`
    
  20.     );
    
  21.     process.exit(1);
    
  22.   }
    
  23. 
    
  24.   // Download and extract artifact
    
  25.   const {CIRCLE_CI_API_TOKEN} = process.env;
    
  26.   let header = '';
    
  27.   // Add Circle CI API token to request header if available.
    
  28.   if (CIRCLE_CI_API_TOKEN != null) {
    
  29.     header = '-H "Circle-Token: ${CIRCLE_CI_API_TOKEN}" ';
    
  30.   }
    
  31.   await exec(`rm -rf ./build`, {cwd});
    
  32.   await exec(
    
  33.     `curl -L $(fwdproxy-config curl) ${buildArtifacts.url} ${header}| tar -xvz`,
    
  34.     {
    
  35.       cwd,
    
  36.     }
    
  37.   );
    
  38. 
    
  39.   // Copy to staging directory
    
  40.   // TODO: Consider staging the release in a different directory from the CI
    
  41.   // build artifacts: `./build/node_modules` -> `./staged-releases`
    
  42.   if (!existsSync(join(cwd, 'build'))) {
    
  43.     await exec(`mkdir ./build`, {cwd});
    
  44.   } else {
    
  45.     await exec(`rm -rf ./build/node_modules`, {cwd});
    
  46.   }
    
  47.   let sourceDir;
    
  48.   // TODO: Rename release channel to `next`
    
  49.   if (releaseChannel === 'stable') {
    
  50.     sourceDir = 'oss-stable';
    
  51.   } else if (releaseChannel === 'experimental') {
    
  52.     sourceDir = 'oss-experimental';
    
  53.   } else if (releaseChannel === 'latest') {
    
  54.     sourceDir = 'oss-stable-semver';
    
  55.   } else {
    
  56.     console.error('Internal error: Invalid release channel: ' + releaseChannel);
    
  57.     process.exit(releaseChannel);
    
  58.   }
    
  59.   await exec(`cp -r ./build/${sourceDir} ./build/node_modules`, {cwd});
    
  60. };
    
  61. 
    
  62. module.exports = async ({build, commit, cwd, releaseChannel}) => {
    
  63.   let buildLabel;
    
  64.   if (commit !== null) {
    
  65.     buildLabel = theme`commit {commit ${commit}} (build {build ${build}})`;
    
  66.   } else {
    
  67.     buildLabel = theme`build {build ${build}}`;
    
  68.   }
    
  69.   return logPromise(
    
  70.     run({build, cwd, releaseChannel}),
    
  71.     theme`Downloading artifacts from Circle CI for ${buildLabel}`
    
  72.   );
    
  73. };