Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor installers update (and offline mode) #14

Merged
merged 33 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0afad5b
Added script for fetching and caching a URL.
neogeek Nov 2, 2019
2968f01
Added first pass at editorInstallersUpdate method.
neogeek Nov 2, 2019
c36ea67
Whitespace.
neogeek Nov 2, 2019
e3d8776
Added offline flag.
neogeek Nov 3, 2019
13ef1b8
Moved utility files to new folder.
neogeek Nov 3, 2019
42007b7
Updated package.
neogeek Nov 6, 2019
75b58df
Prevent throwing error when unity.com can't be reached.
neogeek Nov 6, 2019
2916da3
Create .npmignore
neogeek Nov 6, 2019
5cf7678
Update .npmignore
neogeek Nov 6, 2019
41c0999
Merge branch 'master' into feature/editor-installers-update
neogeek Nov 6, 2019
40f96a7
Merge branch 'master' into feature/editor-installers-update
neogeek Nov 6, 2019
afd8759
Merge branch 'master' into feature/editor-installers-update
neogeek Nov 6, 2019
0227dff
Extracted contents of method.
neogeek Nov 6, 2019
4452a80
Added custom error if fetch fails.
neogeek Nov 6, 2019
ccb6292
Sorted imports.
neogeek Nov 6, 2019
a602e42
Added editorInstallersUpdate to exports.
neogeek Nov 6, 2019
39341fa
Added appveyor.yml to .npmignore
neogeek Nov 6, 2019
29fea08
Added attribute to editorInstallersUpdate for supplying file path.
neogeek Nov 6, 2019
a07507f
Update README.md
neogeek Nov 6, 2019
bfa06fa
Fixed invalid paths.
neogeek Nov 6, 2019
911e2d6
Load editor-installers.json on request.
neogeek Nov 6, 2019
d0eac49
Updated error.
neogeek Nov 6, 2019
a7b61d1
Added url constant back to scripts.
neogeek Nov 6, 2019
c7e9606
Added utilities to export.
neogeek Nov 6, 2019
8faf350
Renamed file and method.
neogeek Nov 6, 2019
f02ccc6
Renamed attribute.
neogeek Nov 6, 2019
d192e59
Updated help text.
neogeek Nov 6, 2019
a53259b
Added optional file path for getUnityUrls method.
neogeek Nov 6, 2019
82c1e96
Made filePath attribute optional.
neogeek Nov 6, 2019
7b4512d
Whitespace.
neogeek Nov 6, 2019
9cc8f60
Update README.md
neogeek Nov 6, 2019
cb85f29
Update README.md
neogeek Nov 6, 2019
c350e65
Update README.md
neogeek Nov 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/

temp/

package-lock.json
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.github/

temp/

.eslintrc
.travis.yml
appveyor.yml

logo.png
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,44 @@ $ echo $UNITY_URL

## API

### `getUnityUrls(string filter)`
### `checkCacheExpiry(string path, integer ttl)`

```javascript
const { checkCacheExpiry } = require("get-unity");

checkCacheExpiry("./data/editor-installers.json", 3600000)
.then(() => console.log("Cache is ok."))
.catch(() => console.log("Cache has expired."));
```

### `fetchWithLocalCache(string path, integer ttl)`

```javascript
const { fetchWithLocalCache } = require("get-unity");

fetchWithLocalCache(
"https://unity3d.com/get-unity/download/archive",
"./temp/archive.html",
3600000
).then(response => console.log(response));
```

### `getUnityUrls(string filter [, string filePath])`

```javascript
const { getUnityUrls } = require("get-unity");

getUnityUrls("2019").then(urls => console.log(urls));
getUnityUrls("2019", "./data/editor-installers.json").then(urls =>
console.log(urls)
);
```

Output:

```json
{
"mac": "https://download.unity3d.com/download_unity/5f859a4cfee5/MacEditorInstaller/Unity-2019.2.11f1.pkg",
"win64": "https://netstorage.unity3d.com/unity/5f859a4cfee5/Windows64EditorInstaller/UnitySetup64-2019.2.11f1.exe"
"win64": "https://download.unity3d.com/download_unity/5f859a4cfee5/Windows64EditorInstaller/UnitySetup64-2019.2.11f1.exe"
}
```

Expand All @@ -80,3 +104,13 @@ Output:
```
2019.2.9f1
```

### `updateEditorInstallers([string filePath])`

```javascript
const { updateEditorInstallers } = require("get-unity");

updateEditorInstallers("./data/editor-installers.json").then(() =>
console.log("Done")
);
```
44 changes: 39 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const os = require('os');
const {readFileSync} = require('fs');
const {join} = require('path');

const chalk = require('chalk');
const meow = require('meow');
Expand All @@ -10,6 +11,7 @@ const updateNotifier = require('update-notifier');

const pkg = require('../package.json');

const updateEditorInstallers = require('../lib/update-editor-installers');
const getUnityUrls = require('../lib/get-unity-urls');
const {parseVersionFromString} = require('../lib/parsers');

Expand All @@ -19,9 +21,10 @@ const cli = meow(
$ get-unity <version> [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.
${chalk.yellow('--file, -f')} Search file for Unity version number.
${chalk.yellow('--offline, -o')} Prevent request to update local cache of editor versions.
${chalk.yellow('--help, -h')} Display this help message.
${chalk.yellow('--version, -v')} Display the current installed version.
`,
{
'flags': {
Expand All @@ -34,6 +37,11 @@ const cli = meow(
'default': false,
'type': 'boolean'
},
'offline': {
'alias': 'o',
'default': false,
'type': 'boolean'
},
'version': {
'alias': 'v',
'default': false,
Expand All @@ -48,6 +56,11 @@ const osKeyMap = {
'Windows_NT': 'win64'
};

const EDITOR_INSTALLERS_FILE_PATH = join(
__dirname,
'../data/editor-installers.json'
);

updateNotifier({pkg}).notify();

if (cli.flags.file) {
Expand All @@ -69,5 +82,26 @@ if (cli.flags.file) {

}

getUnityUrls(cli.input[0]).then(urls =>
process.stdout.write(`${urls[osKeyMap[os.type()]]}`));
if (cli.flags.offline) {

getUnityUrls(
cli.input[0],
EDITOR_INSTALLERS_FILE_PATH
).then(urls =>
process.stdout.write(`${urls[osKeyMap[os.type()]]}`));

} else {

updateEditorInstallers(EDITOR_INSTALLERS_FILE_PATH)
.catch(({message}) => {

process.stderr.write(`${chalk.red('Error:')} ${message}`);

})
.then(() => getUnityUrls(
cli.input[0],
EDITOR_INSTALLERS_FILE_PATH
))
.then(urls => process.stdout.write(`${urls[osKeyMap[os.type()]]}`));

}
28 changes: 10 additions & 18 deletions data/editor-installers.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"2019.2.11f1": {
"mac": "https://download.unity3d.com/download_unity/5f859a4cfee5/MacEditorInstaller/Unity-2019.2.11f1.pkg",
"win64": "https://netstorage.unity3d.com/unity/5f859a4cfee5/Windows64EditorInstaller/UnitySetup64-2019.2.11f1.exe"
"win64": "https://download.unity3d.com/download_unity/5f859a4cfee5/Windows64EditorInstaller/UnitySetup64-2019.2.11f1.exe"
},
"2019.2.10f1": {
"mac": "https://download.unity3d.com/download_unity/923acd2d43aa/MacEditorInstaller/Unity-2019.2.10f1.pkg",
Expand Down Expand Up @@ -541,42 +541,34 @@
},
"5.6.7f1": {
"mac": "https://download.unity3d.com/download_unity/e80cc3114ac1/MacEditorInstaller/Unity-5.6.7f1.pkg",
"win64": "https://download.unity3d.com/download_unity/e80cc3114ac1/Windows64EditorInstaller/UnitySetup64-5.6.7f1.exe",
"win32": "https://download.unity3d.com/download_unity/e80cc3114ac1/Windows32EditorInstaller/UnitySetup32-5.6.7f1.exe"
"win64": "https://download.unity3d.com/download_unity/e80cc3114ac1/Windows64EditorInstaller/UnitySetup64-5.6.7f1.exe"
},
"5.6.6f2": {
"mac": "https://download.unity3d.com/download_unity/6bac21139588/MacEditorInstaller/Unity-5.6.6f2.pkg",
"win64": "https://download.unity3d.com/download_unity/6bac21139588/Windows64EditorInstaller/UnitySetup64-5.6.6f2.exe",
"win32": "https://download.unity3d.com/download_unity/6bac21139588/Windows32EditorInstaller/UnitySetup32-5.6.6f2.exe"
"win64": "https://download.unity3d.com/download_unity/6bac21139588/Windows64EditorInstaller/UnitySetup64-5.6.6f2.exe"
},
"5.6.5f1": {
"mac": "https://download.unity3d.com/download_unity/2cac56bf7bb6/MacEditorInstaller/Unity-5.6.5f1.pkg",
"win64": "https://download.unity3d.com/download_unity/2cac56bf7bb6/Windows64EditorInstaller/UnitySetup64-5.6.5f1.exe",
"win32": "https://download.unity3d.com/download_unity/2cac56bf7bb6/Windows32EditorInstaller/UnitySetup32-5.6.5f1.exe"
"win64": "https://download.unity3d.com/download_unity/2cac56bf7bb6/Windows64EditorInstaller/UnitySetup64-5.6.5f1.exe"
},
"5.6.4f1": {
"mac": "https://download.unity3d.com/download_unity/ac7086b8d112/MacEditorInstaller/Unity-5.6.4f1.pkg",
"win64": "https://download.unity3d.com/download_unity/ac7086b8d112/Windows64EditorInstaller/UnitySetup64-5.6.4f1.exe",
"win32": "https://download.unity3d.com/download_unity/ac7086b8d112/Windows32EditorInstaller/UnitySetup32-5.6.4f1.exe"
"win64": "https://download.unity3d.com/download_unity/ac7086b8d112/Windows64EditorInstaller/UnitySetup64-5.6.4f1.exe"
},
"5.6.3f1": {
"mac": "https://download.unity3d.com/download_unity/d3101c3b8468/MacEditorInstaller/Unity-5.6.3f1.pkg",
"win64": "https://download.unity3d.com/download_unity/d3101c3b8468/Windows64EditorInstaller/UnitySetup64-5.6.3f1.exe",
"win32": "https://download.unity3d.com/download_unity/d3101c3b8468/Windows32EditorInstaller/UnitySetup32-5.6.3f1.exe"
"win64": "https://download.unity3d.com/download_unity/d3101c3b8468/Windows64EditorInstaller/UnitySetup64-5.6.3f1.exe"
},
"5.6.2f1": {
"mac": "https://download.unity3d.com/download_unity/a2913c821e27/MacEditorInstaller/Unity-5.6.2f1.pkg",
"win64": "https://download.unity3d.com/download_unity/a2913c821e27/Windows64EditorInstaller/UnitySetup64-5.6.2f1.exe",
"win32": "https://download.unity3d.com/download_unity/a2913c821e27/Windows32EditorInstaller/UnitySetup32-5.6.2f1.exe"
"win64": "https://download.unity3d.com/download_unity/a2913c821e27/Windows64EditorInstaller/UnitySetup64-5.6.2f1.exe"
},
"5.6.1f1": {
"mac": "https://download.unity3d.com/download_unity/2860b30f0b54/MacEditorInstaller/Unity-5.6.1f1.pkg",
"win64": "https://download.unity3d.com/download_unity/2860b30f0b54/Windows64EditorInstaller/UnitySetup64-5.6.1f1.exe",
"win32": "https://download.unity3d.com/download_unity/2860b30f0b54/Windows32EditorInstaller/UnitySetup32-5.6.1f1.exe"
"win64": "https://download.unity3d.com/download_unity/2860b30f0b54/Windows64EditorInstaller/UnitySetup64-5.6.1f1.exe"
},
"5.6.0f3": {
"mac": "https://download.unity3d.com/download_unity/497a0f351392/MacEditorInstaller/Unity-5.6.0f3.pkg",
"win64": "https://download.unity3d.com/download_unity/497a0f351392/Windows64EditorInstaller/UnitySetup64-5.6.0f3.exe",
"win32": "https://download.unity3d.com/download_unity/497a0f351392/Windows32EditorInstaller/UnitySetup32-5.6.0f3.exe"
"win64": "https://download.unity3d.com/download_unity/497a0f351392/Windows64EditorInstaller/UnitySetup64-5.6.0f3.exe"
}
}
}
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const checkCacheExpiry = require('./utils/check-cache-expiry');
const fetchWithLocalCache = require('./utils/fetch-with-local-cache');
const getUnityUrls = require('./lib/get-unity-urls');
const parsers = require('./lib/parsers');
const updateEditorInstallers = require('./lib/update-editor-installers');

module.exports = {
checkCacheExpiry,
fetchWithLocalCache,
getUnityUrls,
parsers
parsers,
updateEditorInstallers
};
44 changes: 28 additions & 16 deletions lib/get-unity-urls.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
const editorInstallers = require('../data/editor-installers.json');
const fs = require('fs');
const {join} = require('path');
const {promisify} = require('util');

const getUnityUrls = (filter = '') =>
new Promise(resolve => {
const readFile = promisify(fs.readFile);

const versions = Object.keys(editorInstallers);
const EDITOR_INSTALLERS_FILE_PATH = join(
__dirname,
'../data/editor-installers.json'
);

const [latest] = versions;
const getUnityUrls = (filter = '', filePath = EDITOR_INSTALLERS_FILE_PATH) =>
readFile(filePath)
.then(data => JSON.parse(data))
.then(editorInstallers => {

const match =
versions.find(version =>
version.match(new RegExp(
`^${filter.replace(
'x',
'[0-9]+'
)}`,
'u'
))) || latest;
const versions = Object.keys(editorInstallers);

resolve(editorInstallers[match]);
const [latest] = versions;

const match =
versions.find(version =>
version.match(new RegExp(
`^${filter.replace(
'x',
'[0-9]+'
)}`,
'u'
))) || latest;

return editorInstallers[match];

});

});
module.exports = getUnityUrls;
85 changes: 85 additions & 0 deletions lib/update-editor-installers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const fs = require('fs');
const {join} = require('path');
const {promisify} = require('util');

const writeFile = promisify(fs.writeFile);

const {JSDOM} = require('jsdom');

const checkCacheExpiry = require('../utils/check-cache-expiry');
const fetchWithLocalCache = require('../utils/fetch-with-local-cache');

const ARCHIVE_FILE_PATH = join(
__dirname,
'../temp/archive.html'
);

const EDITOR_INSTALLERS_FILE_PATH = join(
__dirname,
'../data/editor-installers.json'
);

const JSON_TAB_WIDTH = 2;

const CACHE_TTL = 3600000;

const parseVersionFromUnityArchive = body => {

const {document} = new JSDOM(body).window;

return [].slice
.call(document.querySelectorAll('a[href^="unityhub://"]'))
.reduce(
(acc, elem) => {

const link = elem.getAttribute('href');

const [
version,
hash
] = link.replace(
'unityhub://',
''
).split('/');

acc[version] = {
'mac': `https://download.unity3d.com/download_unity/${hash}/MacEditorInstaller/Unity-${version}.pkg`,
'win64': `https://download.unity3d.com/download_unity/${hash}/Windows64EditorInstaller/UnitySetup64-${version}.exe`
};

return acc;

},
{}
);

};

const updateEditorInstallers = (filePath = EDITOR_INSTALLERS_FILE_PATH) =>
checkCacheExpiry(
filePath,
CACHE_TTL
)
.catch(() =>
fetchWithLocalCache(
'https://unity3d.com/get-unity/download/archive',
ARCHIVE_FILE_PATH,
CACHE_TTL
)
.then(parseVersionFromUnityArchive)
.then(data =>
writeFile(
filePath,
JSON.stringify(
data,
null,
JSON_TAB_WIDTH
)
)))
.catch(() => {

throw new Error('There was an error fetching the latest versions from unity3d.com');

});

module.exports = updateEditorInstallers;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"license": "MIT",
"dependencies": {
"chalk": "2.4.2",
"jsdom": "15.2.1",
"meow": "5.0.0",
"request": "2.88.0",
"update-notifier": "3.0.1"
},
"devDependencies": {
Expand Down
Loading