Skip to content

Commit

Permalink
fix(nx-container): fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Badeau, Jose committed Nov 29, 2024
1 parent 6d53fa5 commit 09b4517
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
2 changes: 2 additions & 0 deletions plugins/nx-container/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
},
"peerDependencies": {
"@nx/devkit": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0",
"@nx/js": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0",
"nx": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0",
"dotenv": ">=16.0.0",
"@swc/helpers": "~0.5.11"
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/nx-container/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tree, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { initGenerator, hasContainerPlugin } from './init';
import { initGenerator } from './init';

describe('init generator', () => {
let tree: Tree;
Expand Down
37 changes: 19 additions & 18 deletions plugins/nx-container/src/generators/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { GeneratorCallback, readNxJson, runTasksInSerial, Tree, updateNxJson } from '@nx/devkit';
import { GeneratorCallback, PluginConfiguration, readNxJson, runTasksInSerial, Tree, updateNxJson } from '@nx/devkit';
import { Schema } from './schema';
import { DEFAULT_ENGINE } from '../configuration/constants';

export async function initGenerator(tree: Tree, options: Schema) {
export async function initGenerator(tree: Tree, options: Schema): Promise<GeneratorCallback> {
const tasks: GeneratorCallback[] = [];

addPlugin(tree);

return runTasksInSerial(...tasks);
}

function addPlugin(tree: Tree) {
const nxJson = readNxJson(tree);

if (!hasContainerPlugin(tree)) {
// Add container plugin to Nx configuration if not already present
if (!hasContainerPlugin(nxJson)) {
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx-tools/nx-container',
options: {},
});

const pluginConfig: PluginConfiguration =
options.defaultEngine !== DEFAULT_ENGINE
? { plugin: '@nx-tools/nx-container', options: { defaultEngine: options.defaultEngine } }
: '@nx-tools/nx-container';

nxJson.plugins.push(pluginConfig);
updateNxJson(tree, nxJson);
}

return runTasksInSerial(...tasks);
}

export function hasContainerPlugin(tree: Tree): boolean {
const nxJson = readNxJson(tree);
return !!nxJson.plugins?.some((p) =>
typeof p === 'string' ? p === '@nx-tools/nx-container' : p.plugin === '@nx-tools/nx-container'
function hasContainerPlugin(nxJson: { plugins?: PluginConfiguration[] }): boolean {
return (
nxJson.plugins?.some((plugin) =>
typeof plugin === 'string' ? plugin === '@nx-tools/nx-container' : plugin.plugin === '@nx-tools/nx-container'
) ?? false
);
}

Expand Down
7 changes: 6 additions & 1 deletion plugins/nx-container/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export interface Schema {}
export interface Schema {
/**
* Provide the default container engine to be used.
*/
defaultEngine?: 'docker' | 'podman' | 'kaniko';
}
30 changes: 28 additions & 2 deletions plugins/nx-container/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
"$id": "Configuration",
"title": "",
"type": "object",
"properties": {},
"required": []
"required": [],
"properties": {
"defaultEngine": {
"type": "string",
"description": "Provide the default container engine to be used.",
"default": "docker",
"enum": ["docker", "podman", "kaniko"],
"x-prompt": {
"message": "Which type of engine would you like to use?",
"type": "list",
"items": [
{
"value": "docker",
"label": "Docker"
},
{
"value": "podman",
"label": "Podman"
},
{
"value": "kaniko",
"label": "Kaniko"
}
]
},
"alias": "e"
}
}
}

0 comments on commit 09b4517

Please sign in to comment.