1. #!/usr/bin/env node
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. const {exec} = require('child-process-promise');
    
  6. const {readJsonSync} = require('fs-extra');
    
  7. const {join} = require('path');
    
  8. const {confirm, execRead} = require('../utils');
    
  9. const theme = require('../theme');
    
  10. 
    
  11. const run = async ({cwd, dry, tags, ci}, packageName, otp) => {
    
  12.   const packagePath = join(cwd, 'build/node_modules', packageName);
    
  13.   const {version} = readJsonSync(join(packagePath, 'package.json'));
    
  14. 
    
  15.   // Check if this package version has already been published.
    
  16.   // If so we might be resuming from a previous run.
    
  17.   // We could infer this by comparing the build-info.json,
    
  18.   // But for now the easiest way is just to ask if this is expected.
    
  19.   const info = await execRead(`npm view ${packageName}@${version}`);
    
  20.   if (info) {
    
  21.     console.log(
    
  22.       theme`{package ${packageName}} {version ${version}} has already been published.`
    
  23.     );
    
  24.     if (!ci) {
    
  25.       await confirm('Is this expected?');
    
  26.     }
    
  27.   } else {
    
  28.     console.log(theme`{spinnerSuccess ✓} Publishing {package ${packageName}}`);
    
  29. 
    
  30.     // Publish the package and tag it.
    
  31.     if (!dry) {
    
  32.       if (!ci) {
    
  33.         await exec(`npm publish --tag=${tags[0]} --otp=${otp}`, {
    
  34.           cwd: packagePath,
    
  35.         });
    
  36.         console.log(theme.command(`  cd ${packagePath}`));
    
  37.         console.log(
    
  38.           theme.command(`  npm publish --tag=${tags[0]} --otp=${otp}`)
    
  39.         );
    
  40.       } else {
    
  41.         await exec(`npm publish --tag=${tags[0]}`, {
    
  42.           cwd: packagePath,
    
  43.         });
    
  44.         console.log(theme.command(`  cd ${packagePath}`));
    
  45.         console.log(theme.command(`  npm publish --tag=${tags[0]}`));
    
  46.       }
    
  47.     }
    
  48. 
    
  49.     for (let j = 1; j < tags.length; j++) {
    
  50.       if (!dry) {
    
  51.         if (!ci) {
    
  52.           await exec(
    
  53.             `npm dist-tag add ${packageName}@${version} ${tags[j]} --otp=${otp}`,
    
  54.             {cwd: packagePath}
    
  55.           );
    
  56.           console.log(
    
  57.             theme.command(
    
  58.               `  npm dist-tag add ${packageName}@${version} ${tags[j]} --otp=${otp}`
    
  59.             )
    
  60.           );
    
  61.         } else {
    
  62.           await exec(`npm dist-tag add ${packageName}@${version} ${tags[j]}`, {
    
  63.             cwd: packagePath,
    
  64.           });
    
  65.           console.log(
    
  66.             theme.command(
    
  67.               `  npm dist-tag add ${packageName}@${version} ${tags[j]}`
    
  68.             )
    
  69.           );
    
  70.         }
    
  71.       }
    
  72.     }
    
  73. 
    
  74.     if (tags.includes('untagged')) {
    
  75.       // npm doesn't let us publish without a tag at all,
    
  76.       // so for one-off publishes we clean it up ourselves.
    
  77.       if (!dry) {
    
  78.         if (!ci) {
    
  79.           await exec(`npm dist-tag rm ${packageName} untagged --otp=${otp}`);
    
  80.           console.log(
    
  81.             theme.command(
    
  82.               `  npm dist-tag rm ${packageName} untagged --otp=${otp}`
    
  83.             )
    
  84.           );
    
  85.         } else {
    
  86.           await exec(`npm dist-tag rm ${packageName} untagged`);
    
  87.           console.log(
    
  88.             theme.command(`  npm dist-tag rm ${packageName} untagged`)
    
  89.           );
    
  90.         }
    
  91.       }
    
  92.     }
    
  93.   }
    
  94. };
    
  95. 
    
  96. module.exports = run;