Yuki Takei 2 лет назад
Родитель
Сommit
d691d0b72b

+ 40 - 3
packages/pluginkit/src/server/utils/v4/template.ts

@@ -1,3 +1,4 @@
+import assert from 'assert';
 import fs from 'fs';
 import path from 'path';
 import { promisify } from 'util';
@@ -109,9 +110,7 @@ export const scanTemplateStatus = async(projectDirRoot: string, templateId: stri
   return status;
 };
 
-export const scanAllTemplateStatus = async(projectDirRoot: string): Promise<TemplateStatus[]> => {
-  const data = await validateTemplatePluginPackageJson(projectDirRoot);
-
+export const scanAllTemplateStatus = async(projectDirRoot: string, data: GrowiTemplatePluginValidationData): Promise<TemplateStatus[]> => {
   const status: TemplateStatus[] = [];
 
   const distDirPath = path.resolve(projectDirRoot, 'dist');
@@ -123,3 +122,41 @@ export const scanAllTemplateStatus = async(projectDirRoot: string): Promise<Temp
 
   return status;
 };
+
+
+export const validateTemplatePlugin = async(projectDirRoot: string): Promise<boolean> => {
+  const data = await validateTemplatePluginPackageJson(projectDirRoot);
+
+  const results = await scanAllTemplateStatus(projectDirRoot, data);
+
+  if (results.length === 0) {
+    throw new Error('This plugin does not have any templates');
+  }
+
+  // construct map
+  // key: id
+  // value: isValid properties
+  const idValidMap: { [id: string]: boolean[] } = {};
+  results.forEach((status) => {
+    const validMap = idValidMap[status.id] ?? [];
+    validMap.push(status.isValid);
+    idValidMap[status.id] = validMap;
+  });
+
+  for (const [id, validMap] of Object.entries(idValidMap)) {
+    assert(validMap.length === data.supportingLocales.length);
+
+    // warn
+    if (!validMap.every(bool => bool)) {
+      // eslint-disable-next-line no-console
+      console.warn(`[WARN] Template '${id}' has invalid status`);
+    }
+
+    // This means the template directory does not have any valid template
+    if (!validMap.some(bool => bool)) {
+      return false;
+    }
+  }
+
+  return true;
+};

+ 1 - 0
packages/pluginkit/vite.config.ts

@@ -23,6 +23,7 @@ export default defineConfig({
         preserveModulesRoot: 'src',
       },
       external: [
+        'assert',
         'fs',
         'path',
         'util',

+ 15 - 38
packages/preset-templates/test/index.test.ts

@@ -1,12 +1,12 @@
 import path from 'node:path';
 
-import { scanAllTemplateStatus, validateTemplatePluginPackageJson } from '@growi/pluginkit/dist/server/utils/v4';
+import { scanAllTemplateStatus, validateTemplatePluginPackageJson, validateTemplatePlugin } from '@growi/pluginkit/dist/server/utils/v4';
 
 
 const projectDirRoot = path.resolve(__dirname, '../');
 
 
-it('Validation should be passed', () => {
+it('Validation for package.json should be passed', () => {
 
   // when
   const caller = () => validateTemplatePluginPackageJson(projectDirRoot);
@@ -15,45 +15,22 @@ it('Validation should be passed', () => {
   expect(caller).not.toThrow();
 });
 
+it('Scanning the templates ends up with no errors', async() => {
 
-describe('Scanning the template package', () => {
+  // setup
+  const data = await validateTemplatePluginPackageJson(projectDirRoot);
 
-  it('ends up with no errors', () => {
-    // when
-    const caller = () => scanAllTemplateStatus(projectDirRoot);
-
-    // then
-    expect(caller).not.toThrow();
-  });
-
-  it('successfully returns results that each template has at least one valid template', async() => {
-    // setup
-    const data = await validateTemplatePluginPackageJson(projectDirRoot);
-
-    // when 1
-    const results = await scanAllTemplateStatus(projectDirRoot);
-
-    // then 1
-    expect(results.length).toBeGreaterThan(0);
-
-    // when 2
-    const idValidMap: { [id: string]: boolean[] } = {};
-    results.forEach((status) => {
-      const validMap = idValidMap[status.id] ?? [];
-      validMap.push(status.isValid);
-      idValidMap[status.id] = validMap;
-    });
+  // when
+  const caller = () => scanAllTemplateStatus(projectDirRoot, data);
 
-    // then 2
-    Object.entries(idValidMap).forEach(([id, validMap]) => {
-      assert(validMap.length === data.supportingLocales.length);
+  // then
+  expect(caller).not.toThrow();
+});
 
-      if (!validMap.every(bool => bool)) {
-        // eslint-disable-next-line no-console
-        console.warn(`[WARN] Template '${id}' has invalid status`);
-      }
-      expect(validMap.some(bool => bool)).toBeTruthy();
-    });
-  });
+it('Validation templates returns true', async() => {
+  // when
+  const result = await validateTemplatePlugin(projectDirRoot);
 
+  // then
+  expect(result).toBeTruthy();
 });