|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +/* eslint-disable no-console */ |
| 5 | +const { packages } = require('../../lib/packages'); |
| 6 | +const { yellow } = require('chalk'); |
| 7 | +const fs = require('fs'); |
| 8 | +const http = require('http'); |
| 9 | +const path = require('path'); |
| 10 | +const semver = require('semver'); |
| 11 | +const spawn = require('child_process').spawn; |
| 12 | + |
| 13 | +function _exec(options, cmd, args) { |
| 14 | + let stdout = ''; |
| 15 | + let stderr = ''; |
| 16 | + const cwd = options.cwd || process.cwd(); |
| 17 | + |
| 18 | + args = args.filter(x => x !== undefined); |
| 19 | + const spawnOptions = { cwd }; |
| 20 | + |
| 21 | + if (process.platform.startsWith('win')) { |
| 22 | + args.unshift('/c', cmd); |
| 23 | + cmd = 'cmd.exe'; |
| 24 | + spawnOptions['stdio'] = 'pipe'; |
| 25 | + } |
| 26 | + |
| 27 | + const childProcess = spawn(cmd, args, spawnOptions); |
| 28 | + childProcess.stdout.on('data', (data) => { |
| 29 | + stdout += data.toString('utf-8'); |
| 30 | + data.toString('utf-8') |
| 31 | + .split(/[\n\r]+/) |
| 32 | + .filter(line => line !== '') |
| 33 | + .forEach(line => console.log(' ' + line)); |
| 34 | + }); |
| 35 | + childProcess.stderr.on('data', (data) => { |
| 36 | + stderr += data.toString('utf-8'); |
| 37 | + data.toString('utf-8') |
| 38 | + .split(/[\n\r]+/) |
| 39 | + .filter(line => line !== '') |
| 40 | + .forEach(line => console.error(yellow(' ' + line))); |
| 41 | + }); |
| 42 | + |
| 43 | + // Create the error here so the stack shows who called this function. |
| 44 | + const err = new Error(`Running "${cmd} ${args.join(' ')}" returned error code `); |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + childProcess.on('exit', (error) => { |
| 47 | + if (!error) { |
| 48 | + resolve({ stdout, stderr }); |
| 49 | + } else { |
| 50 | + err.message += `${error}...\n\nSTDOUT:\n${stdout}\n`; |
| 51 | + reject(err); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Promise.resolve() |
| 60 | + .then(() => { |
| 61 | + // Login into NPM. |
| 62 | + const NpmToken = process.env['NPM_TOKEN']; |
| 63 | + if (!NpmToken) { |
| 64 | + throw new Error('Need an NPM_TOKEN to be able to log into NPM.'); |
| 65 | + } |
| 66 | + |
| 67 | + fs.writeFileSync(path.join(process.env['HOME'], '.npmrc'), |
| 68 | + `//registry.npmjs.org/:_authToken=${NpmToken}\n`); |
| 69 | + }) |
| 70 | + // .then(() => runTool('publish', 'build')) |
| 71 | + .then(() => Promise.all(Object.keys(packages).map(name => { |
| 72 | + const pkg = packages[name]; |
| 73 | + const version = require(pkg.packageJson).version; |
| 74 | + const cwd = pkg.dist; |
| 75 | + |
| 76 | + return new Promise((resolve, reject) => { |
| 77 | + const url = `http://registry.npmjs.org/${name.replace(/\//g, '%2F')}`; |
| 78 | + const request = http.request(url, response => { |
| 79 | + let data = ''; |
| 80 | + |
| 81 | + response.on('data', chunk => data += chunk); |
| 82 | + response.on('end', () => { |
| 83 | + const json = JSON.parse(data); |
| 84 | + resolve(json); |
| 85 | + }); |
| 86 | + response.on('error', err => reject(err)); |
| 87 | + }); |
| 88 | + |
| 89 | + request.end(); |
| 90 | + }) |
| 91 | + .then(json => { |
| 92 | + // Verify that the package is newer that latest, minor or next, or skip. This promise |
| 93 | + // will release with an array of dist-tags. |
| 94 | + if (json['name'] !== name) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + const { latest, next } = json['dist-tags']; |
| 98 | + const isNext = semver.eq(version, next); |
| 99 | + const gtNext = semver.gt(version, next); |
| 100 | + const isLatest = semver.eq(version, latest); |
| 101 | + |
| 102 | + if (isNext || isLatest) { |
| 103 | + return null; |
| 104 | + } else if (gtNext) { |
| 105 | + return 'next'; |
| 106 | + } else { |
| 107 | + return 'latest'; |
| 108 | + } |
| 109 | + }) |
| 110 | + .then(distTag => { |
| 111 | + if (!distTag) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + console.log(`Publishing ${name} @ ${version}...`); |
| 116 | + return _exec({ cwd }, 'npm', ['publish', '--tag', distTag]); |
| 117 | + }); |
| 118 | + }))) |
| 119 | + .then(() => { |
| 120 | + fs.writeFileSync(path.join(process.env['HOME'], '.npmrc'), ''); |
| 121 | + }, err => { |
| 122 | + fs.writeFileSync(path.join(process.env['HOME'], '.npmrc'), ''); |
| 123 | + console.error(err); |
| 124 | + process.exit(1); |
| 125 | + }); |
0 commit comments