From d56df1f806904035ac9d3ef4f29712f459bafcb1 Mon Sep 17 00:00:00 2001 From: Masaharu Tashiro Date: Fri, 25 Oct 2024 06:07:57 +0900 Subject: [PATCH] refactor manifest module --- src/plugin/packer/cli.ts | 98 +++++++++---------- src/plugin/packer/create-contents-zip.ts | 2 +- .../__tests__/sourcelist.test.ts | 0 .../packer/{ => manifest}/gen-error-msg.ts | 0 src/plugin/packer/manifest/index.ts | 17 ++++ .../packer/{ => manifest}/sourcelist.ts | 0 src/plugin/packer/manifest/validate.ts | 64 ++++++++++++ src/plugin/packer/zip.ts | 4 +- 8 files changed, 133 insertions(+), 52 deletions(-) rename src/plugin/packer/{ => manifest}/__tests__/sourcelist.test.ts (100%) rename src/plugin/packer/{ => manifest}/gen-error-msg.ts (100%) create mode 100644 src/plugin/packer/manifest/index.ts rename src/plugin/packer/{ => manifest}/sourcelist.ts (100%) create mode 100644 src/plugin/packer/manifest/validate.ts diff --git a/src/plugin/packer/cli.ts b/src/plugin/packer/cli.ts index 9d79590812..34b0b7b585 100644 --- a/src/plugin/packer/cli.ts +++ b/src/plugin/packer/cli.ts @@ -5,11 +5,10 @@ import os from "os"; import * as chokidar from "chokidar"; import { mkdirp } from "mkdirp"; import _debug from "debug"; -import validate from "@kintone/plugin-manifest-validator"; import packer from "./index"; -import { generateErrorMessages } from "./gen-error-msg"; import { createContentsZip } from "./create-contents-zip"; import { logger } from "../../utils/log"; +import { validateManifestJson } from "./manifest/validate"; const debug = _debug("cli"); const writeFile = promisify(fs.writeFile); @@ -40,7 +39,8 @@ const cli = (pluginDir: string, options_?: Options) => { // 3. validate manifest.json const manifest = loadJson(manifestJsonPath); - throwIfInvalidManifest(manifest, pluginDir); + // throwIfInvalidManifest(manifest, pluginDir); + validateManifestJson(JSON.stringify(manifest), pluginDir); let outputDir = path.dirname(path.resolve(pluginDir)); let outputFile = path.join(outputDir, "plugin.zip"); @@ -111,28 +111,28 @@ const cli = (pluginDir: string, options_?: Options) => { export = cli; -const throwIfInvalidManifest = (manifest: any, pluginDir: string) => { - const result = validate(manifest, { - maxFileSize: validateMaxFileSize(pluginDir), - fileExists: validateFileExists(pluginDir), - }); - debug(result); - - if (result.warnings && result.warnings.length > 0) { - result.warnings.forEach((warning) => { - logger.warn(warning.message); - }); - } - - if (!result.valid) { - const msgs = generateErrorMessages(result.errors ?? []); - logger.error("Invalid manifest.json:"); - msgs.forEach((msg) => { - logger.error(`- ${msg}`); - }); - throw new Error("Invalid manifest.json"); - } -}; +// const throwIfInvalidManifest = (manifest: any, pluginDir: string) => { +// const result = validate(manifest, { +// maxFileSize: validateMaxFileSize(pluginDir), +// fileExists: validateFileExists(pluginDir), +// }); +// debug(result); +// +// if (result.warnings && result.warnings.length > 0) { +// result.warnings.forEach((warning) => { +// logger.warn(warning.message); +// }); +// } +// +// if (!result.valid) { +// const msgs = generateErrorMessages(result.errors ?? []); +// logger.error("Invalid manifest.json:"); +// msgs.forEach((msg) => { +// logger.error(`- ${msg}`); +// }); +// throw new Error("Invalid manifest.json"); +// } +// }; /** * Create and save plugin.zip @@ -149,27 +149,27 @@ const loadJson = (jsonPath: string) => { return JSON.parse(content); }; -/** - * Return validator for `maxFileSize` keyword - */ -const validateMaxFileSize = (pluginDir: string) => { - return (maxBytes: number, filePath: string) => { - try { - const stat = fs.statSync(path.join(pluginDir, filePath)); - return stat.size <= maxBytes; - } catch (_) { - return false; - } - }; -}; - -const validateFileExists = (pluginDir: string) => { - return (filePath: string) => { - try { - const stat = fs.statSync(path.join(pluginDir, filePath)); - return stat.isFile(); - } catch (_) { - return false; - } - }; -}; +// /** +// * Return validator for `maxFileSize` keyword +// */ +// const validateMaxFileSize = (pluginDir: string) => { +// return (maxBytes: number, filePath: string) => { +// try { +// const stat = fs.statSync(path.join(pluginDir, filePath)); +// return stat.size <= maxBytes; +// } catch (_) { +// return false; +// } +// }; +// }; +// +// const validateFileExists = (pluginDir: string) => { +// return (filePath: string) => { +// try { +// const stat = fs.statSync(path.join(pluginDir, filePath)); +// return stat.isFile(); +// } catch (_) { +// return false; +// } +// }; +// }; diff --git a/src/plugin/packer/create-contents-zip.ts b/src/plugin/packer/create-contents-zip.ts index 9390cb7d68..7731aeccca 100644 --- a/src/plugin/packer/create-contents-zip.ts +++ b/src/plugin/packer/create-contents-zip.ts @@ -1,7 +1,7 @@ import path from "path"; import { ZipFile } from "yazl"; import streamBuffers from "stream-buffers"; -import { sourceList } from "./sourcelist"; +import { sourceList } from "./manifest/sourcelist"; import _debug from "debug"; const debug = _debug("create-contents-zip"); diff --git a/src/plugin/packer/__tests__/sourcelist.test.ts b/src/plugin/packer/manifest/__tests__/sourcelist.test.ts similarity index 100% rename from src/plugin/packer/__tests__/sourcelist.test.ts rename to src/plugin/packer/manifest/__tests__/sourcelist.test.ts diff --git a/src/plugin/packer/gen-error-msg.ts b/src/plugin/packer/manifest/gen-error-msg.ts similarity index 100% rename from src/plugin/packer/gen-error-msg.ts rename to src/plugin/packer/manifest/gen-error-msg.ts diff --git a/src/plugin/packer/manifest/index.ts b/src/plugin/packer/manifest/index.ts new file mode 100644 index 0000000000..d8053edf19 --- /dev/null +++ b/src/plugin/packer/manifest/index.ts @@ -0,0 +1,17 @@ +import { sourceList } from "./sourcelist"; + +interface ManifestInterface { + sourceList(): string[]; +} + +class ManifestV1 implements ManifestInterface { + manifest: any; + + constructor(json: string) { + /* noop */ + } + + sourceList(): string[] { + return sourceList(sourceList(this.manifest)); + } +} diff --git a/src/plugin/packer/sourcelist.ts b/src/plugin/packer/manifest/sourcelist.ts similarity index 100% rename from src/plugin/packer/sourcelist.ts rename to src/plugin/packer/manifest/sourcelist.ts diff --git a/src/plugin/packer/manifest/validate.ts b/src/plugin/packer/manifest/validate.ts new file mode 100644 index 0000000000..4debb668bb --- /dev/null +++ b/src/plugin/packer/manifest/validate.ts @@ -0,0 +1,64 @@ +import validate from "@kintone/plugin-manifest-validator"; +import _debug from "debug"; +import fs from "fs"; +import path from "path"; +import { logger } from "../../../utils/log"; +import { generateErrorMessages } from "./gen-error-msg"; + +const debug = _debug("validate"); + +export const validateManifestJson = ( + manifestJson: string, + pluginDir: string, +) => { + const manifest = JSON.parse(manifestJson); + const result = validate(manifest, { + maxFileSize: validateMaxFileSize(pluginDir), + fileExists: validateFileExists(pluginDir), + }); + debug(result); + + if (result.warnings && result.warnings.length > 0) { + result.warnings.forEach((warning) => { + logger.warn(warning.message); + }); + } + + if (!result.valid) { + const msgs = generateErrorMessages(result.errors ?? []); + logger.error("Invalid manifest.json:"); + msgs.forEach((msg) => { + logger.error(`- ${msg}`); + }); + throw new Error("Invalid manifest.json"); + } +}; + +/** + * Return validator for `maxFileSize` keyword + */ +const validateMaxFileSize = (pluginDir: string) => { + return (maxBytes: number, filePath: string) => { + try { + const stat = fs.statSync(path.join(pluginDir, filePath)); + return stat.size <= maxBytes; + } catch (_) { + return false; + } + }; +}; + +/** + * + * @param pluginDir + */ +const validateFileExists = (pluginDir: string) => { + return (filePath: string) => { + try { + const stat = fs.statSync(path.join(pluginDir, filePath)); + return stat.isFile(); + } catch (_) { + return false; + } + }; +}; diff --git a/src/plugin/packer/zip.ts b/src/plugin/packer/zip.ts index 245bec7188..02c0a3c9f9 100644 --- a/src/plugin/packer/zip.ts +++ b/src/plugin/packer/zip.ts @@ -5,8 +5,8 @@ import { promisify } from "util"; import validate from "@kintone/plugin-manifest-validator"; import * as streamBuffers from "stream-buffers"; -import { generateErrorMessages } from "./gen-error-msg"; -import { sourceList } from "./sourcelist"; +import { generateErrorMessages } from "./manifest/gen-error-msg"; +import { sourceList } from "./manifest/sourcelist"; import type internal from "stream"; type ManifestJson = any;