From f2818a685c88db5ba82c128f70fbd8aff298eb4e Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Sat, 3 Nov 2018 17:31:21 +0100 Subject: [PATCH] [fix] index.ts must not use "module.exports" --- app/cli.ts | 3 +-- app/modules/DuniterModule.ts | 24 +++++++++---------- app/modules/keypair/index.ts | 4 ++-- bin/duniter | 3 +-- index.ts | 8 +++---- test/fast/modules/bma/bma-module-test.ts | 4 ++-- .../crawler/crawler-peers-garbaging.ts | 4 ++-- .../modules/keypair/keypair-module-test.ts | 6 ++--- .../identity/identity-absorption.ts | 15 ++++++------ test/integration/misc/cli.ts | 4 ++-- test/integration/protocol/v1.0-modules-api.ts | 14 +++++------ 11 files changed, 43 insertions(+), 46 deletions(-) diff --git a/app/cli.ts b/app/cli.ts index 33472bb2a..e6ae5f105 100644 --- a/app/cli.ts +++ b/app/cli.ts @@ -13,8 +13,7 @@ const Command = require('commander').Command; const pjson = require('../package.json'); -const duniter = require('../index'); - + export const ExecuteCommand = () => { const options:any = []; diff --git a/app/modules/DuniterModule.ts b/app/modules/DuniterModule.ts index 45f4de7d8..3a65f9b44 100644 --- a/app/modules/DuniterModule.ts +++ b/app/modules/DuniterModule.ts @@ -19,17 +19,17 @@ export interface DuniterDependency { beforeSave: (conf: ConfDTO, program: ProgramOptions, logger:any, confDAL: ConfDAL) => void } onReset?: { - data: (conf: ConfDTO, program: ProgramOptions, logger:any, confDAL: ConfDAL) => void - config: (conf: ConfDTO, program: ProgramOptions, logger:any, confDAL: ConfDAL) => void + data?: (conf: ConfDTO, program: ProgramOptions, logger:any, confDAL: ConfDAL) => void + config?: (conf: ConfDTO, program: ProgramOptions, logger:any, confDAL: ConfDAL) => void } wizard?: { - [k: string]: (conf: ConfDTO, program: ProgramOptions) => Promise<void> + [k: string]: (conf: ConfDTO, program: ProgramOptions, logger:any) => Promise<void> } service?: { - input: (server: Server, conf: ConfDTO, logger:any) => ReadableDuniterService - process: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService - output: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService - neutral: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService + input?: (server: Server, conf: ConfDTO, logger:any) => ReadableDuniterService + process?: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService + output?: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService + neutral?: (server: Server, conf: ConfDTO, logger:any) => TransformableDuniterService } } @@ -44,10 +44,10 @@ export interface CliCommand { desc: string logs?: boolean preventIfRunning?: boolean - onConfiguredExecute?: (server: Server, conf?: ConfDTO, program?: ProgramOptions, params?: string[], wizardTasks?: any, stack?: Stack) => Promise<void> - onDatabaseExecute?: (server: Server, conf?: ConfDTO, program?: ProgramOptions, params?: string[], - startServices?: () => Promise<void>, - stopServices?: () => Promise<void>, - stack?: Stack + onConfiguredExecute?: (server: Server, conf: ConfDTO, program: ProgramOptions, params: string[], wizardTasks: any, stack: Stack) => Promise<any> + onDatabaseExecute?: (server: Server, conf: ConfDTO, program: ProgramOptions, params: string[], + startServices: () => Promise<void>, + stopServices: () => Promise<void>, + stack: Stack ) => Promise<void> } diff --git a/app/modules/keypair/index.ts b/app/modules/keypair/index.ts index 50f8a3d11..d91d50895 100644 --- a/app/modules/keypair/index.ts +++ b/app/modules/keypair/index.ts @@ -52,14 +52,14 @@ export const KeypairDependency = { name: 'pub', desc: 'Shows the node public key', logs: false, - onConfiguredExecute: (server:Server, conf:ConfDTO) => { + onConfiguredExecute: async (server:Server, conf:ConfDTO) => { console.log(conf.pair.pub) } }, { name: 'sec', desc: 'Shows the node secret key', logs: false, - onConfiguredExecute: (server:Server, conf:ConfDTO) => { + onConfiguredExecute: async (server:Server, conf:ConfDTO) => { console.log(conf.pair.sec) } }], diff --git a/bin/duniter b/bin/duniter index d91cfe530..47f0e6e7b 100755 --- a/bin/duniter +++ b/bin/duniter @@ -15,8 +15,7 @@ process.on('uncaughtException', (err) => { (async () => { try { - const duniter = require('../index'); - const stack = duniter.statics.autoStack(); + const stack = require('../index').Statics.autoStack(); await stack.executeStack(process.argv); // Everything went well, close Duniter quietly. process.exit(); diff --git a/index.ts b/index.ts index 1f9e45ca6..c8d5db1a2 100644 --- a/index.ts +++ b/index.ts @@ -53,7 +53,7 @@ process.on('unhandledRejection', (reason) => { logger.error(reason); }); -class Stacks { +export class Stacks { static todoOnRunDone:() => any = () => process.exit() @@ -84,7 +84,7 @@ class Stacks { return Stacks.todoOnRunDone() } - static autoStack(priorityModules:any) { + static autoStack(priorityModules?:DuniterModule[]) { const duniterModules = []; let duniterDeps:any = [] @@ -140,11 +140,11 @@ const DEFAULT_DEPENDENCIES = MINIMAL_DEPENDENCIES.concat([ const PRODUCTION_DEPENDENCIES = DEFAULT_DEPENDENCIES.concat([ ]); -module.exports = function (home:string, memory:boolean, overConf:any) { +export function NewDuniterServer(home:string, memory:boolean, overConf:any) { return new Server(home, memory, overConf); } -module.exports.statics = { +export const Statics = { logger: logger, diff --git a/test/fast/modules/bma/bma-module-test.ts b/test/fast/modules/bma/bma-module-test.ts index 4e85f3973..58c20ea81 100644 --- a/test/fast/modules/bma/bma-module-test.ts +++ b/test/fast/modules/bma/bma-module-test.ts @@ -14,13 +14,13 @@ import {BmaDependency} from "../../../../app/modules/bma/index" import {KeypairDependency} from "../../../../app/modules/keypair/index" import {Network} from "../../../../app/modules/bma/lib/network" +import {Statics} from "../../../../index" const assert = require('assert'); const should = require('should'); -const duniter = require('../../../../index') const rp = require('request-promise'); -const stack = duniter.statics.minimalStack(); +const stack = Statics.minimalStack(); stack.registerDependency(KeypairDependency, 'duniter-keypair'); stack.registerDependency(BmaDependency, 'duniter-bma'); diff --git a/test/fast/modules/crawler/crawler-peers-garbaging.ts b/test/fast/modules/crawler/crawler-peers-garbaging.ts index 5f51ce67e..63eefa112 100644 --- a/test/fast/modules/crawler/crawler-peers-garbaging.ts +++ b/test/fast/modules/crawler/crawler-peers-garbaging.ts @@ -13,9 +13,9 @@ import {cleanLongDownPeers} from "../../../../app/modules/crawler/lib/garbager" import {Server} from "../../../../server" +import {Statics} from "../../../../index" const should = require('should'); -const duniter = require('../../../../index') let stack:any @@ -23,7 +23,7 @@ describe('Peers garbaging', () => { before(() => { - stack = duniter.statics.autoStack([{ + stack = Statics.autoStack([{ name: 'garbager', required: { duniter: { diff --git a/test/fast/modules/keypair/keypair-module-test.ts b/test/fast/modules/keypair/keypair-module-test.ts index 2b5f5db65..45998ad1e 100644 --- a/test/fast/modules/keypair/keypair-module-test.ts +++ b/test/fast/modules/keypair/keypair-module-test.ts @@ -12,16 +12,16 @@ // GNU Affero General Public License for more details. import {KeypairDependency} from "../../../../app/modules/keypair/index" +import {Statics} from "../../../../index" const should = require('should'); -const duniter = require('../../../../index') describe('Module usage', () => { it('wrong options should throw', async () => { let errMessage; try { - const stack = duniter.statics.minimalStack(); + const stack = Statics.minimalStack(); stack.registerDependency(KeypairDependency, 'duniter-keypair'); await stack.executeStack(['node', 'index.js', 'config', '--memory', '--keyN', '2048']); } catch (e) { @@ -32,7 +32,7 @@ describe('Module usage', () => { }) it('no options on brand new node should generate random key', async () => { - const stack = duniter.statics.minimalStack(); + const stack = Statics.minimalStack(); stack.registerDependency(KeypairDependency, 'duniter-keypair'); const res = await stack.executeStack(['node', 'index.js', 'config', '--memory']); // This is extremely very unlikely to happen diff --git a/test/integration/identity/identity-absorption.ts b/test/integration/identity/identity-absorption.ts index 4ee1051e0..ec7ed91ce 100644 --- a/test/integration/identity/identity-absorption.ts +++ b/test/integration/identity/identity-absorption.ts @@ -11,7 +11,7 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. -import {TestingServer} from "../tools/toolbox" +import {NewTestingServer, TestingServer} from "../tools/toolbox" import {TestUser} from "../tools/TestUser" import {BmaDependency} from "../../../app/modules/bma/index" import {shouldFail} from "../../unit-tools" @@ -19,7 +19,6 @@ import {Underscore} from "../../../app/lib/common-libs/underscore" import {shutDownEngine} from "../tools/shutdown-engine" import {expectAnswer} from "../tools/http-expect" -const duniter = require('../../../index'); const rp = require('request-promise'); const MEMORY_MODE = true; @@ -39,10 +38,10 @@ describe("Identity absorption", () => { before(async () => { - s1 = duniter( - '/bb12', - MEMORY_MODE, + s1 = NewTestingServer( Underscore.extend({ + name: 'bb12', + memory: MEMORY_MODE, port: '4450', pair: { pub: 'HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', @@ -50,10 +49,10 @@ describe("Identity absorption", () => { } }, commonConf)); - s2 = duniter( - '/bb12', - MEMORY_MODE, + s2 = NewTestingServer( Underscore.extend({ + name: 'bb12', + memory: MEMORY_MODE, port: '4451', pair: { pub: 'DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV', diff --git a/test/integration/misc/cli.ts b/test/integration/misc/cli.ts index 7da9a1253..4a02c6a5e 100644 --- a/test/integration/misc/cli.ts +++ b/test/integration/misc/cli.ts @@ -14,11 +14,11 @@ import {hashf} from "../../../app/lib/common" import {fakeSyncServer} from "../tools/toolbox" import {Underscore} from "../../../app/lib/common-libs/underscore" +import {Statics} from "../../../index" const spawn = require('child_process').spawn; const path = require('path'); const should = require('should'); -const duniter = require('../../../index'); const DB_NAME = "unit_tests"; @@ -157,7 +157,7 @@ describe("CLI", function() { */ async function execute(args:(string)[]) { const finalArgs = [process.argv[0], __filename].concat(args).concat(['--mdb', DB_NAME]); - const stack = duniter.statics.autoStack(); + const stack = Statics.autoStack(); // Executes the command return stack.executeStack(finalArgs); } diff --git a/test/integration/protocol/v1.0-modules-api.ts b/test/integration/protocol/v1.0-modules-api.ts index 28c2798a4..76f309372 100644 --- a/test/integration/protocol/v1.0-modules-api.ts +++ b/test/integration/protocol/v1.0-modules-api.ts @@ -17,27 +17,27 @@ import {Server} from "../../../server" import {ConfDTO} from "../../../app/lib/dto/ConfDTO" import {KeypairDependency} from "../../../app/modules/keypair/index" import {BmaDependency} from "../../../app/modules/bma/index" +import {Statics} from "../../../index" const should = require('should'); const util = require('util'); const path = require('path'); -const duniter = require('../../../index'); const querablep = require('querablep'); describe("v1.0 Module API", () => { it('should be able to execute `hello` command with quickRun', async () => { - duniter.statics.setOnRunDone(() => { /* Do not exit the process */ }) + Statics.setOnRunDone(() => { /* Do not exit the process */ }) const absolutePath = path.join(__dirname, '../scenarios/hello-plugin.js') process.argv = ['', absolutePath, 'hello-world', '--memory'] - const res = await duniter.statics.quickRun(absolutePath) + const res = await Statics.quickRun(absolutePath) res.should.equal('Hello world! from within Duniter.') }) it('should be able to execute `hello` command', async () => { - const sStack = duniter.statics.simpleStack(); - const aStack = duniter.statics.autoStack(); + const sStack = Statics.simpleStack(); + const aStack = Statics.autoStack(); const helloDependency = { duniter: { @@ -77,7 +77,7 @@ describe("v1.0 Module API", () => { before(async () => { - stack = duniter.statics.simpleStack(); + stack = Statics.simpleStack(); const configurationDependency = { duniter: { cliOptions: [ @@ -174,7 +174,7 @@ describe("v1.0 Module API", () => { before(async () => { - stack = duniter.statics.simpleStack(); + stack = Statics.simpleStack(); fakeI = new FakeStream((that:any, data:any) => { // Note: we never pass here if (typeof data == "string") { -- GitLab