@kitschpatrol/mdat-config 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{cosmiconfig as e}from"cosmiconfig";import{TypeScriptLoader as t}from"cosmiconfig-typescript-loader";import{execa as n}from"execa";import r from"fs-extra";import i from"node:fs";import a from"node:path";import{PassThrough as o,Transform as s}from"node:stream";import{fileURLToPath as c}from"node:url";import{packageUp as l,packageUpSync as u}from"package-up";import d from"picocolors";import f from"yargs";import{hideBin as p}from"yargs/helpers";import m from"@pinojs/json-colorizer";import h from"decircular";import g from"deepmerge";import _ from"json-stringify-pretty-compact";import{findWorkspaces as v,findWorkspacesRoot as y}from"find-workspaces";import b from"node:fs/promises";import{stripVTControlCharacters as x}from"node:util";import{loadConfig as S}from"mdat";var C=`7.1.0`;function w(e){return e instanceof Error&&`exitCode`in e&&typeof e.exitCode==`number`}function T(e){return m(_(h(e),{indent:2,replacer(e,t){return typeof t==`function`?t.name:t}}),{colors:{BRACKET:`gray`}})}const E=(e,t,n)=>{let r=[...e];for(let[i,a]of t.entries())r[i]===void 0?r[i]=n.cloneUnlessOtherwiseSpecified(a,n):n.isMergeableObject(a)?r[i]=D(e[i],a,n):e.includes(a)||r.push(a);return r};function D(e,t,n={arrayMerge:E}){return g(e,t,n)}function O(e,t){return e.startsWith(t+a.sep)}function k(){let e=A(),t=new Set([e]),n=v();if(n!==null)for(let r of n){let n=a.resolve(r.location);O(n,e)&&t.add(n)}return[...t]}function A(){let e=u();if(e===void 0)throw Error(`No package.json found.`);return a.dirname(e)}function j(){let e=y();return e===null?A():a.resolve(e.location)}function M(e){if(e===`workspace-root`)return j();if(e===`package-dir`)return A();if(typeof e==`string`){if(!r.pathExistsSync(e))throw Error(`Custom cwd directory does not exist: ${e}`);return e}return process.cwd()}async function ee(e,t){try{let{default:n}=await import(`prettier`),r=await n.resolveConfig(e),i=await n.format(t,{filepath:e,...r});await b.writeFile(e,i,`utf8`)}catch{console.warn(`Skipped formatting ${e} since Prettier is not installed.`)}}async function N(e){try{await ee(e,await b.readFile(e,`utf8`))}catch{}}const P=/\r?\n/;function F(e){return new s({transform(t,n,r){let i=t.toString().split(P).filter(t=>t.trim()!==``&&!e(x(t))).join(`
2
+ import{cosmiconfig as e}from"cosmiconfig";import{TypeScriptLoader as t}from"cosmiconfig-typescript-loader";import{execa as n}from"execa";import r from"fs-extra";import i from"node:fs";import a from"node:path";import{PassThrough as o,Transform as s}from"node:stream";import{fileURLToPath as c}from"node:url";import{packageUp as l,packageUpSync as u}from"package-up";import d from"picocolors";import f from"yargs";import{hideBin as p}from"yargs/helpers";import m from"@pinojs/json-colorizer";import h from"decircular";import g from"deepmerge";import _ from"json-stringify-pretty-compact";import{findWorkspaces as v,findWorkspacesRoot as y}from"find-workspaces";import b from"node:fs/promises";import{stripVTControlCharacters as x}from"node:util";import{loadConfig as S}from"mdat";var C=`7.2.0`;function w(e){return e instanceof Error&&`exitCode`in e&&typeof e.exitCode==`number`}function T(e){return m(_(h(e),{indent:2,replacer(e,t){return typeof t==`function`?t.name:t}}),{colors:{BRACKET:`gray`}})}const E=(e,t,n)=>{let r=[...e];for(let[i,a]of t.entries())r[i]===void 0?r[i]=n.cloneUnlessOtherwiseSpecified(a,n):n.isMergeableObject(a)?r[i]=D(e[i],a,n):e.includes(a)||r.push(a);return r};function D(e,t,n={arrayMerge:E}){return g(e,t,n)}function O(e,t){return e.startsWith(t+a.sep)}function k(){let e=A(),t=new Set([e]),n=v();if(n!==null)for(let r of n){let n=a.resolve(r.location);O(n,e)&&t.add(n)}return[...t]}function A(){let e=u();if(e===void 0)throw Error(`No package.json found.`);return a.dirname(e)}function j(){let e=y();return e===null?A():a.resolve(e.location)}function M(e){if(e===`workspace-root`)return j();if(e===`package-dir`)return A();if(typeof e==`string`){if(!r.pathExistsSync(e))throw Error(`Custom cwd directory does not exist: ${e}`);return e}return process.cwd()}async function ee(e,t){try{let{default:n}=await import(`prettier`),r=await n.resolveConfig(e),i=await n.format(t,{filepath:e,...r});await b.writeFile(e,i,`utf8`)}catch{console.warn(`Skipped formatting ${e} since Prettier is not installed.`)}}async function N(e){try{await ee(e,await b.readFile(e,`utf8`))}catch{}}const P=/\r?\n/;function F(e){return new s({transform(t,n,r){let i=t.toString().split(P).filter(t=>t.trim()!==``&&!e(x(t))).join(`
3
3
  `);this.push(i+`
4
4
  `),r()}})}function I(e,t){return new s({transform(n,r,i){let a=n.toString().split(P).filter(e=>e.trim().length>0).map(n=>`${e?t===void 0?e:d[t](e):``} ${n}\n`).join(``);this.push(a),i()}})}async function L(e){let t=[];return new Promise((n,r)=>{e.on(`data`,e=>t.push(e)),e.on(`error`,e=>{r(e)}),e.on(`end`,()=>{n(Buffer.concat(t).toString(`utf8`))})})}function R(e,t){return t===1?e:e+`s`}async function z(e,t,n,r,i){let a=1,o;if(r.logPrefix===void 0)o=e;else{let t=I(r.logPrefix,r.logColor);t.pipe(e),o=t}i&&o.write(d.bold(`Running: "${r.name}() with Positional arguments: ${String(t)} and Option flags: ${String(n)}"`));try{a=await r.execute(o,t,n)}catch(e){console.error(String(e)),a=1}return a}async function B(e,t,r,i,a){let s=1,c;if(i.logPrefix===void 0)c=e;else{let t=I(i.logPrefix,i.logColor);t.pipe(e),c=t}let l=i.subcommands??[],u=[...i.receivePositionalArguments?t:[],...i.positionalArguments??[]],d=[...i.receiveOptionFlags?r:[],...i.optionFlags??[]],f=[...l,...d,...u],p=M(i.cwdOverride);a&&c.write(`Running: "${i.name} ${f.join(` `)}"`);let m=i.prettyJsonOutput?new o:c;try{let e=n(i.name,f,{cwd:p,env:{...process.env.NO_COLOR===void 0?{FORCE_COLOR:`true`}:{}},preferLocal:!0,reject:!1,stdin:`inherit`});if(i.outputFilter){let t=F(i.outputFilter),n=F(i.outputFilter);e.stdout.pipe(t).pipe(m,{end:!1}),e.stderr.pipe(n).pipe(m,{end:!1})}else e.stdout.pipe(m,{end:!1}),e.stderr.pipe(m,{end:!1});if(await e,i.prettyJsonOutput){m.end();let e=await L(m),t=T(JSON.parse(e)).split(`
5
5
  `);for(let e of t)c.write(`${e}\n`)}s=e.exitCode??1}catch(e){console.error(`${i.name} failed with error:`),console.error(e),w(e)&&(s=typeof e.exitCode==`number`?e.exitCode:1)}return s}function V(e){return`execute`in e}const H=/^ksc-/;function U(e){return e.replace(H,``)}function W(e){return e===void 0||e.length===0?[]:e.flatMap(e=>e.split(`,`)).map(e=>U(e.trim()))}function G(e){return e.option(`skip`,{array:!0,describe:`Tool names to skip (with or without "ksc-" prefix).`,type:`string`})}async function K(e,t,n,r,i,a,o){let s=o??[],c=[],l=[];for(let e of r)s.length>0&&s.includes(U(e.name))?l.push(e):c.push(e);if(s.length>0){let t=new Set(l.map(e=>U(e.name))),n=s.filter(e=>!t.has(e));if(n.length>0){let t=r.map(e=>U(e.name)).join(`, `);e.write(`⚠️ ${d.yellow(`Unrecognized --skip ${R(`value`,n.length)}: ${n.join(`, `)}. Available: ${t}`)}\n`)}}let u=[];for(let r of c){let a=await(V(r)?z(e,t,n,r,i):B(e,t,n,r,i));u.push({exitCode:a,name:r.name})}let f=r.length;if(l.length>0){let t=l.map(({name:e})=>e);e.write(`⏭️ ${d.dim(d.bold(`${t.length} / ${f} ${R(`Command`,t.length)} Skipped:`))} ${d.dim(t.join(`, `))}\n`)}if(a){let t=u.filter(({exitCode:e})=>e===0).map(({name:e})=>e),n=u.filter(({exitCode:e})=>e!==0).map(({name:e})=>e);t.length>0&&e.write(`✅ ${d.green(d.bold(`${t.length} / ${f} ${R(`Command`,t.length)} Succeeded:`))} ${d.green(t.join(`, `))}\n`),n.length>0&&e.write(`❌ ${d.red(d.bold(`${n.length} / ${f} ${R(`Command`,n.length)} Failed:`))} ${d.red(n.join(`, `))}\n`)}return u.every(({exitCode:e})=>e===0)?0:1}async function q(e,t,n,o){let s=await l();if(s===void 0)throw Error("The `init` command must be used in a directory with a package.json file");let u=await l({cwd:c(import.meta.url)});if(u===void 0)return e.write(`Error: The script being called was not in a package, weird.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Config } from "mdat";
1
+ import { Config, Config as MdatConfig } from "mdat";
2
2
 
3
- //#region src/index.d.ts
3
+ //#region src/config.d.ts
4
4
  declare const sharedMdatConfig: Config;
5
5
  /**
6
6
  * **@Kitschpatrol's Shared Mdat Configuration**
@@ -15,4 +15,31 @@ declare const sharedMdatConfig: Config;
15
15
  */
16
16
  declare function mdatConfig(config?: Config): Config;
17
17
  //#endregion
18
- export { sharedMdatConfig as default, mdatConfig };
18
+ //#region src/api.d.ts
19
+ /**
20
+ * Expand Mdat comment placeholders in a Markdown string using the shared
21
+ * configuration.
22
+ *
23
+ * @param source - The Markdown source string.
24
+ * @param config - Optional `MdatConfig` overrides merged on top of the shared
25
+ * config.
26
+ *
27
+ * @returns The expanded Markdown string.
28
+ */
29
+ declare function fix(source: string, config?: MdatConfig): Promise<string>;
30
+ /**
31
+ * Expand Mdat comment placeholders in a Markdown file in place using the shared
32
+ * configuration.
33
+ *
34
+ * @param filePath - Path to the Markdown file.
35
+ * @param config - Optional `MdatConfig` overrides merged on top of the shared
36
+ * config.
37
+ */
38
+ declare function fixFile(filePath: string, config?: MdatConfig): Promise<void>;
39
+ /**
40
+ * Clear the cached Mdat module. Subsequent calls to `fix` or `fixFile` will
41
+ * re-import Mdat.
42
+ */
43
+ declare function clearCache(): void;
44
+ //#endregion
45
+ export { type MdatConfig, clearCache, sharedMdatConfig as default, sharedMdatConfig, fix, fixFile, mdatConfig };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{mergeConfig as e}from"mdat";const t={"shared-config":`## Project configuration
1
+ import e from"node:fs/promises";import{mergeConfig as t}from"mdat";const n={"shared-config":`## Project configuration
2
2
 
3
- This project uses [@kitschpatrol/shared-config](https://github.com/kitschpatrol/shared-config) to consolidate various linting and formatting tool configurations under a single dependency and the CLI command \`ksc\`. (ESLint, Prettier, CSpell, etc.)`};function n(n){return e(t,n??{})}export{t as default,n as mdatConfig};
3
+ This project uses [@kitschpatrol/shared-config](https://github.com/kitschpatrol/shared-config) to consolidate various linting and formatting tool configurations under a single dependency and the CLI command \`ksc\`. (ESLint, Prettier, CSpell, etc.)`};function r(e){return t(n,e??{})}let i;async function a(){if(!i){let e=await import(`mdat`);i={expandString:e.expandString,mergeConfig:e.mergeConfig}}return i}async function o(e,t){let{expandString:r,mergeConfig:i}=await a(),o=await r(e,t?i(n,t):n,{format:!0});return String(o)}async function s(t,r){let i=await e.readFile(t,`utf8`),{expandString:o,mergeConfig:s}=await a(),c=await o(i,r?s(n,r):n,{format:!0});await e.writeFile(t,String(c),`utf8`)}function c(){i=void 0}export{c as clearCache,n as default,n as sharedMdatConfig,o as fix,s as fixFile,r as mdatConfig};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kitschpatrol/mdat-config",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "MDAT configuration for @kitschpatrol/shared-config.",
5
5
  "keywords": [
6
6
  "shared-config",
@@ -53,7 +53,7 @@
53
53
  "find-workspaces": "^0.3.1",
54
54
  "fs-extra": "^11.3.4",
55
55
  "json-stringify-pretty-compact": "^4.0.0",
56
- "mdat": "^2.2.0",
56
+ "mdat": "^2.2.1",
57
57
  "package-up": "^5.0.0",
58
58
  "picocolors": "^1.1.1",
59
59
  "prettier": "^3.8.1",
@@ -67,6 +67,7 @@
67
67
  },
68
68
  "scripts": {
69
69
  "build": "tsdown",
70
- "cli": "node ./bin/cli.js"
70
+ "cli": "node ./bin/cli.js",
71
+ "test": "vitest run"
71
72
  }
72
73
  }
package/readme.md CHANGED
@@ -187,6 +187,30 @@ ksc-mdat print-config
187
187
 
188
188
  <!-- /cli-help -->
189
189
 
190
+ ### API
191
+
192
+ The package also exports `fix`, `fixFile` functions for expanding Mdat comment placeholders programmatically, pre-configured with the shared Mdat configuration. The [mdat](https://github.com/kitschpatrol/mdat) project already provides a robust TypeScript API and CLI for general use cases, but these proxies are provided for convenience in @kitschpatrol/shared-config projects.
193
+
194
+ ```typescript
195
+ import { clearCache, fix, fixFile } from '@kitschpatrol/mdat-config'
196
+
197
+ // Expand mdat placeholders in a string using the default config
198
+ const expanded = await fix('<!-- shared-config -->\n<!-- /shared-config -->\n')
199
+
200
+ // Expand with custom rules
201
+ const customExpanded = await fix(source, { 'my-rule': '**Custom content.**' })
202
+
203
+ // Expand with custom rules in a file in place
204
+ await fixFile('./readme.md', { 'my-rule': '**Custom content.**' })
205
+
206
+ // Clear cached Mdat module
207
+ clearCache()
208
+ ```
209
+
210
+ Config is merged in priority order: shared defaults < per-call overrides (via mdat's `mergeConfig`).
211
+
212
+ The Mdat module is cached internally for performance across multiple calls. Use `clearCache()` to force re-initialization.
213
+
190
214
  <!-- license -->
191
215
 
192
216
  ## License