8000 downloadTarball · avdim/github-script@4aadf9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4aadf9a

Browse files
committed
downloadTarball
1 parent b854be2 commit 4aadf9a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
!/.vscode/
33
.idea
44
token.txt
5+
.tmp

__test__/npm-api.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as path from "path";
2-
import {addUser, createEmptyNpmRegistry, getModuleInfo, publishAsync} from "../src/npm-api";
2+
import {addUser, createEmptyNpmRegistry, downloadTarball, getModuleInfo, publishAsync} from "../src/npm-api";
33

44
const npm = require('../src/npm.js')
55
const REGISTRY = 'http://localhost:55552'
66
const MODULE_NAME = 'test-package'
7+
const MODULE_VERSION = '1.0.1'
78

89
const TGZ_PATH = path.resolve(__dirname, "resources", "test-package-1.0.1.tgz");
910
const AUTH = {
@@ -36,7 +37,9 @@ describe('npm-api', () => {
3637
//https://github.com/postmanlabs/npm-cli-login/blob/master/lib/login.js#L51
3738
await addUser(registryUrl, AUTH)
3839
await publishAsync(registryUrl, TGZ_PATH, MODULE_NAME, AUTH)
39-
expect(await getModuleInfo(registryUrl, MODULE_NAME, "1.0.1", AUTH) != undefined).toEqual(true)
40+
const moduleInfo:any = await getModuleInfo(registryUrl, MODULE_NAME, MODULE_VERSION, AUTH);
41+
expect(moduleInfo != undefined).toEqual(true)
42+
const tmpTgz = await downloadTarball(registryUrl, MODULE_NAME, MODULE_VERSION, AUTH);
4043
} finally {
4144
startedContainer.stop()
4245
}

src/npm-api.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as fs from "fs";
2+
import * as http from "http";
23
import {GenericContainer, StartedTestContainer} from "testcontainers";
4+
import * as path from "path";
35

46
const RegClient = require('npm-registry-client');
57
const DEFAULT_NPM_PORT = 4873
@@ -49,6 +51,25 @@ export async function publishAsync(registryUrl: string, tgzPath: string, moduleN
4951
});
5052
}
5153

54+
export async function downloadTarball(registryUrl: string, moduleName: String, version: string, auth: any): Promise<string> {
55+
let moduleInfo:any = await getModuleInfo(registryUrl, moduleName, version, auth);
56+
const tarballUrl = moduleInfo.dist.tarball as string;
57+
const tempTgzPath: string = path.join(__dirname, `../.tmp/tmp-${Math.random()}.tgz`)
58+
await new Promise(resolve => {
59+
fs.mkdir(path.join(__dirname, `../.tmp/`), () => {
60+
resolve()
61+
})
62+
})
63+
const file = fs.createWriteStream(tempTgzPath);
64+
return await new Promise<string>(resolve => {
65+
const request = http.get(tarballUrl, function(response) {
66+
response.pipe(file);
67+
resolve(tempTgzPath)
68+
});
69+
})
70+
71+
}
72+
5273
export async function getModuleInfo(registryUrl: string, moduleName: String, version: string, auth: any): Promise<unknown> {
5374
let regInfo: any = await new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
5475
const regClient = new RegClient();

0 commit comments

Comments
 (0)
0