From 0c5e725b68e4193ad32ddb729e5f486cef7185ce Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 14 Dec 2018 18:44:46 -0500 Subject: [PATCH] chore(@embark/compiler): move compiler to typescript --- src/lib/modules/compiler/index.js | 68 ----------------------------- src/lib/modules/compiler/index.ts | 72 +++++++++++++++++++++++++++++++ src/typings/plugins.d.ts | 1 + 3 files changed, 73 insertions(+), 68 deletions(-) delete mode 100644 src/lib/modules/compiler/index.js create mode 100644 src/lib/modules/compiler/index.ts diff --git a/src/lib/modules/compiler/index.js b/src/lib/modules/compiler/index.js deleted file mode 100644 index 9be06d562..000000000 --- a/src/lib/modules/compiler/index.js +++ /dev/null @@ -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; diff --git a/src/lib/modules/compiler/index.ts b/src/lib/modules/compiler/index.ts new file mode 100644 index 000000000..2863f4ef8 --- /dev/null +++ b/src/lib/modules/compiler/index.ts @@ -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; diff --git a/src/typings/plugins.d.ts b/src/typings/plugins.d.ts index 90b0d6edf..d21ee8570 100644 --- a/src/typings/plugins.d.ts +++ b/src/typings/plugins.d.ts @@ -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[]; }