Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 77c1a5e

Browse files
authored
Merge pull request #207 from NodeSecure/prepublish-enhancement
Prepublish enhancement
2 parents ce3e6b4 + a235532 commit 77c1a5e

File tree

5 files changed

+135
-102
lines changed

5 files changed

+135
-102
lines changed

README.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ $ yarn add @nodesecure/gitlab
2929
import * as gitlab from "@nodesecure/gitlab";
3030

3131
// Note: repository can be either namespace path or repository ID
32-
const is = await gitlab.download("NodeSecure.utils");
33-
console.log(is.location);
32+
const result = await gitlab.download(
33+
"NodeSecure.utils"
34+
);
35+
console.log(result);
3436
```
3537

3638
## API
3739

40+
Both `download` and `downloadAndExtract` functions use the same set of options.
41+
3842
```ts
39-
export interface DownloadOptions {
43+
interface DownloadOptions {
4044
/**
4145
* The destination (location) to extract the tar.gz
4246
*
@@ -61,8 +65,13 @@ export interface DownloadOptions {
6165
*/
6266
gitlab?: string;
6367
}
68+
```
6469

65-
export interface DownloadResult {
70+
### download(repository: string, options?: DownloadOptions): Promise< DownloadResult >
71+
Download the tar.gz archive of the GIT repository.
72+
73+
```ts
74+
interface DownloadResult {
6675
/** Archive or repository location on disk */
6776
location: string;
6877
/** Gitlab repository name */
@@ -72,35 +81,20 @@ export interface DownloadResult {
7281
/** Gitlab branch name */
7382
branch: string;
7483
}
84+
```
7585

76-
export function download(
77-
repo: string,
78-
options?: DownloadOptions
79-
): Promise<DownloadResult>;
86+
### downloadAndExtract(repository: string, options?: DownloadExtractOptions): Promise< DownloadResult >
87+
Use download but extract the tar.gz archive.
8088

81-
export interface DownloadExtractOptions extends DownloadOptions {
89+
```ts
90+
interface DownloadExtractOptions extends DownloadOptions {
8291
/**
8392
* Remove the tar.gz archive after a succesfull extraction
8493
*
8594
* @default true
8695
*/
8796
removeArchive?: boolean;
8897
}
89-
90-
export function downloadAndExtract(
91-
repo: string,
92-
options?: DownloadExtractOptions
93-
): Promise<DownloadResult>;
94-
```
95-
96-
### Custom gitlab URL
97-
98-
To work with a custom gitlab instance you can either setup a `GITLAB_URL` system variable or use `setUrl` method:
99-
100-
```js
101-
import * as gitlab from "@nodesecure/gitlab";
102-
103-
gitlab.setUrl("...");
10498
```
10599

106100
## Contributors ✨

test/functions/download.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Import Node.js Dependencies
2+
import path from "node:path";
3+
import fs from "node:fs";
4+
import assert from "node:assert";
5+
import { describe, test } from "node:test";
6+
7+
// Import Third-party Dependencies
8+
import is from "@slimio/is";
9+
10+
// Import Internal Dependency
11+
import * as gitlab from "../../src/index.js";
12+
13+
describe("download", () => {
14+
test("should be an asyncFunction", () => {
15+
assert.ok(is.func(gitlab.download));
16+
assert.ok(is.asyncFunction(gitlab.download));
17+
});
18+
19+
test("must throw: repository must be a string!", async() => {
20+
await assert.rejects(
21+
async() => await gitlab.download(10 as any),
22+
{
23+
name: "TypeError",
24+
message: "repository must be a string!"
25+
}
26+
);
27+
});
28+
29+
test("extract tar.gz at in the current working dir", async() => {
30+
try {
31+
const { location, ...resultRest } = await gitlab.download("polychromatic.plombier-chauffagiste");
32+
33+
fs.accessSync(location);
34+
35+
assert.strictEqual(path.extname(location), ".gz");
36+
assert.deepEqual(resultRest, {
37+
branch: "master",
38+
organization: "polychromatic",
39+
repository: "plombier-chauffagiste"
40+
});
41+
}
42+
finally {
43+
const tarGzLocation = path.join(process.cwd(), "plombier-chauffagiste-master.tar.gz");
44+
if (fs.existsSync(tarGzLocation)) {
45+
fs.unlinkSync(tarGzLocation);
46+
}
47+
}
48+
});
49+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Import Node.js Dependencies
2+
import os from "node:os";
3+
import path from "node:path";
4+
import fs from "node:fs";
5+
import assert from "node:assert";
6+
import { describe, test, it, beforeEach, afterEach } from "node:test";
7+
8+
// Import Third-party Dependencies
9+
import is from "@slimio/is";
10+
11+
// Import Internal Dependency
12+
import * as gitlab from "../../src/index.js";
13+
14+
describe("downloadAndExtract", () => {
15+
let tempDownloadDir: string;
16+
17+
beforeEach(() => {
18+
tempDownloadDir = fs.mkdtempSync(
19+
path.join(os.tmpdir(), "nsecure-gitlab-")
20+
);
21+
});
22+
23+
afterEach(() => {
24+
fs.rmSync(tempDownloadDir, { recursive: true, force: true });
25+
});
26+
27+
test("gitlab.downloadAndExtract should be an asyncFunction", () => {
28+
assert.ok(is.func(gitlab.downloadAndExtract));
29+
assert.ok(is.asyncFunction(gitlab.downloadAndExtract));
30+
});
31+
32+
it("should download and extract the given gitlab repository (and not delete the tar.gz archive)", async() => {
33+
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
34+
dest: tempDownloadDir,
35+
removeArchive: false
36+
});
37+
38+
assert.ok(
39+
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz"))
40+
);
41+
assert.doesNotThrow(
42+
() => fs.accessSync(location)
43+
);
44+
});
45+
46+
it("should download and extract the given gitlab repository (and delete the tar.gz archive)", async() => {
47+
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
48+
dest: tempDownloadDir,
49+
removeArchive: true
50+
});
51+
52+
assert.strictEqual(
53+
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz")),
54+
false
55+
);
56+
assert.doesNotThrow(
57+
() => fs.accessSync(location)
58+
);
59+
});
60+
});

test/test.spec.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

test/utils.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Import Node.js Dependencies
2-
import { test } from "node:test";
2+
import { describe, test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependency
66
import { getRepositoryPath } from "../src/utils.js";
77

8-
test("getRepositoryPath must return id", () => {
9-
assert.equal(getRepositoryPath("10"), "10");
10-
});
8+
describe("getRepositoryPath", () => {
9+
test("must return id", () => {
10+
assert.strictEqual(getRepositoryPath("10"), "10");
11+
});
1112

12-
test("getRepositoryPath must return gitlab path", () => {
13-
assert.equal(getRepositoryPath("nodesecure.boo"), "nodesecure%2Fboo");
13+
test("must return GitLab path (encoded for URL)", () => {
14+
assert.strictEqual(getRepositoryPath("nodesecure.boo"), "nodesecure%2Fboo");
15+
});
1416
});

0 commit comments

Comments
 (0)