-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement health-check pre-publish script (#1155)
- Loading branch information
Showing
23 changed files
with
659 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
coverage |
13 changes: 13 additions & 0 deletions
13
packages/ui/atomic/health-check/__stub__/invalidProperties.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,13 @@ | ||
{ | ||
"name": "invalid-properties", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"author": "", | ||
"description": "Short description", | ||
"unpkg": "path/to/file", | ||
"keywords": ["foo", "bar"], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"homepage": "https://github.com/coveo/cli" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/ui/atomic/health-check/__stub__/missingProperties.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": "missing-properties", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"author": "", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
Empty file.
13 changes: 13 additions & 0 deletions
13
packages/ui/atomic/health-check/__stub__/validPackage.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,13 @@ | ||
{ | ||
"name": "valid-package", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"author": "", | ||
"description": "Nulla fermentum ipsum vel quam euismod, id pulvinar magna aliquet. Aliquam in sapien sit amet est feugiat ultrices. Cras vulputate leo ut mattis interdum.", | ||
"unpkg": "path/to/file", | ||
"homepage": "https://github.com/foo/bar#readme", | ||
"keywords": ["foo", "bar", "coveo-atomic-component"], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
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,14 @@ | ||
export default { | ||
preset: 'ts-jest/presets/default-esm', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
testEnvironment: 'node', | ||
verbose: true, | ||
collectCoverage: true, | ||
clearMocks: true, | ||
silent: true, | ||
testTimeout: 60e3, | ||
testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
}; |
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,45 @@ | ||
{ | ||
"name": "@coveo/atomic-component-health-check", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"description": "A Health checker for custom Atomic component", | ||
"main": "./dist/index.js", | ||
"author": "Coveo", | ||
"homepage": "https://github.com/coveo/cli#readme", | ||
"bin": { | ||
"atomic-meta-check": "./dist/index.js" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"release:phase1": "npx -p=@coveord/release npm-publish", | ||
"build": "rimraf dist && tsc -b tsconfig.json", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/coveo/cli.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/coveo/cli/issues" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [], | ||
"dependencies": { | ||
"chalk": "4.1.2", | ||
"zod": "^3.21.4" | ||
}, | ||
"devDependencies": { | ||
"@coveo/verdaccio-starter": "^1.0.0", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^18.14.6", | ||
"jest": "^29.5.0", | ||
"stdout-stderr": "^0.1.13", | ||
"ts-jest": "^29.0.5", | ||
"typescript": "^4.9.5" | ||
} | ||
} |
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 @@ | ||
{ | ||
"name": "atomic-component-health-check", | ||
"root": "packages/ui/atomic/health-check", | ||
"sourceRoot": "packages/ui/atomic/health-check/src", | ||
"projectType": "application" | ||
} |
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,5 @@ | ||
<!-- TODO: CDX-1376 --> | ||
|
||
## Testing | ||
|
||
Before running the tests, make sure to build the project with `npm run build` |
46 changes: 46 additions & 0 deletions
46
packages/ui/atomic/health-check/src/__snapshots__/index.spec.ts.snap
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 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`@coveo/atomic-component-health-check when all conditions are met should print green checks only 1`] = ` | ||
"[32m✔[39m [1mReadme file[22m | ||
[32m✔[39m [1mRequired properties in package.json[22m | ||
" | ||
`; | ||
|
||
exports[`@coveo/atomic-component-health-check when package.json conditions are not met should warn about invalid properties 1`] = ` | ||
"[32m✔[39m [1mReadme file[22m | ||
[31m✖[39m [1mRequired properties in package.json[22m | ||
[31m✖[39m [1mInvalid keywords: [22mThe package.json \`keywords\` array should include \`coveo-atomic-component\`, otherwise your component will not be listed along with other Coveo custom components. See https://docs.npmjs.com/cli/v9/configuring-npm/package-json#keywords | ||
[31m✖[39m [1mInvalid description: [22mThe component description must be at least 20 characters long | ||
Publish aborted because some conditions have not been met | ||
Make sure to address the above errors before publishing again | ||
" | ||
`; | ||
|
||
exports[`@coveo/atomic-component-health-check when package.json conditions are not met should warn about missing properties 1`] = ` | ||
"[32m✔[39m [1mReadme file[22m | ||
[31m✖[39m [1mRequired properties in package.json[22m | ||
[31m✖[39m [1mInvalid unpkg: [22mYou must provide the path to the \`.esm.js\` file of your custom component. You can find it under the \`dist/\` folder once you build the component. | ||
[31m✖[39m [1mInvalid keywords: [22mYou must populate the \`keywords\` property. See https://docs.npmjs.com/cli/v9/configuring-npm/package-json#keywords | ||
[31m✖[39m [1mInvalid description: [22mYou must provide a description at least 20 characters long to explain the component purpose. | ||
[31m✖[39m [1mInvalid homepage: [22mYou must provide a URL to the component source code. See https://docs.npmjs.com/cli/v9/configuring-npm/package-json#homepage | ||
Publish aborted because some conditions have not been met | ||
Make sure to address the above errors before publishing again | ||
" | ||
`; | ||
|
||
exports[`@coveo/atomic-component-health-check when readme condition is not met should warn about missing readme file 1`] = ` | ||
"[31m✖[39m [1mReadme file[22m | ||
Missing README file. Make sure to include a \`README.md\` file in your component directory | ||
[32m✔[39m [1mRequired properties in package.json[22m | ||
Publish aborted because some conditions have not been met | ||
Make sure to address the above errors before publishing again | ||
" | ||
`; |
22 changes: 22 additions & 0 deletions
22
packages/ui/atomic/health-check/src/__snapshots__/logger.spec.ts.snap
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,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Logger #failure should print an error message 1`] = ` | ||
"✖ Better luck next time | ||
" | ||
`; | ||
|
||
exports[`Logger #log should print multiple lines to stdout 1`] = ` | ||
"Maecenas purus lacus | ||
Donec ut sem cursus | ||
" | ||
`; | ||
|
||
exports[`Logger #newLine should print a new line 1`] = ` | ||
" | ||
" | ||
`; | ||
|
||
exports[`Logger #success should print a successful message 1`] = ` | ||
"✔ You win! | ||
" | ||
`; |
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,36 @@ | ||
jest.mock('node:fs'); | ||
jest.mock('./schema.js'); | ||
|
||
import {ensureReadme, ensureRequiredProperties} from './assertions.js'; | ||
import {existsSync, readFileSync} from 'node:fs'; | ||
import schema from './schema.js'; | ||
|
||
const mockedExistsSync = jest.mocked(existsSync); | ||
const mockedReadFileSync = jest.mocked(readFileSync); | ||
const mockedSchemaParse = jest.fn(); | ||
|
||
describe('assertions', () => { | ||
beforeEach(() => { | ||
jest.mocked(schema).parse.mockImplementation(mockedSchemaParse); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('#ensureRequiredProperties should parse package.json', () => { | ||
mockedReadFileSync.mockReturnValue('{}'); | ||
ensureRequiredProperties(); | ||
expect(mockedSchemaParse).toBeCalled(); | ||
}); | ||
|
||
it('#ensureReadme should throw if readme file is missing', () => { | ||
mockedExistsSync.mockReturnValue(false); | ||
expect(() => ensureReadme()).toThrow(); | ||
}); | ||
|
||
it('#ensureReadme should not throw if readme file is not missing', () => { | ||
mockedExistsSync.mockReturnValue(true); | ||
expect(() => ensureReadme()).not.toThrow(); | ||
}); | ||
}); |
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,23 @@ | ||
import packageJsonSchema from './schema.js'; | ||
import {join} from 'node:path'; | ||
import {readFileSync, existsSync} from 'node:fs'; | ||
import {cwd} from 'process'; | ||
|
||
export function ensureRequiredProperties() { | ||
const jsonPkg = readFileSync(join(cwd(), 'package.json')); | ||
const parsed = JSON.parse(jsonPkg.toString()); | ||
packageJsonSchema.parse(parsed); | ||
} | ||
|
||
export function ensureReadme() { | ||
const exists = existsSync(join(cwd(), 'readme.md')); | ||
if (!exists) { | ||
throw new Error( | ||
'Missing README file. Make sure to include a `README.md` file in your component directory' | ||
); | ||
} | ||
} | ||
|
||
export function ensureInternalScope() { | ||
// TODO: CDX-1266: Ensure internal components tags are tagged with the appropriate scope. | ||
} |
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,24 @@ | ||
import chalk from 'chalk'; | ||
import {ZodError} from 'zod'; | ||
import {failure, groupEnd, groupStart, log} from './logger.js'; | ||
|
||
export function prettifyZodError({errors}: ZodError, indent = true) { | ||
if (indent) { | ||
groupStart(); | ||
} | ||
|
||
for (const error of errors) { | ||
const prefix = chalk.bold(`Invalid ${error.path.join('.')}: `); | ||
failure(`${prefix}${error.message}`); | ||
} | ||
|
||
if (indent) { | ||
groupEnd(); | ||
} | ||
} | ||
|
||
export function prettifyError(error: any) { | ||
if ('message' in error) { | ||
log(error.message); | ||
} | ||
} |
Oops, something went wrong.