Skip to content

Commit

Permalink
Merge pull request #144 from NodeSecure/prepublish-enhancement
Browse files Browse the repository at this point in the history
Prepublish enhancement
  • Loading branch information
fraxken authored Aug 15, 2024
2 parents f51b342 + 666111f commit bf7fbcc
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 146 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.REPO_SECRET }}
run: npm run test
automerge:
if: >
github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'
needs:
- test
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Merge Dependabot PR
uses: fastify/github-action-merge-dependabot@9e7bfb249c69139d7bdcd8d984f9665edd49020b # v3.10.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ console.log(scanner.location);

## 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 @@ -58,34 +60,36 @@ export interface DownloadOptions {
*/
token?: 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;
/** Github repository name */
repository: string;
/** Github organization name */
organization: string;
/** Github 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>;
```

## Contributors ✨
Expand Down
7 changes: 5 additions & 2 deletions src/functions/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface DownloadResult {
repository: string;
/** Github organization name */
organization: string;
/** Github branch name */
branch: string;
}

/**
Expand Down Expand Up @@ -67,7 +69,7 @@ export async function download(
headers: {
"User-Agent": "NodeSecure",
"Accept-Encoding": "gzip, deflate",
Authorization: `token ${token}`
Authorization: typeof token === "string" ? `token ${token}` : void 0
},
maxRedirections: 1
});
Expand All @@ -76,6 +78,7 @@ export async function download(
return {
location,
organization,
repository: repo
repository: repo,
branch
};
}
79 changes: 79 additions & 0 deletions test/download.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Import Node.js Dependencies
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import assert from "node:assert";
import { describe, after, test, before } from "node:test";

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

// Import Internal Dependencies
import * as github from "../src/index.js";

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

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

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

test("should export as an asyncFunction", () => {
assert.ok(is.func(github.download));
assert.ok(is.asyncFunction(github.download));
});

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

test("download public repository (without extraction)", async() => {
const { location, repository, organization } = await github.download("SlimIO.Config", {
dest: tempDownloadDir,
branch: "master"
});
assert.strictEqual(repository, "Config");
assert.strictEqual(organization, "SlimIO");
assert.strictEqual(location, path.join(tempDownloadDir, "Config-master.tar.gz"));

fs.accessSync(location);
fs.unlinkSync(location);
});

test("download public repository (at current working dir)", async() => {
const { location } = await github.download("NodeSecure.utils");
assert.strictEqual(
location,
path.join(process.cwd(), "utils-main.tar.gz")
);

fs.accessSync(location);
fs.unlinkSync(location);
});

test("download private repository (without extraction)", async() => {
const { location } = await github.download("SlimIO.Core", {
dest: tempDownloadDir,
branch: "master"
});
assert.strictEqual(
location,
path.join(tempDownloadDir, "Core-master.tar.gz")
);

fs.accessSync(location);
fs.unlinkSync(location);
});
});
69 changes: 69 additions & 0 deletions test/downloadAndExtract.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Import Node.js Dependencies
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import assert from "node:assert";
import { describe, after, test, before } from "node:test";

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

// Import Internal Dependencies
import * as github from "../src/index.js";

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

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

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

test("should export as an asyncFunction", () => {
assert.ok(is.func(github.downloadAndExtract));
assert.ok(is.asyncFunction(github.downloadAndExtract));
});

test("download public repository (with extraction)", async() => {
const result = await github.downloadAndExtract("SlimIO.is", {
dest: tempDownloadDir,
branch: "master"
});

assert.strictEqual(
result.location,
path.join(tempDownloadDir, "is-master")
);
assert.ok(fs.statSync(result.location).isDirectory());
assert.throws(
() => fs.accessSync(path.join(tempDownloadDir, "is-master.tar.gz")),
{
name: "Error",
code: "ENOENT",
message: /no such file or directory/
}
);
});

test("download public repository (with extraction and removeArchive disabled)", async() => {
const { location } = await github.downloadAndExtract("SlimIO.Safe-emitter", {
dest: tempDownloadDir,
branch: "master",
removeArchive: false
});

fs.accessSync(
path.join(tempDownloadDir, "Safe-emitter-master.tar.gz")
);
assert.strictEqual(
location,
path.join(tempDownloadDir, "Safe-emitter-master")
);
assert.ok(fs.statSync(location).isDirectory());
});
});
132 changes: 0 additions & 132 deletions test/index.spec.ts

This file was deleted.

0 comments on commit bf7fbcc

Please sign in to comment.