aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/publish.ts
blob: 997759db82ce5a97513e1bcdbf5612b3435a2619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as path from 'path';
import { execSync } from 'child_process';
import { program } from 'commander';
import * as fs from 'fs';
import * as semver from 'semver';

import * as common from './common';

function main() {
  common.checkForUncommittedChanges();
  program.option('-ext, --extension <string>', 'Path to target extension root');
  program.option('--pre-release', 'Publish as pre-release');
  program.option('--git-remote <string>', 'Git remote to push to');
  program.parse(process.argv);
  const options = program.opts();
  const targetExtension = options.extension as string;
  const extensionRoot = path.resolve(__dirname, '../');
  const targetExtensionRoot = path.join(extensionRoot, targetExtension);
  const preRelease = options.preRelease as boolean;
  const remote = (options.gitRemote as string)
    ? (options.gitRemote as string)
    : 'origin';
  const publishCommand = `npx vsce publish ${preRelease ? '--pre-release' : ''}`;
  const version = common.getExtensionVersion(targetExtensionRoot);
  const isEven = (num: number) => num % 2 === 0;
  const parsedVersion = semver.parse(version);
  if (parsedVersion === null) {
    throw new Error(`Invalid version: ${version}`);
  }
  if (isEven(parsedVersion.minor) && preRelease) {
    throw new Error(
      `Cannot publish pre-release version for even minor version: ${version}`
    );
  }
  if (!isEven(parsedVersion.minor) && !preRelease) {
    throw new Error(
      `Cannot publish stable version for odd minor version: ${version}`
    );
  }

  execSync(`npm run _prepublish`, { stdio: 'inherit' });
  execSync(`npm run ci:${targetExtension}`, { stdio: 'inherit' });
  execSync(`npm run compile:${targetExtension}`, { stdio: 'inherit' });
  execSync(`npm run ci-lint:${targetExtension}`, { stdio: 'inherit' });
  execSync(`npm run checkChangelog -- --dir="${targetExtensionRoot}"`, {
    cwd: extensionRoot,
    stdio: 'inherit'
  });
  execSync(publishCommand, {
    cwd: targetExtensionRoot,
    stdio: 'inherit'
  });
  // Remove the generated `commit` file
  fs.unlinkSync(path.join(targetExtension, 'commit'));

  common.pushTag(extensionRoot, targetExtension, version, remote);

  console.log(
    `Successfully published ${targetExtension} extension with version ${version}`
  );
}

main();