Jean-Yves Didier

fixing install process

#!/usr/bin/env -S node
import ARCS from './arcs.js';
import process from 'process';
import path from 'path';
import fs from 'fs';
//import application from './appli.json';
function usage() {
let sp = process.argv[1].lastIndexOf(path.delimiter);
console.log("usage:");
console.log("\t",
process.argv[1],
"description.json"
);
}
if (process.argv.length < 3) {
usage();
process.exit(1);
}
var appDescription = fs.readFileSync(process.argv[2]);
if (appDescription === '') {
console.error("File '"+process.argv[2]+"' is empty.");
process.exit(2);
}
var application = JSON.parse(appDescription);
var aap = new ARCS.Application();
aap.import(application);
aap.start();
......@@ -57,4 +57,4 @@ ARCS.__lib__`
}
}
}
`
\ No newline at end of file
`;
\ No newline at end of file
......
......@@ -9,7 +9,8 @@
},
"bin": {
"arcsrepos": "tools/arcsrepos.js",
"arcsm" : "tools/arcsm.js"
"arcsm" : "tools/arcsm.js",
"arcsnode": "tools/arcs_node.js"
},
"main": "build/arcs.js",
"scripts": {
......
......@@ -5,7 +5,9 @@ import process from 'node:process';
import * as fs from 'node:fs';
import {URL} from 'node:url';
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {execSync} from 'node:child_process';
let repositoryFile = null;
......@@ -41,7 +43,12 @@ const getRepository = function() {
return JSON.parse(fs.readFileSync(actualUrl.path, 'utf8'));
}
} catch (e) {
return JSON.parse(fs.readFileSync(repositoryFile, 'utf8'));
try {
return JSON.parse(fs.readFileSync(repositoryFile, 'utf8'));
} catch (e) {
console.log("Error: cannot read repository file", repositoryFile);
return null;
}
}
}
return null;
......@@ -56,16 +63,16 @@ const search = function(keyword) {
for (let i in components) {
let component = components[i];
if (i.toLowerCase().indexOf(keyword) >= 0) {
result.push(`${i}\t${component.library}`);
result.push(`${i}\t${component.library}\t${component.description}`);
}
if (component.description && component.description.toLowerCase().indexOf(keyword) >= 0) {
result.push(`${i}\t${component.library}`);
result.push(`${i}\t${component.library}\t${component.description}`);
}
if (component.keywords !== undefined) {
for (let j=0; j<component.keywords.length; j++) {
let word = component.keywords[j];
if (word.toLowerCase().indexOf(keyword) >= 0) {
result.push(`${i}\t${component.library}`);
result.push(`${i}\t${component.library}\t${component.description}`);
break;
}
}
......@@ -92,13 +99,20 @@ const projectPath = function() {
};
const processRecipe = function(recipe, projectPath) {
console.log(recipe);
if (recipe.from === undefined || recipe.to === undefined) { return ; }
let fileContents = fs.readFileSync(path.join(projectPath, recipe.from), 'utf8');
let outContents = fileContents;
if (recipe.transform) {
fileContents = recipe.transform(fileContents);
if (recipe.replace !== undefined) {
let contentsString = fileContents.toString();
for(let r in recipe.replace) {
if (recipe.replace.hasOwnProperty(r)) {
contentsString = contentsString.replace(r, recipe.replace[r]);
}
}
outContents = Buffer.from(contentsString);
}
let outPath = path.join(projectPath, recipe.to);
fs.mkdirSync(path.dirname(outPath), {recursive: true});
......@@ -108,7 +122,8 @@ const processRecipe = function(recipe, projectPath) {
const install = function(name) {
let components = repository.components;
let libraries = repository.libraries;
let library = libraries[name] || components[name]?.library;
let library = (libraries[name]?name:null) || components[name]?.library;
console.log("Installing library", library);
if (library === undefined) { return ; }
let libraryPath;
......@@ -135,20 +150,26 @@ const install = function(name) {
process.exit(1);
}
let componentPath = path.join(projectPath, 'components');
let componentPath = path.join(project, 'components');
let outLibraryPath = path.join(componentPath, relativePath);
fs.mkdirSync(path.dirname(outLibraryPath), {recursive: true});
fs.writeFileSync(outLibraryPath, libraryContent, 'utf8');
// this was the first part of the installation, now we need to install the dependencies
if (library.dependencies !== undefined) {
library.dependencies.forEach((dep) => {
process.execSync(`npm install ${dep}`, {stdio: 'inherit'});
console.log("Library installed");
let lib = libraries[library];
if (lib === undefined) { return ; }
if (lib.dependencies !== undefined) {
console.log("Installing dependencies");
lib.dependencies.forEach((dep) => {
execSync(`npm install ${dep}`, {stdio: 'inherit'});
});
}
if (library.recipes !== undefined) {
library.recipes.forEach((recipe) => {
processRecipe(recipe, projectPath);
if (lib.recipes !== undefined) {
console.log("Processing recipes")
lib.recipes.forEach((recipe) => {
processRecipe(recipe, project);
});
}
};
......
......@@ -77,31 +77,40 @@ for(const element of files) {
const contents = fs.readFileSync(element, 'utf8');
const exportMatch = contents.toString().match(/__lib__\`(.*)\`/s);
if (exportMatch !== null) {
const exportDefault = JSON.parse(exportMatch[1]);
console.log(exportDefault);
let metaCmp = exportDefault.components;
for(let d in metaCmp) {
cmp[d] = metaCmp[d];
cmp[d].library = path.relative(inputDir,element);
try {
if (exportMatch !== null) {
const exportDefault = JSON.parse(exportMatch[1]);
let metaCmp = exportDefault.components;
for(let d in metaCmp) {
cmp[d] = metaCmp[d];
cmp[d].library = path.relative(inputDir,element);
}
if (libraries[element] === undefined) {
libraries[path.relative(inputDir,element)] = {};
}
let metaLib = exportDefault.dependencies;
if (metaLib !== undefined) {
libraries[path.relative(inputDir,element)].dependencies = metaLib;
}
let metaRecipes = exportDefault.recipes;
if (metaRecipes !== undefined) {
libraries[path.relative(inputDir,element)].recipes = metaRecipes;
}
}
let metaLib = exportDefault.dependencies;
if (metaLib !== undefined) {
libraries[path.relative(inputDir,element)] = metaLib;
for (let c in cmp) {
if (components[c] === undefined) {
components[c] = cmp[c];
} else {
components[c] = [ components[c], cmp[c] ];
}
}
if (libraries[element] === undefined) {
libraries[path.relative(inputDir,element)] = null;
}
} catch (e) {
console.error("Could not parse library", element, e);
}
for (let c in cmp) {
if (components[c] === undefined) {
components[c] = cmp[c];
} else {
components[c] = [ components[c], cmp[c] ];
}
}
}
......