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

refactor(commons): add FeathersGeneratorError and flexible version re… #3556

Open
wants to merge 1 commit into
base: dove
Choose a base branch
from
Open
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
49 changes: 40 additions & 9 deletions packages/generators/src/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ export interface FeathersBaseContext extends PinionContext {
* @param versions The dependency version list
* @returns A list of dependencies with their versions
*/
export const addVersions = (dependencies: string[], versions: DependencyVersions) =>
dependencies.map((dep) => `${dep}@${versions[dep] ? versions[dep] : 'latest'}`)

export const addVersions = (
dependencies: string[],
versions: DependencyVersions,
defaultVersion = 'latest'
): string[] => dependencies.map((dep) => `${dep}@${versions[dep] || defaultVersion}`)
/**
* Loads the application package.json and populates information like the library and test directory
* and Feathers app specific information.
Expand Down Expand Up @@ -138,6 +140,30 @@ export const initializeBaseContext =
feathers: ctx.pkg?.feathers
}))

/**
* A special error that can be thrown by generators. It contains additional
* information about the error that can be used to display a more helpful
* error message to the user.
*/
export class FeathersGeneratorError extends Error {
/**
* Additional information about the error. This can include things like
* the reason for the error and suggested actions to take.
*/
context?: Record<string, unknown>

/**
* Creates a new FeathersGeneratorError
* @param message The error message
* @param context Additional information about the error
*/
constructor(message: string, context?: Record<string, unknown>) {
super(message)
this.name = 'FeathersGeneratorError'
this.context = context
}
}

/**
* Checks if the current context contains a valid generated application. This is necesary for most
* generators (besides the app generator).
Expand All @@ -149,11 +175,11 @@ export const checkPreconditions =
() =>
async <T extends FeathersBaseContext>(ctx: T) => {
if (!ctx.feathers) {
throw new Error(`Can not run generator since the current folder does not appear to be a Feathers application.
Either your package.json is missing or it does not have \`feathers\` property.
`)
throw new FeathersGeneratorError('Invalid Feathers application context', {
reason: 'Missing Feathers configuration',
suggestedAction: 'Verify package.json contains Feathers metadata'
})
}

return ctx
}

Expand Down Expand Up @@ -203,8 +229,13 @@ export const PRETTIERRC: PrettierOptions = {
printWidth: 110,
semi: false,
trailingComma: 'none',
singleQuote: true
}
singleQuote: true,
arrowParens: 'avoid',
bracketSpacing: true,
endOfLine: 'auto',
jsxSingleQuote: false,
quoteProps: 'as-needed'
};

/*
* Format a source file using Prettier. Will use the local configuration, the settings set in
Expand Down