Skip to content

Commit

Permalink
Better typescript setup for monorepo (#17)
Browse files Browse the repository at this point in the history
* feat(mnonorepo): project paths

* feat: build:static. project refrences

* fix: include for utils-general. ts update

* fix: references. main in package.json. compiler flags

* fix: types path in utils-general

* Fix part of packages

* Update imports

* Faster incremental UI builds

* Publish only required files

* merge fix

* fix: include schemas dir

* Update tests

* Remove test that is deleted on master as well
  • Loading branch information
kosecki123 authored Sep 3, 2019
1 parent b6c2a31 commit 9e4b725
Show file tree
Hide file tree
Showing 75 changed files with 686 additions and 414 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
},
"scripts": {
"build": "lerna run build",
"build:ui": "lerna run build --scope @energyweb/origin-ui --stream",
"lint": "lerna run lint --parallel --stream",
"clean": "lerna run clean --parallel",
"build:static": "lerna run build:static --parallel",
"lint:simulator": "lerna run lint --scope @energyweb/solar-simulator --stream",
"lint-fix:simulator": "lerna run lint-fix --scope @energyweb/solar-simulator --stream",
"prettier:simulator": "lerna run prettier --scope @energyweb/solar-simulator --stream",
Expand All @@ -33,7 +36,8 @@
"test": "yarn test:ui && yarn test:contracts && yarn test:matcher",
"test:ui": "lerna run test --scope @energyweb/origin-ui --stream",
"test:contracts": "lerna run --scope @energyweb/asset-registry --stream test-contracts && lerna run --scope @energyweb/origin --stream test-contracts && lerna run --scope @energyweb/user-registry --stream test-contracts && lerna run --scope @energyweb/market --stream test-contracts",
"test:matcher": "lerna run test --scope @energyweb/market-matcher --stream"
"test:matcher": "lerna run test --scope @energyweb/market-matcher --stream",
"test:user-registry": "lerna run test --scope @energyweb/user-registry --stream"
},
"workspaces": [
"packages/*"
Expand Down
11 changes: 8 additions & 3 deletions packages/asset-registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"main": "dist/js/src/index.js",
"files": [
"contracts",
"dist"
"dist/js/build",
"dist/js/src",
"dist/js/schemas",
"dist/schemas"
],
"directories": {
"test": "test"
Expand All @@ -27,7 +30,7 @@
"build-schema:ProducingAssetPropertiesOffChain": "typescript-json-schema --ignoreErrors --required src/blockchain-facade/ProducingAsset.ts IOffChainProperties > schemas/ProducingAssetPropertiesOffChain.schema.json",
"prebuild-schemas": "rm -rf dist/schemas && mkdir -p dist/schemas && rm -rf schemas && mkdir schemas",
"build-schemas": "npm run build-schema:AssetPropertiesOffChain && npm run build-schema:ProducingAssetPropertiesOffChain && cp -R schemas dist/schemas",
"build-ts": "npm run build-schemas && rm -rf dist/js && tsc",
"build-ts": "npm run build-schemas && rm -rf dist/js && tsc -b tsconfig.build.json --verbose --pretty",
"compile": "truffle compile",
"compile-contracts": "node build/ts/utils/deployment/compile",
"deploy-contracts": "truffle migrate",
Expand All @@ -38,7 +41,9 @@
"start-test-backend": "node ../../node_modules/@energyweb/utils-testbackend/dist/js/src/index.js",
"test": "npm run build-schemas && npm run build && mocha dist/js/src/test/ --timeout 60000",
"prettier": "prettier --write --config-precedence file-override './src/**/*'",
"test-contracts": "concurrently --success first --kill-others -n eth,backend,test \"yarn start-ganache\" \"yarn start-test-backend\" \"yarn test\""
"test-contracts": "concurrently --success first --kill-others -n eth,backend,test \"yarn start-ganache\" \"yarn start-test-backend\" \"yarn test\"",
"clean": "rm -rf build dist",
"build:static": "yarn compile && yarn build-schemas"
},
"types": "dist/js/src/index.d.ts",
"dependencies": {
Expand Down
83 changes: 41 additions & 42 deletions packages/asset-registry/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,66 @@ const path = require('path');
const fs = require('fs-extra');

function relativePath(pathToAdd) {
return path.join(__dirname, pathToAdd);
return path.join(__dirname, pathToAdd);
}

function getRootDirectory() {
const PATHS_TO_TEST = [
relativePath('../')
];
const PATHS_TO_TEST = [relativePath('../')];

for (const path of PATHS_TO_TEST) {
if (fs.existsSync(path)) {
return path;
for (const path of PATHS_TO_TEST) {
if (fs.existsSync(path)) {
return path;
}
}
}

throw `Can't find contracts directory`;
throw `Can't find contracts directory`;
}

const ROOT_DIRECTORY = getRootDirectory();

async function executeCommand(command, directory) {
const options = {};

if (directory) {
options.cwd = directory;
}

try {
const { stdout, stderr } = await exec(command, options);

console.log(stdout);
console.error(stderr);
} catch (error) {
if (error && error.stdout) {
console.error(error.stdout);
} else {
console.error(error);
const options = {};

if (directory) {
options.cwd = directory;
}

try {
const { stdout, stderr } = await exec(command, options);

console.log(stdout);
console.error(stderr);
} catch (error) {
if (error && error.stdout) {
console.error(error.stdout);
} else {
console.error(error);
}
process.exit(1);
}
}
}

async function run() {
console.log('EW-ASSET-REGISTRY-LIB: Building...');
console.log('EW-ASSET-REGISTRY-LIB: Building...');

await executeCommand('yarn compile', ROOT_DIRECTORY)
await executeCommand('yarn build-ts', ROOT_DIRECTORY)
await executeCommand('yarn compile', ROOT_DIRECTORY);
await executeCommand('yarn build-ts', ROOT_DIRECTORY);

if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/src`))) {
await fs.move(`${ROOT_DIRECTORY}/dist/js`, `${ROOT_DIRECTORY}/dist/js-temp`);
await fs.ensureDir(`${ROOT_DIRECTORY}/dist/js`);
await fs.move(`${ROOT_DIRECTORY}/dist/js-temp`, `${ROOT_DIRECTORY}/dist/js/src`);
}
if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/src`))) {
await fs.move(`${ROOT_DIRECTORY}/dist/js`, `${ROOT_DIRECTORY}/dist/js-temp`);
await fs.ensureDir(`${ROOT_DIRECTORY}/dist/js`);
await fs.move(`${ROOT_DIRECTORY}/dist/js-temp`, `${ROOT_DIRECTORY}/dist/js/src`);
}

if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/schemas`))) {
await fs.move(`${ROOT_DIRECTORY}/schemas`, `${ROOT_DIRECTORY}/dist/js/schemas`);
}
if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/schemas`))) {
await fs.move(`${ROOT_DIRECTORY}/schemas`, `${ROOT_DIRECTORY}/dist/js/schemas`);
}

if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/build`))) {
await fs.move(`${ROOT_DIRECTORY}/build`, `${ROOT_DIRECTORY}/dist/js/build`);
}
if (!(await fs.pathExists(`${ROOT_DIRECTORY}/dist/js/build`))) {
await fs.move(`${ROOT_DIRECTORY}/build`, `${ROOT_DIRECTORY}/dist/js/build`);
}

console.log('EW-ASSET-REGISTRY-LIB: Done.');
console.log('EW-ASSET-REGISTRY-LIB: Done.');
}

run();
run();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneralFunctions, SpecialTx, SearchLog } from './GeneralFunctions';
import Web3 = require('web3');
import Web3 from 'web3';
import AssetConsumingDBJSON from '../../build/contracts/AssetConsumingDB.json';

export class AssetConsumingDB extends GeneralFunctions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneralFunctions, SpecialTx, SearchLog, getClientVersion } from './GeneralFunctions';
import Web3 = require('web3');
import Web3 from 'web3';
import AssetConsumingRegistryLogicJSON from '../../build/contracts/AssetConsumingRegistryLogic.json';
import moment from 'moment';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneralFunctions, SpecialTx, SearchLog, getClientVersion } from './GeneralFunctions';
import Web3 = require('web3');
import Web3 from 'web3';
import AssetContractLookupJSON from '../../build/contracts/AssetContractLookup.json';

export class AssetContractLookup extends GeneralFunctions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneralFunctions, SpecialTx, SearchLog, getClientVersion } from './GeneralFunctions';
import Web3 = require('web3');
import Web3 from 'web3';
import AssetProducingDBJSON from '../../build/contracts/AssetProducingDB.json';

export class AssetProducingDB extends GeneralFunctions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneralFunctions, SpecialTx, SearchLog, getClientVersion } from './GeneralFunctions';
import Web3 = require('web3');
import Web3 from 'web3';
import AssetProducingRegistryLogicJSON from '../../build/contracts/AssetProducingRegistryLogic.json';
import moment from 'moment';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Web3 = require('web3');
import Web3 from 'web3';
import { Tx } from 'web3/eth/types';
import { TransactionReceipt, Logs } from 'web3/types';

Expand Down
20 changes: 20 additions & 0 deletions packages/asset-registry/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"composite": true,
"rootDir": "./",
"outDir": "./dist/js"
},
"include": ["./src/**/*", "./schemas/*.json", "./build/**/*.json"],
"references": [
{
"path": "../user-registry/tsconfig.build.json"
},
{
"path": "../utils-general/tsconfig.build.json"
},
{
"path": "../utils-testbackend/tsconfig.build.json"
}
]
}
6 changes: 2 additions & 4 deletions packages/asset-registry/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/js"
},
"include": ["./src/**/*"]
}
}
}
1 change: 0 additions & 1 deletion packages/market-matcher/.npmignore

This file was deleted.

18 changes: 12 additions & 6 deletions packages/market-matcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@
"url": "https://github.com/energywebfoundation/ewf-monorepo/issues"
},
"version": "1.1.0",
"main": "dist/js/src/exports.js",
"main": "dist/js/src/index.js",
"scripts": {
"build": "scripts/build.js",
"build-ts": "rm -rf dist && tsc",
"debug": "node --inspect-brk dist/js/src/index.js",
"build-ts": "rm -rf dist && tsc -b tsconfig.build.json --verbose --pretty",
"debug": "node --inspect-brk dist/js/src/main.js",
"generate-conf-schema": "typescript-json-schema --required --ignoreErrors tsconfig.json IMatcherConf > schemas/conf.schema.json",
"generate-rule-schema": "typescript-json-schema --required --ignoreErrors tsconfig.json IRuleConf > schemas/rule.schema.json",
"generate-schemas": "rm -rf schemas/ && mkdir schemas/ && npm run generate-sim-flow-schema && npm run generate-rule-schema && npm run generate-conf-schema",
"generate-sim-flow-schema": "typescript-json-schema --required --ignoreErrors tsconfig.json ISimulationFlow > schemas/simulation-description.schema.json",
"lint": "tslint 'src/**/*{.ts,.tsx}'",
"lint-fix": "tslint --fix 'src/**/*{.ts,.tsx}'",
"prepare": "scripts/build.js",
"start": "npm run build && node dist/js/src/index.js",
"start": "npm run build && node dist/js/src/main.js",
"start-ganache": "ganache-cli -m 'chalk park staff buzz chair purchase wise oak receive avoid avoid home' -l 8000000 -e 1000000 -a 20",
"start-test-backend": "node ../../node_modules/@energyweb/utils-testbackend/dist/js/src/index.js",
"prettier": "prettier --write --config-precedence file-override './src/**/*'",
"test": "concurrently --success first --kill-others -n eth,backend,test \"yarn start-ganache\" \"yarn start-test-backend\" \"yarn build && mocha --exit dist/js/src/test/\"",
"test-ts": "concurrently --success first --kill-others -n eth,backend,test \"yarn start-ganache\" \"yarn start-test-backend\" \"yarn generate-schemas && mocha --exit -r ts-node/register src/test/*.ts\""
"test-ts": "concurrently --success first --kill-others -n eth,backend,test \"yarn start-ganache\" \"yarn start-test-backend\" \"yarn generate-schemas && mocha --exit -r ts-node/register src/test/*.ts\"",
"clean": "rm -rf build dist",
"build:static": "yarn generate-schemas"
},
"types": "dist/js/src/exports.d.ts",
"dependencies": {
Expand Down Expand Up @@ -64,5 +66,9 @@
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
}
},
"files": [
"dist/js/src",
"dist/js/schemas"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
createBlockchainProperties as issuerCreateBlockchainProperties
} from '@energyweb/origin';
import { Configuration, ContractEventHandler, EventHandlerManager } from '@energyweb/utils-general';
import { EthAccount } from '@energyweb/utils-general/dist/js/blockchain-facade/Configuration';
import Web3 from 'web3';

import { logger } from '../Logger';
Expand Down Expand Up @@ -67,7 +66,7 @@ export const initMatchingManager = async (controller: Controller, conf: Configur

export const createBlockchainConf = async (
blockchainSectionConfFile: IBlockchainDataSource,
matcherAccount: EthAccount
matcherAccount: Configuration.EthAccount
): Promise<Configuration.Entity> => {
const web3 = new Web3(blockchainSectionConfFile.web3Url);
const marketConf = await marketCreateBlockchainProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Agreement, Demand, Supply } from '@energyweb/market';
import { Certificate } from '@energyweb/origin';
import { Configuration, TimeFrame } from '@energyweb/utils-general';

import { METHOD_NOT_IMPLEMENTED } from '../exports';
import { METHOD_NOT_IMPLEMENTED } from '..';
import { logger } from '../Logger';
import * as SimulationFlowDef from '../schema-defs/simulation-flow';
import { initEventHandling, initMatchingManager } from './BlockchainConnection';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { TimeFrame } from '@energyweb/utils-general';
import * as fs from 'fs';
import * as LogSymbols from 'log-symbols';

import { METHOD_NOT_IMPLEMENTED } from '..';
import * as SimulationDescriptionSchema from '../../schemas/simulation-description.schema.json';
import { METHOD_NOT_IMPLEMENTED } from '../exports';
import { logger } from '../Logger';
import { ISimulationDataSource } from '../schema-defs/MatcherConf';
import * as SimulationFlowDef from '../schema-defs/simulation-flow/';
Expand Down
Loading

0 comments on commit 9e4b725

Please sign in to comment.