@sketch-hq/sketch-assistant-utils
Advanced tools
Comparing version 1.0.0 to 2.0.0
# @sketch-hq/sketch-assistant-utils | ||
## 2.0.0 | ||
### Major Changes | ||
- b65ee46: rework `runAssistant` function so that it doesn't mutate its inputs | ||
### Minor Changes | ||
- b65ee46: add a testRule test helper function | ||
### Patch Changes | ||
- b65ee46: do not invoke inactive rules when running an assistant | ||
- b65ee46: when merging assistants, do not overwrite values with an empty string | ||
## 1.0.0 | ||
### Major Changes | ||
- d472691: Major refactor to support Sketch Assistants architecture. |
@@ -15,5 +15,5 @@ "use strict"; | ||
return { | ||
title: acc.title, | ||
description: acc.description, | ||
name: acc.name, | ||
title: acc.title || curr.title || '', | ||
description: acc.description || curr.description || '', | ||
name: acc.name || curr.name || '', | ||
config: Object.assign(Object.assign({}, (typeof acc.config.defaultSeverity === 'undefined' | ||
@@ -20,0 +20,0 @@ ? {} |
@@ -1,6 +0,10 @@ | ||
import { RunContext, AssistantDefinition, RuleContext, ProcessedSketchFile, AssistantEnv, Violation, RunOperation, GetImageMetadata } from '../types'; | ||
declare const createRunContext: (file: ProcessedSketchFile, assistant: AssistantDefinition, env: AssistantEnv, violations: Violation[], operation: RunOperation, getImageMetadata: GetImageMetadata) => RunContext; | ||
declare const createRuleContext: (runContext: RunContext, ruleName: string) => RuleContext; | ||
declare const runAssistant: (runContext: RunContext) => Promise<Error[]>; | ||
export { runAssistant, createRunContext, createRuleContext }; | ||
import { AssistantDefinition, ProcessedSketchFile, AssistantEnv, RunOperation, GetImageMetadata, RunResult } from '../types'; | ||
declare class RuleInvocationError extends Error { | ||
cause: Error; | ||
assistantName: string; | ||
ruleName: string; | ||
constructor(cause: Error, assistantName: string, ruleName: string); | ||
} | ||
declare const runAssistant: (file: ProcessedSketchFile, assistant: AssistantDefinition, env: AssistantEnv, operation: RunOperation, getImageMetadata: GetImageMetadata) => Promise<RunResult>; | ||
export { runAssistant, RuleInvocationError }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -11,13 +11,2 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -29,2 +18,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const rule_utils_1 = require("../rule-utils"); | ||
const assistant_config_1 = require("../assistant-config"); | ||
class RuleInvocationError extends Error { | ||
@@ -40,41 +30,39 @@ constructor(cause, assistantName, ruleName) { | ||
} | ||
const createRunContext = (file, assistant, env, violations, operation, getImageMetadata) => { | ||
exports.RuleInvocationError = RuleInvocationError; | ||
const runAssistant = (file, assistant, env, operation, getImageMetadata) => __awaiter(void 0, void 0, void 0, function* () { | ||
const violations = []; | ||
const createUtils = rule_utils_1.createRuleUtilsCreator(file, violations, assistant, operation, getImageMetadata); | ||
return { | ||
const context = { | ||
env, | ||
file, | ||
createUtils, | ||
assistant, | ||
operation, | ||
getImageMetadata, | ||
assistant, | ||
env, | ||
}; | ||
}; | ||
exports.createRunContext = createRunContext; | ||
const createRuleContext = (runContext, ruleName) => { | ||
const { createUtils } = runContext, rest = __rest(runContext, ["createUtils"]); | ||
return Object.assign(Object.assign({}, rest), { utils: createUtils(ruleName) }); | ||
}; | ||
exports.createRuleContext = createRuleContext; | ||
const runAssistant = (runContext) => __awaiter(void 0, void 0, void 0, function* () { | ||
const { assistant } = runContext; | ||
try { | ||
yield p_map_1.default(Array(assistant.rules.length).fill(runContext), (context, i) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (!context.operation.cancelled) { | ||
const rule = assistant.rules[i]; | ||
const { rule: ruleFunction, name: ruleName } = rule; | ||
try { | ||
yield ruleFunction(createRuleContext(runContext, ruleName)); | ||
} | ||
catch (error) { | ||
throw new RuleInvocationError(error, assistant.name, ruleName); | ||
} | ||
yield p_map_1.default(assistant.rules.filter(rule => assistant_config_1.isRuleActive(assistant.config, rule.name)), (rule) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (operation.cancelled) | ||
return; | ||
const { rule: ruleFunction, name: ruleName } = rule; | ||
const ruleContext = Object.assign(Object.assign({}, context), { utils: createUtils(ruleName) }); | ||
try { | ||
yield ruleFunction(ruleContext); | ||
} | ||
catch (error) { | ||
throw new RuleInvocationError(error, assistant.name, ruleName); | ||
} | ||
}), { concurrency: 1, stopOnError: false }); | ||
} | ||
catch (error) { | ||
return Array.from(error); | ||
return { | ||
violations, | ||
errors: Array.from(error), | ||
}; | ||
} | ||
return []; | ||
return { | ||
violations, | ||
errors: [], | ||
}; | ||
}); | ||
exports.runAssistant = runAssistant; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { FileFormat, Assistant, RuleOptionsCreator, RuleFunction, RuleDefinition, AssistantDefinition, AssistantConfig, ViolationSeverity, RuleConfigGroup } from '../types'; | ||
import { FileFormat, Assistant, RuleOptionsCreator, RuleFunction, RuleDefinition, AssistantDefinition, AssistantConfig, ViolationSeverity, RuleConfigGroup, AssistantEnv, RunResult } from '../types'; | ||
declare const createRule: ({ title, description, rule, getOptions, name, debug, }?: { | ||
@@ -31,3 +31,4 @@ title?: string | undefined; | ||
}; | ||
export declare const testRule: (filepath: string, configGroup: RuleConfigGroup, assistant: Assistant, env?: AssistantEnv) => Promise<RunResult>; | ||
export { createRule, createDummyRectNode, createAssistantConfig, createAssistant, createAssistantDefinition, }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,6 +12,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const from_file_1 = require("../from-file"); | ||
const process_1 = require("../process"); | ||
const assistant_1 = require("../assistant"); | ||
const run_assistant_1 = require("../run-assistant"); | ||
const get_image_metadata_1 = require("../get-image-metadata"); | ||
const createRule = ({ title, description, rule, getOptions, name, debug, } = {}) => ({ | ||
name: name || 'dummy-assistant/dummy-rule', | ||
title: title || 'Dummy Rule', | ||
description: description || 'Dummy rule created in a test helper', | ||
name: name !== null && name !== void 0 ? name : 'dummy-assistant/dummy-rule', | ||
title: title !== null && title !== void 0 ? title : 'Dummy Rule', | ||
description: description !== null && description !== void 0 ? description : 'Dummy rule created in a test helper', | ||
rule: rule || (() => __awaiter(void 0, void 0, void 0, function* () { })), | ||
@@ -25,5 +30,5 @@ getOptions, | ||
const createAssistantDefinition = ({ title, description, name, config, rules, } = {}) => ({ | ||
title: title || 'Dummy Assistant', | ||
description: description || 'Dummy assistant created as a test helper', | ||
name: name || 'dummy-assistant', | ||
title: title !== null && title !== void 0 ? title : 'Dummy Assistant', | ||
description: description !== null && description !== void 0 ? description : 'Dummy assistant created as a test helper', | ||
name: name !== null && name !== void 0 ? name : 'dummy-assistant', | ||
config: config || createAssistantConfig(), | ||
@@ -45,2 +50,21 @@ rules: rules || [], | ||
exports.createDummyRectNode = createDummyRectNode; | ||
exports.testRule = (filepath, configGroup, assistant, env = { locale: 'en', platform: 'node' }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const file = yield from_file_1.fromFile(filepath); | ||
const op = { cancelled: false }; | ||
const processedFile = yield process_1.process(file, op); | ||
const TestAssistant = [ | ||
assistant, | ||
() => __awaiter(void 0, void 0, void 0, function* () { | ||
return ({ | ||
name: '', | ||
description: '', | ||
title: '', | ||
rules: [], | ||
config: { rules: Object.assign({}, configGroup) }, | ||
}); | ||
}), | ||
]; | ||
const assistantDefinition = yield assistant_1.prepare(TestAssistant, env); | ||
return yield run_assistant_1.runAssistant(processedFile, assistantDefinition, env, op, get_image_metadata_1.getImageMetadata); | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -39,10 +39,2 @@ import { FileFormat3 } from '@sketch-hq/sketch-file-format-ts'; | ||
}; | ||
export declare type RunContext = { | ||
file: ProcessedSketchFile; | ||
createUtils: RuleUtilsCreator; | ||
assistant: AssistantDefinition; | ||
operation: RunOperation; | ||
getImageMetadata: GetImageMetadata; | ||
env: AssistantEnv; | ||
}; | ||
export declare type RunOperation = { | ||
@@ -53,4 +45,13 @@ cancelled: boolean; | ||
}; | ||
export declare type RuleContext = Omit<RunContext, 'createUtils'> & { | ||
export declare type RunResult = { | ||
violations: Violation[]; | ||
errors: Error[]; | ||
}; | ||
export declare type RuleContext = { | ||
utils: RuleUtils; | ||
file: ProcessedSketchFile; | ||
assistant: AssistantDefinition; | ||
operation: RunOperation; | ||
getImageMetadata: GetImageMetadata; | ||
env: AssistantEnv; | ||
}; | ||
@@ -57,0 +58,0 @@ export declare type RuleUtilsCreator = (ruleName: string) => RuleUtils; |
@@ -13,5 +13,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return { | ||
title: acc.title, | ||
description: acc.description, | ||
name: acc.name, | ||
title: acc.title || curr.title || '', | ||
description: acc.description || curr.description || '', | ||
name: acc.name || curr.name || '', | ||
config: Object.assign(Object.assign({}, (typeof acc.config.defaultSeverity === 'undefined' | ||
@@ -18,0 +18,0 @@ ? {} |
@@ -1,6 +0,10 @@ | ||
import { RunContext, AssistantDefinition, RuleContext, ProcessedSketchFile, AssistantEnv, Violation, RunOperation, GetImageMetadata } from '../types'; | ||
declare const createRunContext: (file: ProcessedSketchFile, assistant: AssistantDefinition, env: AssistantEnv, violations: Violation[], operation: RunOperation, getImageMetadata: GetImageMetadata) => RunContext; | ||
declare const createRuleContext: (runContext: RunContext, ruleName: string) => RuleContext; | ||
declare const runAssistant: (runContext: RunContext) => Promise<Error[]>; | ||
export { runAssistant, createRunContext, createRuleContext }; | ||
import { AssistantDefinition, ProcessedSketchFile, AssistantEnv, RunOperation, GetImageMetadata, RunResult } from '../types'; | ||
declare class RuleInvocationError extends Error { | ||
cause: Error; | ||
assistantName: string; | ||
ruleName: string; | ||
constructor(cause: Error, assistantName: string, ruleName: string); | ||
} | ||
declare const runAssistant: (file: ProcessedSketchFile, assistant: AssistantDefinition, env: AssistantEnv, operation: RunOperation, getImageMetadata: GetImageMetadata) => Promise<RunResult>; | ||
export { runAssistant, RuleInvocationError }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,15 +10,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
import pMap from 'p-map'; | ||
import { createRuleUtilsCreator } from '../rule-utils'; | ||
import { isRuleActive } from '../assistant-config'; | ||
class RuleInvocationError extends Error { | ||
@@ -34,39 +24,38 @@ constructor(cause, assistantName, ruleName) { | ||
} | ||
const createRunContext = (file, assistant, env, violations, operation, getImageMetadata) => { | ||
const runAssistant = (file, assistant, env, operation, getImageMetadata) => __awaiter(void 0, void 0, void 0, function* () { | ||
const violations = []; | ||
const createUtils = createRuleUtilsCreator(file, violations, assistant, operation, getImageMetadata); | ||
return { | ||
const context = { | ||
env, | ||
file, | ||
createUtils, | ||
assistant, | ||
operation, | ||
getImageMetadata, | ||
assistant, | ||
env, | ||
}; | ||
}; | ||
const createRuleContext = (runContext, ruleName) => { | ||
const { createUtils } = runContext, rest = __rest(runContext, ["createUtils"]); | ||
return Object.assign(Object.assign({}, rest), { utils: createUtils(ruleName) }); | ||
}; | ||
const runAssistant = (runContext) => __awaiter(void 0, void 0, void 0, function* () { | ||
const { assistant } = runContext; | ||
try { | ||
yield pMap(Array(assistant.rules.length).fill(runContext), (context, i) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (!context.operation.cancelled) { | ||
const rule = assistant.rules[i]; | ||
const { rule: ruleFunction, name: ruleName } = rule; | ||
try { | ||
yield ruleFunction(createRuleContext(runContext, ruleName)); | ||
} | ||
catch (error) { | ||
throw new RuleInvocationError(error, assistant.name, ruleName); | ||
} | ||
yield pMap(assistant.rules.filter(rule => isRuleActive(assistant.config, rule.name)), (rule) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (operation.cancelled) | ||
return; | ||
const { rule: ruleFunction, name: ruleName } = rule; | ||
const ruleContext = Object.assign(Object.assign({}, context), { utils: createUtils(ruleName) }); | ||
try { | ||
yield ruleFunction(ruleContext); | ||
} | ||
catch (error) { | ||
throw new RuleInvocationError(error, assistant.name, ruleName); | ||
} | ||
}), { concurrency: 1, stopOnError: false }); | ||
} | ||
catch (error) { | ||
return Array.from(error); | ||
return { | ||
violations, | ||
errors: Array.from(error), | ||
}; | ||
} | ||
return []; | ||
return { | ||
violations, | ||
errors: [], | ||
}; | ||
}); | ||
export { runAssistant, createRunContext, createRuleContext }; | ||
export { runAssistant, RuleInvocationError }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { FileFormat, Assistant, RuleOptionsCreator, RuleFunction, RuleDefinition, AssistantDefinition, AssistantConfig, ViolationSeverity, RuleConfigGroup } from '../types'; | ||
import { FileFormat, Assistant, RuleOptionsCreator, RuleFunction, RuleDefinition, AssistantDefinition, AssistantConfig, ViolationSeverity, RuleConfigGroup, AssistantEnv, RunResult } from '../types'; | ||
declare const createRule: ({ title, description, rule, getOptions, name, debug, }?: { | ||
@@ -31,3 +31,4 @@ title?: string | undefined; | ||
}; | ||
export declare const testRule: (filepath: string, configGroup: RuleConfigGroup, assistant: Assistant, env?: AssistantEnv) => Promise<RunResult>; | ||
export { createRule, createDummyRectNode, createAssistantConfig, createAssistant, createAssistantDefinition, }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,6 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { fromFile } from '../from-file'; | ||
import { process } from '../process'; | ||
import { prepare } from '../assistant'; | ||
import { runAssistant } from '../run-assistant'; | ||
import { getImageMetadata } from '../get-image-metadata'; | ||
const createRule = ({ title, description, rule, getOptions, name, debug, } = {}) => ({ | ||
name: name || 'dummy-assistant/dummy-rule', | ||
title: title || 'Dummy Rule', | ||
description: description || 'Dummy rule created in a test helper', | ||
name: name !== null && name !== void 0 ? name : 'dummy-assistant/dummy-rule', | ||
title: title !== null && title !== void 0 ? title : 'Dummy Rule', | ||
description: description !== null && description !== void 0 ? description : 'Dummy rule created in a test helper', | ||
rule: rule || (() => __awaiter(void 0, void 0, void 0, function* () { })), | ||
@@ -21,5 +26,5 @@ getOptions, | ||
const createAssistantDefinition = ({ title, description, name, config, rules, } = {}) => ({ | ||
title: title || 'Dummy Assistant', | ||
description: description || 'Dummy assistant created as a test helper', | ||
name: name || 'dummy-assistant', | ||
title: title !== null && title !== void 0 ? title : 'Dummy Assistant', | ||
description: description !== null && description !== void 0 ? description : 'Dummy assistant created as a test helper', | ||
name: name !== null && name !== void 0 ? name : 'dummy-assistant', | ||
config: config || createAssistantConfig(), | ||
@@ -38,3 +43,22 @@ rules: rules || [], | ||
}); | ||
export const testRule = (filepath, configGroup, assistant, env = { locale: 'en', platform: 'node' }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const file = yield fromFile(filepath); | ||
const op = { cancelled: false }; | ||
const processedFile = yield process(file, op); | ||
const TestAssistant = [ | ||
assistant, | ||
() => __awaiter(void 0, void 0, void 0, function* () { | ||
return ({ | ||
name: '', | ||
description: '', | ||
title: '', | ||
rules: [], | ||
config: { rules: Object.assign({}, configGroup) }, | ||
}); | ||
}), | ||
]; | ||
const assistantDefinition = yield prepare(TestAssistant, env); | ||
return yield runAssistant(processedFile, assistantDefinition, env, op, getImageMetadata); | ||
}); | ||
export { createRule, createDummyRectNode, createAssistantConfig, createAssistant, createAssistantDefinition, }; | ||
//# sourceMappingURL=index.js.map |
@@ -39,10 +39,2 @@ import { FileFormat3 } from '@sketch-hq/sketch-file-format-ts'; | ||
}; | ||
export declare type RunContext = { | ||
file: ProcessedSketchFile; | ||
createUtils: RuleUtilsCreator; | ||
assistant: AssistantDefinition; | ||
operation: RunOperation; | ||
getImageMetadata: GetImageMetadata; | ||
env: AssistantEnv; | ||
}; | ||
export declare type RunOperation = { | ||
@@ -53,4 +45,13 @@ cancelled: boolean; | ||
}; | ||
export declare type RuleContext = Omit<RunContext, 'createUtils'> & { | ||
export declare type RunResult = { | ||
violations: Violation[]; | ||
errors: Error[]; | ||
}; | ||
export declare type RuleContext = { | ||
utils: RuleUtils; | ||
file: ProcessedSketchFile; | ||
assistant: AssistantDefinition; | ||
operation: RunOperation; | ||
getImageMetadata: GetImageMetadata; | ||
env: AssistantEnv; | ||
}; | ||
@@ -57,0 +58,0 @@ export declare type RuleUtilsCreator = (ruleName: string) => RuleUtils; |
{ | ||
"name": "@sketch-hq/sketch-assistant-utils", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"module": "dist/esm/index", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
170417
2049