Skip to content

Commit 0338a32

Browse files
committed
Add npm publish helper tool
Summary: The helper tool extract files from CircleCI stored dist.tgz. And publish dist/ packages to jsc-android@{VERSION} with {TAG} dist.unstripped/ to jsc-android@{VERSION}-unstripped with {TAG}-unstripped Usage: python scripts/publish.py -T {TAG} /path/to/dist.tgz [--dry-run] E.g. python scripts/publish.py -T next ~/Downloads/dist.tgz --dry-run python scripts/publish.py -T latest ~/Downloads/dist.tgz
1 parent dbcedc8 commit 0338a32

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

scripts/publish.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)