Skip to content

Commit

Permalink
Add downloadPluging implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugen Kremer committed Jun 14, 2020
1 parent 39a1cd8 commit 239e59d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 18 deletions.
80 changes: 79 additions & 1 deletion deploy/jN/lib/PluginUpdater.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("fs");

(function (){

Expand All @@ -14,11 +15,88 @@ function (registryPath, localPath){

this.updatePlugin = function (name){
var plugins = this.getPluginsInTheRegistry();
var plugin = plugins["name"];
var plugin = plugins[name];
if (!plugin)
throw new Error("Plugin not found");

var tmpDir = createTempDir();
downloadPlugin(plugin.url, tmpDir);
fs.renameSync(tmpDir, localPath+"\\"+name);
}

function createTempDir(){
var path = fs.mkdtempSync(process.env["TEMP"]+"\\");
fs.mkdirSync(path);
return path;
}

function downloadPlugin(packageUrl, destDir){
var packageJson = fetch(packageUrl, asText());
var packageObj = JSON.parse(packageJson);

var files = Array.isArray(packageObj.files)?packageObj.files:[];
var packagePath = packageUrl.replace(/[^\/]+$/, "");

for(var i=0; i<files.length; i++){
var destFile = destDir+"\\"+files[i];
fs.mkdirSync(destFile.replace(/[\\\/][^\\\/]+$/g, ""))
fetch(packagePath+files[i], asBinary(destFile));
}

fetch(packageUrl, asBinary(destDir+"\\"+"package.json"))
}

function mkdirP(path){


}

function asText(){
return function(response){
return response.responseText;
}
}
function asBinary(dest){
return function (response){

var stream = new ActiveXObject("ADODB.Stream");

stream.Type = 1; // binary
stream.Open();
stream.Write(response.responseBody);

stream.SaveToFile(dest, 2); // overwrite
}
}

function fetch(url, destFn){
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlHttp)
throw new Error("Could not create http request");

xmlHttp.open('GET', url, false);

var finishFn = function(){ throw new Error("Failed to fetch") };

xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState != 4)
return;

finishFn = function(){
if (xmlHttp.status != 200)
throw new Error("Unexpected status "+xmlHttp.status);

return destFn(xmlHttp);
}
};

xmlHttp.send(null);

return finishFn();
}

function fetchAsBinary(url, dest){

}

function fetchAsText(url){
Expand Down
15 changes: 12 additions & 3 deletions deploy/jN/lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
return fso.FileExists(path) || fso.FolderExists(path);
},
mkdirSync: function(path, options){
if (!!options)
if (!!options && !!options.mode )
throw new Error("Not implemented for options")

if (fso.FolderExists(path))
return;

return path;

if (!!options && options.recursive){
fs.mkdirSync(path.replace(/[\/\\][^\/\\]*$/g, ""), options);
}

fso.CreateFolder(path);

return path;
},
rmdirSync: function(path, options){
if (!!options)
Expand All @@ -27,6 +33,9 @@
throw new Error("Not implemented for options")

return prefix + fso.GetTempName();
},
renameSync: function(oldPath, newPath){
fso.MoveFolder(oldPath, newPath);
}
}

Expand Down
9 changes: 9 additions & 0 deletions deploy/jN/tests/fs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe("fs", function (){
it("creates directory does not fail if exists", function(){
var tmpDir = process.env["TEMP"];
fs.mkdirSync(tmpDir);
});
it("creates directory recursive", function(){
var tmpDir = process.env["TEMP"] + "/fs.spec.rec\\ursive";
fs.mkdirSync(tmpDir, {recursive:true});

expect(fs.existsSync(process.env["TEMP"] + "/fs.spec.rec")).eq(true);
expect(fs.existsSync(process.env["TEMP"] + "/fs.spec.rec\\ursive")).eq(true);

fs.rmdirSync(tmpDir);
});
});
describe("rmdirSync", function(){
Expand Down
15 changes: 8 additions & 7 deletions deploy/jN/tests/pluginupdater.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require("describe");
require("expect");
require("testenv")

var PluginUpdater = require("../lib/PluginUpdater").PluginUpdater;

describe("PluginUpdater", function(){

describe("Ctor", function(){
it("works", function (){
new PluginUpdater();
Expand All @@ -30,15 +32,14 @@ describe("PluginUpdater", function(){
expect(function(){ pu.updatePlugin("this plugin does not exist")}).throws();
});

it("downloads plugin declaration and corresponding files", function(){
var pu = new PluginUpdater("https://raw.githubusercontent.com/jn-npp-plugin/plugin-registry/master/plugins.json", "");
it("downloads plugin declaration and corresponding files", testenv(function(env){
var pu = new PluginUpdater("https://raw.githubusercontent.com/jn-npp-plugin/plugin-registry/master/plugins.json", env.directory);
var plugins = pu.getPluginsInTheRegistry();

expect(plugins).not().eq(null);
pu.updatePlugin("XML");

var xmlPlugin = plugins["XML"];
expect(xmlPlugin).not().eq(null);
expect(typeof(xmlPlugin.url)).eq("string");
})
expect(fs.existsSync(env.directory+"\\XML\\package.json")).eq(true)
expect(fs.existsSync(env.directory+"\\XML\\src\\xml.js")).eq(true)
}))
})
})
15 changes: 8 additions & 7 deletions deploy/jN/tests/runner.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ function require(file){
else if (fso.FileExists(require.currentDir+"\\"+file+".js"))
file = require.currentDir+"\\"+file+".js";
else
throw "File does not exist: " + file;

throw new Error("File does not exist: " + file);
// use full path to script, to avoid multiple loads
var fileObj = fso.GetFile(file);
file = fileObj.Path;

// file already loaded
if (require.hash[file])
if (require.hash[file]){
return require.hash[file].exports;

}

var script = readFile(file, "UTF-8");
var oldDir = require.currentDir;
var oldModule = typeof(module) != "undefined"? module : "undefined";
var oldModule = typeof(module) == "undefined" ? undefined : module;
try{
require.currentDir = fileObj.ParentFolder.Path;
module = {exports:{}};

execScript(script, file);

require.hash[file] = { exports: module.exports };
Expand All @@ -40,7 +41,7 @@ function require(file){
require.currentDir = oldDir;
delete module;

if (oldModule != "undefined")
if (oldModule != undefined)
module = oldModule;
}

Expand Down
22 changes: 22 additions & 0 deletions deploy/jN/tests/testenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require("../lib/Nodejs");

(function(){
testenv = function(testeeFn){
return function (){
var tmpDir = fs.mkdirSync(fs.mkdtempSync(process.env["TEMP"]+"\\"));

var env = {
directory: tmpDir
};

try{
testeeFn(env);
}finally{
fs.rmdirSync(tmpDir);
}
}
}

module.exports.testenv = testenv;
})();

1 comment on commit 239e59d

@sieukrem
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#89

Please sign in to comment.