Skip to content

Commit

Permalink
feat(nx-commands): added nx commands for affected, projects with task…
Browse files Browse the repository at this point in the history
…, project info and etc
  • Loading branch information
sridharmallela committed Jan 20, 2025
1 parent 167761f commit dcb8191
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 13 deletions.
28 changes: 28 additions & 0 deletions docs/nx-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- TOC -->

- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Usage](#usage)
- [License](#license)

<!-- /TOC -->

## 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:
Expand Down
5 changes: 3 additions & 2 deletions packages/nx-commands/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -31,6 +31,7 @@
},
"keywords": [],
"peerDependencies": {
"tslib": ">=2"
"tslib": ">=2",
"@nx/devkit": ">=18"
}
}
2 changes: 1 addition & 1 deletion packages/nx-commands/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/nx';
export * from './lib/nx.command';
78 changes: 78 additions & 0 deletions packages/nx-commands/src/lib/nx.command.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
});
30 changes: 30 additions & 0 deletions packages/nx-commands/src/lib/nx.command.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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()
);
}
}
7 changes: 0 additions & 7 deletions packages/nx-commands/src/lib/nx.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/nx-commands/src/lib/nx.ts

This file was deleted.

0 comments on commit dcb8191

Please sign in to comment.