D
Deno

help

walk() include only last directory...

MMqx1/22/2024
Hey, I'm currently trying to write a replacer for file names, file content and folder names. My goal is to crawl over a certain directory and then replace a certain pattern in file names, file content and folder names. Currently I use the walk() function from the std library. My problem now is that walk() returns every single directory and not just the last one. In other words, walk() does not just give me:
file/to/directory
file/to/directory
but:
file
file/to
file/to/directory
file
file/to
file/to/directory
What is the best way to filter this? There may also be a better method... This is what my current attempt looks like:
import { parseArgs } from 'https://deno.land/std@0.212.0/cli/mod.ts';
import { walkSync } from 'https://deno.land/std@0.212.0/fs/mod.ts';
import { parseString as parseRegExpString } from 'https://raw.githubusercontent.com/TypeScriptPlayground/std/main/src/regexp/mod.ts'

const args = parseArgs<{
path : string,
pattern : string,
replacer : string
}>(Deno.args);

if (!args.path || !args.pattern || !args.replacer) {
console.log('Missing arguments');
Deno.exit();
}

const pattern = parseRegExpString(args.pattern);

const pathsToUpdate: Array<{ oldPath: string, newPath: string, isFile: boolean }> = [];

Deno.chdir(args.path);
for (const entry of walkSync('.')) {
const path = entry.path;
const pathReplaced = path.replace(pattern, args.replacer);

if (path !== pathReplaced) {
pathsToUpdate.push({ oldPath: path, newPath: pathReplaced, isFile: entry.isFile });
}
}

for (const { oldPath, newPath, isFile } of pathsToUpdate) {
if (isFile) {
const content = Deno.readTextFileSync(oldPath);
const contentReplaced = content.replace(pattern, args.replacer);
Deno.writeTextFileSync(newPath, contentReplaced);
} else {
Deno.renameSync(oldPath, newPath); //<- Error: /path/{{pattern}}/{{pattern}}
}
console.log(`Updated: ${oldPath} -> ${newPath}`);
}
import { parseArgs } from 'https://deno.land/std@0.212.0/cli/mod.ts';
import { walkSync } from 'https://deno.land/std@0.212.0/fs/mod.ts';
import { parseString as parseRegExpString } from 'https://raw.githubusercontent.com/TypeScriptPlayground/std/main/src/regexp/mod.ts'

const args = parseArgs<{
path : string,
pattern : string,
replacer : string
}>(Deno.args);

if (!args.path || !args.pattern || !args.replacer) {
console.log('Missing arguments');
Deno.exit();
}

const pattern = parseRegExpString(args.pattern);

const pathsToUpdate: Array<{ oldPath: string, newPath: string, isFile: boolean }> = [];

Deno.chdir(args.path);
for (const entry of walkSync('.')) {
const path = entry.path;
const pathReplaced = path.replace(pattern, args.replacer);

if (path !== pathReplaced) {
pathsToUpdate.push({ oldPath: path, newPath: pathReplaced, isFile: entry.isFile });
}
}

for (const { oldPath, newPath, isFile } of pathsToUpdate) {
if (isFile) {
const content = Deno.readTextFileSync(oldPath);
const contentReplaced = content.replace(pattern, args.replacer);
Deno.writeTextFileSync(newPath, contentReplaced);
} else {
Deno.renameSync(oldPath, newPath); //<- Error: /path/{{pattern}}/{{pattern}}
}
console.log(`Updated: ${oldPath} -> ${newPath}`);
}
DAHDeno AI Helper1/22/2024
The walk() function from the Deno standard library indeed returns every directory and file within the specified directory, recursively. If you want to filter out intermediate directories and only work with the files or the last directory in a path, you'll need to implement a custom filter. One approach to achieve this is to use the entry object that walk() yields. This object contains information about the path, including whether it's a file or a directory. You can use this information to decide whether to process the entry or not. Here's a modified version of your code that filters out intermediate directories: ```ts import { parseArgs } from 'https://deno.land/std@0.212.0/cli/mod.ts'; import { walkSync } from 'https://deno.land/std@0.212.0/fs/mod.ts'; import { parseString as parseRegExpString } from 'https://raw.githubusercontent.com/TypeScriptPlayground/std/main/src/regexp/mod.ts' const args = parseArgs<{ path: string, pattern: string, replacer: string }>(Deno.args); if (!args.path !args.pattern !args.replacer) { console.log('Missing arguments'); Deno.exit(); } const pattern = parseRegExpString(args.pattern); const pathsToUpdate: Array<{ oldPath: string, newPath: string, isFile: boolean }> = []; Deno.chdir(args.path); for (const entry of walkSync('.')) { // Skip intermediate directories if (entry.isDirectory && entry.name !== '.') { continue; } const path = entry.path; const pathReplaced = path.replace(pattern, args.replacer); if (path !== pathReplaced) { pathsToUpdate.push({ oldPath: path, newPath: pathReplaced, isFile: entry.isFile }); } } for (const { oldPath, newPath, isFile } of pathsToUpdate) { if (isFile) { const content = Deno.readTextFileSync(oldPath); const contentReplaced =
DAHDeno AI Helper1/22/2024
content.replace(pattern, args.replacer); Deno.writeTextFileSync(newPath, contentReplaced); } else { Deno.renameSync(oldPath, newPath); } console.log(Updated: ${oldPath} -> ${newPath}); } `` In this modified version, we skip over intermediate directories by checking if entry.isDirectory is true and if the entry.name is not '.'. This way, we only process files and the last directory in a given path. Please note that the parseString function from the regexp module is not part of the Deno standard library, and I'm not sure about its behavior or reliability since it's not mentioned in the documentation. Make sure to test this function thoroughly or consider using the native JavaScript RegExp` constructor for parsing regular expressions. Remember to handle errors that may occur during file and
MMqx1/22/2024
This throws an error that the path for writeTextFileSync does not exist.
DAHDeno AI Helper1/22/2024
The error you're encountering suggests that the new path for writeTextFileSync does not exist. This can happen if you're trying to write to a file in a directory that hasn't been created yet. When you rename files and directories, you need to ensure that the directory structure for the new path exists before you attempt to write to a file or rename a directory. One way to handle this is to check if the directory for the new path exists before writing the file, and if it doesn't, create the necessary directories using Deno.mkdirSync or Deno.mkdir with the recursive option set to true. Here's a snippet that demonstrates how you might do this:
// ... rest of your code ...

for (const { oldPath, newPath, isFile } of pathsToUpdate) {
if (isFile) {
// Ensure the directory for the new file path exists
const newDir = newPath.substring(0, newPath.lastIndexOf('/'));
try {
Deno.statSync(newDir);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// Directory does not exist, create it
Deno.mkdirSync(newDir, { recursive: true });
} else {
throw error; // Rethrow unexpected errors
}
}

// Now that we know the directory exists, write the file
const content = Deno.readTextFileSync(oldPath);
const contentReplaced = content.replace(pattern, args.replacer);
Deno.writeTextFileSync(newPath, contentReplaced);
} else {
// For directories, we assume that the parent directory already exists
Deno.renameSync(oldPath, newPath);
}
console.log(`Updated: ${oldPath} -> ${newPath}`);
}

// ... rest of your code ...
// ... rest of your code ...

for (const { oldPath, newPath, isFile } of pathsToUpdate) {
if (isFile) {
// Ensure the directory for the new file path exists
const newDir = newPath.substring(0, newPath.lastIndexOf('/'));
try {
Deno.statSync(newDir);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// Directory does not exist, create it
Deno.mkdirSync(newDir, { recursive: true });
} else {
throw error; // Rethrow unexpected errors
}
}

// Now that we know the directory exists, write the file
const content = Deno.readTextFileSync(oldPath);
const contentReplaced = content.replace(pattern, args.replacer);
Deno.writeTextFileSync(newPath, contentReplaced);
} else {
// For directories, we assume that the parent directory already exists
Deno.renameSync(oldPath, newPath);
}
console.log(`Updated: ${oldPath} -> ${newPath}`);
}

// ... rest of your code ...
In this snippet, before writing the file, we check if the directory exists by using Deno.statSync. If it throws a NotFound error, we create the directory
DAHDeno AI Helper1/22/2024
with Deno.mkdirSync and the recursive option set to true. This ensures that all the necessary parent directories are created. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. Make sure to test your code thoroughly to handle any edge cases or errors that might occur.

Looking for more? Join the community!

Recommended Posts
How to link local NPM package with DenoI have a package which has a couple of peer dependencies, which I want to link locally and patch. WhSubhosting API protectionIs there a way to setup custom auth via some header for example for all subhosting routes? I.e. Denolvim tsx language serverafter i clone fresh example in deno deploy, I get an error "Cannot find name 'React' " while openingHow to debug tests in Deno on VSCODEHello everyone, I am trying to debug a unit test in Deno. I have tried using deno test --inspect-brkDynamically importing NPM modules errorsThe following works fine in Node: ```js const parser = 'acorn'; const { parse } = await import(parsedeno kv watch vs broadcast channelFor creating a chat app for example, which one should i use? What are the differences? Kv watch geIf I use FFI with Deno Jupyter, will it not print the shared lib's stdout? (like Rust println!())If I use FFI with Deno Jupyter, will it not print the shared lib's stdout? This is an example of hStrange behavior of the assertRejects() method.Behavior of the `assertRejects()` method. `assertRejects()` fails when a non-stubbed method is callModule loadingHey, I am just testing the deno_core. I know, that the deno runtime with the command line supports Can I use deno_ast to transpile arbitrary typescript?I'm just wondering if it's safe to use deno_ast to transpile arbitrary (user-generated) typescript t`deno cache` with `node_modules_dir` moves sub-dependencies into `.deno/node_modules` dirIf I execute `deno cache --node-modules-dir package-lock.json`, a `node_modules` directory is createdeno options not available in vscode debugger optionslatest vscode, latest deno, yes ive got the extension installed