Skip to content

Commit

Permalink
add ManifestStaticInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 25, 2024
1 parent 97837a8 commit c2d994a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/plugin/packer/contents-zip/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ManifestInterface } from "../manifest";
import type { ManifestInterface, ManifestStaticInterface } from "../manifest";
import { ManifestV1 } from "../manifest";
import { ZipFile } from "../zip";
import streamBuffers from "stream-buffers";
Expand Down Expand Up @@ -40,24 +40,7 @@ export class ContentsZip extends ZipFile implements ContentsZipInterface {
path: string;
manifest: ManifestV1;
}> {
const fileLists = await this.fileList();
const entries = await this.entries();
const manifestList = fileLists.filter(
(file) => path.basename(file) === "manifest.json",
);
if (manifestList.length === 0) {
throw new Error("The zip file has no manifest.json");
} else if (manifestList.length > 1) {
throw new Error("The zip file has many manifest.json files");
}
const manifestPath = manifestList[0];
const manifestEntry = entries.get(manifestPath);
if (manifestEntry === undefined) {
throw new Error("Failed to find manifest.json from the zip file");
}
const manifestJson = await this.getFileAsString(manifestPath);
const manifest = ManifestV1.parseJson(manifestJson);
return { path: manifestPath, manifest };
return extractManifestFromContentsZip(this, ManifestV1);

Check failure on line 43 in src/plugin/packer/contents-zip/index.ts

View workflow job for this annotation

GitHub Actions / Node.js ubuntu-latest 18.x

Type '{ path: string; manifest: ManifestInterface; }' is not assignable to type '{ path: string; manifest: ManifestV1; }'.

Check failure on line 43 in src/plugin/packer/contents-zip/index.ts

View workflow job for this annotation

GitHub Actions / Prepare npm package

Type '{ path: string; manifest: ManifestInterface; }' is not assignable to type '{ path: string; manifest: ManifestV1; }'.

Check failure on line 43 in src/plugin/packer/contents-zip/index.ts

View workflow job for this annotation

GitHub Actions / Node.js ubuntu-latest 18.x

Type '{ path: string; manifest: ManifestInterface; }' is not assignable to type '{ path: string; manifest: ManifestV1; }'.

Check failure on line 43 in src/plugin/packer/contents-zip/index.ts

View workflow job for this annotation

GitHub Actions / Unit test - Node.js ubuntu-latest 18.x

Type '{ path: string; manifest: ManifestInterface; }' is not assignable to type '{ path: string; manifest: ManifestV1; }'.
}
}

Expand All @@ -83,3 +66,27 @@ const createContentsZip = async (
debug(`plugin.zip: ${size} bytes`);
return output.getContents() as any;
};

const extractManifestFromContentsZip = async (
contentsZip: ContentsZipInterface,
Manifest: ManifestStaticInterface,
) => {
const fileLists = await contentsZip.fileList();
const entries = await contentsZip.entries();
const manifestList = fileLists.filter(
(file) => path.basename(file) === "manifest.json",
);
if (manifestList.length === 0) {
throw new Error("The zip file has no manifest.json");
} else if (manifestList.length > 1) {
throw new Error("The zip file has many manifest.json files");
}
const manifestPath = manifestList[0];
const manifestEntry = entries.get(manifestPath);
if (manifestEntry === undefined) {
throw new Error("Failed to find manifest.json from the zip file");
}
const manifestJson = await contentsZip.getFileAsString(manifestPath);
const manifest = Manifest.parseJson(manifestJson);
return { path: manifestPath, manifest };
};
3 changes: 3 additions & 0 deletions src/plugin/packer/contents-zip/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { finished } from "node:stream/promises";
import type { ContentsZipInterface } from "./index";
import type { Entries } from "../zip";

// rezip() has been only used in web version plugin packer.
// We are keeping this function to support a web version in the future.

/**
* Extract and rezip contents.zip
*/
Expand Down
7 changes: 7 additions & 0 deletions src/plugin/packer/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import fs from "fs";

const debug = _debug("manifest");

export interface ManifestStaticInterface {
parseJson(manifestJson: string): ManifestInterface;
loadJsonFile(jsonFilePath: string): Promise<ManifestInterface>;
}

export interface ManifestInterface {
validate(options?: ValidatorOptions): ReturnType<typeof validate>;
sourceList(): string[];
Expand Down Expand Up @@ -40,6 +45,8 @@ export class ManifestV1 implements ManifestInterface {
}
}

const _ = ManifestV1 satisfies ManifestStaticInterface;

// TODO: These types must be exported from kintone/plugin-manifest-validator
type ValidatorOptions = {
relativePath?: (filePath: string) => boolean;
Expand Down

0 comments on commit c2d994a

Please sign in to comment.