From dcb819169dbcfdc7c1001e4f277fcf550ec4cede Mon Sep 17 00:00:00 2001 From: Sridhar Mallela Date: Mon, 20 Jan 2025 03:16:42 -0600 Subject: [PATCH] feat(nx-commands): added nx commands for affected, projects with task, project info and etc --- docs/nx-commands.md | 28 +++++++ packages/nx-commands/package.json | 5 +- packages/nx-commands/src/index.ts | 2 +- .../nx-commands/src/lib/nx.command.spec.ts | 78 +++++++++++++++++++ packages/nx-commands/src/lib/nx.command.ts | 30 +++++++ packages/nx-commands/src/lib/nx.spec.ts | 7 -- packages/nx-commands/src/lib/nx.ts | 3 - 7 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 packages/nx-commands/src/lib/nx.command.spec.ts create mode 100644 packages/nx-commands/src/lib/nx.command.ts delete mode 100644 packages/nx-commands/src/lib/nx.spec.ts delete mode 100644 packages/nx-commands/src/lib/nx.ts diff --git a/docs/nx-commands.md b/docs/nx-commands.md index bcae1c3..f2908c2 100644 --- a/docs/nx-commands.md +++ b/docs/nx-commands.md @@ -8,15 +8,43 @@ permalink: '/nx-commands' [![GitHub tag](https://img.shields.io/github/tag/sridharmallela/smallela-workspace.svg?style=plastic)](https://github.com/sridharmallela/smallela-workspace/tags) [![GitHub release](https://img.shields.io/github/release/sridharmallela/smallela-workspace.svg?style=plastic)](https://github.com/sridharmallela/smallela-workspace/releases) [![GitHub issues](https://img.shields.io/github/issues/sridharmallela/smallela-workspace.svg?style=plastic)](https://github.com/sridharmallela/smallela-workspace/issues) [![GitHub pull requests](https://img.shields.io/github/issues-pr/sridharmallela/smallela-workspace.svg?style=plastic)](https://github.com/sridharmallela/smallela-workspace/pulls) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=plastic)](https://raw.githubusercontent.com/sridharmallela/smallela-workspace/main/LICENSE) +This module exports all the commands that Nx supports programmatically. + ## Table of Contents - [Table of Contents](#table-of-contents) +- [Installation](#installation) +- [Usage](#usage) - [License](#license) +## Installation + +```bash + $ npm i --save-dev nx-commands-smallela +``` + +## Usage + +```ts +import { NxCommands } from 'nx-commands-smallela'; + +// check what things have been modified for Nx Project +affected = await NxCommands.getAffected(destBranch); + +// check test coverage for Nx Project +testCoverage = await NxCommands.captureCodeCoverage([...projects]); + +// retrieve all projects with publish task +projects = await NxCommands.getProjectsWithTask('publish'); + +// get project info +projectInfo = await NxCommands.getProjectInfo('test-project'); +``` + ## License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/packages/nx-commands/package.json b/packages/nx-commands/package.json index c1c46f4..4fa6c4d 100644 --- a/packages/nx-commands/package.json +++ b/packages/nx-commands/package.json @@ -1,6 +1,6 @@ { "name": "nx-commands-smallela", - "version": "0.0.2", + "version": "1.0.0", "description": "Reusable commands for NPM", "author": "Sridhar Mallela", "license": "MIT", @@ -31,6 +31,7 @@ }, "keywords": [], "peerDependencies": { - "tslib": ">=2" + "tslib": ">=2", + "@nx/devkit": ">=18" } } diff --git a/packages/nx-commands/src/index.ts b/packages/nx-commands/src/index.ts index 679ef21..bf638da 100644 --- a/packages/nx-commands/src/index.ts +++ b/packages/nx-commands/src/index.ts @@ -1 +1 @@ -export * from './lib/nx'; +export * from './lib/nx.command'; diff --git a/packages/nx-commands/src/lib/nx.command.spec.ts b/packages/nx-commands/src/lib/nx.command.spec.ts new file mode 100644 index 0000000..741c627 --- /dev/null +++ b/packages/nx-commands/src/lib/nx.command.spec.ts @@ -0,0 +1,78 @@ +jest.mock('node:child_process'); +jest.mock('node:console', () => { + return { + error: jest.fn().mockImplementation(), + info: jest.fn().mockImplementation(), + log: jest.fn().mockImplementation(), + warn: jest.fn().mockImplementation() + }; +}); + +import * as cp from 'node:child_process'; + +import { NxCommands } from './nx.command'; + +describe('Nx Commands ---', () => { + let execSyncSpy: jest.SpyInstance; + + beforeEach(() => { + execSyncSpy = jest.spyOn(cp, 'execSync'); + expect(execSyncSpy).toBeDefined(); + execSyncSpy.mockReturnValue('TEST'); + expect(execSyncSpy).not.toHaveBeenCalled(); + }); + + afterEach(() => { + execSyncSpy.mockRestore(); + }); + + describe('should test getAffected ---', () => { + test.each(['', 'TEST'])('for "%s"', async nx => { + execSyncSpy.mockReturnValue(nx); + const affected = await NxCommands.getAffected('main'); + expect(affected).toEqual([nx || undefined]); + expect(execSyncSpy).toHaveBeenCalled(); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy).toHaveBeenCalledWith( + 'nx show projects --affected --base=origin/main' + ); + }); + }); + + describe('should test getProjectsWithTask ---', () => { + test.each(['', 'TEST'])('for "%s"', nx => { + execSyncSpy.mockReturnValue(nx); + const projects = NxCommands.getProjectsWithTask('build'); + expect(projects).toEqual([nx]); + expect(execSyncSpy).toHaveBeenCalled(); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy).toHaveBeenCalledWith( + 'nx show projects --with-target build' + ); + }); + + test('for multiple', () => { + execSyncSpy.mockReturnValue('TEST1\nTEST2\nTEST3\nTEST4'); + const projects = NxCommands.getProjectsWithTask('publish'); + expect(projects).toEqual(['TEST1', 'TEST2', 'TEST3', 'TEST4']); + expect(execSyncSpy).toHaveBeenCalled(); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy).toHaveBeenCalledWith( + 'nx show projects --with-target publish' + ); + }); + }); + + describe('should test getProjectInfo ---', () => { + test.each(['{}', '{"test":"test"}'])('for "%s"', nx => { + execSyncSpy.mockReturnValue(nx); + const projectInfo = NxCommands.getProjectInfo('test-project'); + expect(projectInfo).toBeDefined(); + expect(execSyncSpy).toHaveBeenCalled(); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy).toHaveBeenCalledWith( + 'nx show project test-project --json' + ); + }); + }); +}); diff --git a/packages/nx-commands/src/lib/nx.command.ts b/packages/nx-commands/src/lib/nx.command.ts new file mode 100644 index 0000000..4af9399 --- /dev/null +++ b/packages/nx-commands/src/lib/nx.command.ts @@ -0,0 +1,30 @@ +import type { ProjectConfiguration } from '@nx/devkit'; +import { execSync } from 'node:child_process'; +import { info } from 'node:console'; + +export abstract class NxCommands { + static async getAffected(dest: string): Promise { + const affected = execSync( + `nx show projects --affected --base=origin/${dest}` + ) + .toString() + .trim() + .split('\n') + .filter(project => project !== null && project.length !== 0); + info(`\n\t\tAffected projects Count: ${affected.length}\n`); + return affected; + } + + static getProjectsWithTask(executionTask: string): string[] { + return execSync(`nx show projects --with-target ${executionTask}`) + .toString() + .trim() + .split('\n'); + } + + static getProjectInfo(project: string): ProjectConfiguration { + return JSON.parse( + execSync(`nx show project ${project} --json`).toString().trim() + ); + } +} diff --git a/packages/nx-commands/src/lib/nx.spec.ts b/packages/nx-commands/src/lib/nx.spec.ts deleted file mode 100644 index 7a5e4d8..0000000 --- a/packages/nx-commands/src/lib/nx.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { nxCommands } from './nx'; - -describe('nxCommands', () => { - it('should work', () => { - expect(nxCommands()).toEqual('nx-commands'); - }); -}); diff --git a/packages/nx-commands/src/lib/nx.ts b/packages/nx-commands/src/lib/nx.ts deleted file mode 100644 index e11900b..0000000 --- a/packages/nx-commands/src/lib/nx.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function nxCommands(): string { - return 'nx-commands'; -}