apiconnect-project
Version:
Configuration module IBM API Connect Developer Toolkit
74 lines (68 loc) • 2.17 kB
JavaScript
/********************************************************* {COPYRIGHT-TOP} ***
* Licensed Materials - Property of IBM
* 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
*
* (C) Copyright IBM Corporation 2016, 2017
*
* All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
********************************************************** {COPYRIGHT-END} **/
// Node module: apiconnect-project
;
var debug = require('debug')('apiconnect-project:lib:path-inspector');
var fs = require('fs');
var path = require('path');
var projectType = require('./project-type');
module.exports = inspectPath;
/**
* Returns information about the file or directory being loaded.
*
* Sync function
*
* @param {string} searchPath.
* @param {object} [options] options.
* @param {boolean} [options.incNodeProjects] allow non-loopback node projects.
*
* @return {PathInfo} file/project information.
* @throws Will throw an error if path is not a file or directory.
*/
function inspectPath(searchPath, options) {
options = options || {};
searchPath = path.resolve(searchPath);
var stat = fs.statSync(searchPath);
var info;
if (stat.isFile()) {
info = { type: 'file', basePath: searchPath, name: path.basename(searchPath) };
debug('inspectPath -> %j', info);
return info;
}
if (stat.isDirectory()) {
info = projectType(searchPath);
switch (info.projectType) {
case 'swiftserver':
info.type = 'project';
break;
case 'loopback':
info.type = 'project';
break;
case 'node':
if (options.incNodeProjects) {
info.type = 'project';
} else {
info.type = 'directory';
info.projectType = 'unknown';
info.name = path.basename(searchPath);
info.basePath = searchPath;
delete info.version;
}
break;
default:
info.type = 'directory';
info.name = path.basename(searchPath);
}
debug('inspectPath -> %j', info);
return info;
}
throw new Error('Unable to load %s', searchPath);
}