oidc-lib
Version:
A library for creating OIDC Service Providers
89 lines (79 loc) • 2.27 kB
JavaScript
module.exports = {
mkDirectoriesInPath: mkDirectoriesInPath,
copyDirectory: copyDirectory
}
const path = require('path');
const fs = require('fs');
// WARNING *** NOT THE SAME AS sts utils because of fs
function mkDirectoriesInPath(target) {
var sep;
var firstSeg = true;
if (isUnixFilesystem()){
target = forwardSlash(target);
sep = '/';
}
else{
target = backSlash(target);
sep = '\\';
}
var segments = target.split(sep);
var path = '';
var prefix = '';
for (var i=0; i < segments.length; i++){
path += prefix + segments[i];
prefix = sep;
if (firstSeg){
firstSeg = false;
continue;
}
prefix = sep;
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
}
}
// forwardSlash is pk.util.slash_normalize - adjusts file paths written
// for windows so they work on unix as well.
function forwardSlash(windows_path){
if (isUnixFilesystem()){
windows_path = windows_path.replace(/\\/g, '/');;
}
return windows_path;
}
// backSlash is slash_denormalize - adjusts file paths written
// for unix so they work on windows as well.
function backSlash(unix_path){
if (!isUnixFilesystem()){
unix_path = unix_path.replace(/\//g, '\\');;
}
return unix_path;
}
// node can run under linux or windows
function usingNode(){
return (typeof window === "undefined");
}
// unix file system only possible running node
function isUnixFilesystem(){
// node rather than browser && /usr/bin in the path
return usingNode() && process.env.PATH.indexOf("/usr/bin") >= 0;
}
function copyDirectory(sourceDirName, destDirName){
mkDirectoriesInPath(destDirName);
var dirents = fs.readdirSync(sourceDirName, {withFileTypes: true});
for (var i=0; i<dirents.length; i++){
var dirent = dirents[i];
if (dirent.isFile()){
var name = dirent.name
var srcPath = path.join(sourceDirName, name);
var destPath = path.join(destDirName, name);
var buffer = fs.readFileSync(srcPath);
fs.writeFileSync(destPath, buffer);
}
}
for (var i=0; i<dirents.length; i++){
var dirent = dirents[i];
if (dirent.isDirectory()){
copyDirectory(path.join(sourceDirName, dirent.name), path.join(destDirName, dirent.name));
}
}
}