2017-02-15 23:54:32 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2020-12-02 10:25:10 +01:00
|
|
|
var fs = require('fs-extra');
|
2017-02-15 23:54:32 +01:00
|
|
|
var fspath = require('path');
|
|
|
|
|
|
|
|
var runtime;
|
|
|
|
|
|
|
|
var exampleRoots = {};
|
|
|
|
var exampleFlows = null;
|
|
|
|
|
2020-12-02 10:25:10 +01:00
|
|
|
async function getFlowsFromPath(path) {
|
|
|
|
var result = {};
|
|
|
|
var validFiles = [];
|
|
|
|
return fs.readdir(path).then(files => {
|
|
|
|
var promises = [];
|
|
|
|
files.forEach(function(file) {
|
|
|
|
var fullPath = fspath.join(path,file);
|
|
|
|
var stats = fs.lstatSync(fullPath);
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
validFiles.push(file);
|
|
|
|
promises.push(getFlowsFromPath(fullPath));
|
|
|
|
} else if (/\.json$/.test(file)){
|
|
|
|
validFiles.push(file);
|
|
|
|
promises.push(Promise.resolve(file.split(".")[0]))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Promise.all(promises)
|
|
|
|
}).then(results => {
|
|
|
|
results.forEach(function(r,i) {
|
|
|
|
if (typeof r === 'string') {
|
|
|
|
result.f = result.f||[];
|
|
|
|
result.f.push(r);
|
|
|
|
} else {
|
|
|
|
result.d = result.d||{};
|
|
|
|
result.d[validFiles[i]] = r;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return result;
|
2017-02-15 23:54:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:32:05 +02:00
|
|
|
function addNodeExamplesDir(module,path) {
|
|
|
|
exampleRoots[module] = path;
|
|
|
|
return getFlowsFromPath(path).then(function(result) {
|
2019-06-29 02:16:02 +02:00
|
|
|
if (JSON.stringify(result).indexOf('{"f":') === -1) { return; }
|
2019-04-25 12:32:09 +02:00
|
|
|
exampleFlows = exampleFlows||{};
|
|
|
|
exampleFlows[module] = result;
|
2019-06-29 02:16:02 +02:00
|
|
|
});
|
2017-02-15 23:54:32 +01:00
|
|
|
}
|
|
|
|
function removeNodeExamplesDir(module) {
|
|
|
|
delete exampleRoots[module];
|
2019-04-25 12:32:09 +02:00
|
|
|
if (exampleFlows) {
|
|
|
|
delete exampleFlows[module];
|
2017-02-15 23:54:32 +01:00
|
|
|
}
|
2019-04-25 12:32:09 +02:00
|
|
|
if (exampleFlows && Object.keys(exampleFlows).length === 0) {
|
2017-02-15 23:54:32 +01:00
|
|
|
exampleFlows = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:32:05 +02:00
|
|
|
function init() {
|
2017-02-15 23:54:32 +01:00
|
|
|
exampleRoots = {};
|
|
|
|
exampleFlows = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getExampleFlows() {
|
|
|
|
return exampleFlows;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getExampleFlowPath(module,path) {
|
|
|
|
if (exampleRoots[module]) {
|
|
|
|
return fspath.join(exampleRoots[module],path)+".json";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
2018-04-26 13:32:05 +02:00
|
|
|
addExamplesDir: addNodeExamplesDir,
|
|
|
|
removeExamplesDir: removeNodeExamplesDir,
|
2017-02-15 23:54:32 +01:00
|
|
|
getExampleFlows: getExampleFlows,
|
|
|
|
getExampleFlowPath: getExampleFlowPath
|
|
|
|
}
|