diff --git a/bin/index.js b/bin/index.js index 5c52fcb..26b32e5 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,6 +1,7 @@ #!/usr/bin/env node const os = require('os'); +const {readFileSync} = require('fs'); const chalk = require('chalk'); const meow = require('meow'); @@ -10,6 +11,7 @@ const updateNotifier = require('update-notifier'); const pkg = require('../package.json'); const getUnityUrls = require('../lib/get-unity-urls'); +const {parseVersionFromString} = require('../lib/parsers'); const cli = meow( ` @@ -17,11 +19,16 @@ const cli = meow( $ get-unity [options] Options + ${chalk.yellow('--file, -f')} Search file for Unity version number. ${chalk.yellow('--help, -h')} Display this help message. ${chalk.yellow('--version, -v')} Display the current installed version. `, { 'flags': { + 'file': { + 'alias': 'f', + 'type': 'string' + }, 'help': { 'alias': 'h', 'default': false, @@ -43,5 +50,24 @@ const osKeyMap = { updateNotifier({pkg}).notify(); +if (cli.flags.file) { + + try { + + cli.input[0] = parseVersionFromString(readFileSync( + cli.flags.file, + 'utf8' + )); + + } catch ({message}) { + + process.stderr.write(`${chalk.red('Error:')} ${message}`); + + process.exit(1); + + } + +} + getUnityUrls(cli.input[0]).then(urls => process.stdout.write(`${urls[osKeyMap[os.type()]]}`)); diff --git a/lib/parsers.js b/lib/parsers.js new file mode 100644 index 0000000..baebd12 --- /dev/null +++ b/lib/parsers.js @@ -0,0 +1,19 @@ +const versionRegex = /[0-9]+\.[0-9]+\.[0-9]+[a-z][0-9]+/u; + +const parseVersionFromString = (contents = '') => { + + const matches = contents.match(versionRegex); + + if (matches && matches.length > 0) { + + return matches[0]; + + } + + throw new Error('Failed to parse version from string.'); + +}; + +module.exports = { + parseVersionFromString +}; diff --git a/package.json b/package.json index 92560b1..767533a 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,11 @@ "devDependencies": { "@neogeek/eslint-config-standards": "github:neogeek/eslint-config-standards", "babel-eslint": "10.0.3", - "eslint": "6.6.0" + "eslint": "6.6.0", + "jest": "24.9.0" + }, + "scripts": { + "test": "jest" }, "keywords": [ "unity" diff --git a/tests/.eslintrc b/tests/.eslintrc new file mode 100644 index 0000000..538e889 --- /dev/null +++ b/tests/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": ["@neogeek/eslint-config-standards/.eslintrc-tests"] +} diff --git a/tests/parsers.test.js b/tests/parsers.test.js new file mode 100644 index 0000000..98a68ce --- /dev/null +++ b/tests/parsers.test.js @@ -0,0 +1,22 @@ +const {parseVersionFromString} = require('../lib/parsers'); + +test( + 'Parse version from ProjectVersion.txt', + () => { + + const projectVersionContents = `m_EditorVersion: 2019.2.9f1 +m_EditorVersionWithRevision: 2019.2.9f1 (ebce4d76e6e8)`; + + expect(parseVersionFromString(projectVersionContents)).toBe('2019.2.9f1'); + + } +); + +test( + 'Fail to parse version from empty string', + () => { + + expect(() => parseVersionFromString('')).toThrow(Error); + + } +);