Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/watcher esm #27

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ steps:
- cd watcher/
- npm ci
- npm run ci-test-token
- npm run ci-test-token-enterprise
- npm run ci-test-native

services:
Expand Down
60 changes: 46 additions & 14 deletions watcher/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
{
"extends": ["airbnb-base", "prettier"],
"env": {
"es2020": true
},
"parserOptions": {
"ecmaVersion": 2020
},
"plugins": ["sonarjs"],
"extends": [
"airbnb-base",
"plugin:import/recommended",
"plugin:sonarjs/recommended",
"prettier"
],
"rules": {
"no-await-in-loop": "off",
"no-console": 0,
"no-constant-condition": ["error", { "checkLoops": false }],
"no-param-reassign": [
"error",
{ "props": true, "ignorePropertyModificationsFor": ["ret"] }
],
"no-underscore-dangle": [
"error",
{ "allow": ["_id", "_isBigNumber", "_websocket"] }
],
"default-param-last": "off",
"new-cap": "off",
"no-console": "error",
"no-underscore-dangle": "off",
"no-template-curly-in-string": "off",
"max-classes-per-file": "off",
"max-len": "off",
"max-classes-per-file": "off"
}
"import/prefer-default-export": "off",
"import/extensions": ["error", "always"],
"sonarjs/cognitive-complexity": ["warn", 15],
"sonarjs/no-duplicate-string": "off"
},
"overrides": [
{
"files": ["generateEsModulesFromJson.cjs"],
"rules": {
"no-console": "off"
}
},
{
"files": ["./test/**/*"],
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false,
"ecmaVersion": 2020
},
"env": {
"jest": true
},
"rules": {
"no-console": "off"
}
}
]
}
97 changes: 97 additions & 0 deletions watcher/generateEsModulesFromJson.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const path = require('path');
const { writeFile, stat, mkdir } = require('fs/promises');

const minifiers = {
package: ({ name, version, description }) => ({ name, version, description }),
abi: ({ abi }) => ({ abi }),
truffleDeployment: ({ abi, networks }) => ({
abi,
networks: Object.fromEntries(
Object.entries(networks).map(([chainId, { address }]) => [
chainId,
{ address },
]),
),
}),
hardhatDeployment: ({ address }) => ({ address }),
};

const sources = [
[
'@iexec/erlc/build/contracts-min/ERLCTokenSwap.json',
{ dir: '@iexec/erlc', minifier: minifiers.truffleDeployment },
],
[
'@iexec/poco/artifacts/contracts/registries/RegistryEntry.sol/RegistryEntry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/IexecInterfaceToken.sol/IexecInterfaceToken.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/IexecInterfaceNative.sol/IexecInterfaceNative.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/apps/AppRegistry.sol/AppRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/workerpools/WorkerpoolRegistry.sol/WorkerpoolRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/datasets/DatasetRegistry.sol/DatasetRegistry.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/apps/App.sol/App.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/workerpools/Workerpool.sol/Workerpool.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
[
'@iexec/poco/artifacts/contracts/registries/datasets/Dataset.sol/Dataset.json',
{ dir: '@iexec/poco', minifier: minifiers.abi },
],
];

const createEsModule = (jsonObj) => {
let module =
'// this file is auto generated do not edit it\n/* eslint-disable */\n';
Object.entries(jsonObj).forEach(([key, value]) => {
module += `export const ${key} = ${JSON.stringify(value)};\n`;
});
module += `export default { ${Object.keys(jsonObj).join(', ')} };`;
return module;
};

console.log('converting json files to es modules');

sources.map(async ([src, options]) => {
// eslint-disable-next-line import/no-dynamic-require, global-require
const jsonObj = require(src);
const minifiedJsonObj = options.minifier
? options.minifier(jsonObj)
: jsonObj;
const name =
(options && options.name) ||
`${src.split('/').pop().split('.json').shift()}.js`;
const module = createEsModule(minifiedJsonObj);
const outDir = path.join(`src/generated`, options && options.dir);

const outDirExists = await stat(outDir)
.then((outDirStat) => outDirStat.isDirectory())
.catch(() => false);
if (!outDirExists) {
await mkdir(outDir, {
recursive: true,
});
}
const outPath = path.join(outDir, name);
await writeFile(outPath, module);
console.log(`${src} => ${outPath}`);
});
Loading