NodeJS
Tips & Tricks
get file's update / modified time
[link](https://thewebdev.info/2022/03/05/how-to-get-a-files-last-modified-date-in-node-js/#:~:text=To get a file’s last modified date in,to get the last modified date for asynchronously.)
// sync
fs.stat("/dir/file.txt", (err, stats) => {
const {
mtime
} = stats;
console.log(mtime);
});
// async
const stats = fs.statSync("/dir/file.txt");
const {
mtime
} = stats;
console.log(mtime);
does the path lead to a file or directory? - source
if(fs.existsSync(srcPath) && fs.lstatSync(srcPath).isDirectory()){
// returns true if path leads to DIRECTORY
} else if(fs.existsSync(srcPath) && fs.lstatSync(srcPath).isFile()) {
// returns true if path leads to FILE
}
get app root directory consistently - source
root-path.mjs
-> put this file in the root of your project
import * as path from 'path'
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const __rootDir = __dirname //import this to use in any file
Copy / Clone directory and files
const { promises: fs } = require("fs")
const path = require("path")
async function copyDir(src, dest) {
await fs.mkdir(dest, { recursive: true });
let entries = await fs.readdir(src, { withFileTypes: true });
for (let entry of entries) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);
entry.isDirectory() ?
await copyDir(srcPath, destPath) :
await fs.copyFile(srcPath, destPath);
}
}
import { promises as fs } from "fs"
import path from "path"
export const copyDirectory = async (src: string, dest: string) => {
const [entries] = await Promise.all([
fs.readdir(src, { withFileTypes: true }),
fs.mkdir(dest, { recursive: true }),
])
await Promise.all(
entries.map((entry) => {
const srcPath = path.join(src, entry.name)
const destPath = path.join(dest, entry.name)
return entry.isDirectory()
? copyDirectory(srcPath, destPath)
: fs.copyFile(srcPath, destPath)
})
)
}
Error: Import statment outside module, typscript babel
javascript - SyntaxError: Cannot use import statement outside a module - Stack Overflow
Recursivley read file paths in directory & return array of strings
https://stackoverflow.com/a/45130990/15579591
const { resolve } = require('path');
const { readdir } = require('fs').promises;
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
// return example
// [
// C:\\users\\you\\app\\folder\\file1
// C:\\users\\you\\app\\folder\\file2
// C:\\users\\you\\app\\folder2\\file1
// C:\\users\\you\\app\\folder2\\file2
// ]
run a javascript (js) module via command line
being able to to run any js
file via a terminal inside of your ReactJS app seems simple, but requires special file naming
serverScript.mjs
--> notice the .mjs
as we'll need that for the import
export function printToConsole(string) {
console.log(string)
}
runMe.mjs
import {printToConsole} from './`serverScript.mjs';
printToConsole('Hello World')
node runMe.mjs
--> don't forget the file extension .mjs
when running this node file
Get current running operating system
let osValue = process.platform;
if (osValue == 'darwin') {
console.log("Mac OS");
}else if(osValue == 'win32'){
console.log("Window OS")
}else if(osValue== 'android') {
console.log("Android OS")
}else if(osValue== 'linux') {
console.log("Linux OS")
}
else{
console.log("Other os")
}
import * as Os from 'os'
console.log(Os.release());
console.log(Os.platform());
// output
// 10.0.21996
// win32