api refactoring

This commit is contained in:
Brian Seymour 2021-03-12 23:01:04 -06:00
parent 5a05537a7f
commit b3d18600cd
11 changed files with 358 additions and 327 deletions

View file

@ -8,53 +8,60 @@ const path = require('path');
const runtimes = [];
class Runtime {
#env_vars
#compiled
constructor(package_dir){
const {language, version, author, build_platform, aliases} = JSON.parse(
let info = JSON.parse(
fss.read_file_sync(path.join(package_dir, 'pkg-info.json'))
);
const { language, version, author, build_platform, aliases } = info;
this.pkgdir = package_dir;
this.language = language;
this.version = semver.parse(version);
this.author = author;
this.aliases = aliases;
if(build_platform != globals.platform){
logger.warn(`Package ${language}-${version} was built for platform ${build_platform}, but our platform is ${globals.platform}`);
if (build_platform !== globals.platform) {
logger.warn(
`Package ${language}-${version} was built for platform ${build_platform}, ` +
`but our platform is ${globals.platform}`
);
}
logger.debug(`Package ${language}-${version} was loaded`);
runtimes.push(this);
}
get env_file_path(){
return path.join(this.pkgdir, 'environment');
get compiled() {
if (this.compiled === undefined) {
this.compiled = fss.exists_sync(path.join(this.pkgdir, 'compile'));
}
return this.compiled;
}
get compiled(){
if(this.#compiled === undefined) this.#compiled = fss.exists_sync(path.join(this.pkgdir, 'compile'));
return this.#compiled;
}
get env_vars(){
if(!this.#env_vars){
get env_vars() {
if (!this.env_vars) {
const env_file = path.join(this.pkgdir, '.env');
const env_content = fss.read_file_sync(env_file).toString();
this.#env_vars = {};
this.env_vars = {};
env_content
.trim()
.split('\n')
.map(line => line.split('=',2))
.forEach(([key,val]) => {
this.#env_vars[key.trim()] = val.trim();
this.env_vars[key.trim()] = val.trim();
});
}
return this.#env_vars;
return this.env_vars;
}
toString(){
toString() {
return `${this.language}-${this.version.raw}`;
}
}
@ -68,4 +75,3 @@ module.exports.get_latest_runtime_matching_language_version = function(lang, ver
return module.exports.get_runtimes_matching_language_version(lang, ver)
.sort((a,b) => semver.rcompare(a.version, b.version))[0];
};