|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | 1 | const chalk = require('chalk'); |
4 | 2 | const child = require('child_process'); |
5 | 3 | const fs = require('fs'); |
| 4 | +const list = require('./utils').list; |
6 | 5 | const path = require('path'); |
7 | | -const pkg = require('./package'); |
8 | | -const plist = require('simple-plist'); |
9 | | -const program = require('commander'); |
10 | 6 |
|
11 | 7 | const cwd = process.cwd(); |
12 | 8 |
|
13 | 9 | const appPkg = require(path.join(cwd, 'package.json')); |
14 | 10 |
|
15 | | -/** |
16 | | - * Splits list items in comma-separated lists |
17 | | - * @param {string} val comma-separated list |
18 | | - * @return {array} list items |
19 | | - */ |
20 | | -function list(val) { |
21 | | - return val.split(','); |
22 | | -} |
| 11 | +const env = { |
| 12 | + target: process.env.RNV && list(process.env.RNV) |
| 13 | +}; |
23 | 14 |
|
24 | | -program |
25 | | -/* eslint-disable max-len */ |
26 | | -.option('-a, --amend', 'Amend the previous commit. This is done automatically when ' + pkg.name + ' is run from the "postversion" npm script. Use "--never-amend" if you never want to amend.') |
27 | | -.option('-A, --never-amend', 'Never amend the previous commit') |
28 | | -.option('-b, --increment-build', 'Only increment build number') |
29 | | -.option('-d, --android [path]', 'Path to your "app/build.gradle" file', path.join(cwd, 'android/app/build.gradle')) |
30 | | -.option('-i, --ios [path]', 'Path to your "Info.plist" file', path.join(cwd, 'ios', appPkg.name, 'Info.plist')) |
31 | | -.option('-t, --target <platforms>', 'Only version specified platforms, eg. "--target android,ios"', list) |
32 | | -/* eslint-enable max-len */ |
33 | | -.parse(process.argv); |
| 15 | +const defaults = { |
| 16 | + android: path.join(cwd, 'android/app/build.gradle'), |
| 17 | + ios: path.join(cwd, 'ios') |
| 18 | +}; |
34 | 19 |
|
35 | 20 | /** |
36 | | - * Amends previous commit with changed gradle and plist files |
| 21 | + * Versions your app. |
| 22 | + * @param {object} program commander/CLI-style options, camelCased |
37 | 23 | */ |
38 | | -function amend() { |
39 | | - if ( |
40 | | - program.amend |
41 | | - || process.env.npm_lifecycle_event === 'postversion' && !program.neverAmend |
42 | | - || !program.incrementBuild |
43 | | - ) { |
44 | | - child.spawnSync('git', ['add', program.android, program.ios]); |
45 | | - child.execSync('git commit --amend --no-edit'); |
46 | | - } |
47 | | -} |
48 | | - |
49 | | -const env = { |
50 | | - target: process.env.RNV && list(process.env.RNV) |
51 | | -}; |
| 24 | +function version(program) { |
| 25 | + program.android = program.android || defaults.android; |
| 26 | + program.ios = program.ios || defaults.ios; |
| 27 | + |
| 28 | + const targets = [] |
| 29 | + .concat(program.target, env.target) |
| 30 | + .filter(function(target) { |
| 31 | + return typeof target !== 'undefined'; |
| 32 | + }); |
52 | 33 |
|
53 | | -const targets = [] |
54 | | -.concat(program.target, env.target) |
55 | | -.filter(function(target) { |
56 | | - return typeof target !== 'undefined'; |
57 | | -}); |
58 | | - |
59 | | -if (!targets.length || targets.indexOf('android') > -1) { |
60 | | - fs.stat(program.android, function(err, stats) { |
61 | | - if (err) { |
62 | | - console.log(chalk.red('No file found at ' + program.android)); |
63 | | - console.log(chalk.yellow('Use the "--android" option to specify the path manually')); |
64 | | - program.outputHelp(); |
65 | | - } else { |
66 | | - const androidFile = fs.readFileSync(program.android, 'utf8'); |
67 | | - var newAndroidFile = androidFile; |
68 | | - |
69 | | - if (!program.incrementBuild) { |
70 | | - newAndroidFile = newAndroidFile.replace( |
71 | | - /versionName "(.*)"/, 'versionName "' + appPkg.version + '"' |
72 | | - ); |
73 | | - } |
74 | | - |
75 | | - newAndroidFile = newAndroidFile.replace(/versionCode (\d+)/, function(match, cg1) { |
76 | | - const newVersionCodeNumber = parseInt(cg1, 10) + 1; |
77 | | - return 'versionCode ' + newVersionCodeNumber; |
| 34 | + const android = new Promise(function(resolve) { |
| 35 | + if (!targets.length || targets.indexOf('android') > -1) { |
| 36 | + fs.stat(program.android, function(err) { |
| 37 | + if (err) { |
| 38 | + console.log(chalk.red('No gradle file found at ' + program.android)); |
| 39 | + |
| 40 | + console.log(chalk.yellow( |
| 41 | + 'Use the "--android" option to specify the path manually' |
| 42 | + )); |
| 43 | + |
| 44 | + program.outputHelp(); |
| 45 | + } else { |
| 46 | + const androidFile = fs.readFileSync(program.android, 'utf8'); |
| 47 | + var newAndroidFile = androidFile; |
| 48 | + |
| 49 | + if (!program.incrementBuild) { |
| 50 | + newAndroidFile = newAndroidFile.replace( |
| 51 | + /versionName "(.*)"/, 'versionName "' + appPkg.version + '"' |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + newAndroidFile = newAndroidFile |
| 56 | + .replace(/versionCode (\d+)/, function(match, cg1) { |
| 57 | + const newVersionCodeNumber = parseInt(cg1, 10) + 1; |
| 58 | + return 'versionCode ' + newVersionCodeNumber; |
| 59 | + }); |
| 60 | + |
| 61 | + fs.writeFileSync(program.android, newAndroidFile); |
| 62 | + resolve(); |
| 63 | + } |
78 | 64 | }); |
| 65 | + } |
| 66 | + }); |
79 | 67 |
|
80 | | - fs.writeFileSync(program.android, newAndroidFile); |
81 | | - amend(); |
| 68 | + const ios = new Promise(function(resolve) { |
| 69 | + if (!targets.length || targets.indexOf('ios') > -1) { |
| 70 | + fs.stat(program.ios, function(err) { |
| 71 | + if (err) { |
| 72 | + console.log(chalk.red('No project folder found at ' + program.ios)); |
| 73 | + |
| 74 | + console.log(chalk.yellow( |
| 75 | + 'Use the "--ios" option to specify the path manually' |
| 76 | + )); |
| 77 | + |
| 78 | + program.outputHelp(); |
| 79 | + } else { |
| 80 | + try { |
| 81 | + child.execSync('xcode-select --print-path', { |
| 82 | + stdio: ['ignore', 'ignore', 'pipe'] |
| 83 | + }); |
| 84 | + } catch (err) { |
| 85 | + console.log(chalk.red(err)); |
| 86 | + |
| 87 | + console.log(chalk.yellow( |
| 88 | + 'Looks like Xcode Command Line Tools aren\'t installed' |
| 89 | + )); |
| 90 | + |
| 91 | + console.log(''); |
| 92 | + console.log(' Install:'); |
| 93 | + console.log(''); |
| 94 | + console.log(' $ xcode-select --install'); |
| 95 | + console.log(''); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + const agvtoolOpts = { |
| 100 | + cwd: program.ios |
| 101 | + }; |
| 102 | + |
| 103 | + try { |
| 104 | + child.execSync('agvtool what-version', agvtoolOpts); |
| 105 | + } catch (err) { |
| 106 | + console.log(chalk.red(err.stdout)); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + |
| 110 | + if (!program.incrementBuild) { |
| 111 | + child.spawnSync( |
| 112 | + 'agvtool', ['new-marketing-version', appPkg.version], agvtoolOpts |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + if (program.resetBuild) { |
| 117 | + child.execSync('agvtool new-version -all 1', agvtoolOpts); |
| 118 | + } else { |
| 119 | + child.execSync('agvtool next-version -all', agvtoolOpts); |
| 120 | + } |
| 121 | + |
| 122 | + resolve(); |
| 123 | + } |
| 124 | + }); |
82 | 125 | } |
83 | 126 | }); |
84 | | -} |
85 | 127 |
|
86 | | -if (!targets.length || targets.indexOf('ios') > -1) { |
87 | | - fs.stat(program.ios, function(err, stats) { |
88 | | - if (err) { |
89 | | - console.log(chalk.red('No file found at ' + program.ios)); |
90 | | - console.log(chalk.yellow('Use the "--ios" option to specify the path manually')); |
91 | | - program.outputHelp(); |
92 | | - } else { |
93 | | - const iosFile = plist.readFileSync(program.ios); |
94 | | - |
95 | | - if (program.incrementBuild) { |
96 | | - iosFile.CFBundleVersion = String(parseInt(iosFile.CFBundleVersion, 10) + 1); |
97 | | - } else { |
98 | | - iosFile.CFBundleShortVersionString = appPkg.version; |
99 | | - iosFile.CFBundleVersion = String(1); |
100 | | - } |
101 | | - |
102 | | - plist.writeFileSync(program.ios, iosFile); |
103 | | - |
104 | | - if (process.platform === 'darwin') { |
105 | | - child.execSync('plutil -convert xml1 ' + program.ios); |
106 | | - } |
107 | | - |
108 | | - amend(); |
| 128 | + Promise |
| 129 | + .all([android, ios]) |
| 130 | + .then(function() { |
| 131 | + if ( |
| 132 | + program.amend |
| 133 | + || process.env.npm_lifecycle_event === 'postversion' && !program.neverAmend |
| 134 | + ) { |
| 135 | + child.spawnSync('git', ['add', program.android, program.ios]); |
| 136 | + child.execSync('git commit --amend --no-edit'); |
109 | 137 | } |
110 | 138 | }); |
111 | 139 | } |
| 140 | + |
| 141 | +module.exports = { |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns default values for some options, namely android/ios file/folder paths |
| 145 | + * @return {object} defaults |
| 146 | + */ |
| 147 | + getDefaults: function() { |
| 148 | + return defaults; |
| 149 | + }, |
| 150 | + |
| 151 | + version: version |
| 152 | + |
| 153 | +}; |
0 commit comments