Skip to content

Commit d343a5b

Browse files
authored
Merge pull request #6 from pelmet/next
Add "--reset-build" and API docs
2 parents ed8e262 + a548327 commit d343a5b

File tree

4 files changed

+151
-123
lines changed

4 files changed

+151
-123
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ react-native-version
6666
-b, --increment-build Only increment build number
6767
-d, --android [path] Path to your "android/app/build.gradle" file
6868
-i, --ios [path] Path to your "ios/" folder
69-
-r, --reset-build Reset build number back to "1" (iOS only)
69+
-r, --reset-build Reset build number back to "1" (iOS only). Unlike Android's "versionCode", iOS doesn't require you to bump the "CFBundleVersion", as long as "CFBundleShortVersionString" changes. To make it consistent across platforms, react-native-version bumps both by default. You can use this option if you prefer to reset the build number after every version change. If you then need to push another build under the same version, you can use "-bt ios".
7070
-t, --target <platforms> Only version specified platforms, eg. "--target android,ios"
7171
```
7272

@@ -99,6 +99,27 @@ RNV=android react-native-version --target ios
9999
```
100100
:rage1: :speak_no_evil:
101101

102+
## API
103+
104+
```javascript
105+
import { version } from 'react-native-version';
106+
107+
version({
108+
amend: true,
109+
resetBuild: true,
110+
// ...
111+
});
112+
```
113+
114+
### Methods
115+
116+
#### version(program)
117+
Versions your app.
118+
119+
| Param | Type | Description |
120+
| --- | --- | --- |
121+
| program | <code>object</code> | commander/CLI-style options, camelCased |
122+
102123
## See also
103124

104125
- [agvtool](https://developer.apple.com/library/content/qa/qa1827/_index.html)

cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
const list = require('./utils').list;
34
const pkg = require('./package');
45
const program = require('commander');
@@ -13,7 +14,7 @@ program
1314
.option('-b, --increment-build', 'Only increment build number')
1415
.option('-d, --android [path]', 'Path to your "android/app/build.gradle" file', defaults.android)
1516
.option('-i, --ios [path]', 'Path to your "ios/" folder', defaults.ios)
16-
.option('-r, --reset-build', 'Reset build number back to "1" (iOS only)')
17+
.option('-r, --reset-build', 'Reset build number back to "1" (iOS only). Unlike Android\'s "versionCode", iOS doesn\'t require you to bump the "CFBundleVersion", as long as "CFBundleShortVersionString" changes. To make it consistent across platforms, ' + pkg.name + ' bumps both by default. You can use this option if you prefer to reset the build number after every version change. If you then need to push another build under the same version, you can use "-bt ios".')
1718
.option('-t, --target <platforms>', 'Only version specified platforms, eg. "--target android,ios"', list)
1819
/* eslint-enable max-len */
1920
.parse(process.argv);

index.js

Lines changed: 121 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -17,135 +17,137 @@ const defaults = {
1717
ios: path.join(cwd, 'ios')
1818
};
1919

20-
module.exports = {
21-
22-
/**
23-
* Returns default values for some options, namely android/ios file/folder paths
24-
* @return {object} defaults
25-
*/
26-
getDefaults: function() {
27-
return defaults;
28-
},
20+
/**
21+
* Versions your app.
22+
* @param {object} program commander/CLI-style options, camelCased
23+
*/
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+
});
33+
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+
}
2954

30-
/**
31-
* Version your app. Pass an object of the same options as for the CLI, except camelCased.
32-
* @param {object} program commander-style options, camelCased
33-
*/
34-
version: function(program) {
35-
program.android = program.android || defaults.android;
36-
program.ios = program.ios || defaults.ios;
37-
38-
const targets = []
39-
.concat(program.target, env.target)
40-
.filter(function(target) {
41-
return typeof target !== 'undefined';
42-
});
43-
44-
const android = new Promise(function(resolve) {
45-
if (!targets.length || targets.indexOf('android') > -1) {
46-
fs.stat(program.android, function(err) {
47-
if (err) {
48-
console.log(chalk.red('No gradle file found at ' + program.android));
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+
}
64+
});
65+
}
66+
});
67+
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));
4986

5087
console.log(chalk.yellow(
51-
'Use the "--android" option to specify the path manually'
88+
'Looks like Xcode Command Line Tools aren\'t installed'
5289
));
5390

54-
program.outputHelp();
55-
} else {
56-
const androidFile = fs.readFileSync(program.android, 'utf8');
57-
var newAndroidFile = androidFile;
58-
59-
if (!program.incrementBuild) {
60-
newAndroidFile = newAndroidFile.replace(
61-
/versionName "(.*)"/, 'versionName "' + appPkg.version + '"'
62-
);
63-
}
64-
65-
newAndroidFile = newAndroidFile
66-
.replace(/versionCode (\d+)/, function(match, cg1) {
67-
const newVersionCodeNumber = parseInt(cg1, 10) + 1;
68-
return 'versionCode ' + newVersionCodeNumber;
69-
});
70-
71-
fs.writeFileSync(program.android, newAndroidFile);
72-
resolve();
91+
console.log('');
92+
console.log(' Install:');
93+
console.log('');
94+
console.log(' $ xcode-select --install');
95+
console.log('');
96+
process.exit(1);
7397
}
74-
});
75-
}
76-
});
7798

78-
const ios = new Promise(function(resolve) {
79-
if (!targets.length || targets.indexOf('ios') > -1) {
80-
fs.stat(program.ios, function(err) {
81-
if (err) {
82-
console.log(chalk.red('No project folder found at ' + program.ios));
99+
const agvtoolOpts = {
100+
cwd: program.ios
101+
};
83102

84-
console.log(chalk.yellow(
85-
'Use the "--ios" option to specify the path manually'
86-
));
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+
}
87115

88-
program.outputHelp();
116+
if (program.resetBuild) {
117+
child.execSync('agvtool new-version -all 1', agvtoolOpts);
89118
} else {
90-
try {
91-
child.execSync('xcode-select --print-path', {
92-
stdio: ['ignore', 'ignore', 'pipe']
93-
});
94-
} catch (err) {
95-
console.log(chalk.red(err));
96-
97-
console.log(chalk.yellow(
98-
'Looks like Xcode Command Line Tools aren\'t installed'
99-
));
100-
101-
console.log('');
102-
console.log(' Install:');
103-
console.log('');
104-
console.log(' $ xcode-select --install');
105-
console.log('');
106-
process.exit(1);
107-
}
108-
109-
const agvtoolOpts = {
110-
cwd: program.ios
111-
};
112-
113-
try {
114-
child.execSync('agvtool what-version', agvtoolOpts);
115-
} catch (err) {
116-
console.log(chalk.red(err.stdout));
117-
process.exit(1);
118-
}
119-
120-
if (!program.incrementBuild) {
121-
child.spawnSync(
122-
'agvtool', ['new-marketing-version', appPkg.version], agvtoolOpts
123-
);
124-
}
125-
126-
if (program.resetBuild) {
127-
child.execSync('agvtool new-version -all 1', agvtoolOpts);
128-
} else {
129-
child.execSync('agvtool next-version -all', agvtoolOpts);
130-
}
131-
132-
resolve();
119+
child.execSync('agvtool next-version -all', agvtoolOpts);
133120
}
134-
});
135-
}
136-
});
137-
138-
Promise
139-
.all([android, ios])
140-
.then(function() {
141-
if (
142-
program.amend
143-
|| process.env.npm_lifecycle_event === 'postversion' && !program.neverAmend
144-
) {
145-
child.spawnSync('git', ['add', program.android, program.ios]);
146-
child.execSync('git commit --amend --no-edit');
147-
}
148-
});
149-
}
121+
122+
resolve();
123+
}
124+
});
125+
}
126+
});
127+
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');
137+
}
138+
});
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
150152

151153
};

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"devDependencies": {
1616
"eslint": "^3.4.0",
17-
"eslint-config-google": "^0.6.0"
17+
"eslint-config-google": "^0.6.0",
18+
"jsdoc-to-markdown": "^1.3.7"
1819
},
1920
"homepage": "https://github.com/stovmascript/react-native-version#readme",
2021
"repository": {
@@ -37,5 +38,8 @@
3738
"version",
3839
"versionCode",
3940
"versionName"
40-
]
41+
],
42+
"scripts": {
43+
"docs": "jsdoc2md -d 4 index.js"
44+
}
4145
}

0 commit comments

Comments
 (0)