Skip to content

Commit

Permalink
feat(core): install plugins from local tarballs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Oct 9, 2019
1 parent 2d5ea1a commit 8f7d0d3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 9 deletions.
4 changes: 2 additions & 2 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
}
extends: ["@commitlint/config-conventional"],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "yarn lerna clean --yes",
"build": "yarn lerna run build",
"lint": "./node_modules/eslint/bin/eslint.js packages --ext .ts --fix",
"format": "yarn lint:code && yarn prettier",
"format": "yarn lint && yarn prettier",
"prettier": "prettier --write \"./*.{ts,js,json,md}\" \"./packages/**/*.{ts,js,json,md}\" \"./__tests__/**/*.{ts,js,json,md}\"",
"test": "cross-env CORE_ENV=test jest --runInBand --forceExit",
"test:coverage": "cross-env CORE_ENV=test jest --coverage --coveragePathIgnorePatterns='/(defaults.ts|index.ts)$' --runInBand --forceExit",
Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/commands/plugin/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { CLIError } from "@oclif/errors";

import { flagsNetwork } from "../../common/flags";
import { parseWithNetwork } from "../../common/parser";
import { Blockchain } from "../../services/plugins/sources/blockchain";
import { Source } from "../../services/plugins/sources/contracts";
import { Git } from "../../services/plugins/sources/git";
import { NPM } from "../../services/plugins/sources/npm";
import { Blockchain, File, Git, NPM, Source } from "../../services/plugins/sources";
import { CommandFlags } from "../../types";

export class InstallCommand extends Command {
Expand All @@ -33,18 +30,28 @@ export class InstallCommand extends Command {
const { args, paths } = await parseWithNetwork(this.parse(InstallCommand));

try {
// Local File
const file: Source = new File(paths);

if (await file.exists(args.package)) {
return file.install(args.package);
}

// Git Repository
const git: Source = new Git(paths);

if (await git.exists(args.package)) {
return git.install(args.package);
}

// NPM Package
const npm: Source = new NPM(paths);

if (await npm.exists(args.package)) {
return npm.install(args.package);
}

// Blockchain Registration
const blockchain: Source = new Blockchain(paths);

if (await blockchain.exists(args.package)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/services/plugins/sources/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Paths } from "env-paths";
import { ensureDirSync } from "fs-extra";

import { Source } from "./contracts";

Expand All @@ -8,6 +9,8 @@ export class Blockchain implements Source {

public constructor(private readonly paths: Paths) {
this.dataPath = `${this.paths.data}/plugins`;

ensureDirSync(this.dataPath);
}

public async exists(value: string): Promise<boolean> {
Expand Down
47 changes: 47 additions & 0 deletions packages/core/src/services/plugins/sources/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Paths } from "env-paths";
import { existsSync } from "fs";
import { ensureDirSync, moveSync, removeSync } from "fs-extra";
import { parse } from "path";
import { extract } from "tar";

import { Source } from "./contracts";

export class File implements Source {
private readonly dataPath: string;

public constructor(private readonly paths: Paths) {
this.dataPath = `${this.paths.data}/plugins`;

ensureDirSync(this.dataPath);
}

public async exists(value: string): Promise<boolean> {
return existsSync(value);
}

public async install(value: string): Promise<void> {
await this.extractPackage(value);

removeSync(value);
}

public async update(value: string): Promise<void> {
await this.install(value);
}

private async extractPackage(file: string): Promise<void> {
removeSync(this.getTargetPath(file));

await extract({
gzip: true,
file,
cwd: this.dataPath,
});

moveSync(`${this.dataPath}/package`, this.getTargetPath(file));
}

private getTargetPath(value: string): string {
return `${this.dataPath}/${parse(value).name.replace(":", "/")}`;
}
}
4 changes: 3 additions & 1 deletion packages/core/src/services/plugins/sources/git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Utils } from "@arkecosystem/core-kernel";
import { Paths } from "env-paths";
import { removeSync } from "fs-extra";
import { ensureDirSync, removeSync } from "fs-extra";
import git from "simple-git/promise";

import { Source } from "./contracts";
Expand All @@ -10,6 +10,8 @@ export class Git implements Source {

public constructor(private readonly paths: Paths) {
this.dataPath = `${this.paths.data}/plugins`;

ensureDirSync(this.dataPath);
}

public async exists(value: string): Promise<boolean> {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/services/plugins/sources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./blockchain";
export * from "./contracts";
export * from "./file";
export * from "./git";
export * from "./npm";
4 changes: 3 additions & 1 deletion packages/core/src/services/plugins/sources/npm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Paths } from "env-paths";
import { createWriteStream } from "fs";
import { moveSync, removeSync } from "fs-extra";
import { ensureDirSync, moveSync, removeSync } from "fs-extra";
import got from "got";
import stream from "stream";
import { extract } from "tar";
Expand All @@ -13,6 +13,8 @@ export class NPM implements Source {

public constructor(private readonly paths: Paths) {
this.dataPath = `${this.paths.data}/plugins`;

ensureDirSync(this.dataPath);
}

public async exists(value: string): Promise<boolean> {
Expand Down

0 comments on commit 8f7d0d3

Please sign in to comment.