-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8856 from weseek/support/add-theme-plugin-validator
support: Add theme plugin validator
- Loading branch information
Showing
10 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@growi/pluginkit': minor | ||
'@growi/core': minor | ||
--- | ||
|
||
Add vaidator for GROWI theme plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validate-growi-plugin-directive'; |
50 changes: 50 additions & 0 deletions
50
packages/pluginkit/src/v4/server/utils/theme/validate-growi-plugin-directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import path from 'path'; | ||
|
||
import { isGrowiThemeMetadata } from '@growi/core'; | ||
|
||
import { validateThemePluginGrowiDirective } from './validate-growi-plugin-directive'; | ||
|
||
|
||
describe('validateThemePluginGrowiDirective()', () => { | ||
|
||
it('returns a data object', async() => { | ||
// setup | ||
const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/theme1'); | ||
|
||
// when | ||
const data = validateThemePluginGrowiDirective(projectDirRoot); | ||
|
||
// then | ||
expect(data).not.toBeNull(); | ||
expect(data.growiPlugin).not.toBeNull(); | ||
expect(data.themes).not.toBeNull(); | ||
expect(isGrowiThemeMetadata(data.themes[0])).toBeTruthy(); | ||
}); | ||
|
||
describe('should throw an GrowiPluginValidationError', () => { | ||
|
||
it("when the pkg does not have 'growiPlugin.themes' directive", () => { | ||
// setup | ||
const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/invalid-theme1'); | ||
|
||
// when | ||
const caller = () => { validateThemePluginGrowiDirective(projectDirRoot) }; | ||
|
||
// then | ||
expect(caller).toThrow("Theme plugin must have 'themes' array and that must have one or more theme metadata"); | ||
}); | ||
|
||
it("when the pkg does not have 'growiPlugin.locale' directive", () => { | ||
// setup | ||
const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/invalid-theme2'); | ||
|
||
// when | ||
const caller = () => { validateThemePluginGrowiDirective(projectDirRoot) }; | ||
|
||
// then | ||
expect(caller).toThrow(/^Some of theme metadata are invalid:/); | ||
}); | ||
|
||
}); | ||
|
||
}); |
46 changes: 46 additions & 0 deletions
46
packages/pluginkit/src/v4/server/utils/theme/validate-growi-plugin-directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { GrowiThemeMetadata } from '@growi/core'; | ||
import { GrowiPluginType, isGrowiThemeMetadata } from '@growi/core'; | ||
|
||
import type { GrowiPluginValidationData, GrowiThemePluginValidationData } from '../../../../model'; | ||
import { GrowiPluginValidationError } from '../../../../model'; | ||
import { validateGrowiDirective } from '../common'; | ||
|
||
|
||
/** | ||
* An utility for theme plugin which wrap 'validateGrowiDirective' of './common' module | ||
* @param projectDirRoot | ||
*/ | ||
export const validateThemePluginGrowiDirective = (projectDirRoot: string): GrowiThemePluginValidationData => { | ||
const data = validateGrowiDirective(projectDirRoot, GrowiPluginType.Theme); | ||
|
||
const { growiPlugin } = data; | ||
|
||
// check themes | ||
if (growiPlugin.themes == null || !Array.isArray(growiPlugin.themes) || growiPlugin.themes.length === 0) { | ||
throw new GrowiPluginValidationError<GrowiPluginValidationData>( | ||
"Theme plugin must have 'themes' array and that must have one or more theme metadata", | ||
); | ||
} | ||
|
||
const validMetadatas: GrowiThemeMetadata[] = []; | ||
const invalidObjects: unknown[] = []; | ||
growiPlugin.themes.forEach((theme: unknown) => { | ||
if (isGrowiThemeMetadata(theme)) { | ||
validMetadatas.push(theme); | ||
} | ||
else { | ||
invalidObjects.push(theme); | ||
} | ||
}); | ||
|
||
if (invalidObjects.length > 0) { | ||
throw new GrowiPluginValidationError<GrowiPluginValidationData>( | ||
`Some of theme metadata are invalid: ${invalidObjects.toString()}`, | ||
); | ||
} | ||
|
||
return { | ||
...data, | ||
themes: validMetadatas, | ||
}; | ||
}; |
Empty file.
9 changes: 9 additions & 0 deletions
9
packages/pluginkit/test/fixtures/example-package/invalid-theme1/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "example-package-invalid-theme1", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"growiPlugin": { | ||
"schemaVersion": "4", | ||
"types": ["theme"] | ||
} | ||
} |
Empty file.
29 changes: 29 additions & 0 deletions
29
packages/pluginkit/test/fixtures/example-package/invalid-theme2/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "example-package-invalid-theme2", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"growiPlugin": { | ||
"schemaVersion": "4", | ||
"types": ["theme"], | ||
"themes": [ | ||
{ | ||
"name": "theme2-1", | ||
"manifestKey": "src/styles/style.scss", | ||
"schemeType": "light", | ||
"lightBg": "#ff0000", | ||
"darkBg": "#0000ff", | ||
"lightSidebar": "#ffff00", | ||
"darkSidebar": "#ff8800", | ||
"lightIcon": "#ff0000", | ||
"darkIcon": "#000000", | ||
"createBtn": "#00ff00" | ||
}, | ||
{ | ||
"name": "theme2-2", | ||
"manifestKey": "src/styles/style.scss", | ||
"schemeType": "light", | ||
"// missing some attributes": "" | ||
} | ||
] | ||
} | ||
} |