chore(@embark/compiler): move compiler to typescript

This commit is contained in:
Iuri Matias 2018-12-14 18:44:46 -05:00
parent 6aa8781ff5
commit 0c5e725b68
3 changed files with 73 additions and 68 deletions

View File

@ -1,68 +0,0 @@
let async = require('../../utils/async_extend.js');
class Compiler {
constructor(embark, options) {
const self = this;
this.plugins = options.plugins;
this.events = embark.events;
this.logger = embark.logger;
this.disableOptimizations = options.disableOptimizations;
this.events.setCommandHandler("compiler:contracts", function(contractFiles, options, cb) {
self.compile_contracts(contractFiles, options, cb);
});
}
compile_contracts(contractFiles, options, cb) {
const self = this;
let available_compilers = {};
if (contractFiles.length === 0) {
return cb(null, {});
}
let pluginCompilers = self.plugins.getPluginsProperty('compilers', 'compilers');
pluginCompilers.forEach(function (compilerObject) {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
let compiledObject = {};
let compilerOptions = {
disableOptimizations: this.disableOptimizations || options.disableOptimizations
};
async.eachObject(available_compilers,
function (extension, compiler, callback) {
let matchingFiles = contractFiles.filter(function (file) {
let fileMatch = file.filename.match(/\.[0-9a-z]+$/);
if (fileMatch && (fileMatch[0] === extension)) {
file.compiled = true;
return true;
}
return false;
});
if (!matchingFiles || !matchingFiles.length) {
return callback();
}
compiler.call(compiler, matchingFiles, compilerOptions, function (err, compileResult) {
Object.assign(compiledObject, compileResult);
callback(err, compileResult);
});
},
function (err) {
contractFiles.forEach(file => {
if (!file.compiled) {
self.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
}
});
cb(err, compiledObject);
}
);
}
}
module.exports = Compiler;

View File

@ -0,0 +1,72 @@
const async = require("../../utils/async_extend.js");
import { Embark } from "../../../typings/embark";
import { Plugins } from "../../../typings/plugins";
interface CompilerPluginObject {
extension: string;
cb: any;
}
class Compiler {
private logger: any;
private plugins: Plugins;
private disableOptimizations: any;
constructor(embark: Embark, options: any) {
this.logger = embark.logger;
this.plugins = options.plugins;
this.disableOptimizations = options.disableOptimizations;
embark.events.setCommandHandler("compiler:contracts", this.compile_contracts.bind(this));
}
private compile_contracts(contractFiles: any[], options: any, cb: any) {
const available_compilers: { [index:string] : any } = {};
if (contractFiles.length === 0) {
return cb(null, {});
}
this.plugins.getPluginsProperty("compilers", "compilers").forEach((compilerObject: CompilerPluginObject) => {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
const compiledObject: { [index:string] : any } = {};
const compilerOptions = {
disableOptimizations: this.disableOptimizations || options.disableOptimizations,
};
async.eachObject(available_compilers,
(extension: string, compiler: any, callback: any) => {
const matchingFiles = contractFiles.filter((file: any) => {
const fileMatch = file.filename.match(/\.[0-9a-z]+$/);
if (fileMatch && (fileMatch[0] === extension)) {
file.compiled = true;
return true;
}
return false;
});
if (!matchingFiles || !matchingFiles.length) {
return callback();
}
compiler.call(compiler, matchingFiles, compilerOptions, (err: any, compileResult: any) => {
Object.assign(compiledObject, compileResult);
callback(err, compileResult);
});
},
(err: any) => {
contractFiles.forEach((file: any) => {
if (!file.compiled) {
this.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
}
});
cb(err, compiledObject);
},
);
}
}
module.exports = Compiler;

View File

@ -5,4 +5,5 @@ export interface Plugin {
export interface Plugins {
getPluginsFor(name: string): [Plugin];
loadInternalPlugin(name: string, options: any): void;
getPluginsProperty(prop: string, name: string): any[];
}