installed pty
[VSoRC/.git] / node_modules / node-pty / scripts / publish.js
1 /**
2  * Copyright (c) 2019, Microsoft Corporation (MIT License).
3  */
4
5 const cp = require('child_process');
6 const fs = require('fs');
7 const path = require('path');
8 const packageJson = require('../package.json');
9
10 // Setup auth
11 fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);
12
13 // Determine if this is a stable or beta release
14 const publishedVersions = getPublishedVersions();
15 const isStableRelease = publishedVersions.indexOf(packageJson.version) === -1;
16
17 // Get the next version
18 let nextVersion = isStableRelease ? packageJson.version : getNextBetaVersion();
19 console.log(`Publishing version: ${nextVersion}`);
20
21 // Set the version in package.json
22 const packageJsonFile = path.resolve(__dirname, '..', 'package.json');
23 packageJson.version = nextVersion;
24 fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2));
25
26 // Publish
27 const args = ['publish'];
28 if (!isStableRelease) {
29   args.push('--tag', 'beta');
30 }
31 const result = cp.spawn('npm', args, { stdio: 'inherit' });
32 result.on('exit', code => process.exit(code));
33
34 function getNextBetaVersion() {
35   if (!/^[0-9]+\.[0-9]+\.[0-9]+$/.exec(packageJson.version)) {
36     console.error('The package.json version must be of the form x.y.z');
37     process.exit(1);
38   }
39   const tag = 'beta';
40   const stableVersion = packageJson.version.split('.');
41   const nextStableVersion = `${stableVersion[0]}.${parseInt(stableVersion[1]) + 1}.0`;
42   const publishedVersions = getPublishedVersions(nextStableVersion, tag);
43   if (publishedVersions.length === 0) {
44     return `${nextStableVersion}-${tag}1`;
45   }
46   const latestPublishedVersion = publishedVersions.sort((a, b) => {
47     const aVersion = parseInt(a.substr(a.search(/[0-9]+$/)));
48     const bVersion = parseInt(b.substr(b.search(/[0-9]+$/)));
49     return aVersion > bVersion ? -1 : 1;
50   })[0];
51   const latestTagVersion = parseInt(latestPublishedVersion.substr(latestPublishedVersion.search(/[0-9]+$/)), 10);
52   return `${nextStableVersion}-${tag}${latestTagVersion + 1}`;
53 }
54
55 function getPublishedVersions(version, tag) {
56   const versionsProcess = cp.spawnSync('npm', ['view', packageJson.name, 'versions', '--json']);
57   const versionsJson = JSON.parse(versionsProcess.stdout);
58   if (tag) {
59     return versionsJson.filter(v => !v.search(new RegExp(`${version}-${tag}[0-9]+`)));
60   }
61   return versionsJson;
62 }