@ai-citizens/utils
Advanced tools
| /** | ||
| * Parse XML from a string | ||
| * @param xml - The XML string to parse | ||
| * @returns The parsed XML as an object | ||
| * @TODO - add dynamic type checking to the parser | ||
| */ | ||
| export declare const parseXml: (xml: string) => any; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.parseXml = void 0; | ||
| const fast_xml_parser_1 = require("fast-xml-parser"); | ||
| const parser = new fast_xml_parser_1.XMLParser(); | ||
| /** | ||
| * Parse XML from a string | ||
| * @param xml - The XML string to parse | ||
| * @returns The parsed XML as an object | ||
| * @TODO - add dynamic type checking to the parser | ||
| */ | ||
| const parseXml = (xml) => { | ||
| const xmlDoc = parser.parse(`<root>${xml}</root>`); | ||
| return xmlDoc.root || {}; | ||
| }; | ||
| exports.parseXml = parseXml; |
| /** | ||
| * Parse XML from a string | ||
| * @param xml - The XML string to parse | ||
| * @returns The parsed XML as an object | ||
| * @TODO - add dynamic type checking to the parser | ||
| */ | ||
| export declare const parseXml: (xml: string) => any; |
| import { XMLParser } from "fast-xml-parser"; | ||
| const parser = new XMLParser(); | ||
| /** | ||
| * Parse XML from a string | ||
| * @param xml - The XML string to parse | ||
| * @returns The parsed XML as an object | ||
| * @TODO - add dynamic type checking to the parser | ||
| */ | ||
| export const parseXml = (xml) => { | ||
| const xmlDoc = parser.parse(`<root>${xml}</root>`); | ||
| return xmlDoc.root || {}; | ||
| }; |
| import { XMLParser } from "fast-xml-parser"; | ||
| const parser = new XMLParser(); | ||
| /** | ||
| * Parse XML from a string | ||
| * @param xml - The XML string to parse | ||
| * @returns The parsed XML as an object | ||
| * @TODO - add dynamic type checking to the parser | ||
| */ | ||
| export const parseXml = (xml: string) => { | ||
| const xmlDoc = parser.parse(`<root>${xml}</root>`); | ||
| return xmlDoc.root || {}; | ||
| }; |
+6
-0
| # @ai-citizens/utils | ||
| ## 0.0.2 | ||
| ### Patch Changes | ||
| - b230327: additional cli improvements, new graphs and functionality | ||
| ## 0.0.1 | ||
@@ -4,0 +10,0 @@ |
| interface ConvertOptions { | ||
| /** | ||
| * An array of paths to ignore. | ||
| */ | ||
| ignore?: string[]; | ||
| /** | ||
| * If provided, the output will be saved to a file named `converted-dir-output.txt` in the specified directory. | ||
| */ | ||
| fileExtensions?: string[]; | ||
| outputPath?: string; | ||
| writeToCWD?: boolean; | ||
| } | ||
| declare function convertDirToTextFile(dirPath: string, options: ConvertOptions): Promise<string>; | ||
| export default convertDirToTextFile; | ||
| export { convertDirToTextFile }; |
@@ -6,8 +6,49 @@ "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.convertDirToTextFile = convertDirToTextFile; | ||
| const promises_1 = __importDefault(require("node:fs/promises")); | ||
| const node_path_1 = __importDefault(require("node:path")); | ||
| const minimatch_1 = require("minimatch"); | ||
| async function convertDirToTextFile(dirPath, options) { | ||
| const { ignore = [], outputPath } = options; | ||
| const { ignore = [ | ||
| "**/.git/**", | ||
| "**/node_modules/**", | ||
| "**/.DS_Store", | ||
| "**/package-lock.json", | ||
| "**/yarn.lock", | ||
| "**/pnpm-lock.yaml", | ||
| ], outputPath, writeToCWD = false, } = options; | ||
| let output = ""; | ||
| // Function to generate directory structure | ||
| const textExtensions = [ | ||
| ".txt", | ||
| ".md", | ||
| ".js", | ||
| ".ts", | ||
| ".jsx", | ||
| ".tsx", | ||
| ".json", | ||
| ".yml", | ||
| ".yaml", | ||
| ".html", | ||
| ".css", | ||
| ".scss", | ||
| ".less", | ||
| ".vue", | ||
| ".svelte", | ||
| ".py", | ||
| ".rb", | ||
| ".java", | ||
| ".c", | ||
| ".cpp", | ||
| ".h", | ||
| ".hpp", | ||
| ".cs", | ||
| ".go", | ||
| ".rs", | ||
| ".php", | ||
| ...(options.fileExtensions || []), | ||
| ]; | ||
| function shouldInclude(filePath) { | ||
| const relativePath = node_path_1.default.relative(dirPath, filePath); | ||
| return !ignore.some((pattern) => (0, minimatch_1.minimatch)(relativePath, pattern, { dot: true })); | ||
| } | ||
| async function generateDirStructure(currentPath, prefix = "") { | ||
@@ -37,31 +78,32 @@ const entries = await promises_1.default.readdir(currentPath, { withFileTypes: true }); | ||
| const entries = await promises_1.default.readdir(currentPath, { withFileTypes: true }); | ||
| const processTasks = entries.map(async (entry) => { | ||
| for (const entry of entries) { | ||
| const fullPath = node_path_1.default.join(currentPath, entry.name); | ||
| const relativePath = node_path_1.default.relative(dirPath, fullPath); | ||
| if (ignore.some((ignorePath) => relativePath.startsWith(ignorePath))) { | ||
| return; | ||
| } | ||
| if (entry.isDirectory()) { | ||
| await processDirectory(fullPath); | ||
| } | ||
| else if (entry.isFile()) { | ||
| const content = await promises_1.default.readFile(fullPath, "utf8"); | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| else if (shouldInclude(fullPath)) { | ||
| try { | ||
| const content = textExtensions.includes(node_path_1.default.extname(fullPath).toLowerCase()) | ||
| ? await promises_1.default.readFile(fullPath, "utf8") | ||
| : "[Ignored file extension]"; | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| } | ||
| catch (error) { | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n[Error reading file: ${error.message}]\n\n`; | ||
| } | ||
| } | ||
| }); | ||
| await Promise.all(processTasks); | ||
| } | ||
| } | ||
| // Generate directory structure | ||
| const dirStructure = await generateDirStructure(dirPath); | ||
| const dirName = node_path_1.default.basename(dirPath); | ||
| output = `${dirName}/\n${dirStructure}\n${output}`; | ||
| output += "Directory structure:\n"; | ||
| output += await generateDirStructure(dirPath); | ||
| output += "\nFile contents:\n"; | ||
| await processDirectory(dirPath); | ||
| if (outputPath) { | ||
| const outputFileName = `${dirName}-converted-dir-output.txt`; | ||
| await promises_1.default.writeFile(node_path_1.default.join(outputPath, outputFileName), output); | ||
| await promises_1.default.writeFile(outputPath, output); | ||
| } | ||
| else if (writeToCWD) { | ||
| await promises_1.default.writeFile(`${process.cwd()}/${dirPath.split("/").pop()}.txt`, output); | ||
| } | ||
| return output; | ||
| } | ||
| exports.default = convertDirToTextFile; | ||
| // example usage | ||
| // convertDirToTextFile('./src/lib/utils', {outputPath: './src/lib/utils/output.txt'}) |
@@ -1,2 +0,3 @@ | ||
| import convertDirToTextFile from "./convert-dir-to-text-file.js"; | ||
| export { convertDirToTextFile }; | ||
| import { convertDirToTextFile } from "./convert-dir-to-text-file.js"; | ||
| import { parseXml } from "./parse-xml.js"; | ||
| export { convertDirToTextFile, parseXml }; |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.convertDirToTextFile = void 0; | ||
| const convert_dir_to_text_file_js_1 = __importDefault(require("./convert-dir-to-text-file.js")); | ||
| exports.convertDirToTextFile = convert_dir_to_text_file_js_1.default; | ||
| exports.parseXml = exports.convertDirToTextFile = void 0; | ||
| const convert_dir_to_text_file_js_1 = require("./convert-dir-to-text-file.js"); | ||
| Object.defineProperty(exports, "convertDirToTextFile", { enumerable: true, get: function () { return convert_dir_to_text_file_js_1.convertDirToTextFile; } }); | ||
| const parse_xml_js_1 = require("./parse-xml.js"); | ||
| Object.defineProperty(exports, "parseXml", { enumerable: true, get: function () { return parse_xml_js_1.parseXml; } }); |
| interface ConvertOptions { | ||
| /** | ||
| * An array of paths to ignore. | ||
| */ | ||
| ignore?: string[]; | ||
| /** | ||
| * If provided, the output will be saved to a file named `converted-dir-output.txt` in the specified directory. | ||
| */ | ||
| fileExtensions?: string[]; | ||
| outputPath?: string; | ||
| writeToCWD?: boolean; | ||
| } | ||
| declare function convertDirToTextFile(dirPath: string, options: ConvertOptions): Promise<string>; | ||
| export default convertDirToTextFile; | ||
| export { convertDirToTextFile }; |
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { minimatch } from "minimatch"; | ||
| async function convertDirToTextFile(dirPath, options) { | ||
| const { ignore = [], outputPath } = options; | ||
| const { ignore = [ | ||
| "**/.git/**", | ||
| "**/node_modules/**", | ||
| "**/.DS_Store", | ||
| "**/package-lock.json", | ||
| "**/yarn.lock", | ||
| "**/pnpm-lock.yaml", | ||
| ], outputPath, writeToCWD = false, } = options; | ||
| let output = ""; | ||
| // Function to generate directory structure | ||
| const textExtensions = [ | ||
| ".txt", | ||
| ".md", | ||
| ".js", | ||
| ".ts", | ||
| ".jsx", | ||
| ".tsx", | ||
| ".json", | ||
| ".yml", | ||
| ".yaml", | ||
| ".html", | ||
| ".css", | ||
| ".scss", | ||
| ".less", | ||
| ".vue", | ||
| ".svelte", | ||
| ".py", | ||
| ".rb", | ||
| ".java", | ||
| ".c", | ||
| ".cpp", | ||
| ".h", | ||
| ".hpp", | ||
| ".cs", | ||
| ".go", | ||
| ".rs", | ||
| ".php", | ||
| ...(options.fileExtensions || []), | ||
| ]; | ||
| function shouldInclude(filePath) { | ||
| const relativePath = path.relative(dirPath, filePath); | ||
| return !ignore.some((pattern) => minimatch(relativePath, pattern, { dot: true })); | ||
| } | ||
| async function generateDirStructure(currentPath, prefix = "") { | ||
@@ -31,31 +71,33 @@ const entries = await fs.readdir(currentPath, { withFileTypes: true }); | ||
| const entries = await fs.readdir(currentPath, { withFileTypes: true }); | ||
| const processTasks = entries.map(async (entry) => { | ||
| for (const entry of entries) { | ||
| const fullPath = path.join(currentPath, entry.name); | ||
| const relativePath = path.relative(dirPath, fullPath); | ||
| if (ignore.some((ignorePath) => relativePath.startsWith(ignorePath))) { | ||
| return; | ||
| } | ||
| if (entry.isDirectory()) { | ||
| await processDirectory(fullPath); | ||
| } | ||
| else if (entry.isFile()) { | ||
| const content = await fs.readFile(fullPath, "utf8"); | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| else if (shouldInclude(fullPath)) { | ||
| try { | ||
| const content = textExtensions.includes(path.extname(fullPath).toLowerCase()) | ||
| ? await fs.readFile(fullPath, "utf8") | ||
| : "[Ignored file extension]"; | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| } | ||
| catch (error) { | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n[Error reading file: ${error.message}]\n\n`; | ||
| } | ||
| } | ||
| }); | ||
| await Promise.all(processTasks); | ||
| } | ||
| } | ||
| // Generate directory structure | ||
| const dirStructure = await generateDirStructure(dirPath); | ||
| const dirName = path.basename(dirPath); | ||
| output = `${dirName}/\n${dirStructure}\n${output}`; | ||
| output += "Directory structure:\n"; | ||
| output += await generateDirStructure(dirPath); | ||
| output += "\nFile contents:\n"; | ||
| await processDirectory(dirPath); | ||
| if (outputPath) { | ||
| const outputFileName = `${dirName}-converted-dir-output.txt`; | ||
| await fs.writeFile(path.join(outputPath, outputFileName), output); | ||
| await fs.writeFile(outputPath, output); | ||
| } | ||
| else if (writeToCWD) { | ||
| await fs.writeFile(`${process.cwd()}/${dirPath.split("/").pop()}.txt`, output); | ||
| } | ||
| return output; | ||
| } | ||
| export default convertDirToTextFile; | ||
| // example usage | ||
| // convertDirToTextFile('./src/lib/utils', {outputPath: './src/lib/utils/output.txt'}) | ||
| export { convertDirToTextFile }; |
@@ -1,2 +0,3 @@ | ||
| import convertDirToTextFile from "./convert-dir-to-text-file.js"; | ||
| export { convertDirToTextFile }; | ||
| import { convertDirToTextFile } from "./convert-dir-to-text-file.js"; | ||
| import { parseXml } from "./parse-xml.js"; | ||
| export { convertDirToTextFile, parseXml }; |
@@ -1,2 +0,3 @@ | ||
| import convertDirToTextFile from "./convert-dir-to-text-file.js"; | ||
| export { convertDirToTextFile }; | ||
| import { convertDirToTextFile } from "./convert-dir-to-text-file.js"; | ||
| import { parseXml } from "./parse-xml.js"; | ||
| export { convertDirToTextFile, parseXml }; |
+7
-4
| { | ||
| "name": "@ai-citizens/utils", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "a collection of utility functions for AI powered applications", | ||
@@ -23,6 +23,9 @@ "type": "module", | ||
| "peerDependencies": { | ||
| "typescript": "^5.5.4", | ||
| "@types/node": "^18" | ||
| "@types/node": "^18", | ||
| "typescript": "^5.5.4" | ||
| }, | ||
| "devDependencies": {}, | ||
| "dependencies": { | ||
| "fast-xml-parser": "^4.4.0", | ||
| "minimatch": "^10.0.1" | ||
| }, | ||
| "scripts": { | ||
@@ -29,0 +32,0 @@ "build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json", |
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { minimatch } from "minimatch"; | ||
| interface ConvertOptions { | ||
| /** | ||
| * An array of paths to ignore. | ||
| */ | ||
| ignore?: string[]; | ||
| /** | ||
| * If provided, the output will be saved to a file named `converted-dir-output.txt` in the specified directory. | ||
| */ | ||
| fileExtensions?: string[]; | ||
| outputPath?: string; | ||
| writeToCWD?: boolean; | ||
| } | ||
@@ -19,6 +16,54 @@ | ||
| ): Promise<string> { | ||
| const { ignore = [], outputPath } = options; | ||
| const { | ||
| ignore = [ | ||
| "**/.git/**", | ||
| "**/node_modules/**", | ||
| "**/.DS_Store", | ||
| "**/package-lock.json", | ||
| "**/yarn.lock", | ||
| "**/pnpm-lock.yaml", | ||
| ], | ||
| outputPath, | ||
| writeToCWD = false, | ||
| } = options; | ||
| let output = ""; | ||
| // Function to generate directory structure | ||
| const textExtensions = [ | ||
| ".txt", | ||
| ".md", | ||
| ".js", | ||
| ".ts", | ||
| ".jsx", | ||
| ".tsx", | ||
| ".json", | ||
| ".yml", | ||
| ".yaml", | ||
| ".html", | ||
| ".css", | ||
| ".scss", | ||
| ".less", | ||
| ".vue", | ||
| ".svelte", | ||
| ".py", | ||
| ".rb", | ||
| ".java", | ||
| ".c", | ||
| ".cpp", | ||
| ".h", | ||
| ".hpp", | ||
| ".cs", | ||
| ".go", | ||
| ".rs", | ||
| ".php", | ||
| ...(options.fileExtensions || []), | ||
| ]; | ||
| function shouldInclude(filePath: string): boolean { | ||
| const relativePath = path.relative(dirPath, filePath); | ||
| return !ignore.some((pattern) => | ||
| minimatch(relativePath, pattern, { dot: true }) | ||
| ); | ||
| } | ||
| async function generateDirStructure( | ||
@@ -66,25 +111,26 @@ currentPath: string, | ||
| const processTasks = entries.map(async (entry) => { | ||
| for (const entry of entries) { | ||
| const fullPath = path.join(currentPath, entry.name); | ||
| const relativePath = path.relative(dirPath, fullPath); | ||
| if (ignore.some((ignorePath) => relativePath.startsWith(ignorePath))) { | ||
| return; | ||
| } | ||
| if (entry.isDirectory()) { | ||
| await processDirectory(fullPath); | ||
| } else if (entry.isFile()) { | ||
| const content = await fs.readFile(fullPath, "utf8"); | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| } else if (shouldInclude(fullPath)) { | ||
| try { | ||
| const content = textExtensions.includes( | ||
| path.extname(fullPath).toLowerCase() | ||
| ) | ||
| ? await fs.readFile(fullPath, "utf8") | ||
| : "[Ignored file extension]"; | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n${content}\n\n`; | ||
| } catch (error) { | ||
| output += `-----------------------------\nFile: ${relativePath}\n-----------------------------\n\n[Error reading file: ${error.message}]\n\n`; | ||
| } | ||
| } | ||
| }); | ||
| await Promise.all(processTasks); | ||
| } | ||
| } | ||
| // Generate directory structure | ||
| const dirStructure = await generateDirStructure(dirPath); | ||
| const dirName = path.basename(dirPath); | ||
| output = `${dirName}/\n${dirStructure}\n${output}`; | ||
| output += "Directory structure:\n"; | ||
| output += await generateDirStructure(dirPath); | ||
| output += "\nFile contents:\n"; | ||
@@ -94,11 +140,13 @@ await processDirectory(dirPath); | ||
| if (outputPath) { | ||
| const outputFileName = `${dirName}-converted-dir-output.txt`; | ||
| await fs.writeFile(path.join(outputPath, outputFileName), output); | ||
| await fs.writeFile(outputPath, output); | ||
| } else if (writeToCWD) { | ||
| await fs.writeFile( | ||
| `${process.cwd()}/${dirPath.split("/").pop()}.txt`, | ||
| output | ||
| ); | ||
| } | ||
| return output; | ||
| } | ||
| export default convertDirToTextFile; | ||
| // example usage | ||
| // convertDirToTextFile('./src/lib/utils', {outputPath: './src/lib/utils/output.txt'}) | ||
| export { convertDirToTextFile }; |
@@ -1,3 +0,3 @@ | ||
| import convertDirToTextFile from "./convert-dir-to-text-file.js"; | ||
| export { convertDirToTextFile }; | ||
| import { convertDirToTextFile } from "./convert-dir-to-text-file.js"; | ||
| import { parseXml } from "./parse-xml.js"; | ||
| export { convertDirToTextFile, parseXml }; |
+1
-1
@@ -16,3 +16,3 @@ { | ||
| "paths": { | ||
| "@/*": ["src/*"] | ||
| "@ai-citizen/*": ["../*"] | ||
| } | ||
@@ -19,0 +19,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
85130
8.4%26
23.81%476
60.81%4
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added