Skip to content

Commit

Permalink
chore(ts): improve third party typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed May 31, 2021
1 parent 13da14e commit 1e10f91
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
101 changes: 62 additions & 39 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,6 @@ export interface Dependency {
text: () => string;
}

export type ThirdPartyOutput = string | ((dependencies: Dependency[]) => void) | {

/**
* Name of file to write licenses to
*/
file: string,

/**
* @default utf-8
*/
encoding?: string,

/**
* @example
* // Template function that can be defined to customize report output
* template(dependencies) {
* return dependencies.map((dependency) => (
* `${dependency.name}:${dependency.version} -- ${dependency.license}`).join('\n')
* );
* },
*
* // Lodash template that can be defined to customize report output
* template: `
* <% _.forEach(dependencies, function (dependency) { %>
* <%= dependency.name %>:<%= dependency.version%> -- <%= dependency.license %>
* <% }) %>
* `
*/
template?: ((dependencies: Dependency[]) => string) | string,
};

/**
* SPDX Licence Identifier.
*/
Expand Down Expand Up @@ -247,12 +216,69 @@ interface ThirdPartyAllowOptions {
failOnViolation?: boolean;
}

export type ThirdParty = ((dependencies: Dependency[]) => void) | {
/**
* Output generator: may write a file to disk, or something else as long as it is a
* synchronous operation.
*/
type ThirdPartyOutputGeneratorFn = (dependencies: Dependency[]) => void;

/**
* Template as a raw string.
*/
type ThirdPartyOutputTemplate = string;

/**
* Template function.
*/
type ThirdPartyOutputTemplateFn = (dependencies: Dependency[]) => void;

/**
* Third Party output options object.
*/
interface ThirdPartyOutputOptions {
/**
* Name of file to write licenses to
*/
file: FilePath;

/**
* @default utf-8
*/
encoding?: FileEncoding;

/**
* Template function that can be defined to customize report output.
*
* @example
* template(dependencies) {
* return dependencies.map((dependency) => (
* `${dependency.name}:${dependency.version} -- ${dependency.license}`).join('\n')
* );
* },
*
* // Lodash template that can be defined to customize report output
* template: `
* <% _.forEach(dependencies, function (dependency) { %>
* <%= dependency.name %>:<%= dependency.version%> -- <%= dependency.license %>
* <% }) %>
* `
*/
template?: ThirdPartyOutputTemplate | ThirdPartyOutputTemplateFn;
}

type ThirdPartyOutput = FilePath | ThirdPartyOutputGeneratorFn | ThirdPartyOutputOptions;

interface ThirdPartyOptions {
/**
* Output for third party report.
*/
output: ThirdPartyOutput | ThirdPartyOutput[];

/**
* If private dependencies should be checked (`private: true` in package.json)
* @default false
*/
includePrivate?: boolean,
includePrivate?: boolean;

/**
* Ensures that dependencies does not violate any license restriction.
Expand All @@ -269,13 +295,10 @@ export type ThirdParty = ((dependencies: Dependency[]) => void) | {
* return dependency.license === 'MIT';
* }
*/
allow?: ThirdPartyValidator | ThirdPartyAllowOptions,
allow?: ThirdPartyValidator | ThirdPartyAllowOptions;
}

/**
* Output file for
*/
output: ThirdPartyOutput | ThirdPartyOutput[],
};
export type ThirdParty = ThirdPartyOutputGeneratorFn | ThirdPartyOptions;

export interface Options {
sourcemap?: boolean | string;
Expand Down
6 changes: 4 additions & 2 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ class LicensePlugin {
.value();

if (_.isFunction(thirdParty)) {
return thirdParty(outputDependencies);
thirdParty(outputDependencies);
return;
}

const allow = thirdParty.allow;
Expand Down Expand Up @@ -521,7 +522,8 @@ class LicensePlugin {
*/
_exportThirdPartiesToOutput(outputDependencies, output) {
if (_.isFunction(output)) {
return output(outputDependencies);
output(outputDependencies);
return;
}

// Default is to export to given file.
Expand Down

0 comments on commit 1e10f91

Please sign in to comment.