Skip to content

Commit

Permalink
refactor manifest module
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 24, 2024
1 parent fa20db6 commit d56df1f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 52 deletions.
98 changes: 49 additions & 49 deletions src/plugin/packer/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand All @@ -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;
// }
// };
// };
2 changes: 1 addition & 1 deletion src/plugin/packer/create-contents-zip.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions src/plugin/packer/manifest/index.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
File renamed without changes.
64 changes: 64 additions & 0 deletions src/plugin/packer/manifest/validate.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
};
4 changes: 2 additions & 2 deletions src/plugin/packer/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d56df1f

Please sign in to comment.