Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul and modernise lint and format processes #261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- checkout
- vault/get-secrets: # Loads vault secrets
template-preset: "semantic-release-ecosystem"
template-preset: 'semantic-release-ecosystem'
- run: npm install
- run: npm run build
- run: npm run semantic-release
Expand Down
20 changes: 0 additions & 20 deletions .eslintrc

This file was deleted.

14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"plugins": ["jest"],
"env": {
"node": true,
"jest/globals": true
},
"ignorePatterns": ["node_modules", "dist"]
}
10 changes: 5 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ version: 2

updates:
- package-ecosystem: npm
directory: "/"
directory: '/'
schedule:
interval: daily
time: "00:00"
time: '00:00'
timezone: UTC
open-pull-requests-limit: 10
ignore:
- dependency-name: figures # ignore ESM only versions
- dependency-name: figures # ignore ESM only versions
versions:
- ">=4.0.0"
- '>=4.0.0'
commit-message:
prefix: build
include: scope
include: scope
4 changes: 2 additions & 2 deletions .github/workflows/dependabot-approve-and-request-merge.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "dependabot approve-and-request-merge"
name: 'dependabot approve-and-request-merge'

on: pull_request_target

Expand All @@ -12,4 +12,4 @@ jobs:
steps:
- uses: contentful/github-auto-merge@v1
with:
VAULT_URL: ${{ secrets.VAULT_URL }}
VAULT_URL: ${{ secrets.VAULT_URL }}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
4 changes: 4 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.{json,md,yaml,yml}": ["prettier --write"],
"*.{js,ts}": ["prettier --write", "eslint --fix"]
}
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
8 changes: 4 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://jestjs.io/docs/configuration
*/

import type { Config } from 'jest'
import type { Config } from 'jest';

const config: Config = {
// All imported modules in your tests should be mocked automatically
Expand Down Expand Up @@ -100,7 +100,7 @@ const config: Config = {
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
preset: 'ts-jest/presets/js-with-ts'
preset: 'ts-jest/presets/js-with-ts',

// Run tests from one or more projects
// projects: undefined,
Expand Down Expand Up @@ -192,6 +192,6 @@ const config: Config = {

// Whether to use watchman for file crawling
// watchman: true,
}
};

export default config
export default config;
10 changes: 5 additions & 5 deletions lib/add-sequence-header.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { v4 as uuidv4 } from 'uuid'
import { v4 as uuidv4 } from 'uuid';

/**
* Adds a sequence header to a header object
*/
export function addSequenceHeader (headers: Record<string, unknown>) {
export function addSequenceHeader(headers: Record<string, unknown>) {
if (typeof headers !== 'object') {
throw new Error('addSequence function expects an object as input')
throw new Error('addSequence function expects an object as input');
}

return {
...headers,
// Unique sequence header
'CF-Sequence': uuidv4()
}
'CF-Sequence': uuidv4(),
};
}
22 changes: 11 additions & 11 deletions lib/get-entity-name.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { isFields, isSysLink } from './type-guards'
import { isFields, isSysLink } from './type-guards';

export function getEntityName (entity?: NonNullable<unknown>): string {
export function getEntityName(entity?: NonNullable<unknown>): string {
if (!entity) {
return 'unknown'
return 'unknown';
}
if ('name' in entity && typeof entity.name === 'string') {
return attachId(entity.name, entity)
return attachId(entity.name, entity);
}

if (isFields(entity)) {
const titleOrNameField = entity.fields.title ?? entity.fields.name
const titleOrNameField = entity.fields.title ?? entity.fields.name;
if (titleOrNameField) {
const locales = Object.keys(titleOrNameField)
return attachId(titleOrNameField[locales[0]], entity)
const locales = Object.keys(titleOrNameField);
return attachId(titleOrNameField[locales[0]], entity);
}
}

return isSysLink(entity) ? entity.sys.id : 'unknown'
return isSysLink(entity) ? entity.sys.id : 'unknown';
}

function attachId (val: string, entity: NonNullable<unknown>) {
function attachId(val: string, entity: NonNullable<unknown>) {
if (isSysLink(entity)) {
return `${val} (${entity.sys.id})`
return `${val} (${entity.sys.id})`;
}
return val
return val;
}
12 changes: 6 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { addSequenceHeader } from './add-sequence-header'
export { getEntityName } from './get-entity-name'
export { wrapTask } from './listr'
export { addSequenceHeader } from './add-sequence-header';
export { getEntityName } from './get-entity-name';
export { wrapTask } from './listr';
export {
logEmitter,
displayErrorLog,
setupLogging,
writeErrorLogFile,
type LogMessage
} from './logging'
export { proxyStringToObject, agentFromProxy } from './proxy'
type LogMessage,
} from './logging';
export { proxyStringToObject, agentFromProxy } from './proxy';
32 changes: 16 additions & 16 deletions lib/listr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ import type {
ListrContext,
ListrDefaultRenderer,
ListrRendererFactory,
ListrTaskFn
} from 'listr2'
import { formatLogMessageOneLine, logToTaskOutput } from './logging'
ListrTaskFn,
} from 'listr2';
import { formatLogMessageOneLine, logToTaskOutput } from './logging';

/**
* Set up log emitter listening from SDK, proper error catching and throwing of SDK errors
*/
export function wrapTask<
Ctx = ListrContext,
Renderer extends ListrRendererFactory = ListrDefaultRenderer,
FallbackRenderer extends ListrRendererFactory = ListrDefaultRenderer
> (
func: ListrTaskFn<Ctx, Renderer, FallbackRenderer>
FallbackRenderer extends ListrRendererFactory = ListrDefaultRenderer,
>(
func: ListrTaskFn<Ctx, Renderer, FallbackRenderer>,
): ListrTaskFn<Ctx, Renderer, FallbackRenderer> {
return async (ctx, task) => {
const teardownTaskListeners = logToTaskOutput(task)
const teardownTaskListeners = logToTaskOutput(task);

try {
await func(ctx, task)
teardownTaskListeners()
await func(ctx, task);
teardownTaskListeners();
} catch (err) {
teardownTaskListeners()
teardownTaskListeners();

// Format message as human readable listr output
const formattedMessage = formatLogMessageOneLine({
ts: new Date().toJSON(),
level: 'error',
error: err as Error
})
error: err as Error,
});

const error = new Error(formattedMessage)
const error = new Error(formattedMessage);

// Enrich error with the original cause
Object.defineProperty(error, 'originalError', { value: err })
Object.defineProperty(error, 'originalError', { value: err });

throw error
throw error;
}
}
};
}
Loading