Skip to content

Commit

Permalink
feat: improve teardown logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Oct 13, 2024
2 parents 41bf879 + 8106fa1 commit cba2e67
Show file tree
Hide file tree
Showing 35 changed files with 468 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ jobs:
- name: Install dependencies
run: npm i
- name: E2E test affected projects
run: npx nx affected -t e2e --exclude="tag:type:example"
run: npx nx affected -t nxv-e2e --exclude="tag:type:example"
25 changes: 11 additions & 14 deletions code-pushup.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CoreConfig } from '@code-pushup/models';
import {CoreConfig} from '@code-pushup/models';
import nxPerformancePlugin, {
nxPerformanceCategoryRefs,
type OnlyAudit,
} from './tooling/measures/nx-performance/nx-performance.plugin';
import {TaskTimeAuditOption} from './tooling/measures/nx-performance';

const onlyAudits: OnlyAudit[] = [
'graph-time-project',
Expand All @@ -11,22 +12,18 @@ const onlyAudits: OnlyAudit[] = [
'task-time',
];
const taskGraphTasks = ['cli-e2e:nxv-env-install'];
const taskTimeTasks = [
'cli-e2e:e2e',
'cli-e2e:nxv-env-setup',
'cli:unit-test',
'cli:build',
'core-e2e:e2e',
'utils-e2e:e2e',
'models-e2e:e2e',
'cli-e2e-original:original-e2e',
const taskTimeTasks: TaskTimeAuditOption[] = [
{task: 'models-e2e:nxv-env-teardown'},
{task: 'models-e2e:nxv-env-bootstrap'},
{task: 'models-e2e:nxv-env-setup'},
{task: 'models-e2e:e2e'},
{task: 'models-e2e:nxv-e2e'},
{task: 'nx-verdaccio-e2e:nxv-e2e'},
{task: 'cli-e2e-original:original-e2e'},
];
const cacheSizeTasks = [
'models-e2e:nxv-env-setup',
'utils-e2e:nxv-env-setup',
'core-e2e:nxv-env-setup',
'cli-e2e:nxv-env-setup',
'playground-e2e:nxv-env-setup',
'nx-verdaccio-e2e:nxv-env-setup',
];
export default {
plugins: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"options": {},
"parallelism": true,
},
"nxv-e2e": {
"configurations": {},
"dependsOn": [
"e2e",
],
"executor": "@push-based/nx-verdaccio:env-teardown",
"options": {},
"parallelism": true,
},
"nxv-env-bootstrap": {
"configurations": {},
"executor": "@push-based/nx-verdaccio:env-bootstrap",
Expand Down Expand Up @@ -52,6 +61,12 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
],
"parallelism": true,
},
"nxv-env-teardown": {
"configurations": {},
"executor": "@push-based/nx-verdaccio:env-teardown",
"options": {},
"parallelism": true,
},
"nxv-verdaccio-start": {
"configurations": {},
"executor": "@nx/js:verdaccio",
Expand Down
11 changes: 11 additions & 0 deletions e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
TARGET_ENVIRONMENT_VERDACCIO_STOP,
} from '@push-based/nx-verdaccio';
import { teardownTestFolder } from '@push-based/test-utils';
import {
TARGET_ENVIRONMENT_E2E,
TARGET_ENVIRONMENT_TEARDOWN,
} from '../../../projects/nx-verdaccio/src/plugin/targets/environment.targets';

describe('nx-verdaccio plugin create-nodes-v2', () => {
let tree: Tree;
Expand Down Expand Up @@ -239,6 +243,13 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
filePath: 'tmp/environments/verdaccio-registry.json',
},
}),
[TARGET_ENVIRONMENT_E2E]: expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-teardown',
dependsOn: ['e2e'],
}),
[TARGET_ENVIRONMENT_TEARDOWN]: expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-teardown',
}),
})
);

Expand Down
1 change: 0 additions & 1 deletion examples/e2e/cli-static-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
"options": {
"environmentRoot": "static-environments/user-lists",
"reportsDirectory": "../../../coverage/projects/cli-static-e2e"
}
}
Expand Down
6 changes: 3 additions & 3 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@
}
},
"plugins": [
{
"plugin": "./examples/e2e/cli-e2e-original/tooling/original.plugin.ts"
},
{
"plugin": "@push-based/nx-verdaccio",
"options": {
Expand All @@ -88,6 +85,9 @@
},
"exclude": ["./examples/e2e/cli-e2e-original"]
},
{
"plugin": "./examples/e2e/cli-e2e-original/tooling/original.plugin.ts"
},
{
"plugin": "@nx/eslint/plugin",
"options": {
Expand Down
10 changes: 3 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"private": true,
"dependencies": {
"memfs": "^4.11.1",
"simple-git": "^3.27.0",
"tslib": "^2.3.0",
"yargs": "^17.7.2"
},
Expand Down Expand Up @@ -68,5 +69,9 @@
"verdaccio",
"performance"
],
"author": "Michael Hladky"
"author": "Michael Hladky",
"directories": {
"doc": "docs",
"example": "examples"
}
}
6 changes: 6 additions & 0 deletions project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"includedScripts": [],
"// targets": "to see all targets run: nx show project workspace-source --web",
"targets": {
"show-project": {
"executor": "nx:run-commands",
"options": {
"command": "tsx --tsconfig=./tooling/bin/tsconfig.bin.json ./tooling/bin/nx-show-project.ts"
}
},
"code-pushup": {
"executor": "nx:run-commands",
"options": {
Expand Down
5 changes: 5 additions & 0 deletions projects/nx-verdaccio/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"schema": "./src/executors/env-setup/schema.json",
"description": "Generate and install test environments in your workspace. Cached and ready for use."
},
"env-teardown": {
"implementation": "./src/executors/env-teardown/executor",
"schema": "./src/executors/env-teardown/schema.json",
"description": "Teardown test environments in your workspace."
},
"pkg-publish": {
"implementation": "./src/executors/pkg-publish/executor",
"schema": "./src/executors/pkg-publish/schema.json",
Expand Down
14 changes: 14 additions & 0 deletions projects/nx-verdaccio/src/executors/env-setup/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TARGET_ENVIRONMENT_VERDACCIO_STOP,
} from '../../plugin/targets/environment.targets';
import { runSingleExecutor } from '../../internal/run-executor';
import { rm } from 'node:fs/promises';

export type ExecutorOutput = {
success: boolean;
Expand Down Expand Up @@ -82,6 +83,19 @@ export default async function runSetupEnvironmentExecutor(
},
context
);
// delete storage, npmrc
await rm(join(environmentRoot, 'storage'), {
recursive: true,
force: true,
retryDelay: 100,
maxRetries: 2,
});
await rm(join(environmentRoot, '.npmrc'), {
recursive: true,
force: true,
retryDelay: 100,
maxRetries: 2,
});
} else {
const { url } = readJsonFile<VerdaccioProcessResult>(
join(environmentRoot, VERDACCIO_REGISTRY_JSON)
Expand Down
53 changes: 53 additions & 0 deletions projects/nx-verdaccio/src/executors/env-teardown/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Teardown Environment Executor

This executor helps to cleanup a [environment](../../../../../README.md#-environment-folders-to-isolate-files-during-e2e-tests) of a given folder.
If a server is running for the given environment, it will be stopped.
If this folder is checked into github all it's changes will be reverted, if it is not checked into github, the folder will be deleted.

**Environment folder**

#### @push-based/nx-verdaccio:env-teardown

## Usage

// project.json

```json
{
"name": "my-project",
"targets": {
"env-teardown": {
"executor": "@push-based/nx-verdaccio:env-teardown"
}
}
}
```

By default, the Nx executor will derive the options from the executor options.

```jsonc
{
"name": "my-project",
"targets": {
"env-teardown": {
"executor": "@code-pushup/nx-verdaccio:env-teardown",
"options": {
"envRoot": "/tmp/test-npm-workspace"
"verbose": true,
}
}
}
}
```

Show what will be executed without actually executing it:

`nx run my-project:env-teardown --print-config`

## Options

| Name | type | description |
| --------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| **envRoot** | `string` (REQUIRED) | The folder in which the package should get published. This folder is the environment folder and contains a configured `.npmrc` file. |
| **verbose** | `boolean` | Show more verbose logs |
| **printConfig** | `boolean` | Print config without executing |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EXECUTOR_ENVIRONMENT_TEARDOWN = 'env-teardown';
42 changes: 42 additions & 0 deletions projects/nx-verdaccio/src/executors/env-teardown/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type ExecutorContext, logger } from '@nx/devkit';
import type { TeardownExecutorOptions } from './schema';
import { teardownEnvironment } from './teardown-env';
import { PACKAGE_NAME } from '../../plugin/constants';
import { EXECUTOR_ENVIRONMENT_TEARDOWN } from './constants';
import { ExecutorOutput } from '../internal/executor-output';

export async function teardownExecutor(
options: TeardownExecutorOptions,
context: ExecutorContext
): Promise<ExecutorOutput> {
const { environmentRoot, verbose } = options;

if (verbose) {
logger.info(
`Execute ${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_TEARDOWN} with options: ${JSON.stringify(
options,
null,
2
)}`
);
}
try {
await teardownEnvironment(context, {
environmentRoot,
verbose,
});
} catch (error) {
logger.error(error);
return {
success: false,
command: error?.message ?? (error as Error).toString(),
};
}

return {
success: true,
command: 'Teared down environment successfully.',
};
}

export default teardownExecutor;
20 changes: 20 additions & 0 deletions projects/nx-verdaccio/src/executors/env-teardown/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {simpleGit} from 'simple-git';

export async function isFolderInGit(folderPath: string): Promise<boolean> {
try {
const git = simpleGit(folderPath);
// Check if the folder is a git repository
const isRepo = (await git.checkIgnore(folderPath)).length === 0;
return isRepo;
} catch (error) {
if (
(error as Error).message.includes(
'Cannot use simple-git on a directory that does not exist'
)
) {
return true;
}
console.log(`${error}`);
return false;
}
}
19 changes: 19 additions & 0 deletions projects/nx-verdaccio/src/executors/env-teardown/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "TeardownExecutorOptions",
"title": "Environment teardown executor options",
"type": "object",
"properties": {
"environmentRoot": {
"type": "string",
"description": "The root directory of the environment",
"aliases": ["envRoot", "e"]
},
"verbose": {
"type": "boolean",
"description": "Print additional logs"
}
},
"additionalProperties": true,
"required": []
}
Loading

0 comments on commit cba2e67

Please sign in to comment.