2 * Copyright (c) 2019, Microsoft Corporation (MIT License).
5 const cp = require('child_process');
6 const fs = require('fs');
7 const path = require('path');
8 const packageJson = require('../package.json');
11 fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);
13 // Determine if this is a stable or beta release
14 const publishedVersions = getPublishedVersions();
15 const isStableRelease = publishedVersions.indexOf(packageJson.version) === -1;
17 // Get the next version
18 let nextVersion = isStableRelease ? packageJson.version : getNextBetaVersion();
19 console.log(`Publishing version: ${nextVersion}`);
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));
27 const args = ['publish'];
28 if (!isStableRelease) {
29 args.push('--tag', 'beta');
31 const result = cp.spawn('npm', args, { stdio: 'inherit' });
32 result.on('exit', code => process.exit(code));
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');
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`;
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;
51 const latestTagVersion = parseInt(latestPublishedVersion.substr(latestPublishedVersion.search(/[0-9]+$/)), 10);
52 return `${nextStableVersion}-${tag}${latestTagVersion + 1}`;
55 function getPublishedVersions(version, tag) {
56 const versionsProcess = cp.spawnSync('npm', ['view', packageJson.name, 'versions', '--json']);
57 const versionsJson = JSON.parse(versionsProcess.stdout);
59 return versionsJson.filter(v => !v.search(new RegExp(`${version}-${tag}[0-9]+`)));