Skip to content

Commit

Permalink
feat(nx-container): revamped configuration generator
Browse files Browse the repository at this point in the history
init generator was replaced by the new configuration generator

fix #881
  • Loading branch information
gperdomor committed Oct 19, 2023
1 parent ba61a99 commit 7c98412
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 39 deletions.
8 changes: 4 additions & 4 deletions packages/nx-container/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"rules": {}
},
{
"files": ["./package.json", "./generators.json", "./executors.json"],
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
"@nx/dependency-checks": "error"
}
},
{
"files": ["*.json"],
"files": ["./package.json", "./generators.json", "./executors.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
"@nx/nx-plugin-checks": "error"
}
}
]
Expand Down
1 change: 0 additions & 1 deletion packages/nx-container/executors.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "http://json-schema.org/schema",
"executors": {
"build": {
"implementation": "./src/executors/build/executor",
Expand Down
9 changes: 3 additions & 6 deletions packages/nx-container/generators.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"$schema": "http://json-schema.org/schema",
"name": "nx-container",
"version": "0.0.1",
"generators": {
"init": {
"factory": "./src/generators/init/generator",
"schema": "./src/generators/init/schema.json",
"configuration": {
"factory": "./src/generators/configuration/generator",
"schema": "./src/generators/configuration/schema.json",
"description": "Configure Container builds for your application"
}
}
Expand Down
8 changes: 1 addition & 7 deletions packages/nx-container/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
export default {
displayName: 'nx-container',
preset: '../../jest.preset.js',
globals: {},
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/nx-container',
Expand Down
6 changes: 4 additions & 2 deletions packages/nx-container/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "@nx-tools/nx-container",
"version": "5.0.3",
"version": "6.0.0-alpha.1",
"author": "gperdomor <gperdomor@gmail.com>",
"repository": "https://github.com/gperdomor/nx-tools",
"bugs": "https://github.com/gperdomor/nx-tools/issues",
"license": "MIT",
"main": "src/index.js",
"main": "./src/index.js",
"type": "commonjs",
"typings": "./src/index.d.ts",
"generators": "./generators.json",
"executors": "./executors.json",
"dependencies": {
Expand Down
8 changes: 6 additions & 2 deletions packages/nx-container/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@
]
}
},
"publish": {
"command": "node tools/scripts/publish.mjs nx-container {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/nx-container/**/*.ts",
"packages/nx-container/generators.json",
"packages/nx-container/package.json",
"packages/nx-container/executors.json",
"packages/nx-container/package.json"
"packages/nx-container/generators.json"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_ENGINE = 'docker';

export const DEFAULT_TEMPLATE = 'empty';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/nginx:stable-alpine
COPY dist/apps/<%= projectName %>/* /usr/share/nginx/html/
COPY dist/apps/<%= projectName %>/* /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Tree, addProjectConfiguration, readProjectConfiguration } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { configurationGenerator } from './generator';
import { ConfigurationSchema } from './schema';

describe('configuration generator', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

test.each([
[1, 'myapp', 'docker', undefined, 'docker', undefined],
[2, 'myapp', 'docker', 'nest', 'docker', 'CMD ["dumb-init", "node", "main.js"]'],
[3, 'myapp', 'docker', 'next', 'docker', 'ENV NEXT_TELEMETRY_DISABLED 1'],
[4, 'myapp', 'docker', 'react-angular-spa', 'docker', 'FROM docker.io/nginx:stable-alpine'],
[5, 'myapp', 'podman', undefined, 'podman', undefined],
[6, 'myapp', 'podman', 'nest', 'podman', 'CMD ["dumb-init", "node", "main.js"]'],
[7, 'myapp', 'podman', 'next', 'podman', 'ENV NEXT_TELEMETRY_DISABLED 1'],
[8, 'myapp', 'podman', 'react-angular-spa', 'podman', 'FROM docker.io/nginx:stable-alpine'],
[9, 'myapp', 'kaniko', undefined, 'kaniko', undefined],
[10, 'myapp', 'kaniko', 'nest', 'kaniko', 'CMD ["dumb-init", "node", "main.js"]'],
[11, 'myapp', 'kaniko', 'next', 'kaniko', 'ENV NEXT_TELEMETRY_DISABLED 1'],
[12, 'myapp', 'kaniko', 'react-angular-spa', 'kaniko', 'FROM docker.io/nginx:stable-alpine'],
])(
'%d - given projectName=%s, engine=%s and template=%s - should generate configuration for %s executor and proper dockerfile',
async (_, projectName, engine, template, executor, text) => {
const options: ConfigurationSchema = { project: projectName, engine, template };

addProjectConfiguration(tree, projectName, { root: `apps/${projectName}` });

await configurationGenerator(tree, options);

const project = readProjectConfiguration(tree, projectName);
expect(tree.exists('./apps/myapp/Dockerfile')).toBeTruthy();

const contents = tree.read('./apps/myapp/Dockerfile', 'utf-8');

if (text) {
expect(contents.includes(text)).toBeTruthy();
expect(contents.includes(`COPY dist/apps/${projectName}/`)).toBeTruthy();
} else {
expect(contents).toBe('');
}

expect(project.targets).toMatchObject({
container: {
executor: `@nx-tools/nx-container:${executor}`,
},
});
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ import {
formatFiles,
generateFiles,
ProjectConfiguration,
readNxJson,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
import * as path from 'path';
import { InitGeneratorSchema } from './schema';
import { DEFAULT_ENGINE, DEFAULT_TEMPLATE } from './constants';
import { ConfigurationSchema } from './schema';

function addFiles(tree: Tree, project: ProjectConfiguration, template) {
const templateOptions = {
projectName: project.name,
template: '',
};
generateFiles(tree, path.join(__dirname, 'files', template || 'empty'), project.root, templateOptions);
generateFiles(tree, path.join(__dirname, 'files', template), project.root, templateOptions);
}

export default async function (tree: Tree, options: InitGeneratorSchema) {
export async function configurationGenerator(tree: Tree, options: ConfigurationSchema) {
const project = readProjectConfiguration(tree, options.project);
const nx = readNxJson(tree);

updateProjectConfiguration(tree, options.project, {
...project,
targets: {
...project.targets,
container: {
executor: '@nx-tools/nx-container:build',
executor: `@nx-tools/nx-container:${options.engine ?? DEFAULT_ENGINE}`,
dependsOn: ['build'],
options: {
engine: options.engine,
metadata: {
images: [`${nx.npmScope}/${project.name}`],
images: [project.name],
load: true,
tags: [
'type=schedule',
Expand All @@ -47,7 +45,7 @@ export default async function (tree: Tree, options: InitGeneratorSchema) {
},
});

addFiles(tree, project, options.template);
addFiles(tree, project, options.template ?? DEFAULT_TEMPLATE);

if (!options.skipFormat) {
await formatFiles(tree);
Expand Down
25 changes: 25 additions & 0 deletions packages/nx-container/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface ConfigurationSchema {
/**
* The name of the project to add the Container setup to.
*/
project: string;
/**
* Provide the container engine to be used.
*/
engine?: string;
/**
* Which type of app you are building?.
*/
template?: string;
/**
* Skips formatting the workspace after the generator completes.
*/
skipFormat?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "Init",
"$id": "Configuration",
"title": "",
"type": "object",
"properties": {
Expand Down
6 changes: 0 additions & 6 deletions packages/nx-container/src/generators/init/schema.d.ts

This file was deleted.

0 comments on commit 7c98412

Please sign in to comment.