From 439f602f45690155f05b5bfc04c7242a729402f6 Mon Sep 17 00:00:00 2001 From: Jeremias Diaz Date: Mon, 21 Feb 2022 16:05:33 -0300 Subject: [PATCH] chore(*): create signing manager generator add contribution guidelines, basic docs, improve commit config --- .cz-config.js | 47 ++++--- .vscode/settings.json | 5 + CONTRIBUTING.md | 38 ++++++ README.md | 97 ++------------ commitlint.config.js | 2 +- nx.json | 25 ++-- package.json | 16 ++- packages/local-signing-manager/README.md | 23 +++- packages/local-signing-manager/package.json | 6 + packages/types/README.md | 6 +- tools/generators/signing-manager/index.ts | 128 +++++++++++++++++++ tools/generators/signing-manager/schema.json | 17 +++ tools/tsconfig.tools.json | 3 +- yarn.lock | 23 ++++ 14 files changed, 312 insertions(+), 124 deletions(-) create mode 100644 CONTRIBUTING.md create mode 100644 tools/generators/signing-manager/index.ts create mode 100644 tools/generators/signing-manager/schema.json diff --git a/.cz-config.js b/.cz-config.js index ebb6cc50..0a472c48 100644 --- a/.cz-config.js +++ b/.cz-config.js @@ -1,8 +1,17 @@ module.exports = { types: [ - { value: 'feat', name: 'feat: A new feature' }, - { value: 'fix', name: 'fix: A bug fix' }, - { value: 'docs', name: 'docs: Documentation only changes' }, + { + value: 'feat', + name: 'feat: A new feature', + }, + { + value: 'fix', + name: 'fix: A bug fix', + }, + { + value: 'docs', + name: 'docs: Documentation only changes', + }, { value: 'style', name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)', @@ -15,24 +24,38 @@ module.exports = { value: 'perf', name: 'perf: A code change that improves performance', }, - { value: 'test', name: 'test: Adding missing tests' }, + { + value: 'test', + name: 'test: Adding missing tests', + }, { value: 'chore', name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation', }, - { value: 'WIP', name: 'WIP: Work in progress' }, + { + value: 'WIP', + name: 'WIP: Work in progress', + }, + ], + scopes: [ + { + name: '*', + }, + { + name: 'types', + }, + { + name: 'local-signing-manager', + }, ], - scopes: [{ name: 'types' }, { name: 'local-signing-manager' }, { name: '*' }], allowTicketNumber: false, isTicketNumberRequired: false, ticketNumberPrefix: 'TICKET-', ticketNumberRegExp: '\\d{1,5}', - // override the messages, defaults are as follows messages: { type: "Select the type of change that you're committing:", scope: '\nDenote the SCOPE of this change (affected package, choose "*" for changes that affect all packages):', - // used if allowCustomScopes is true customScope: 'Denote the SCOPE of this change:', subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n', body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n', @@ -41,12 +64,6 @@ module.exports = { confirmCommit: 'Are you sure you want to proceed with the commit above?', }, allowCustomScopes: false, - allowBreakingChanges: ['feat', 'fix'], - // skip any questions you want - skipQuestions: ['body'], - // limit subject length + allowBreakingChanges: ['feat', 'fix', 'chore'], subjectLimit: 100, - // breaklineChar: '|', // It is supported for fields body and footer. - // footerPrefix : 'ISSUES CLOSED:' - // askForBreakingChangeFirst : true, // default is false }; diff --git a/.vscode/settings.json b/.vscode/settings.json index 509917d4..8860b551 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,10 +5,15 @@ "editor.tabSize": 2, "editor.formatOnSave": true, "cSpell.words": [ + "breakline", + "buildable", + "commitlint", + "hashicorp", "nrwl", "Polkadot", "polymathnetwork", "Polymesh", + "sonarcloud", "tscpaths" ], "cSpell.ignorePaths": [ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..edb74977 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,38 @@ +# Contributing + +_DISCLAIMER: These guidelines (as well as all documentation) are in constant development. Any feedback is greatly appreciated. Just open a github issue and we will do our best to incorporate your suggestions within reason._ + +Thanks for your efforts in helping the Polymesh ecosystem grow! + +We accept PRs with all kinds of contributions, including the creation of custom signing managers to support currently unsupported use cases. + +## Custom Signing Managers + +To create a new signing manager, run `yarn generate:signing-manager `. This will create a `packages/` directory with all the necessary configuration to build/test/release an npm package called `@polymathnetwork/`. Unit tests must be written using the [jest](https://jestjs.io/) testing framework. + +### Naming + +The package should be called `-signing-manager` (i.e. `hashicorp-vault-signing-manager`). Use existing packages as an example. + +## Commits + +This repo uses [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/). For automated release versions to work properly, commits must have the affected package name as a scope. For example, if adding a new feature to the `local-signing-manager` package, the commit message should be `feat(local-signing-manager): sign raw messages`. Commits that affect more than one package can use `*` as a scope. + +In order to ensure commits are properly formatted, we use husky to hijack the `git commit` command, so that it launches the commitizen CLI. Additionally, commit messages are validated with commitlint. + +## PR Requirements + +In order for a PR to be merged, it must: + +- Produce no linting errors +- Have the proper type, scope and description in all commits +- Be reasonably type safe (avoid type assertions, non-null assertions and use of `any` and `unknown` within reason) +- Pass all unit tests +- Pass the sonarcloud quality gate (no code smells and 100% unit test coverage) +- Be properly documented with JSDoc comments +- Be added to the table in the project README +- Have an installation and usage example in its own README + +On top of that, all PRs will be subject to review by the Polymesh association. The association reserves the right to reject submissions on any basis. That said, we certainly welcome and encourage external contributions. + +As soon as your PR is merged, our CI pipeline will handle releasing new versions of all affected packages to npm. diff --git a/README.md b/README.md index c264edc7..cf5d7c36 100644 --- a/README.md +++ b/README.md @@ -1,94 +1,19 @@ - - # SigningManagers -This project was generated using [Nx](https://nx.dev). - -

- -🔎 **Smart, Fast and Extensible Build System** - -## Adding capabilities to your workspace - -Nx supports many plugins which add capabilities for developing different types of applications and different tools. - -These capabilities include generating applications, libraries, etc as well as the devtools to test, and build projects as well. - -Below are our core plugins: - -- [React](https://reactjs.org) - - `npm install --save-dev @nrwl/react` -- Web (no framework frontends) - - `npm install --save-dev @nrwl/web` -- [Angular](https://angular.io) - - `npm install --save-dev @nrwl/angular` -- [Nest](https://nestjs.com) - - `npm install --save-dev @nrwl/nest` -- [Express](https://expressjs.com) - - `npm install --save-dev @nrwl/express` -- [Node](https://nodejs.org) - - `npm install --save-dev @nrwl/node` - -There are also many [community plugins](https://nx.dev/community) you could add. - -## Generate an application - -Run `nx g @nrwl/react:app my-app` to generate an application. - -> You can use any of the plugins above to generate applications as well. - -When using Nx, you can create multiple applications and libraries in the same workspace. - -## Generate a library - -Run `nx g @nrwl/react:lib my-lib` to generate a library. - -> You can also use any of the plugins above to generate libraries as well. - -Libraries are shareable across libraries and applications. They can be imported from `@signing-managers/mylib`. +Monorepo for Polymesh SDK compatible Signing Managers. -## Development server - -Run `nx serve my-app` for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files. - -## Code scaffolding - -Run `nx g @nrwl/react:component my-component --project=my-app` to generate a new component. - -## Build - -Run `nx build my-app` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. - -## Running unit tests - -Run `nx test my-app` to execute the unit tests via [Jest](https://jestjs.io). - -Run `nx affected:test` to execute the unit tests affected by a change. - -## Running end-to-end tests - -Run `nx e2e my-app` to execute the end-to-end tests via [Cypress](https://www.cypress.io). - -Run `nx affected:e2e` to execute the end-to-end tests affected by a change. - -## Understand your workspace - -Run `nx graph` to see a diagram of the dependencies of your projects. - -## Further help - -Visit the [Nx Documentation](https://nx.dev) to learn more. - - - -## ☁ Nx Cloud +This project was generated using [Nx](https://nx.dev). -### Distributed Computation Caching & Distributed Task Execution +## Projects -

+| Project | Package | Version | Links | +| ------------------------- | --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **Local Signing Manager** | [`@polymathnetwork/local-signing-manager`](https://npmjs.com/package/@ng-easy/builders) | [![npm latest version](https://img.shields.io/npm/v/@polymathnetwork/local-signing-manager/latest.svg)](https://www.npmjs.com/package/@polymathnetwork/local-signing-manager) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/local-signing-manager/README.md) [![CHANGELOG](https://img.shields.io/badge/CHANGELOG--orange.svg)](/packages/local-signing-manager/CHANGELOG.md) | -Nx Cloud pairs with Nx in order to enable you to build and test code more rapidly, by up to 10 times. Even teams that are new to Nx can connect to Nx Cloud and start saving time instantly. +## Scripts -Teams using Nx gain the advantage of building full-stack applications with their preferred framework alongside Nx’s advanced code generation and project dependency graph, plus a unified experience for both frontend and backend developers. +- Linting: `yarn lint ` +- Testing: `yarn test ` +- Building: `yarn build ` -Visit [Nx Cloud](https://nx.app/) to learn more. +You can also use nx commands directly with `yarn nx` (i.e. `yarn nx affected --targets=lint,test`) diff --git a/commitlint.config.js b/commitlint.config.js index 62f9a8a9..ad3fb366 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -1,3 +1,3 @@ module.exports = { - extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'], + extends: ['@commitlint/config-conventional', 'cz'], }; diff --git a/nx.json b/nx.json index defaa772..77b5bcf4 100644 --- a/nx.json +++ b/nx.json @@ -11,17 +11,26 @@ "default": { "runner": "@nrwl/workspace/tasks-runners/default", "options": { - "cacheableOperations": [ - "build", - "lint", - "test", - "e2e" - ] + "cacheableOperations": ["build", "lint", "test", "e2e"] } } }, "targetDependencies": { - "build": [{ "target": "build", "projects": "dependencies" }], - "release": [{ "target": "build", "projects": "self" }, { "target": "release", "projects": "dependencies" }] + "build": [ + { + "target": "build", + "projects": "dependencies" + } + ], + "release": [ + { + "target": "build", + "projects": "self" + }, + { + "target": "release", + "projects": "dependencies" + } + ] } } diff --git a/package.json b/package.json index 04fbb9ec..9f2a6382 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,11 @@ "license": "MIT", "scripts": { "prepare": "is-ci || husky install", - "nx": "npx nx" + "nx": "npx nx", + "generate:signing-manager": "nx workspace-generator signing-manager", + "lint": "nx lint", + "test": "nx test", + "build": "nx build" }, "private": true, "dependencies": { @@ -25,6 +29,7 @@ "@commitlint/config-nx-scopes": "^16.2.1", "@ng-easy/builders": "^5.1.1", "@nrwl/cli": "13.8.2", + "@nrwl/devkit": "13.8.2", "@nrwl/eslint-plugin-nx": "13.8.2", "@nrwl/jest": "13.8.2", "@nrwl/js": "13.8.2", @@ -34,9 +39,11 @@ "@nrwl/workspace": "13.8.2", "@types/jest": "27.0.2", "@types/node": "16.11.7", + "@types/require-from-string": "^1.2.1", "@typescript-eslint/eslint-plugin": "~5.10.0", "@typescript-eslint/parser": "~5.10.0", "commitizen": "^4.2.4", + "commitlint-config-cz": "^0.13.3", "cz-conventional-changelog": "^3.3.0", "cz-customizable": "^6.3.0", "eslint": "~8.7.0", @@ -46,16 +53,11 @@ "jest": "27.2.3", "lint-staged": "^12.3.4", "prettier": "^2.5.1", + "require-from-string": "^2.0.2", "rxjs": "~6.6.7", "ts-jest": "27.0.5", "typescript": "~4.5.2" }, - "peerDependencies": { - "@polkadot/api": "5.1.1", - "@polkadot/util": "^7.0.2", - "@polkadot/util-crypto": "^7.0.2", - "@polymathnetwork/polymesh-sdk": ">=13.0.0" - }, "workspaces": [ "packages/**" ], diff --git a/packages/local-signing-manager/README.md b/packages/local-signing-manager/README.md index 24515e39..f2bf7ff9 100644 --- a/packages/local-signing-manager/README.md +++ b/packages/local-signing-manager/README.md @@ -1,7 +1,26 @@ # local-signing-manager +Polymesh SDK (v13+) compatible signing manager that stores private keys in memory. + This library was generated with [Nx](https://nx.dev). -## Running unit tests +## Usage + +```typescript +import { LocalSigningManager } from '@polymathnetwork/local-signing-manager'; +import { Polymesh } from '@polymathnetwork/polymesh-sdk'; + +// setup +const signingManager = await LocalSigningManager.create({ + accounts: [ + { + mnemonic: 'secret words will not be shared', + }, + ], +}); -Run `nx test local-signing-manager` to execute the unit tests via [Jest](https://jestjs.io). +const sdk = await Polymesh.connect({ + nodeUrl, + signingManager, +}); +``` diff --git a/packages/local-signing-manager/package.json b/packages/local-signing-manager/package.json index a0d954c9..33422328 100644 --- a/packages/local-signing-manager/package.json +++ b/packages/local-signing-manager/package.json @@ -6,5 +6,11 @@ }, "dependencies": { "@polymathnetwork/signing-manager-types": "^1.0.1" + }, + "peerDependencies": { + "@polkadot/api": "5.1.1", + "@polkadot/util": "^7.0.2", + "@polkadot/util-crypto": "^7.0.2", + "@polymathnetwork/polymesh-sdk": ">=13.0.0" } } \ No newline at end of file diff --git a/packages/types/README.md b/packages/types/README.md index 5e6821be..a46a197a 100644 --- a/packages/types/README.md +++ b/packages/types/README.md @@ -1,7 +1,5 @@ # types -This library was generated with [Nx](https://nx.dev). - -## Running unit tests +Base types for Signing Managers -Run `nx test types` to execute the unit tests via [Jest](https://jestjs.io). +This library was generated with [Nx](https://nx.dev). diff --git a/tools/generators/signing-manager/index.ts b/tools/generators/signing-manager/index.ts new file mode 100644 index 00000000..83c48585 --- /dev/null +++ b/tools/generators/signing-manager/index.ts @@ -0,0 +1,128 @@ +import { Tree, formatFiles, installPackagesTask, updateJson } from '@nrwl/devkit'; +import { libraryGenerator } from '@nrwl/node'; +import requireFromString from 'require-from-string'; + +interface Schema { + name: string; +} + +export default async function (tree: Tree, schema: Schema) { + const { name } = schema; + const importPath = `@polymathnetwork/${name}`; + const projectPath = `packages/${name}`; + const srcPath = `${projectPath}/src`; + + await libraryGenerator(tree, { + name, + buildable: true, + publishable: true, + importPath, + }); + + // add types dependency and publish config + updateJson(tree, `${projectPath}/package.json`, contents => { + contents.publishConfig = { + access: 'public', + }; + contents.dependencies = { + ...contents.dependencies, + '@polymathnetwork/signing-manager-types': '*', + }; + contents.peerDependencies = { + ...contents.peerDependencies, + '@polymathnetwork/polymesh-sdk': '>=13.0.0', + }; + + return contents; + }); + + // fix jest config to support ESM dependencies (remove "transform property") + updateJsConfig(tree, `${projectPath}/jest.config.js`, jestConfig => { + const { transform: _, ...rest } = jestConfig; + + return rest; + }); + + // modify build config and add release config + updateJson(tree, `${projectPath}/project.json`, projectConfig => { + projectConfig.targets.build.options = { + ...projectConfig.targets.build.options, + srcRootForCompilationRoot: srcPath, + }; + + projectConfig.targets.release = { + builder: '@ng-easy/builders:semantic-release', + configurations: { + local: { + force: true, + }, + }, + }; + + return projectConfig; + }); + + // modify project tsconfig + updateJson(tree, `${projectPath}/tsconfig.json`, tsConfig => { + tsConfig.compilerOptions = { + ...tsConfig.compilerOptions, + forceConsistentCasingInFileNames: true, + strict: true, + noImplicitReturns: true, + noFallthroughCasesInSwitch: true, + }; + + return tsConfig; + }); + + // modify test tsconfig + updateJson(tree, `${projectPath}/tsconfig.spec.json`, testTsConfig => { + testTsConfig.compilerOptions = { + ...testTsConfig.compilerOptions, + allowJs: true, + esModuleInterop: true, + }; + + return testTsConfig; + }); + + // modify import paths in base tsconfig + updateJson(tree, 'tsconfig.base.json', tsConfigBase => { + tsConfigBase.compilerOptions.paths = { + ...tsConfigBase.compilerOptions.paths, + [importPath]: [srcPath], + }; + + return tsConfigBase; + }); + + // add the package name to the allowed commit scopes + updateJsConfig(tree, '.cz-config.js', czConfig => { + czConfig.scopes = [ + ...czConfig.scopes, + { + name, + }, + ]; + + return czConfig; + }); + + await formatFiles(tree); + return () => { + installPackagesTask(tree); + }; +} + +/** + * Update a .config.js file's contents + */ +function updateJsConfig(tree: Tree, path: string, updater: (contents: any) => any) { + const contents = tree.read(path, 'utf8'); + + const contentsAsObject = requireFromString(contents); + + const updatedContents = updater(contentsAsObject); + + tree.write(path, `module.exports = ${JSON.stringify(updatedContents, null, 2)}`); +} diff --git a/tools/generators/signing-manager/schema.json b/tools/generators/signing-manager/schema.json new file mode 100644 index 00000000..5ac62ced --- /dev/null +++ b/tools/generators/signing-manager/schema.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/schema", + "cli": "nx", + "$id": "signing-manager", + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Package name (without the org prefix)", + "$default": { + "$source": "argv", + "index": 0 + } + } + }, + "required": ["name"] +} diff --git a/tools/tsconfig.tools.json b/tools/tsconfig.tools.json index 99428e14..62b4f9d6 100644 --- a/tools/tsconfig.tools.json +++ b/tools/tsconfig.tools.json @@ -6,7 +6,8 @@ "module": "commonjs", "target": "es5", "types": ["node"], - "importHelpers": false + "importHelpers": false, + "esModuleInterop": true }, "include": ["**/*.ts"] } diff --git a/yarn.lock b/yarn.lock index 9339f845..ac1135d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3102,6 +3102,11 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc" integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw== +"@types/require-from-string@^1.2.1": + version "1.2.1" + resolved "https://registry.yarnpkg.com/@types/require-from-string/-/require-from-string-1.2.1.tgz#50292d824e2244628c0b961c6a1a583fdc494554" + integrity sha512-mIDK7lTHc0uW67SxPIqkwCrxmdKBV5aAET560hyZnT8c6Ekp9Aah3GPqe8Pl1Yzn/i2NMYmYv+HiMLwjGDCIAQ== + "@types/retry@^0.12.0": version "0.12.1" resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.1.tgz#d8f1c0d0dc23afad6dc16a9e993a0865774b4065" @@ -3624,6 +3629,11 @@ anymatch@^3.0.3, anymatch@~3.1.2: normalize-path "^3.0.0" picomatch "^2.0.4" +app-root-path@~3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-3.0.0.tgz#210b6f43873227e18a4b810a032283311555d5ad" + integrity sha512-qMcx+Gy2UZynHjOHOIXPNvpf+9cjvk3cWrBBK7zg4gH9+clobJRb9NGzcT7mQTcV/6Gm/1WelUtqxVXnNlrwcw== + aproba@^1.0.3: version "1.2.0" resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a" @@ -4669,6 +4679,14 @@ commitizen@^4.0.3, commitizen@^4.2.4: strip-bom "4.0.0" strip-json-comments "3.0.1" +commitlint-config-cz@^0.13.3: + version "0.13.3" + resolved "https://registry.yarnpkg.com/commitlint-config-cz/-/commitlint-config-cz-0.13.3.tgz#a3e00bf2b98626f98a7e92c142ce081b1d6d179b" + integrity sha512-6LmCvGiFDTVSmLF0mzVVp1etMM8lAqLmPRlU7Oml1J8J9oOLadf+2g4uMTchdxOvvYLgll99SESFUHgmc6oATA== + dependencies: + app-root-path "~3.0.0" + lodash.clonedeep "~4.5.0" + common-ancestor-path@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz#4f7d2d1394d91b7abdf51871c62f71eadb0182a7" @@ -8814,6 +8832,11 @@ lodash.capitalize@^4.2.1: resolved "https://registry.yarnpkg.com/lodash.capitalize/-/lodash.capitalize-4.2.1.tgz#f826c9b4e2a8511d84e3aca29db05e1a4f3b72a9" integrity sha1-+CbJtOKoUR2E46yinbBeGk87cqk= +lodash.clonedeep@~4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" + integrity sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8= + lodash.debounce@^4.0.8: version "4.0.8" resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"