Skip to content

Commit

Permalink
feat: implement health-check pre-publish script (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Mar 21, 2023
1 parent 8e3ddd6 commit 5efc79a
Show file tree
Hide file tree
Showing 23 changed files with 659 additions and 3 deletions.
41 changes: 38 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ui/atomic/health-check/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
13 changes: 13 additions & 0 deletions packages/ui/atomic/health-check/__stub__/invalidProperties.json
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"
}
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 packages/ui/atomic/health-check/__stub__/validPackage.json
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"
}
}
14 changes: 14 additions & 0 deletions packages/ui/atomic/health-check/jest.config.js
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/'],
};
45 changes: 45 additions & 0 deletions packages/ui/atomic/health-check/package.json
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"
}
}
6 changes: 6 additions & 0 deletions packages/ui/atomic/health-check/project.json
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"
}
5 changes: 5 additions & 0 deletions packages/ui/atomic/health-check/readme.md
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`
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`] = `
"✔ Readme file
✔ Required properties in package.json
"
`;

exports[`@coveo/atomic-component-health-check when package.json conditions are not met should warn about invalid properties 1`] = `
"✔ Readme file
✖ Required properties in package.json
✖ Invalid keywords: The 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
✖ Invalid description: The 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`] = `
"✔ Readme file
✖ Required properties in package.json
✖ Invalid unpkg: You 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.
✖ Invalid keywords: You must populate the \`keywords\` property. See https://docs.npmjs.com/cli/v9/configuring-npm/package-json#keywords
✖ Invalid description: You must provide a description at least 20 characters long to explain the component purpose.
✖ Invalid homepage: You 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`] = `
"✖ Readme file
Missing README file. Make sure to include a \`README.md\` file in your component directory
✔ Required properties in package.json
Publish aborted because some conditions have not been met
Make sure to address the above errors before publishing again
"
`;
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!
"
`;
36 changes: 36 additions & 0 deletions packages/ui/atomic/health-check/src/assertions.spec.ts
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();
});
});
23 changes: 23 additions & 0 deletions packages/ui/atomic/health-check/src/assertions.ts
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.
}
24 changes: 24 additions & 0 deletions packages/ui/atomic/health-check/src/error.ts
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);
}
}
Loading

0 comments on commit 5efc79a

Please sign in to comment.