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

Prepublish enhancement #207

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 18 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ $ yarn add @nodesecure/gitlab
import * as gitlab from "@nodesecure/gitlab";

// Note: repository can be either namespace path or repository ID
const is = await gitlab.download("NodeSecure.utils");
console.log(is.location);
const result = await gitlab.download(
"NodeSecure.utils"
);
console.log(result);
```

## API

Both `download` and `downloadAndExtract` functions use the same set of options.

```ts
export interface DownloadOptions {
interface DownloadOptions {
/**
* The destination (location) to extract the tar.gz
*
Expand All @@ -61,8 +65,13 @@ export interface DownloadOptions {
*/
gitlab?: string;
}
```

export interface DownloadResult {
### download(repository: string, options?: DownloadOptions): Promise< DownloadResult >
Download the tar.gz archive of the GIT repository.

```ts
interface DownloadResult {
/** Archive or repository location on disk */
location: string;
/** Gitlab repository name */
Expand All @@ -72,35 +81,20 @@ export interface DownloadResult {
/** Gitlab branch name */
branch: string;
}
```

export function download(
repo: string,
options?: DownloadOptions
): Promise<DownloadResult>;
### downloadAndExtract(repository: string, options?: DownloadExtractOptions): Promise< DownloadResult >
Use download but extract the tar.gz archive.

export interface DownloadExtractOptions extends DownloadOptions {
```ts
interface DownloadExtractOptions extends DownloadOptions {
/**
* Remove the tar.gz archive after a succesfull extraction
*
* @default true
*/
removeArchive?: boolean;
}

export function downloadAndExtract(
repo: string,
options?: DownloadExtractOptions
): Promise<DownloadResult>;
```

### Custom gitlab URL

To work with a custom gitlab instance you can either setup a `GITLAB_URL` system variable or use `setUrl` method:

```js
import * as gitlab from "@nodesecure/gitlab";

gitlab.setUrl("...");
```

## Contributors ✨
Expand Down
49 changes: 49 additions & 0 deletions test/functions/download.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Import Node.js Dependencies
import path from "node:path";
import fs from "node:fs";
import assert from "node:assert";
import { describe, test } from "node:test";

// Import Third-party Dependencies
import is from "@slimio/is";

// Import Internal Dependency
import * as gitlab from "../../src/index.js";

describe("download", () => {
test("should be an asyncFunction", () => {
assert.ok(is.func(gitlab.download));
assert.ok(is.asyncFunction(gitlab.download));
});

test("must throw: repository must be a string!", async() => {
await assert.rejects(
async() => await gitlab.download(10 as any),
{
name: "TypeError",
message: "repository must be a string!"
}
);
});

test("extract tar.gz at in the current working dir", async() => {
try {
const { location, ...resultRest } = await gitlab.download("polychromatic.plombier-chauffagiste");

fs.accessSync(location);

assert.strictEqual(path.extname(location), ".gz");
assert.deepEqual(resultRest, {
branch: "master",
organization: "polychromatic",
repository: "plombier-chauffagiste"
});
}
finally {
const tarGzLocation = path.join(process.cwd(), "plombier-chauffagiste-master.tar.gz");
if (fs.existsSync(tarGzLocation)) {
fs.unlinkSync(tarGzLocation);
}
}
});
});
60 changes: 60 additions & 0 deletions test/functions/downloadAndExtract.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Import Node.js Dependencies
import os from "node:os";
import path from "node:path";
import fs from "node:fs";
import assert from "node:assert";
import { describe, test, it, beforeEach, afterEach } from "node:test";

// Import Third-party Dependencies
import is from "@slimio/is";

// Import Internal Dependency
import * as gitlab from "../../src/index.js";

describe("downloadAndExtract", () => {
let tempDownloadDir: string;

beforeEach(() => {
tempDownloadDir = fs.mkdtempSync(
path.join(os.tmpdir(), "nsecure-gitlab-")
);
});

afterEach(() => {
fs.rmSync(tempDownloadDir, { recursive: true, force: true });
});

test("gitlab.downloadAndExtract should be an asyncFunction", () => {
assert.ok(is.func(gitlab.downloadAndExtract));
assert.ok(is.asyncFunction(gitlab.downloadAndExtract));
});

it("should download and extract the given gitlab repository (and not delete the tar.gz archive)", async() => {
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
dest: tempDownloadDir,
removeArchive: false
});

assert.ok(
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz"))
);
assert.doesNotThrow(
() => fs.accessSync(location)
);
});

it("should download and extract the given gitlab repository (and delete the tar.gz archive)", async() => {
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
dest: tempDownloadDir,
removeArchive: true
});

assert.strictEqual(
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz")),
false
);
assert.doesNotThrow(
() => fs.accessSync(location)
);
});
});
72 changes: 0 additions & 72 deletions test/test.spec.ts

This file was deleted.

14 changes: 8 additions & 6 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Import Node.js Dependencies
import { test } from "node:test";
import { describe, test } from "node:test";
import assert from "node:assert";

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

test("getRepositoryPath must return id", () => {
assert.equal(getRepositoryPath("10"), "10");
});
describe("getRepositoryPath", () => {
test("must return id", () => {
assert.strictEqual(getRepositoryPath("10"), "10");
});

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