MCPcopy Index your code
hub / github.com/tinyplex/tinybase / copyDefinition

Function copyDefinition

gulpfile.mjs:263–340  ·  view source on GitHub ↗
(dir, module)

Source from the content-addressed store, hash-verified

261};
262
263const copyDefinition = async (dir, module) => {
264 const labelBlocks = await getLabelBlocks();
265 const {version} = JSON.parse(readFileSync('./package.json', UTF8));
266 const siteRoot = version.includes('beta')
267 ? 'https://beta.tinybase.org'
268 : 'https://tinybase.org';
269 // Add easier-to-read with-schemas blocks
270 const codeBlocks = new Map();
271 [
272 ...(
273 await promises.readFile(`src/@types/${module}/index.d.ts`, UTF8)
274 ).matchAll(TYPES_DOC_CODE_BLOCKS),
275 ].forEach(([_, label, code]) => {
276 const prefix = code.match(/^\n\s*/m)?.[0];
277 if (prefix) {
278 codeBlocks.set(
279 label,
280 code
281 .replace(/export type \S+ =\s/, '')
282 .replace(/export function /, '')
283 .replaceAll(prefix, prefix + ' * '),
284 );
285 }
286 });
287 const absolutizeDocMedia = (block) =>
288 block.replace(/\]\((?:\s*\*\s*)?\/shots\//g, `](${siteRoot}/shots/`);
289
290 const fileRewrite = (block, addOverrideSnippet) =>
291 absolutizeDocMedia(
292 block.replace(TYPES_DOC_CODE_BLOCKS, (_, label, code) => {
293 if (labelBlocks.has(label)) {
294 const codeOverride = codeBlocks.get(label);
295 let block = labelBlocks.get(label);
296 if (
297 addOverrideSnippet &&
298 codeBlocks.has(label) &&
299 code.includes('<') &&
300 code.includes('Schema') &&
301 !codeOverride.endsWith('{')
302 ) {
303 const prefix = block.match(/^\s+\*$/m)?.[0];
304 if (prefix) {
305 const line = '\n' + prefix;
306 block = block.replace(
307 /^\s+\*$/m,
308 `${prefix}${line}` +
309 ' This has schema-based typing.' +
310 ' The following is a simplified representation:' +
311 `${line}${line} \`\`\`ts override` +
312 codeOverride.trimEnd() +
313 `${line} \`\`\`${line}`,
314 );
315 }
316 code = code.replace(/^\s*?\/\/\/.*?\n/gm, '');
317 }
318 return block + code;
319 }
320 throw `Missing docs label ${label} in ${module}`;

Callers 1

copyDefinitionsFunction · 0.85

Calls 4

getLabelBlocksFunction · 0.85
allOfFunction · 0.85
ensureDirFunction · 0.85
fileRewriteFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…