UNPKG

@clerc/plugin-strict-flags

Version:
59 lines (54 loc) 1.45 kB
import { definePlugin } from '@clerc/core'; const locales$1 = { "en": { "utils.and": "%s and %s" }, "zh-CN": { "utils.and": "%s \u548C %s" } }; function semanticArray(arr, { add, t }) { add(locales$1); if (arr.length <= 1) { return arr[0]; } return t("utils.and", arr.slice(0, -1).join(", "), arr[arr.length - 1]); } const locales = { "en": { "strictFlags.unexpectedSingle": "Unexpected flag: %s.", "strictFlags.unexpectedMore": "Unexpected flags: %s.", "strictFlags.and": "%s and %s" }, "zh-CN": { "strictFlags.unexpectedSingle": "\u9884\u671F\u4E4B\u5916\u7684\u6807\u5FD7: %s\u3002", "strictFlags.unexpectedMore": "\u9884\u671F\u4E4B\u5916\u7684\u6807\u5FD7: %s\u3002", "strictFlags.and": "%s \u548C %s" } }; const strictFlagsPlugin = () => definePlugin({ setup: (cli) => { const { add, t } = cli.i18n; add(locales); return cli.interceptor((ctx, next) => { const keys = Object.keys(ctx.unknownFlags); if (!ctx.resolved || keys.length === 0) { next(); } else { const error = keys.length > 1 ? new Error( t( "strictFlags.unexpectedMore", semanticArray(keys, cli.i18n) ) ) : new Error( t( "strictFlags.unexpectedSingle", semanticArray(keys, cli.i18n) ) ); throw error; } }); } }); export { strictFlagsPlugin };