Skip to content

Commit

Permalink
create plugin core module
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 26, 2024
1 parent ece62b1 commit 96692f4
Show file tree
Hide file tree
Showing 58 changed files with 30 additions and 38 deletions.
Empty file removed src/plugin/core/contents.ts
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import fs from "fs";

import { PrivateKey } from "../../crypto";
import { PrivateKey } from "../index";

const privateKeyPath = path.join(__dirname, "fixtures", "private.ppk");

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type { DriverInterface } from "./interface";
export { LocalFSDriver } from "./fs";
export { LocalFSDriver } from "./local-fs";
export { ZipFileDriver } from "./zip";
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 7 additions & 5 deletions src/plugin/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// import type { ManifestInterface } from "../packer/manifest";

// class Plugin {
// manifest: ManifestInterface;
// }
export { PluginZip } from "./plugin-zip";
export type { PluginZipInterface } from "./plugin-zip";
export { ManifestFactory, ManifestV1, ManifestV2 } from "./manifest";
export type { ManifestInterface } from "./manifest";
export { ZipFileDriver, LocalFSDriver } from "./driver";
export type { DriverInterface } from "./driver";
export { PrivateKey, PublicKey } from "./crypto";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DriverInterface } from "../driver/";
import { LocalFSDriver } from "../driver/";
import type { DriverInterface } from "../driver";
import { LocalFSDriver } from "../driver";
import type { ManifestInterface, ManifestStaticInterface } from "./interface";
import { ManifestV1 } from "./v1";
import { ManifestV2 } from "./v2";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugin/info/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs/promises";
import { PluginZip } from "../packer";
import { PluginZip } from "../core";

export type OutputFormat = "plain" | "json";

Expand Down
42 changes: 17 additions & 25 deletions src/plugin/packer/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import path from "path";
import fs from "fs";
import { promisify } from "util";
import fs from "fs/promises";
import os from "os";
import * as chokidar from "chokidar";
import { mkdirp } from "mkdirp";
import _debug from "debug";
import { logger } from "../../../utils/log";
import { ManifestFactory } from "../manifest";
import { validateFileExists, validateMaxFileSize } from "../manifest/validate";
import type { PluginZipInterface } from "../plugin-zip";
import { PluginZip } from "../plugin-zip";
import { LocalFSDriver } from "../driver";
import { PrivateKey } from "../crypto";
import {
ManifestFactory,
PluginZip,
PrivateKey,
LocalFSDriver,
} from "../../core";
import {
validateFileExists,
validateMaxFileSize,
} from "../../core/manifest/validate";

const debug = _debug("cli");
const writeFile = promisify(fs.writeFile);

type Options = Partial<{
ppk: string;
Expand All @@ -29,13 +31,13 @@ export const run = async (pluginDir: string, options_?: Options) => {

try {
// 1. check if pluginDir is a directory
if (!fs.statSync(pluginDir).isDirectory()) {
if (!(await fs.stat(pluginDir)).isDirectory()) {
throw new Error(`${pluginDir} should be a directory.`);
}

// 2. check pluginDir/manifest.json
const manifestJsonPath = path.join(pluginDir, "manifest.json");
if (!fs.statSync(manifestJsonPath).isFile()) {
if (!(await fs.stat(manifestJsonPath)).isFile()) {
throw new Error("Manifest file $PLUGIN_DIR/manifest.json not found.");
}

Expand Down Expand Up @@ -78,7 +80,7 @@ export const run = async (pluginDir: string, options_?: Options) => {
let privateKey: PrivateKey;
if (ppkFile) {
debug(`loading an existing key: ${ppkFile}`);
const ppk = fs.readFileSync(ppkFile, "utf8");
const ppk = await fs.readFile(ppkFile, "utf8");
privateKey = PrivateKey.importKey(ppk);
} else {
debug("generating a new key");
Expand All @@ -98,7 +100,7 @@ export const run = async (pluginDir: string, options_?: Options) => {

const ppkFilePath = path.join(outputDir, `${id}.ppk`);
if (!ppkFile) {
fs.writeFileSync(ppkFilePath, privateKey.exportPrivateKey(), "utf8");
await fs.writeFile(ppkFilePath, privateKey.exportPrivateKey(), "utf8");
}

if (options.watch) {
Expand All @@ -125,7 +127,8 @@ export const run = async (pluginDir: string, options_?: Options) => {
);
});
}
await outputPlugin(outputFile, pluginZip);

await fs.writeFile(outputFile, pluginZip.buffer);

logger.info(`Succeeded: ${outputFile}`);
return outputFile;
Expand All @@ -134,14 +137,3 @@ export const run = async (pluginDir: string, options_?: Options) => {
return Promise.reject(error);
}
};

/**
* Create and save plugin.zip
*/
const outputPlugin = async (
outputPath: string,
plugin: PluginZipInterface,
): Promise<string> => {
await writeFile(outputPath, plugin.buffer);
return outputPath;
};
4 changes: 1 addition & 3 deletions src/plugin/packer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export { run } from "./cli";
export { PluginZip } from "./plugin-zip";
export { ManifestFactory, ManifestV1, ManifestV2 } from "./manifest";
export { run } from "../packer/cli";

0 comments on commit 96692f4

Please sign in to comment.