Intro
At Digikare we have started to change our git philosophy, by using git monorepo. We work around Angular/NestJs and the right tools at this time is 💚 NX.After a few months of development we have chosen to also expose our API to a CLI client. So we have started to find the best framework CLI, and we found OCLIF.
NX & OCLIF problems
We thought NX & OCLIF would easily work together and just add OCLIF as an NX app and plugins as libraries. But no, in fact OCLIF needs its own build tooling and should be a full project. OCLIF requires some dependencies and npm scripts to execute its build process.
After many attempts we finally found the easiest way to work with NX & OCLIF.
Example
We will create this monorepo structure: 2 apps and 2 libs. The Oclif app will use Auth lib which depends on User lib.
Create workspace
First of all create nx workspace
$ npx create-nx-workspace
After create oclif app. Be sure to be located at your monorepo's root folder.
$ npx oclif multi cli
Now we have NX monorepo and OCLIF project in same git repository.
Creating our reusable libraries
As you can see we need to create 2 libraries, Auth
& User
, but only the oclif
app depends of Auth
, so on NX for doing a lib dependable out of NX we need to create a library publishable
.
- Create the Auth library
$ nx generate @nrwl/node:library auth --publishable
- Create the User library
$ nx generate @nrwl/node:library user --buildable
user
lib is buildable because auth depends on this lib so when auth will be built user should be built also.
🤘 Let's do some code
Create a authentication use case
// libs/auth/src/lib/auth.ts
export class Auth {
authenticate() {
return true;
}
}
Now that our authentication usecase is done, we want to use it on our CLI application.
We need to build the auth
and link to oclif
app.
🏗 Build lib
$ nx build auth
🔗 Link it
cd dist/libs/auth && npm link
Go to
cli
app to use our usecase.
Run the link to your project and add it to your dependencies npm onpackage.json
$ npm link @xxx/auth
Now in the default command hello.ts
on CLI app we call the Auth usecase.
import {Command, flags} from '@oclif/command'
import { Auth } from '@xxxx/auth';
export default class Hello extends Command {
async run() {
this.log(`Authenticated : ${new Auth().authenticate()}`)
}
}
Run it and you should have the result
Authenticated : true
Add user
lib on auth
lib.
- Create a simple
User
class.
// libs/user/src/lib/user.ts
export class User {
firstName: string;
lastName: string;
roles: string[];
}
- And change our usecase to only authenticate users with admin role.
// libs/auth/src/lib/auth.ts
import { User } from '@xxx/user';
export class Auth {
authenticate(user: User) {
return user.roles.some(role => role === 'admin');
}
}
Built auth
lib
$ nx build auth --with-deps
--with-deps will also build user
lib.
Now return to hello.ts
command, your IDE displays an error on authenticated because of missing parameter.
// hello.ts
export default class Hello extends Command {
async run() {
this.log(`Should Not Authenticated : ${new Auth().authenticate({
firstName: 'John',
lastName: 'Doe',
roles: ['user'],
})}`);
this.log(`Should Authenticated : ${new Auth().authenticate({
firstName: 'John',
lastName: 'Doe',
roles: ['admin'],
})}`);
}
}
Should Not Authenticated : false
Should Authenticated : true
❗️❗️ IMPORTANT ❗️❗️
You need to link your publishable lib before depending on any other buildable lib, otherwise the npm link will not work because the second lib user in our example is not referenced on registry.
Conclusion
Using NX lib on OCLIF is easy after reading this article. Now you can use your usecase in your Angular & OCLIF application.
✅ BY THE WAY WE ARE HIRING
Feel free to contact.
Top comments (0)