Просмотр исходного кода

WIP: make importPackageJson synchronous

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

+ 1 - 1
packages/pluginkit/src/v4/server/utils/package-json/import.spec.ts

@@ -4,7 +4,7 @@ import { importPackageJson } from './import';
 
 it('importPackageJson() returns an object', async() => {
   // when
-  const pkg = await importPackageJson(path.resolve(__dirname, '../../../../../test/fixtures/example-package/template1'));
+  const pkg = importPackageJson(path.resolve(__dirname, '../../../../../test/fixtures/example-package/template1'));
 
   // then
   expect(pkg).not.toBeNull();

+ 5 - 2
packages/pluginkit/src/v4/server/utils/package-json/import.ts

@@ -1,6 +1,9 @@
+import { readFileSync } from 'fs';
 import path from 'path';
 
-export const importPackageJson = async(projectDirRoot: string): Promise<any> => {
+import type { GrowiPluginPackageData } from '~/model';
+
+export const importPackageJson = (projectDirRoot: string): GrowiPluginPackageData => {
   const packageJsonUrl = path.resolve(projectDirRoot, 'package.json');
-  return import(packageJsonUrl);
+  return JSON.parse(readFileSync(packageJsonUrl, 'utf-8'));
 };

+ 24 - 24
packages/pluginkit/src/v4/server/utils/package-json/validate.spec.ts

@@ -19,10 +19,10 @@ describe('validatePackageJson()', () => {
 
   it('returns a data object', async() => {
     // setup
-    mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
+    mocks.importPackageJsonMock.mockReturnValue(examplePkg);
 
     // when
-    const data = await validatePackageJson('package.json');
+    const data = validatePackageJson('package.json');
 
     // then
     expect(data).not.toBeNull();
@@ -30,10 +30,10 @@ describe('validatePackageJson()', () => {
 
   it("with the 'expectedPluginType' argument returns a data object", async() => {
     // setup
-    mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
+    mocks.importPackageJsonMock.mockReturnValue(examplePkg);
 
     // when
-    const data = await validatePackageJson('package.json', GrowiPluginType.Template);
+    const data = validatePackageJson('package.json', GrowiPluginType.Template);
 
     // then
     expect(data).not.toBeNull();
@@ -41,65 +41,65 @@ describe('validatePackageJson()', () => {
 
   describe('should throw an GrowiPluginValidationError', () => {
 
-    it("when the pkg does not have 'growiPlugin' directive", async() => {
+    it("when the pkg does not have 'growiPlugin' directive", () => {
       // setup
-      mocks.importPackageJsonMock.mockResolvedValue({});
+      mocks.importPackageJsonMock.mockReturnValue({});
 
       // when
-      const caller = async() => { await validatePackageJson('package.json') };
+      const caller = () => { validatePackageJson('package.json') };
 
       // then
-      await expect(caller).rejects.toThrow("The package.json does not have 'growiPlugin' directive.");
+      expect(caller).toThrow("The package.json does not have 'growiPlugin' directive.");
     });
 
-    it("when the 'schemaVersion' is NaN", async() => {
+    it("when the 'schemaVersion' is NaN", () => {
       // setup
-      mocks.importPackageJsonMock.mockResolvedValue({
+      mocks.importPackageJsonMock.mockReturnValue({
         growiPlugin: {
           schemaVersion: 'foo',
         },
       });
 
       // when
-      const caller = async() => { await validatePackageJson('package.json') };
+      const caller = () => { validatePackageJson('package.json') };
 
       // then
-      await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
+      expect(caller).toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
     });
 
-    it("when the 'schemaVersion' is less than 4", async() => {
+    it("when the 'schemaVersion' is less than 4", () => {
       // setup
-      mocks.importPackageJsonMock.mockResolvedValue({
+      mocks.importPackageJsonMock.mockReturnValue({
         growiPlugin: {
           schemaVersion: 3,
         },
       });
 
       // when
-      const caller = async() => { await validatePackageJson('package.json') };
+      const caller = () => { validatePackageJson('package.json') };
 
       // then
-      await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
+      expect(caller).toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
     });
 
-    it("when the 'types' directive does not exist", async() => {
+    it("when the 'types' directive does not exist", () => {
       // setup
-      mocks.importPackageJsonMock.mockResolvedValue({
+      mocks.importPackageJsonMock.mockReturnValue({
         growiPlugin: {
           schemaVersion: 4,
         },
       });
 
       // when
-      const caller = async() => { await validatePackageJson('package.json') };
+      const caller = () => { validatePackageJson('package.json') };
 
       // then
-      await expect(caller).rejects.toThrow("The growiPlugin directive does not have 'types' directive.");
+      expect(caller).toThrow("The growiPlugin directive does not have 'types' directive.");
     });
 
-    it("when the 'types' directive does not have expected plugin type", async() => {
+    it("when the 'types' directive does not have expected plugin type", () => {
       // setup
-      mocks.importPackageJsonMock.mockResolvedValue({
+      mocks.importPackageJsonMock.mockReturnValue({
         growiPlugin: {
           schemaVersion: 4,
           types: [GrowiPluginType.Template],
@@ -107,10 +107,10 @@ describe('validatePackageJson()', () => {
       });
 
       // when
-      const caller = async() => { await validatePackageJson('package.json', GrowiPluginType.Script) };
+      const caller = () => { validatePackageJson('package.json', GrowiPluginType.Script) };
 
       // then
-      await expect(caller).rejects.toThrow("The growiPlugin directive does not have expected plugin type in 'types' directive.");
+      expect(caller).toThrow("The growiPlugin directive does not have expected plugin type in 'types' directive.");
     });
   });
 

+ 4 - 4
packages/pluginkit/src/v4/server/utils/package-json/validate.ts

@@ -5,13 +5,13 @@ import { type GrowiPluginValidationData, GrowiPluginValidationError } from '~/mo
 import { importPackageJson } from './import';
 
 
-export const validatePackageJson = async(projectDirRoot: string, expectedPluginType?: GrowiPluginType): Promise<GrowiPluginValidationData> => {
-  const pkg = await importPackageJson(projectDirRoot);
-
-  const data: GrowiPluginValidationData = { projectDirRoot };
+export const validatePackageJson = (projectDirRoot: string, expectedPluginType?: GrowiPluginType): GrowiPluginValidationData => {
+  const pkg = importPackageJson(projectDirRoot);
 
   const { growiPlugin } = pkg;
 
+  const data: GrowiPluginValidationData = { projectDirRoot, schemaVersion: NaN, growiPlugin };
+
   if (growiPlugin == null) {
     throw new GrowiPluginValidationError("The package.json does not have 'growiPlugin' directive.", data);
   }

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

@@ -9,7 +9,7 @@ import { GrowiPluginValidationError } from '~/model';
 
 import { isTemplateStatusValid, type TemplateStatus, type TemplateSummary } from '../../interfaces';
 
-import { importPackageJson, validatePackageJson } from './package-json';
+import { validatePackageJson } from './package-json';
 
 
 const statAsync = promisify(fs.stat);
@@ -19,13 +19,13 @@ const statAsync = promisify(fs.stat);
  * An utility for template plugin which wrap 'validatePackageJson' of './package-json.ts' module
  * @param projectDirRoot
  */
-export const validateTemplatePluginPackageJson = async(projectDirRoot: string): Promise<GrowiTemplatePluginValidationData> => {
-  const data = await validatePackageJson(projectDirRoot, GrowiPluginType.Template);
+export const validateTemplatePluginPackageJson = (projectDirRoot: string): GrowiTemplatePluginValidationData => {
+  const data = validatePackageJson(projectDirRoot, GrowiPluginType.Template);
 
-  const pkg = await importPackageJson(projectDirRoot);
+  const { growiPlugin } = data;
 
   // check supporting locales
-  const supportingLocales: string[] | undefined = pkg.growiPlugin.locales;
+  const supportingLocales: string[] | undefined = growiPlugin.locales;
   if (supportingLocales == null || supportingLocales.length === 0) {
     throw new GrowiPluginValidationError<GrowiPluginValidationData & { supportingLocales?: string[] }>(
       "Template plugin must have 'supportingLocales' and that must have one or more locales",
@@ -130,7 +130,7 @@ export const scanAllTemplateStatus = async(
     },
 ): Promise<TemplateSummary[]> => {
 
-  const data = opts?.data ?? await validateTemplatePluginPackageJson(projectDirRoot);
+  const data = opts?.data ?? validateTemplatePluginPackageJson(projectDirRoot);
 
   const summaries: TemplateSummary[] = [];
 
@@ -161,7 +161,7 @@ export const scanAllTemplateStatus = async(
 };
 
 export const validateTemplatePlugin = async(projectDirRoot: string): Promise<boolean> => {
-  const data = await validateTemplatePluginPackageJson(projectDirRoot);
+  const data = validateTemplatePluginPackageJson(projectDirRoot);
 
   const results = await scanAllTemplateStatus(projectDirRoot, { data, returnsInvalidTemplates: true });
 

+ 22 - 5
packages/preset-templates/test/index.test.ts

@@ -15,21 +15,38 @@ it('Validation for package.json should be passed', () => {
   expect(caller).not.toThrow();
 });
 
+it('Validation for package.json should be return data', () => {
+
+  // when
+  const data = validateTemplatePluginPackageJson(projectDirRoot);
+
+  // then
+  expect(data).not.toBeNull();
+});
+
 it('Scanning the templates ends up with no errors', async() => {
+  // when
+  const caller = async() => { await scanAllTemplateStatus(projectDirRoot) };
+
+  // then
+  await expect(caller).rejects.not.toThrow();
+});
+
+it('Scanning the templates ends up with no errors with opts.data', async() => {
 
   // setup
-  const data = await validateTemplatePluginPackageJson(projectDirRoot);
+  const data = validateTemplatePluginPackageJson(projectDirRoot);
 
   // when
-  const caller = () => scanAllTemplateStatus(projectDirRoot, data);
+  const caller = async() => { await scanAllTemplateStatus(projectDirRoot, { data }) };
 
   // then
-  expect(caller).not.toThrow();
+  await expect(caller).rejects.not.toThrow();
 });
 
-it('Validation templates returns true', async() => {
+it('Validation templates returns true', () => {
   // when
-  const result = await validateTemplatePlugin(projectDirRoot);
+  const result = validateTemplatePlugin(projectDirRoot);
 
   // then
   expect(result).toBeTruthy();