Skip to content

Commit 6075372

Browse files
committed
Basic downloader
0 parents  commit 6075372

7 files changed

Lines changed: 1116 additions & 0 deletions

File tree

.eslintrc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
extends: ['plugin:prettier/recommended'],
3+
parserOptions: { ecmaVersion: 2018, sourceType: 'module' },
4+
overrides: [
5+
{
6+
files: ['*.ts'],
7+
parser: '@typescript-eslint/parser',
8+
extends: ['plugin:@typescript-eslint/recommended', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
9+
rules: {
10+
'@typescript-eslint/camelcase': 0,
11+
'@typescript-eslint/no-var-requires': 0,
12+
'@typescript-eslint/no-explicit-any': 0,
13+
},
14+
},
15+
],
16+
};

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
*.log
3+
.vscode
4+
dist/
5+
out/

.prettierrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: true,
3+
trailingComma: 'all',
4+
singleQuote: true,
5+
printWidth: 120,
6+
tabWidth: 2,
7+
};

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "nodegui-qt-installer",
3+
"version": "1.0.0",
4+
"description": "A minimal qt installer for nodegui",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"start": "node dist/index.js",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"author": "atulanand94@gmail.com",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"@types/fs-extra": "^8.0.1",
15+
"@types/node-fetch": "^2.5.4",
16+
"@types/progress": "^2.0.3",
17+
"@typescript-eslint/eslint-plugin": "^2.19.0",
18+
"@typescript-eslint/parser": "^2.19.0",
19+
"eslint": "^6.8.0",
20+
"eslint-config-prettier": "^6.10.0",
21+
"eslint-plugin-prettier": "^3.1.2",
22+
"prettier": "^1.19.1",
23+
"typescript": "^3.7.5"
24+
},
25+
"dependencies": {
26+
"7zip-min": "^1.1.1",
27+
"fs-extra": "^8.1.0",
28+
"node-fetch": "^2.6.0",
29+
"progress": "^2.0.3"
30+
}
31+
}

src/index.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import fetch from 'node-fetch';
2+
import util from 'util';
3+
import fs from 'fs';
4+
import path from 'path';
5+
import stream from 'stream';
6+
import { mkdirp } from 'fs-extra';
7+
import Progress from 'progress';
8+
9+
const streamPipeline = util.promisify(stream.pipeline);
10+
11+
function progressBar(tokens: string, total: number): stream.PassThrough {
12+
const pt = new stream.PassThrough();
13+
const bar = new Progress(tokens, { total });
14+
pt.on('pipe', () => {
15+
pt.on('data', chunk => bar.tick(chunk.length));
16+
});
17+
return pt;
18+
}
19+
20+
const macOsLink =
21+
'https://download.qt.io/online/qtsdkrepository/mac_x64/desktop/qt5_5130/qt.qt5.5130.clang_64/5.13.0-0-201906171525qtbase-MacOS-MacOS_10_13-Clang-MacOS-MacOS_10_13-X86_64.7z';
22+
23+
type DownloadOptions = {
24+
name?: string;
25+
};
26+
async function downloadArchive(archiveLink: string, outPath: string, options: DownloadOptions = {}): Promise<void> {
27+
const name = options.name || '';
28+
const response = await fetch(archiveLink);
29+
if (!response.ok) {
30+
throw new Error(`Error while downloading ${name}:${archiveLink}. ${response.statusText}`);
31+
}
32+
await mkdirp(path.dirname(outPath));
33+
const total = parseInt(`${response.headers.get('content-length')}`, 10);
34+
const totalInMb = (total / 1024 / 1024).toFixed(2);
35+
await streamPipeline(
36+
response.body,
37+
progressBar(`Downloading ${name} [:bar] :percent of ${totalInMb}MB :etas`, total),
38+
fs.createWriteStream(outPath),
39+
);
40+
}
41+
42+
const main = async (): Promise<void> => {
43+
await downloadArchive(macOsLink, './out/download.7zip', { name: 'Qt for Mac' });
44+
};
45+
46+
main()
47+
.then(console.log)
48+
.catch(console.error);

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"allowJs": true,
6+
"checkJs": true,
7+
"declaration": true,
8+
"removeComments": true,
9+
"strict": true,
10+
"allowSyntheticDefaultImports": true,
11+
"esModuleInterop": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"outDir": "./dist",
14+
"moduleResolution": "node",
15+
"resolveJsonModule": true
16+
},
17+
"include": ["src"]
18+
}

0 commit comments

Comments
 (0)