|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | +ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 10 | + |
| 11 | + |
| 12 | +class PackageConfigPatcher: |
| 13 | + def __init__(self, suffix=''): |
| 14 | + self._suffix = suffix |
| 15 | + self._config_path = os.path.join(ROOT_DIR, 'package.json') |
| 16 | + self._orig_config = '' |
| 17 | + |
| 18 | + def __enter__(self): |
| 19 | + with open(self._config_path, 'r') as f: |
| 20 | + self._orig_config = f.read() |
| 21 | + patched_config = self._create_patched_config(self._orig_config) |
| 22 | + with open(self._config_path, 'w') as f: |
| 23 | + f.write(patched_config) |
| 24 | + |
| 25 | + def __exit__(self, exc_type, exc_value, traceback): |
| 26 | + with open(self._config_path, 'w') as f: |
| 27 | + f.write(self._orig_config) |
| 28 | + |
| 29 | + def _create_patched_config(self, config): |
| 30 | + patched_config = json.loads(config) |
| 31 | + if self._suffix: |
| 32 | + patched_config[ |
| 33 | + 'version'] = patched_config['version'] + '-' + self._suffix |
| 34 | + return json.dumps(patched_config, indent=2) |
| 35 | + |
| 36 | + |
| 37 | +def parse_args(): |
| 38 | + arg_parser = argparse.ArgumentParser() |
| 39 | + |
| 40 | + arg_parser.add_argument( |
| 41 | + '--dry-run', action='store_true', help='Dry run mode for npm publish') |
| 42 | + arg_parser.add_argument( |
| 43 | + '--tag', '-T', type=str, required=True, help='NPM published tag') |
| 44 | + arg_parser.add_argument( |
| 45 | + 'dist_tar_file', action='store', help='dist.tgz created from CI') |
| 46 | + |
| 47 | + args = arg_parser.parse_args() |
| 48 | + if not args.dist_tar_file: |
| 49 | + arg_parser.print_help() |
| 50 | + sys.exit(1) |
| 51 | + return args |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + args = parse_args() |
| 56 | + |
| 57 | + workdir = os.path.join(ROOT_DIR, 'build', 'publish') |
| 58 | + if not os.path.exists(workdir): |
| 59 | + os.makedirs(workdir) |
| 60 | + distdir = os.path.join(ROOT_DIR, 'dist') |
| 61 | + dryrun = '--dry-run' if args.dry_run else '' |
| 62 | + |
| 63 | + print('\n\n========== Publish standard package ==========') |
| 64 | + with PackageConfigPatcher(''): |
| 65 | + if os.path.exists(distdir): |
| 66 | + shutil.rmtree(distdir) |
| 67 | + subprocess.check_call( |
| 68 | + ['tar', '-xf', args.dist_tar_file, '-C', workdir]) |
| 69 | + shutil.move(os.path.join(workdir, 'dist'), distdir) |
| 70 | + subprocess.check_call(['npm', 'publish', dryrun, '--tag', args.tag]) |
| 71 | + |
| 72 | + print('\n\n========== Publish unstripped package ==========') |
| 73 | + with PackageConfigPatcher('unstripped'): |
| 74 | + if os.path.exists(distdir): |
| 75 | + shutil.rmtree(distdir) |
| 76 | + subprocess.check_call( |
| 77 | + ['tar', '-xf', args.dist_tar_file, '-C', workdir]) |
| 78 | + shutil.move(os.path.join(workdir, 'dist.unstripped'), distdir) |
| 79 | + subprocess.check_call( |
| 80 | + ['npm', 'publish', dryrun, '--tag', args.tag + '-unstripped']) |
| 81 | + |
| 82 | + shutil.rmtree(workdir) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + main() |
0 commit comments