@dotcom-tool-kit/types
Advanced tools
Comparing version 2.3.0 to 2.4.0
import type { Logger } from 'winston'; | ||
import { Schema, SchemaOutput } from './schema'; | ||
export declare abstract class Task<O extends Schema = Record<string, never>> { | ||
declare const typeSymbol: unique symbol; | ||
export interface Invalid { | ||
valid: false; | ||
reasons: string[]; | ||
} | ||
export interface Valid<T> { | ||
valid: true; | ||
value: T; | ||
} | ||
export declare type Validated<T> = Invalid | Valid<T>; | ||
export declare function mapValidated<T, U>(validated: Validated<T>, f: (val: T) => U): Validated<U>; | ||
export declare function mapValidationError<T>(validated: Validated<T>, f: (reasons: string[]) => string[]): Validated<T>; | ||
export declare function joinValidated<T, U>(first: Validated<T>, second: Validated<U>): Validated<[T, U]>; | ||
export declare function reduceValidated<T>(validated: Validated<T>[]): Validated<T[]>; | ||
declare abstract class Base { | ||
static version: string; | ||
version: string; | ||
static get [typeSymbol](): symbol; | ||
get [typeSymbol](): symbol; | ||
static is<T extends Base>(objectToCheck: any): objectToCheck is T; | ||
static isCompatible<T extends Base>(objectToCheck: unknown): Validated<T>; | ||
} | ||
export declare abstract class Task<O extends Schema = Record<string, never>> extends Base { | ||
static description: string; | ||
static plugin?: Plugin; | ||
static id?: string; | ||
static get [typeSymbol](): symbol; | ||
get [typeSymbol](): symbol; | ||
static defaultOptions: Record<string, unknown>; | ||
@@ -16,3 +40,3 @@ options: SchemaOutput<O>; | ||
} & typeof Task; | ||
export declare abstract class Hook { | ||
export declare abstract class Hook extends Base { | ||
id?: string; | ||
@@ -22,2 +46,4 @@ plugin?: Plugin; | ||
static description?: string; | ||
static get [typeSymbol](): symbol; | ||
get [typeSymbol](): symbol; | ||
constructor(logger: Logger); | ||
@@ -53,2 +79,3 @@ abstract check(): Promise<boolean>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
135
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Hook = exports.Task = void 0; | ||
class Task { | ||
exports.Hook = exports.Task = exports.reduceValidated = exports.joinValidated = exports.mapValidationError = exports.mapValidated = void 0; | ||
const tslib_1 = require("tslib"); | ||
const logger_1 = require("@dotcom-tool-kit/logger"); | ||
const fs_1 = (0, tslib_1.__importDefault)(require("fs")); | ||
const path_1 = (0, tslib_1.__importDefault)(require("path")); | ||
const semver_1 = (0, tslib_1.__importDefault)(require("semver")); | ||
const packageJsonPath = path_1.default.resolve(__dirname, '../package.json'); | ||
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf8')); | ||
const version = packageJson.version; | ||
// uses Symbol.for, not Symbol, so that they're compatible across different | ||
// @dotcom-tool-kit/types instances | ||
// used as the name for the property we use to identify classes | ||
const typeSymbol = Symbol.for('@dotcom-tool-kit/types'); | ||
// used to identify the Base, Task and Hook classes | ||
const baseSymbol = Symbol.for('@dotcom-tool-kit/types/base'); | ||
const taskSymbol = Symbol.for('@dotcom-tool-kit/types/task'); | ||
const hookSymbol = Symbol.for('@dotcom-tool-kit/types/hook'); | ||
function mapValidated(validated, f) { | ||
if (validated.valid) { | ||
return { valid: true, value: f(validated.value) }; | ||
} | ||
else { | ||
return validated; | ||
} | ||
} | ||
exports.mapValidated = mapValidated; | ||
function mapValidationError(validated, f) { | ||
if (validated.valid) { | ||
return validated; | ||
} | ||
else { | ||
return { valid: false, reasons: f(validated.reasons) }; | ||
} | ||
} | ||
exports.mapValidationError = mapValidationError; | ||
function joinValidated(first, second) { | ||
if (first.valid) { | ||
if (second.valid) { | ||
return { valid: true, value: [first.value, second.value] }; | ||
} | ||
else { | ||
return second; | ||
} | ||
} | ||
else { | ||
if (second.valid) { | ||
return first; | ||
} | ||
else { | ||
return { valid: false, reasons: [...first.reasons, ...second.reasons] }; | ||
} | ||
} | ||
} | ||
exports.joinValidated = joinValidated; | ||
function reduceValidated(validated) { | ||
let sequenced = { valid: true, value: [] }; | ||
for (const val of validated) { | ||
if (sequenced.valid) { | ||
if (val.valid) { | ||
sequenced.value.push(val.value); | ||
} | ||
else { | ||
sequenced = { valid: false, reasons: val.reasons }; | ||
} | ||
} | ||
else if (!val.valid) { | ||
sequenced.reasons.push(...val.reasons); | ||
} | ||
} | ||
return sequenced; | ||
} | ||
exports.reduceValidated = reduceValidated; | ||
class Base { | ||
constructor() { | ||
this.version = version; | ||
} | ||
static get [typeSymbol]() { | ||
return baseSymbol; | ||
} | ||
get [typeSymbol]() { | ||
return baseSymbol; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
static is(objectToCheck) { | ||
return objectToCheck[typeSymbol] === this[typeSymbol]; | ||
} | ||
static isCompatible(objectToCheck) { | ||
if (!this.is(objectToCheck)) { | ||
return { | ||
valid: false, | ||
reasons: [ | ||
`${logger_1.styles.plugin('@dotcom-tool-kit/types')} type symbol is missing, make sure that this object derives from the ${logger_1.styles.code('Task')} or ${logger_1.styles.code('Hook')} class defined by the plugin` | ||
] | ||
}; | ||
} | ||
// an 'objectToCheck' from a plugin is compatible with this CLI if its | ||
// version is semver-compatible with the @dotcom-tool-kit/types included by | ||
// the CLI (which is what's calling this). so, prepend ^ to our version, | ||
// and check our version satisfies that. | ||
// this lets e.g. a CLI that includes types@2.2.0 load any plugin | ||
// that depends on any higher minor version of types. | ||
const range = `^${this.version}`; | ||
if (semver_1.default.satisfies(objectToCheck.version, range)) { | ||
return { valid: true, value: objectToCheck }; | ||
} | ||
else { | ||
return { | ||
valid: false, | ||
reasons: [ | ||
`object is from an outdated version of ${logger_1.styles.plugin('@dotcom-tool-kit/types')}, make sure you're using at least version ${logger_1.styles.heading(this.version)} of the plugin` | ||
] | ||
}; | ||
} | ||
} | ||
} | ||
Base.version = version; | ||
class Task extends Base { | ||
constructor(logger, options = {}) { | ||
super(); | ||
const staticThis = this.constructor; | ||
@@ -10,10 +126,23 @@ this.options = Object.assign({}, staticThis.defaultOptions, options); | ||
} | ||
static get [typeSymbol]() { | ||
return taskSymbol; | ||
} | ||
get [typeSymbol]() { | ||
return taskSymbol; | ||
} | ||
} | ||
exports.Task = Task; | ||
Task.defaultOptions = {}; | ||
class Hook { | ||
class Hook extends Base { | ||
constructor(logger) { | ||
super(); | ||
this.logger = logger.child({ hook: this.constructor.name }); | ||
} | ||
static get [typeSymbol]() { | ||
return hookSymbol; | ||
} | ||
get [typeSymbol]() { | ||
return hookSymbol; | ||
} | ||
} | ||
exports.Hook = Hook; |
{ | ||
"name": "@dotcom-tool-kit/types", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "", | ||
@@ -27,4 +27,6 @@ "main": "lib", | ||
"@dotcom-tool-kit/error": "^2.0.0", | ||
"@dotcom-tool-kit/logger": "^2.1.0", | ||
"lodash.isplainobject": "^4.0.6", | ||
"lodash.mapvalues": "^4.6.0" | ||
"lodash.mapvalues": "^4.6.0", | ||
"semver": "^7.3.7" | ||
}, | ||
@@ -36,4 +38,5 @@ "devDependencies": { | ||
"@types/prompts": "^2.0.14", | ||
"@types/semver": "^7.3.9", | ||
"winston": "^3.5.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42136
694
5
6
1
+ Addedsemver@^7.3.7
+ Added@colors/colors@1.6.0(transitive)
+ Added@dabh/diagnostics@2.0.3(transitive)
+ Added@dotcom-tool-kit/logger@2.2.1(transitive)
+ Added@types/triple-beam@1.3.5(transitive)
+ Addedabort-controller@3.0.0(transitive)
+ Addedansi-colors@4.1.3(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedcolor@3.2.1(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcolor-string@1.9.1(transitive)
+ Addedcolorspace@1.1.4(transitive)
+ Addedenabled@2.0.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedfecha@4.2.3(transitive)
+ Addedfn.name@1.1.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arrayish@0.3.2(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedkuler@2.0.0(transitive)
+ Addedlogform@2.6.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addedone-time@1.0.0(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@3.6.24.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafe-stable-stringify@2.5.0(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedsimple-swizzle@0.2.2(transitive)
+ Addedstack-trace@0.0.10(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtext-hex@1.0.0(transitive)
+ Addedtriple-beam@1.4.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwinston@3.15.0(transitive)
+ Addedwinston-transport@4.8.0(transitive)