Skip to content

Commit

Permalink
feat: add abstract-cli as a dependency
Browse files Browse the repository at this point in the history
BREAKING CHANGE: cliPath is no longer configurable
  • Loading branch information
bitpshr committed Aug 19, 2019
1 parent 6c2d9db commit 65911c4
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 77 deletions.
3 changes: 0 additions & 3 deletions abstract-sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ interface ErrorData {

export class APITokenError extends BaseError {}
export class BaseError extends Error {}
export class CLIPathError extends BaseError {}
export class FileAPIError extends BaseError {}

export class EndpointUndefinedError extends BaseError {
Expand Down Expand Up @@ -96,7 +95,6 @@ interface EndpointRequest<T> {

interface Endpoint {
apiUrl: string | Promise<string>;
cliPath?: string | Promise<string>;
client: Client;
lastCalledEndpoint?: string;
maxCacheSize: number;
Expand Down Expand Up @@ -1717,7 +1715,6 @@ type AccessTokenOption =
type CommandOptions = {
accessToken: AccessTokenOption,
apiUrl: string | Promise<string>,
cliPath: string | Promise<string>,
maxCacheSize: number,
previewsUrl: string | Promise<string>,
transportMode: "auto" | "api" | "cli",
Expand Down
6 changes: 0 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ _Default value: `https://api.goabstract.com`_

This option can be used to specify a custom URL that points to an instance of the Abstract HTTP API. This is used when building the request URLs used by the [API transport](/docs/transports). This option is useful if HTTP requests should be routed locally or through a proxy server.

### `cliPath`

_Default value: `/Applications/Abstract.app/Contents/Resources/app.asar.unpacked/node_modules/@elasticprojects/abstract-cli/bin/abstract-cli`_

This option can be used to specify a custom file location that points to the Abstract CLI included with the Abstract desktop application. This option is useful if the Abstract desktop application exists at a non-default application path outside of `/Applications`.

### `maxCacheSize`

_Default value: `0`_
Expand Down
1 change: 0 additions & 1 deletion docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ The Abstract API is a transport mode that uses HTTP to interact with Abstract's

The Abstract CLI is a transport mode that uses the Abstract desktop application to interact with data stored locally on a given machine. This transport does not require internet connectivity or authentication, and requests are not subject to rate limits. This transport does require access to the Abstract desktop application and its underlying data, as such it can only be used on macOS currently.


## Auto

The SDK provides an "auto" transport that can be used in environments where both the API and the CLI transports are available. Any time an SDK method is called, the auto transport will first check the CLI, then the API, for an available implementation. This allows transport-specific SDK methods to be called without worrying about which transport is being used. This transport also helps to reduce rate limit errors since the CLI transport will be favored in most cases.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"dependencies": {
"@babel/runtime-corejs2": "^7.3.4",
"@elasticprojects/abstract-cli": "^2.3.0",
"cross-fetch": "^3.0.1",
"debug": "^4.0.1",
"flow-typed": "^2.5.1",
Expand Down
2 changes: 0 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export default class Client {
options = {
accessToken: process.env.ABSTRACT_TOKEN,
apiUrl: "https://api.goabstract.com",
cliPath:
"/Applications/Abstract.app/Contents/Resources/app.asar.unpacked/node_modules/@elasticprojects/abstract-cli/bin/abstract-cli",
maxCacheSize: 0,
previewsUrl: "https://previews.goabstract.com",
transportMode: "api",
Expand Down
30 changes: 9 additions & 21 deletions src/endpoints/Commits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,15 @@ describe("#list", () => {
});

test("cli - file id", async () => {
mockCLI(
[
"commits",
"project-id",
"branch-id",
"--file-id",
"file-id",
"--limit",
"10"
],
{ commits: [] }
);
const response = await CLI_CLIENT.commits.list(
{
projectId: "project-id",
branchId: "branch-id",
fileId: "file-id",
sha: "sha"
},
{ limit: 10 }
);
mockCLI(["commits", "project-id", "branch-id", "--file-id", "file-id"], {
commits: []
});
const response = await CLI_CLIENT.commits.list({
projectId: "project-id",
branchId: "branch-id",
fileId: "file-id",
sha: "sha"
});

expect(response).toEqual([]);
});
Expand Down
19 changes: 5 additions & 14 deletions src/endpoints/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* global fetch */
/* istanbul ignore file */
import "cross-fetch/polyfill";
import { existsSync } from "fs";
import { join } from "path";
import { spawn } from "child_process";
import uuid from "uuid/v4";
import Client from "../Client";
Expand All @@ -11,10 +11,8 @@ import { log } from "../debug";
import { version } from "../../package.json";
import {
APITokenError,
CLIPathError,
EndpointUndefinedError,
logAPIError,
logCLIError,
throwAPIError,
throwCLIError
} from "../errors";
Expand All @@ -40,7 +38,6 @@ export type EndpointRequest<T> = {
export default class Endpoint {
_optionAccessToken: ?AccessTokenOption;
apiUrl: string | Promise<string>;
cliPath: ?string | Promise<?string>;
client: Client;
lastCalledEndpoint: ?string;
maxCacheSize: number;
Expand All @@ -56,7 +53,6 @@ export default class Endpoint {
constructor(client: Client, options: CommandOptions) {
this._optionAccessToken = options.accessToken;
this.apiUrl = options.apiUrl;
this.cliPath = options.cliPath;
this.client = client;
this.maxCacheSize = options.maxCacheSize;
this.previewsUrl = options.previewsUrl;
Expand Down Expand Up @@ -137,16 +133,11 @@ export default class Endpoint {

async cliRequest(args: string[]) {
const token = await this.accessToken();
const cliPath = await this.cliPath;
const cliPath = join(
__dirname,
"../../node_modules/@elasticprojects/abstract-cli/bin/abstract-cli"
);
const tokenArgs = typeof token === "string" ? ["--user-token", token] : [];

if (!cliPath || !existsSync(cliPath)) {
const error = new CLIPathError();
/* istanbul ignore next */
logCLIError.enabled && logCLIError(error);
throw error;
}

const spawnArgs = [
cliPath,
[...tokenArgs, "--api-url", await this.apiUrl, ...args]
Expand Down
6 changes: 0 additions & 6 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ export class BaseError extends Error {
}
}

export class CLIPathError extends BaseError {
constructor() {
super("Cannot find abstract-cli.");
}
}

export class FileAPIError extends BaseError {
constructor() {
super(
Expand Down
19 changes: 0 additions & 19 deletions src/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CLI_CLIENT } from "./testing";
import Client from "./Client";
import {
APITokenError,
CLIPathError,
EndpointUndefinedError,
FileAPIError,
ForbiddenError,
Expand Down Expand Up @@ -35,24 +34,6 @@ describe("errors", () => {
global.Blob = globalBlob;
});

test("CLIPathError", async () => {
const client = new Client({
cliPath: "./nonexistent",
transportMode: "cli"
});

try {
await client.commits.list({
projectId: "project-id",
branchId: "branch-id",
fileId: "file-id",
sha: "sha"
});
} catch (error) {
expect(error).toBeInstanceOf(CLIPathError);
}
});

test("FileAPIError", async () => {
try {
await CLI_CLIENT.previews.url({
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as sketch from "./sketch";
import {
APITokenError,
BaseError,
CLIPathError,
EndpointUndefinedError,
FileAPIError,
ForbiddenError,
Expand All @@ -23,7 +22,6 @@ export {
// Custom errors
APITokenError,
BaseError,
CLIPathError,
EndpointUndefinedError,
FileAPIError,
ForbiddenError,
Expand Down
2 changes: 0 additions & 2 deletions src/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const API_CLIENT = new Client({
export const CLI_CLIENT = new Client({
accessToken: "accessToken",
apiUrl: "http://apiUrl",
cliPath: ".",
previewsUrl: "http://previewsUrl",
transportMode: "cli"
});
Expand All @@ -47,7 +46,6 @@ export const API_CLIENT_CACHED = new Client({
export const CLI_CLIENT_CACHED = new Client({
accessToken: "accessToken",
apiUrl: "http://apiUrl",
cliPath: ".",
maxCacheSize: 10,
previewsUrl: "http://previewsUrl",
transportMode: "cli"
Expand Down
1 change: 0 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,6 @@ export type AccessTokenOption =
export type CommandOptions = {
accessToken: AccessTokenOption,
apiUrl: string | Promise<string>,
cliPath: string | Promise<string>,
maxCacheSize: number,
previewsUrl: string | Promise<string>,
transportMode: "auto" | "api" | "cli",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@
dependencies:
find-up "^2.1.0"

"@elasticprojects/abstract-cli@^2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@elasticprojects/abstract-cli/-/abstract-cli-2.3.0.tgz#c52879563bff4a4f3fae5e96349cba072b72d3bc"
integrity sha512-gBjYGBVPZgkRKyjpCVsYoYxt8/woQ7NDnP26NEFye8ZzxIgbcVwEufPRLazHKRzp6MUWB1Vo8Inbm1CsWQuyjw==

"@elasticprojects/eslint-config-abstract@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@elasticprojects/eslint-config-abstract/-/eslint-config-abstract-4.1.0.tgz#879707dde84d05126c7eb452eda51def433c5e25"
Expand Down

0 comments on commit 65911c4

Please sign in to comment.