File tree Expand file tree Collapse file tree 5 files changed +75
-1
lines changed Expand file tree Collapse file tree 5 files changed +75
-1
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env node
22
33const os = require ( 'os' ) ;
4+ const { readFileSync} = require ( 'fs' ) ;
45
56const chalk = require ( 'chalk' ) ;
67const meow = require ( 'meow' ) ;
@@ -10,18 +11,24 @@ const updateNotifier = require('update-notifier');
1011const pkg = require ( '../package.json' ) ;
1112
1213const getUnityUrls = require ( '../lib/get-unity-urls' ) ;
14+ const { parseVersionFromString} = require ( '../lib/parsers' ) ;
1315
1416const 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
4451updateNotifier ( { 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+
4672getUnityUrls ( cli . input [ 0 ] ) . then ( urls =>
4773 process . stdout . write ( `${ urls [ osKeyMap [ os . type ( ) ] ] } ` ) ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 1+ {
2+ "extends": ["@neogeek/eslint-config-standards/.eslintrc-tests"]
3+ }
Original file line number Diff line number Diff line change 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+ ) ;
You can’t perform that action at this time.
0 commit comments