diff --git a/README.md b/README.md index 73c3b89..ec5a013 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,18 @@ If the `branch` provided does not exist, the plugin will error. To automatically In addition, it accepts `changes` which is an array of objects containing a `message` and a `files` object ```javascript -let { Octokit } = require("@octokit/rest"); -Octokit = Octokit.plugin(require("octokit-commit-multiple-files")); +import { Octokit } from "@octokit/rest"; +import { CreateOrUpdateFiles } from "octokit-commit-multiple-files"; -const octokit = new Octokit(); +const OctokitWithPlugin = Octokit.plugin(CreateOrUpdateFiles); + +const octokit = new OctokitWithPlugin(); + +// Define your repository details +const owner = "your-username"; +const repo = "your-repo"; +const branch = "main"; +const createBranch = false; const commits = await octokit.createOrUpdateFiles({ owner, @@ -71,7 +79,7 @@ const commits = await octokit.createOrUpdateFiles({ By default the plugin will append commits to an existing branch. If you want to reset the branch to the state of `base` before adding any changes, set `forkFromBaseBranch: true`: ```javascript -const commits = await octokit.rest.repos.createOrUpdateFiles({ +const commits = await octokit.createOrUpdateFiles({ owner, repo, branch, diff --git a/create-or-update-files.test.ts b/create-or-update-files.test.ts index cb27a76..79ff6b4 100644 --- a/create-or-update-files.test.ts +++ b/create-or-update-files.test.ts @@ -1,5 +1,5 @@ -import CreateOrUpdateMultipleFiles from "."; -import CreateOrUpdateMultipleFilesHandler from "./create-or-update-files"; +import { CreateOrUpdateFiles } from "."; +import { createOrUpdateFiles } from "./create-or-update-files"; const { Octokit } = require("@octokit/rest"); const nock = require("nock"); @@ -7,7 +7,7 @@ nock.disableNetConnect(); const octokit = new Octokit(); function run(body: any) { - return CreateOrUpdateMultipleFilesHandler(octokit, body); + return createOrUpdateFiles(octokit, body); } type RequestStructure = { @@ -512,13 +512,13 @@ test("failure (fileToDelete is missing)", async () => { }); test("Loads plugin", () => { - const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles); + const TestOctokit = Octokit.plugin(CreateOrUpdateFiles); const testOctokit = new TestOctokit(); expect(testOctokit).toHaveProperty("createOrUpdateFiles"); }); test("Does not overwrite other methods", () => { - const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles); + const TestOctokit = Octokit.plugin(CreateOrUpdateFiles); const testOctokit = new TestOctokit(); expect(testOctokit).toHaveProperty("rest.repos.acceptInvitation"); }); diff --git a/create-or-update-files.ts b/create-or-update-files.ts index 0a5383d..07e4d4a 100644 --- a/create-or-update-files.ts +++ b/create-or-update-files.ts @@ -1,20 +1,20 @@ import { Octokit } from "@octokit/rest"; import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods"; -interface FileChange { +export interface FileChange { contents?: string | Buffer; mode?: "100644" | "100755" | "040000" | "160000" | "120000"; type?: "blob" | "tree" | "commit"; } -interface Change { +export interface Change { message: string; files?: Record; filesToDelete?: string[]; ignoreDeletionFailures?: boolean; } -interface Options { +export interface Options { owner: string; repo: string; branch: string; @@ -27,7 +27,7 @@ interface Options { forkFromBaseBranch?: boolean; } -interface CommitResult { +export interface CommitResult { commits: Array< RestEndpointMethodTypes["git"]["createCommit"]["response"]["data"] >; @@ -56,7 +56,7 @@ function isBase64(str: string | Buffer): boolean { ); } -export default function ( +const createOrUpdateFiles = function ( octokit: Octokit | any, opts: Options, ): Promise { @@ -292,7 +292,7 @@ export default function ( return reject(e); } }); -} +}; async function fileExistsInRepo( octokit: Octokit | any, @@ -414,3 +414,5 @@ const chunk = (input: T[], size: number): T[][] => { : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]]; }, []); }; + +export { createOrUpdateFiles }; diff --git a/index.ts b/index.ts index 22894a1..b10eb73 100644 --- a/index.ts +++ b/index.ts @@ -1,9 +1,11 @@ import { Octokit } from "@octokit/rest"; -import createOrUpdateFilesPlugin from "./create-or-update-files"; +import { createOrUpdateFiles, Options } from "./create-or-update-files"; -export default function (octokit: Octokit | any) { +const CreateOrUpdateFiles = function (octokit: Octokit | any) { return { - createOrUpdateFiles: createOrUpdateFilesPlugin.bind(null, octokit), + createOrUpdateFiles: (opts: Options) => createOrUpdateFiles(octokit, opts), }; -} +}; + +export { CreateOrUpdateFiles }; diff --git a/package.json b/package.json index d9d74fe..4f414df 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,14 @@ "description": "Octokit plugin to create/update multiple files at once", "main": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "require": "./dist/index.js", + "import": "./dist/index.js", + "default": "./dist/index.js" + } + }, "files": [ "dist" ],