Path manipulation in NodeJS - node.js

I want to create some new paths by modifying a copy of an existing path. The Node path object seems very basic. I can see how it's possible with the default path object but it seems clunky.
What is the neatest way to change
a/path/to/file.json
Into two other paths
a/path/to/file-original.json
a/path/to/file-comparand.json
The path could be relative or absolute so I'm hoping for something that just allows me to change the name without having to worry about root or dir objects.
Is there an Advanced Path module or something else I'm missing?
Thanks for any help

Assuming there's not a library which will implement this functionality for you, using path.parse doesn't have to be clunky - I actually think using that is probably the cleanest way of doing this:
let { dir, name, ext } = path.parse("a/path/to/file.json");
let path1 = path.join(dir, name + "-original" + ext);
let path2 = path.join(dir, name + "-comarand" + ext);
That code snippet uses destructuring, so you'll need a recent-ish version of Node to run it. That said, you could just replace that with accessing the parsed path object normally:
let p = path.parse("a/path/to/file.json");
let path1 = path.join(p.dir, p.name + "-original" + p.ext);
let path2 = path.join(p.dir, p.name + "-comarand" + p.ext);
Not that much worse!
If this is something you'd be doing frequently in your project, it wouldn't be too hard to lift it out into a utility function, like so:
function suffixFilename(path, suffix) {
let { dir, name, ext } = path.parse(path);
return path.join(dir, name + "-" + suffix + ext);
}
let path1 = suffixFilename("a/path/to/file.json", "original");
let path2 = suffixFilename("a/path/to/file.json", "comparand");

In addition to #joe's answer, here's a simpler version that needs the modify-filename package.
var modifyFilename = require('modify-filename');
const originalPath = "a/path/to/file.json";
const originalFilename = modifyFilename(originalPath, (name, ext) => {
return `${name}-original${ext}`;
});
const comparandFilename = modifyFilename(originalPath, (name, ext) => {
return `${name}-comparand${ext}`;
});

Related

How can I convert the first page of a PDF to a JPG using the NodeJS imagemagick library?

I am attempting to convert pdf files into a preview icon jpg
using the imagemagick library for NodeJS. I am trying to
generate a preview of only the first page (for multi-page pdfs).
In the normal command line imagemagick program this can be
done easily by saying "convert file.pdf[0] file.jpg"
where the [0] tells it to only convert the first page.
However I am not sure how to do that with this library.
I tried concatenating [0] to the filename, but it just
reads it as part of the real file name. Anyone know of
a way around this using this library?
I had a look around for a while and found this, but
they are not using this library.
Convert PDF to PNG Node.JS
The specific library I am using is located here: https://www.npmjs.com/package/imagemagick
The code I am working with is below:
let path = '/tmp/';
let pageNumber = '[0]';
let filePath = path + fileId + fileName + pageNumber;
let imgFilePath = path + fileId + '.jpg';
let writeStream = fs.createWriteStream(filePath);
writeStream.on('error',err => {
reject(err);
});
stream.pipe(writeStream);
im.convert([
filePath,
'-background','white',
'-alpha','remove',
'-resize','192x192',
'-quality','100',
imgFilePath
],
The problem is that you are concatenating the [0] part onto the filename before you do the conversion. You should concatenate the [0] within the scope of the convert function like this:
let path = '/tmp/';
let filePath = path + fileId + fileName;
let imgFilePath = path + fileId + '.jpg';
let writeStream = fs.createWriteStream(filePath);
writeStream.on('error',err => {
reject(err);
});
stream.pipe(writeStream);
im.convert([
filePath + '[0]',
'-background','white',
'-alpha','remove',
'-resize','192x192',
'-quality','100',
imgFilePath
],
This solution is tested working.

Why does node.js say invisible files don't exist?

Executing the below function test ()
with Node.js v8.11.2 on my Windows-10
prints out this:
DOES NOT EXIST ? C:\pagefile.sys
DOES NOT EXIST ? C:\hiberfil.sys
DOES NOT EXIST ? C:\swapfile.sys
DOES NOT EXIST ? C:\System Volume Information
These all seem to be invisible system files yes.
But they obviously exist. So why does Node.js
tell me they don't exist? Are there other cases
where file or folder is listed by readdirSync()
yet existsSync() returns false for them?
Is this a feature or bug?
function test ()
{ let Fs = require("fs");
let Path = require("path");
let path = "C:/";
let ents = Fs.readdirSync(path) ;
let dirs = ents.map
( e =>
{ let absPath = Path.join (path, e);
if (! Fs.existsSync(absPath))
{ console.log ("DOES NOT EXIST ? " + absPath)
} else
{ console.log ("DOES EXIST: " + absPath)
}
}
);
}

How to read from a specified file which is in a folder below without knowing the absolute path

So I have my project directory:
Here I read ArticleFile:
function _getDataFromFile() {
var jsonArray = csvjson.toObject(fs.readFileSync('ArticleFile.csv', { encoding: 'utf8' }));
var result = [];
for (var idx = 0; idx < jsonArray.length; idx++) {
var currArt = jsonArray[idx];
// if (!checkIfElementIsArticle(currArt)) throw "loaded object IS NOT an article!";
result.push(new Article(currArt.imageLocation, currArt.title, currArt.description, parseInt(currArt.quantity),parseInt(currArt.price)));
}
return result;
}
The problem is if "ArticleFile.csv" is in lets say contentsKopie I have to know the absolute path such as : C:\Users\noone_000\Desktop\BSD\Hausübungen\WebServer\WebServer\contentsKopie\ArticleFile.csv . How can I set the path like: fs.readFileSync('/ContentsCopie/ArticleFile.csv', { encoding: 'utf8' })
PS: csvjson is a module (require("csvjson"))
When you start the path with / it is interpreted as an absolute path. If you want to open a file in sub directory, you can use ./ContentsCopie/ArticleFile.csv
The . in front of the slash means that the path is relative to the current directory.
Conversely, if you needed to go up a level you can prefix your path with ..
Isn't ./contentsKopie/ArticleFile.csv enough?
When using /contentsKopie/ArticleFile.csv it's actually looking in C:/contentsKopie/ArticleFile.csv. So you have to prepend your path with a . to tell it to start at the working directory (WebServer here).
Otherwise, you can have the absolute path of the current file with __dirname then you could compose your path using the path module from node.
var path = require('path');
var article = path.join(__dirname, './contentsKopie/ArticleFile.csv');
This way, you'll get the absolute path of your file.

Get cordova package name from javascript hook

I am writing a cordova plugin with a node hook to run after_prepare .
This is for Android only.
From within this hook I need to get the cordova package name, so I can copy a file to the src/com/example/myproject folder (if the package is com.example.myproject).
If I know the package name I can make this path. I have it working hardcoded now but I need this to work with two different package names.
Is there a way to get the package name from within this code in the plugin hook?
module.exports = function(ctx){
var fs = ctx.requireCordovaModule('fs');
var path = ctx.requireCordovaModule('path');
var deferral = ctx.requireCordovaModule('q').defer();
//get package name here
//do other stuff
}
I have done a lot of research but have not been able to find this.
Thanks.
It doesn't look like it is available off of the context object, but you could try to parse the config.xml.
module.exports = function(context) {
var fs = require('fs');
var path = require('path');
var config_xml = path.join(context.opts.projectRoot, 'config.xml');
var et = context.requireCordovaModule('elementtree');
var data = fs.readFileSync(config_xml).toString();
var etree = et.parse(data);
console.log(etree.getroot().attrib.id);
};
The local-webserver plugin uses a similar strategy for reading config properties.
Here my compilation from different answers that works in 2021.
I use it to update some parameters in Xcode project for plugins compilation.
You can see that I am getting here app id and name from config.xml
And you can add it to after_prepare hook:
<hook src="scripts/addBuildSettingsToXcode.js" type="after_prepare" />
#!/usr/bin/env node
let fs = require('fs');
let xcode = require('xcode');
let path = require('path');
let et = require('elementtree');
module.exports = function (context) {
//console.log(context);
function addBuildPropertyToDebugAndRelease(prop, value) {
console.log('Xcode Adding ' + prop + '=' + value);
myProj.addBuildProperty(prop, value, 'Debug');
myProj.addBuildProperty(prop, value, 'Release');
}
function updateBuildPropertyToDebugAndRelease(prop, value) {
console.log('Xcode Updating ' + prop + '=' + value );
myProj.updateBuildProperty(prop, value, 'Debug');
myProj.updateBuildProperty(prop, value, 'Release');
}
// Getting app id and name from config.xml
let config_xml = path.join(context.opts.projectRoot, 'config.xml');
let data = fs.readFileSync(config_xml).toString();
let etree = et.parse(data);
let appId = etree.getroot().attrib.id ;
let appName = etree.getroot().find('name')['text'];
// Building project path
let projectPath = 'platforms/ios/' + appName + '.xcodeproj/project.pbxproj';
// Opening Xcode project and parsing it
myProj = xcode.project(projectPath);
myProj = myProj.parseSync();
// Common properties
addBuildPropertyToDebugAndRelease('DEVELOPMENT_TEAM', 'CGXXXXXXX');
addBuildPropertyToDebugAndRelease('CODE_SIGN_IDENTITY', '"Apple Development"');
// Compilation properties
addBuildPropertyToDebugAndRelease('ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES', 'YES');
// Save project file
fs.writeFileSync(projectPath, myProj.writeSync());
};

Nodejs absolute paths in windows with forward slash

Can I have absolute paths with forward slashes in windows in nodejs? I am using something like this :
global.__base = __dirname + '/';
var Article = require(__base + 'app/models/article');
But on windows the build is failing as it is requiring something like C:\Something\Something/apps/models/article. I aam using webpack. So how to circumvent this issue so that the requiring remains the same i.e. __base + 'app/models/src'?
I know it is a bit late to answer but I think my answer will help some visitors.
In Node.js you can easily get your current running file name and its directory by just using __filename and __dirname variables respectively.
In order to correct the forward and back slash accordingly to your system you can use path module of Node.js
var path = require('path');
Like here is a messed path and I want it to be correct if I want to use it on my server. Here the path module do everything for you
var randomPath = "desktop//my folder/\myfile.txt";
var correctedPath = path.normalize(randomPath); //that's that
console.log(correctedPath);
desktop/my folder/myfile.txt
If you want the absolute path of a file then you can also use resolve function of path module
var somePath = "./img.jpg";
var resolvedPath = path.resolve(somePath);
console.log(resolvedPath);
/Users/vikasbansal/Desktop/temp/img.jpg
it's 2020, 5 years from the question was published, but I hope that for somebody my answer will be useful. I've used the replace method, here is my code(express js project):
const viewPath = (path.join(__dirname, '../views/')).replace(/\\/g, '/')
exports.articlesList = function(req, res) {
res.sendFile(viewPath + 'articlesList.html');
}
I finally did it like this:
var slash = require('slash');
var dirname = __dirname;
if (process.platform === 'win32') dirname = slash(dirname);
global.__base = dirname + '/';
And then to require var Article = require(__base + 'app/models/article');. This uses the npm package slash (which replaces backslashes by slashes in paths and handles some more cases)
The accepted answer doesn't actually answer the question most people come here for.
If you're looking to normalize all path separators (possibly for string work), here's what you need.
All the code segments have the node.js built-in module path imported to the path variable.
They also have the variable they work from stored in the immutable variable str, unless otherwise specified.
If you have a string, here's a quick one-liner normalize the string to a forward slash (/):
const answer = path.resolve(str).split(path.sep).join("/");
You can normalize to any other separator by replacing the forward slash (/).
If you want just an array of the parts of the path, use this:
const answer = path.resolve(str).split(path.sep);
Once you're done with your string work, use this to create a path able to be used:
const answer = path.resolve(str);
From an array, use this:
// assume the array is stored in constant variable arr
const answer = path.join(...arr);
I recommend against this, as it is patching node itself, but... well, no changes in how you require things.
(function() {
"use strict";
var path = require('path');
var oldRequire = require;
require = function(module) {
var fixedModule = path.join.apply(path, module.split(/\/|\\/));
oldRequire(fixedModule);
}
})();
This is the approach I use, to save some processing:
const path = require('path');
// normalize based on the OS
const normalizePath = (value: string): string {
return path.sep === '\'
? value.replace(/\\/g, '/')
: value;
}
console.log('abc/def'); // leaves as is
console.log('abc\def'); // on windows converts to `abc/def`, otherwise leave as is
Windows uses \, Linux and mac use / for path prefixes
For Windows : 'C:\\Results\\user1\\file_23_15_30.xlsx'
For Mac/Linux: /Users/user1/file_23_15_30.xlsx
If the file has \ - it is windows, use fileSeparator as \, else use /
let path=__dirname; // or filePath
fileSeparator=path.includes('\')?"\":"/"
newFilePath = __dirname + fileSeparator + "fileName.csv";
Use path module
const path = require("path");
var str = "test\test1 (1).txt";
console.log(str.split(path.sep)) // This is only on Windows

Resources