Skip to content

Commit 9883e1a

Browse files
authored
Add parsing file (ProjectVersion.txt) via CLI flag support. (#12)
* Added parseVersionFromString library method. Added tests. * Read in file flag and replace input value with result. * Catch error and display message. * Add flag to help output. * Removed flag default. * Added exit code.
1 parent 2a69019 commit 9883e1a

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

bin/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
const os = require('os');
4+
const {readFileSync} = require('fs');
45

56
const chalk = require('chalk');
67
const meow = require('meow');
@@ -10,18 +11,24 @@ const updateNotifier = require('update-notifier');
1011
const pkg = require('../package.json');
1112

1213
const getUnityUrls = require('../lib/get-unity-urls');
14+
const {parseVersionFromString} = require('../lib/parsers');
1315

1416
const cli = meow(
1517
`
1618
Usage
1719
$ get-unity <version> [options]
1820
1921
Options
22+
${chalk.yellow('--file, -f')} Search file for Unity version number.
2023
${chalk.yellow('--help, -h')} Display this help message.
2124
${chalk.yellow('--version, -v')} Display the current installed version.
2225
`,
2326
{
2427
'flags': {
28+
'file': {
29+
'alias': 'f',
30+
'type': 'string'
31+
},
2532
'help': {
2633
'alias': 'h',
2734
'default': false,
@@ -43,5 +50,24 @@ const osKeyMap = {
4350

4451
updateNotifier({pkg}).notify();
4552

53+
if (cli.flags.file) {
54+
55+
try {
56+
57+
cli.input[0] = parseVersionFromString(readFileSync(
58+
cli.flags.file,
59+
'utf8'
60+
));
61+
62+
} catch ({message}) {
63+
64+
process.stderr.write(`${chalk.red('Error:')} ${message}`);
65+
66+
process.exit(1);
67+
68+
}
69+
70+
}
71+
4672
getUnityUrls(cli.input[0]).then(urls =>
4773
process.stdout.write(`${urls[osKeyMap[os.type()]]}`));

lib/parsers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const versionRegex = /[0-9]+\.[0-9]+\.[0-9]+[a-z][0-9]+/u;
2+
3+
const parseVersionFromString = (contents = '') => {
4+
5+
const matches = contents.match(versionRegex);
6+
7+
if (matches && matches.length > 0) {
8+
9+
return matches[0];
10+
11+
}
12+
13+
throw new Error('Failed to parse version from string.');
14+
15+
};
16+
17+
module.exports = {
18+
parseVersionFromString
19+
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
"devDependencies": {
1919
"@neogeek/eslint-config-standards": "github:neogeek/eslint-config-standards",
2020
"babel-eslint": "10.0.3",
21-
"eslint": "6.6.0"
21+
"eslint": "6.6.0",
22+
"jest": "24.9.0"
23+
},
24+
"scripts": {
25+
"test": "jest"
2226
},
2327
"keywords": [
2428
"unity"

tests/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@neogeek/eslint-config-standards/.eslintrc-tests"]
3+
}

tests/parsers.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const {parseVersionFromString} = require('../lib/parsers');
2+
3+
test(
4+
'Parse version from ProjectVersion.txt',
5+
() => {
6+
7+
const projectVersionContents = `m_EditorVersion: 2019.2.9f1
8+
m_EditorVersionWithRevision: 2019.2.9f1 (ebce4d76e6e8)`;
9+
10+
expect(parseVersionFromString(projectVersionContents)).toBe('2019.2.9f1');
11+
12+
}
13+
);
14+
15+
test(
16+
'Fail to parse version from empty string',
17+
() => {
18+
19+
expect(() => parseVersionFromString('')).toThrow(Error);
20+
21+
}
22+
);

0 commit comments

Comments
 (0)