oidc-lib
Version:
A library for creating OIDC Service Providers
111 lines (94 loc) • 3.76 kB
JavaScript
var fs = require('fs');
var path = require('path');
var base64url = require('base64url');
var root, branch;
var dirOfInterest = 'views';
var viewSet = {};
var scriptSet = '';
var linefeed = '';
var args = process.argv.slice(2);
if (args.length === 0){
root = path.resolve('.');
processDir(root , root, dirOfInterest);
}
else if (args.length === 1){
var configArg = args[0].split('=');
if (configArg[0] !== 'config'){
console.log('Args error. Require config="c:/somepath"');
process.exit(1);
}
var handlebarControl = configArg[1];
var handlebarConfigStream = fs.readFileSync(handlebarControl, {encoding: 'utf8'});
var handlebarConfig = JSON.parse(handlebarConfigStream);
root = path.resolve(handlebarConfig.root);
for (var i=0; i < handlebarConfig.branches.length; i++){
var branch = forwardSlash(root + '\\' + handlebarConfig.branches[i].replace(/\//g, '\\'));
processDir(root , branch, dirOfInterest);
}
}
else {
console.log('Args error. Use config= to specify a config file or no parameters to simply look for Views direction under the current directory');
process.exit(1);
}
var packageFile = dirOfInterest + '_package.js';
var fd = fs.openSync(packageFile, 'w');
fs.writeSync(fd, 'module.exports = {\r\n\t\'viewCollection\': ');
fs.writeSync(fd, JSON.stringify(viewSet));
fs.writeSync(fd, ",\r\n\t\'scripts\': {\r\n");
fs.writeSync(fd, scriptSet);
fs.writeSync(fd, "\r\n\t}\r\n}\r\n");
fs.closeSync(fd);
function processDir(origin, dir, dirTarget){
var dirObjects = fs.readdirSync(dir);
for (var dCount=0; dCount < dirObjects.length; dCount++){
var dirObject = dirObjects[dCount];
if (dirObject === 'node_modules'){
continue;
}
var fullpath = path.resolve(dir, dirObject);
var stat = fs.statSync(fullpath);
if (stat && stat.isDirectory()){
processDir(origin, fullpath, dirTarget);
}
if (stat && stat.isFile()){
var dirTail = dir.substring(dir.lastIndexOf(forwardSlash('\\')) + 1);
if (dirTail.toLowerCase() === dirTarget){
var view = dir.replace(origin, "");
view = backSlash(view + '/' + dirObject);
view = view.toLowerCase();
if (view.endsWith('.handlebars')){
view = view.substring(0, view.length - 11);
var fileInfo = {
view: view,
file: forwardSlash(dir + '\\' + dirObject)
};
var stringContent = fs.readFileSync(fileInfo.file, {encoding: 'utf8'});
var content64 = base64url.encode(stringContent);
viewSet[fileInfo.view] = content64;
}
if (view.endsWith('.js')){
view = view.substring(0, view.length - 3);
var scriptPath = '..' + view.replace(/\\/g, '/');
scriptSet += linefeed + '\t\t' + JSON.stringify(view) + ': require("' + scriptPath + '")';
linefeed = ",\r\n";
}
}
}
}
}
// forwardSlash is pk.util.slash_normalize - adjusts file paths written
// for windows so they work on unix as well.
function forwardSlash(windows_path){
if (process.env.PATH.indexOf("/usr/bin") >= 0){
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 (process.env.PATH.indexOf("/usr/bin") < 0){
unix_path = unix_path.replace(/\//g, '\\');;
}
return unix_path;
}