Skip to content

Commit

Permalink
feat: support local disk module
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriodegoiania committed Apr 12, 2020
1 parent 9131029 commit 795d996
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "mocha --config scripts/mocha.json",
"test:coverage": "nyc --nycrc-path scripts/nyc.json npm run test",
"start": "node_modules/.bin/tsc -w && npm run build:static",
"build": "npm run build:clean && node_modules/.bin/tsc && npm run build:static",
"build": "npm run build:clean && node_modules/.bin/tsc && npm run build:static && chmod +x dist/bin/webfaas.js",
"build:clean": "rm -rf dist",
"build:static": "cp package.json dist && cp README.md dist && cp .npmignore dist && cp -R src/test/data dist/test",
"setnpmtoken": "rm ~/.npmrc && echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > ~/.npmrc",
Expand All @@ -29,7 +29,7 @@
"webfaas": "bin/webfaas.js"
},
"dependencies": {
"@webfaas/webfaas-core": "^0.7",
"@webfaas/webfaas-core": "^0.8",
"@webfaas/webfaas-plugin-packageregistry-disk": "0.1.0",
"@webfaas/webfaas-plugin-packageregistry-github": "0.1.0",
"@webfaas/webfaas-plugin-packageregistry-npm": "0.1.0",
Expand Down
151 changes: 136 additions & 15 deletions src/WebFaaS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ export class WebFaaS {
private config: Config | null = null;
private core: Core | null = null;
private pluginManager: PluginManager | null = null;
private pathConfigFile: string = "";

private pathConfigFile: string | null = null;
private pathNodeModulesDirectory: string | null = null;
private pathRootPackageDirectory: string | null = null;
private pathCurrentWorkingDirectory: string | null = null;

/**
* return WebFaaS - Config
*/
getConfig(): Config{
if (!this.config){
if (!this.pathConfigFile){
this.pathConfigFile = this.searchConfigFile();
}
let newConfig = new Config(this.pathConfigFile);
let newConfig = new Config(this.getPathConfigFile());
this.setConfig(new Config());
return newConfig;
}
else{
return this.config;
}
}

/**
* set config
* @param config
Expand All @@ -45,15 +46,98 @@ export class WebFaaS {
return this.core;
}

/**
* return cwd
*/
getPathCurrentWorkingDirectory(): string{
if (this.pathCurrentWorkingDirectory === null){
this.setPathCurrentWorkingDirectory(process.cwd());
return process.cwd();
}
else{
return this.pathCurrentWorkingDirectory;
}

}
/**
* set cwd
* @param value cwd
*/
setPathCurrentWorkingDirectory(value: string): void{
this.pathCurrentWorkingDirectory = value;
}

/**
* return path config files
*/
getPathConfigFile(): string{
if (this.pathConfigFile === null){
let newPathConfigFile = this.searchConfigFile();
this.setPathConfigFile(newPathConfigFile);
return newPathConfigFile;
}
else{
return this.pathConfigFile;
}
}
/**
* set path config files
* @param value path config files
*/
setPathConfigFile(value: string): void{
this.pathConfigFile = value;
}

/**
* return path node modules directory
*/
getPathNodeModulesDirectory(): string{
if (this.pathNodeModulesDirectory === null){
let newPathNodeModulesDirectory = this.searchNodeModulesDirectory();
this.setPathNodeModulesDirectory(newPathNodeModulesDirectory);
return newPathNodeModulesDirectory;
}
else{
return this.pathNodeModulesDirectory;
}
}
/**
* set path node_modules directory
* @param value path node modules
*/
setPathNodeModulesDirectory(value: string): void{
this.pathNodeModulesDirectory = value;
}

/**
* return path root package directory
*/
getPathRootPackageDirectory(): string{
if (this.pathRootPackageDirectory === null){
let newPathRootPackageDirectory = this.searchRootPackageDirectory();
this.setPathRootPackageDirectory(newPathRootPackageDirectory);
return newPathRootPackageDirectory;
}
else{
return this.pathRootPackageDirectory;
}
}
/**
* set path root package directory
* @param value path node modules
*/
setPathRootPackageDirectory(value: string): void{
this.pathRootPackageDirectory = value;
}

/**
* return plugin manager
*/
getPluginManager(): PluginManager{
if (!this.pluginManager){
this.pluginManager = new PluginManager(this.getCore());
let folderModules = this.searchModulesFolder();
if (folderModules){
this.loadPluginsByFolder(folderModules);
if (this.getPathNodeModulesDirectory()){
this.loadPluginsByFolder(this.getPathNodeModulesDirectory());
}
}
return this.pluginManager;
Expand All @@ -67,6 +151,10 @@ export class WebFaaS {
this.getPluginManager().loadPluginsByFolder(baseFolder);
}

/**
* convert args to command data
* @param args
*/
convertArgsToCommandData(args: string[]): IWebFaaSCommandData{
var commandParameter = {} as IWebFaaSCommandData;

Expand Down Expand Up @@ -102,35 +190,60 @@ export class WebFaaS {
return commandParameter;
}

/**
* print help
*/
printHelp(){
console.log("Usage:");
console.log(" webfaas --help");
console.log(" webfaas --config [path]");
console.log(" webfaas --config [path] --plugins [path, ...]");
}

searchConfigFile(): string{
/**
* search config file
*/
private searchConfigFile(): string{
const listFile: Array<string> = [];
if (process.env["WEBFAAS_CONFIG"]){
listFile.push(process.env["WEBFAAS_CONFIG"]);
}
listFile.push(path.join(process.cwd(), "webfaas.json"));
listFile.push(path.join(this.getPathCurrentWorkingDirectory(), "webfaas.json"));
listFile.push(path.join(__dirname, "webfaas.json"));

return this.searchExistFileInArray(listFile);
}

searchModulesFolder(): string{
/**
* search node modules directory
*/
private searchNodeModulesDirectory(): string{
const listFile: Array<string> = [];
listFile.push(path.join(process.cwd(), "node_modules"));
listFile.push(path.join(this.getPathCurrentWorkingDirectory(), "node_modules"));
if (require.main){
listFile.push(...require.main.paths);
}

return this.searchExistFileInArray(listFile);
}

searchExistFileInArray(listFile: Array<string>): string{
/**
* search root package directory
*/
private searchRootPackageDirectory(): string{
if (this.searchExistFileInArray([path.join(this.getPathCurrentWorkingDirectory(), "package.json")])){
return this.getPathCurrentWorkingDirectory();
}
else{
return "";
}
}

/**
* search exist file in array
* @param listFile
*/
private searchExistFileInArray(listFile: Array<string>): string{
for (let i = 0; i < listFile.length; i++){
let file = listFile[i];
if (fs.existsSync(file)){
Expand All @@ -145,12 +258,20 @@ export class WebFaaS {
* @param parameter
*/
configureByCommandData(commandData: IWebFaaSCommandData){
this.pathConfigFile = commandData.config;
if (commandData.config){
this.setPathConfigFile(commandData.config);
}

for (let i = 0; i < commandData.plugins.length; i++){
let folderPlugin = commandData.plugins[i];
this.loadPluginsByFolder(folderPlugin);
}

let rootPackageDirectory = this.getPathRootPackageDirectory();

if (rootPackageDirectory && (rootPackageDirectory.substr(-8) !== (path.sep + "webfaas"))){ //ignore webfaas module
this.getCore().getModuleManager().getModuleManagerCache().addLocalDiskModuleToCache(rootPackageDirectory);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/test/commandData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe("CommandData", () => {
chai.expect(command1.plugins).to.eql([""]);
})

it("configureByCommandData - default", function(){
const faas = new WebFaaS();
let command1 = faas.convertArgsToCommandData([]);
faas.configureByCommandData(command1);
})

it("configureByCommandData - plugins", function(){
const faas = new WebFaaS();
let command1 = faas.convertArgsToCommandData(["--config", "config1", "--plugins", "folder1,folder2,folder3"]);
Expand All @@ -79,4 +85,13 @@ describe("CommandData", () => {
let command1 = faas.convertArgsToCommandData(["--config", "config1"]);
faas.configureByCommandData(command1);
})

it("configureByCommandData - load local module", async function(){
const faas = new WebFaaS();
faas.setPathRootPackageDirectory(path.join(__dirname, "data", "modules", "moduletest1"));
let command1 = faas.convertArgsToCommandData([]);
faas.configureByCommandData(command1);
let response = await faas.getCore().invokeAsync("moduletest1", "1.0.0", undefined, [2,3]);
chai.expect(response).to.eq(5);
})
})
2 changes: 1 addition & 1 deletion src/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Config", () => {
it("searchConfigFile", function(){
const faas = new WebFaaS();
process.env["WEBFAAS_CONFIG"] = "/tmp";
chai.expect(faas.searchConfigFile()).to.eq("/tmp");
chai.expect(faas.getPathConfigFile()).to.eq("/tmp");
process.env["WEBFAAS_CONFIG"] = "";
})
})
3 changes: 3 additions & 0 deletions src/test/data/modules/moduletest1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(x, y){
return x + y;
}
11 changes: 11 additions & 0 deletions src/test/data/modules/moduletest1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "moduletest1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
10 changes: 10 additions & 0 deletions src/test/pathConfigFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as chai from "chai";
import { WebFaaS } from "../WebFaaS";

describe("PathConfigFile", () => {
it("getPathConfigFile", function(){
const faas = new WebFaaS();
chai.expect(faas.getPathConfigFile()).to.eq("");
chai.expect(faas.getPathConfigFile()).to.eq(""); //force cache
})
})
10 changes: 10 additions & 0 deletions src/test/pathNodeModulesDirectory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as chai from "chai";
import { WebFaaS } from "../WebFaaS";

describe("PathNodeModulesDirectory", () => {
it("getPathNodeModulesDirectory", function(){
const faas = new WebFaaS();
chai.expect(faas.getPathNodeModulesDirectory()).to.include("node_modules");
chai.expect(faas.getPathNodeModulesDirectory()).to.include("node_modules"); //force cache
})
})
17 changes: 17 additions & 0 deletions src/test/pathRootPackageDirectory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as chai from "chai";
import { WebFaaS } from "../WebFaaS";

describe("PathRootPackageDirectory", () => {
it("getPathRootPackageDirectory", function(){
const faas = new WebFaaS();
chai.expect(faas.getPathRootPackageDirectory()).to.include("webfaas");
chai.expect(faas.getPathRootPackageDirectory()).to.include("webfaas"); //force cache
})

it("getPathRootPackageDirectory - simulate not exist", function(){
const faas = new WebFaaS();
faas.setPathCurrentWorkingDirectory("/notexist");
chai.expect(faas.getPathRootPackageDirectory()).to.include("");
chai.expect(faas.getPathRootPackageDirectory()).to.include(""); //force cache
})
})
8 changes: 7 additions & 1 deletion src/test/pluginManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import * as chai from "chai";
import { WebFaaS } from "../WebFaaS";

describe("PluginManager", () => {
it("convertArgsToCommandData", function(){
it("default", function(){
const faas = new WebFaaS();
chai.expect(faas.getPluginManager().listPlugin.length).to.eql(5);
})

it("simulate whithout node_module", function(){
const faas = new WebFaaS();
faas.setPathNodeModulesDirectory("");
chai.expect(faas.getPluginManager().listPlugin.length).to.eql(0);
})
})
19 changes: 0 additions & 19 deletions src/test/searchModulesFolder.test.ts

This file was deleted.

0 comments on commit 795d996

Please sign in to comment.