@nx/docker 0.0.0-pr-31634-cb74636

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2017-2025 Narwhal Technologies Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ <p style="text-align: center;">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-dark.svg">
4
+ <img alt="Nx - Smart Repos · Fast Builds" src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-light.svg" width="100%">
5
+ </picture>
6
+ </p>
7
+
8
+ <div style="text-align: center;">
9
+
10
+ [![CircleCI](https://circleci.com/gh/nrwl/nx.svg?style=svg)](https://circleci.com/gh/nrwl/nx)
11
+ [![License](https://img.shields.io/npm/l/@nx/workspace.svg?style=flat-square)]()
12
+ [![NPM Version](https://badge.fury.io/js/nx.svg)](https://www.npmjs.com/package/nx)
13
+ [![Semantic Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)]()
14
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
15
+ [![Join the chat at https://gitter.im/nrwl-nx/community](https://badges.gitter.im/nrwl-nx/community.svg)](https://gitter.im/nrwl-nx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
16
+ [![Join us on the Official Nx Discord Server](https://img.shields.io/discord/1143497901675401286?label=discord)](https://go.nx.dev/community)
17
+
18
+ </div>
19
+
20
+
21
+ <hr>
22
+
23
+ # Nx: Smart Repos · Fast Builds
24
+
25
+ An AI-first build platform that connects everything from your editor to CI. Helping you deliver fast, without breaking things.
26
+
27
+ This package is a [Docker plugin for Nx](https://nx.dev/nx-api/docker).
28
+
29
+ ## Getting Started
30
+
31
+ ### Creating an Nx Workspace
32
+
33
+ **Using `npx`**
34
+
35
+ ```bash
36
+ npx create-nx-workspace
37
+ ```
38
+
39
+ **Using `npm init`**
40
+
41
+ ```bash
42
+ npm init nx-workspace
43
+ ```
44
+
45
+ **Using `yarn create`**
46
+
47
+ ```bash
48
+ yarn create nx-workspace
49
+ ```
50
+
51
+ ### Adding Nx to an Existing Repository
52
+
53
+ Run:
54
+
55
+ ```bash
56
+ npx nx@latest init
57
+ ```
58
+
59
+ ## Documentation & Resources
60
+
61
+ - [Nx.Dev: Documentation, Guides, Tutorials](https://nx.dev)
62
+ - [Intro to Nx](https://nx.dev/getting-started/intro)
63
+ - [Official Nx YouTube Channel](https://www.youtube.com/@NxDevtools)
64
+ - [Blog Posts About Nx](https://nx.dev/blog)
65
+
66
+ <p style="text-align: center;"><a href="https://nx.dev/#learning-materials" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-courses-and-videos.svg"
67
+ width="100%" alt="Nx - Smart Repos · Fast Builds"></a></p>
68
+
package/executors.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "executors": {
4
+ "release-publish": {
5
+ "implementation": "./src/executors/release-publish/release-publish.impl",
6
+ "schema": "./src/executors/release-publish/schema.json",
7
+ "description": "DO NOT INVOKE DIRECTLY WITH `nx run`. Use `nx release publish` instead.",
8
+ "hidden": true
9
+ }
10
+ }
11
+ }
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { createNodesV2, DockerPluginOptions } from './src/plugins/plugin';
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createNodesV2 = void 0;
4
+ var plugin_1 = require("./src/plugins/plugin");
5
+ Object.defineProperty(exports, "createNodesV2", { enumerable: true, get: function () { return plugin_1.createNodesV2; } });
@@ -0,0 +1 @@
1
+ {}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@nx/docker",
3
+ "description": "The Nx Plugin for Docker to aid in containerizing projects.",
4
+ "version": "0.0.0-pr-31634-cb74636",
5
+ "type": "commonjs",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/nrwl/nx.git",
12
+ "directory": "packages/docker"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/nrwl/nx/issues"
16
+ },
17
+ "keywords": [
18
+ "Monorepo",
19
+ "Docker",
20
+ "Containers"
21
+ ],
22
+ "author": "Colum Ferry",
23
+ "license": "MIT",
24
+ "homepage": "https://nx.dev",
25
+ "main": "index.js",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./index.d.ts",
29
+ "default": "./index.js"
30
+ },
31
+ "./release/version-utils": {
32
+ "types": "./src/release/version-utils.d.ts",
33
+ "default": "./src/release/version-utils.js"
34
+ },
35
+ "./package.json": {
36
+ "default": "./package.json"
37
+ }
38
+ },
39
+ "executors": "./executors.json",
40
+ "dependencies": {
41
+ "@nx/devkit": "0.0.0-pr-31634-cb74636",
42
+ "tslib": "^2.3.0"
43
+ },
44
+ "types": "./index.d.ts"
45
+ }
@@ -0,0 +1,5 @@
1
+ import { ExecutorContext } from '@nx/devkit';
2
+ import { DockerReleasePublishSchema } from './schema';
3
+ export default function dockerReleasePublish(schema: DockerReleasePublishSchema, context: ExecutorContext): Promise<{
4
+ success: boolean;
5
+ }>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = dockerReleasePublish;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const child_process_1 = require("child_process");
6
+ const path_1 = require("path");
7
+ const fs_1 = require("fs");
8
+ async function dockerReleasePublish(schema, context) {
9
+ const projectConfig = context.projectGraph.nodes[context.projectName];
10
+ const options = normalizeOptions(projectConfig, schema);
11
+ if (!options.dryRun) {
12
+ const digest = dockerPush(options.imageReference);
13
+ devkit_1.logger.log(`Successfully pushed ${options.imageReference}. Digest: ${digest}`);
14
+ }
15
+ else {
16
+ devkit_1.logger.log(`Docker Image ${options.imageReference} was not pushed as --dry-run is enabled.`);
17
+ }
18
+ return {
19
+ success: true,
20
+ };
21
+ }
22
+ function normalizeOptions(projectConfig, schema) {
23
+ return {
24
+ imageReference: findImageReference(projectConfig, schema),
25
+ dryRun: process.env.NX_DRY_RUN === 'true' || schema.dryRun || false,
26
+ };
27
+ }
28
+ function findImageReference(projectConfig, schema) {
29
+ let imageRef = readVersionFromFile(projectConfig.data.root);
30
+ if (imageRef) {
31
+ if (checkDockerImageExistsLocally(imageRef)) {
32
+ return imageRef;
33
+ }
34
+ throw new Error(`Could not find Docker Image ${imageRef}. Did you run 'nx release version'?`);
35
+ }
36
+ }
37
+ function readVersionFromFile(projectRoot) {
38
+ const versionFilePath = (0, path_1.join)(projectRoot, '.docker-version');
39
+ if (!(0, fs_1.existsSync)(versionFilePath)) {
40
+ throw new Error("Could not find .docker-version file. Did you run 'nx release version'?");
41
+ }
42
+ const version = (0, fs_1.readFileSync)(versionFilePath, { encoding: 'utf8' });
43
+ return version.trim();
44
+ }
45
+ function checkDockerImageExistsLocally(imageRef) {
46
+ try {
47
+ const result = (0, child_process_1.execSync)(`docker images --filter "reference=${imageRef}" --quiet`, { encoding: 'utf8' });
48
+ return result.trim().length > 0;
49
+ }
50
+ catch {
51
+ return false;
52
+ }
53
+ }
54
+ function dockerPush(imageReference) {
55
+ try {
56
+ const result = (0, child_process_1.execSync)(`docker push ${imageReference} --quiet`, {
57
+ encoding: 'utf8',
58
+ });
59
+ return result.trim();
60
+ }
61
+ catch (e) {
62
+ devkit_1.logger.error(`Failed to push ${imageReference}`);
63
+ throw e;
64
+ }
65
+ }
@@ -0,0 +1,8 @@
1
+ export interface DockerReleasePublishSchema {
2
+ dryRun?: boolean;
3
+ }
4
+
5
+ export interface NormalizedDockerReleasePublishSchema {
6
+ imageReference: string;
7
+ dryRun: boolean;
8
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "version": 2,
4
+ "title": "Implementation details of `nx release publish`",
5
+ "description": "DO NOT INVOKE DIRECTLY WITH `nx run`. Use `nx release publish` instead.",
6
+ "type": "object",
7
+ "properties": {
8
+ "dryRun": {
9
+ "type": "boolean",
10
+ "description": "Whether to run the command without actually publishing the image to the registry."
11
+ }
12
+ },
13
+ "required": []
14
+ }
@@ -0,0 +1,6 @@
1
+ import { type CreateNodesV2 } from '@nx/devkit';
2
+ export interface DockerPluginOptions {
3
+ buildTarget?: string;
4
+ runTarget?: string;
5
+ }
6
+ export declare const createNodesV2: CreateNodesV2<DockerPluginOptions>;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createNodesV2 = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
6
+ const file_hasher_1 = require("nx/src/hasher/file-hasher");
7
+ const cache_directory_1 = require("nx/src/utils/cache-directory");
8
+ const fs_1 = require("fs");
9
+ const path_1 = require("path");
10
+ const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs");
11
+ function readTargetsCache(cachePath) {
12
+ return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {};
13
+ }
14
+ function writeTargetsCache(cachePath, results) {
15
+ (0, devkit_1.writeJsonFile)(cachePath, results ?? {});
16
+ }
17
+ const dockerfileGlob = '**/Dockerfile';
18
+ exports.createNodesV2 = [
19
+ dockerfileGlob,
20
+ async (configFilePaths, options, context) => {
21
+ const optionsHash = (0, file_hasher_1.hashObject)(options);
22
+ const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `docker-${optionsHash}.hash`);
23
+ const targetsCache = readTargetsCache(cachePath);
24
+ try {
25
+ return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache), configFilePaths, options, context);
26
+ }
27
+ finally {
28
+ writeTargetsCache(cachePath, targetsCache);
29
+ }
30
+ },
31
+ ];
32
+ async function createNodesInternal(configFilePath, options, context, targetsCache) {
33
+ const projectRoot = (0, path_1.dirname)(configFilePath);
34
+ const normalizedOptions = normalizePluginOptions(options);
35
+ const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, normalizedOptions, context);
36
+ targetsCache[hash] ??= await createDockerTargets(projectRoot, normalizedOptions, context);
37
+ const { targets, metadata } = targetsCache[hash];
38
+ return {
39
+ projects: {
40
+ [projectRoot]: {
41
+ root: projectRoot,
42
+ targets,
43
+ metadata,
44
+ },
45
+ },
46
+ };
47
+ }
48
+ async function createDockerTargets(projectRoot, options, context) {
49
+ const imageRef = projectRoot.replace(/^[\\/]/, '').replace(/[\\/\s]+/g, '-');
50
+ const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
51
+ const targets = {};
52
+ targets[options.buildTarget] = {
53
+ command: `docker build .`,
54
+ options: {
55
+ cwd: projectRoot,
56
+ args: [`--tag ${imageRef}`],
57
+ },
58
+ inputs: [
59
+ ...('production' in namedInputs
60
+ ? ['production', '^production']
61
+ : ['default', '^default']),
62
+ ],
63
+ metadata: {
64
+ technologies: ['docker'],
65
+ description: `Run Docker build`,
66
+ help: {
67
+ command: `docker build --help`,
68
+ example: {},
69
+ },
70
+ },
71
+ };
72
+ targets[options.runTarget] = {
73
+ dependsOn: [options.buildTarget],
74
+ command: `docker run {args} ${imageRef}`,
75
+ options: {
76
+ cwd: projectRoot,
77
+ },
78
+ inputs: [
79
+ ...('production' in namedInputs
80
+ ? ['production', '^production']
81
+ : ['default', '^default']),
82
+ ],
83
+ metadata: {
84
+ technologies: ['docker'],
85
+ description: `Run Docker run`,
86
+ help: {
87
+ command: `docker run --help`,
88
+ example: {},
89
+ },
90
+ },
91
+ };
92
+ targets['nx-release-publish'] = {
93
+ executor: '@nx/docker:release-publish',
94
+ };
95
+ return { targets, metadata: {} };
96
+ }
97
+ function normalizePluginOptions(options) {
98
+ return {
99
+ buildTarget: options.buildTarget ?? 'docker:build',
100
+ runTarget: options.runTarget ?? 'docker:run',
101
+ };
102
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Support Tokens in Version Patterns
3
+ * {projectName} - the name of the project
4
+ * {currentDate} - the current date in YY.MM.DD format
5
+ * {currentDate|DATE FORMAT} - the current date with custom format such as YYMM.DD
6
+ * {commitSha} - The full commit sha for the current commit
7
+ * {shortCommitSha} - The seven character commit sha for the current commit
8
+ */
9
+ export interface PatternTokens {
10
+ projectName: string;
11
+ currentDate: Date;
12
+ commitSha: string;
13
+ shortCommitSha: string;
14
+ }
15
+ export declare function interpolateVersionPattern(versionPattern: string, data: Partial<PatternTokens>): string;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interpolateVersionPattern = interpolateVersionPattern;
4
+ const git_utils_1 = require("nx/src/utils/git-utils");
5
+ const tokenRegex = /\{([^|{}]+)(?:\|([^{}]+))?\}/g;
6
+ function formatDate(date, format) {
7
+ const year = String(date.getFullYear());
8
+ const month = String(date.getMonth() + 1).padStart(2, '0');
9
+ const day = String(date.getDate()).padStart(2, '0');
10
+ const hours = String(date.getHours()).padStart(2, '0');
11
+ const minutes = String(date.getMinutes()).padStart(2, '0');
12
+ const seconds = String(date.getSeconds()).padStart(2, '0');
13
+ return format
14
+ .replace(/YYYY/g, year)
15
+ .replace(/YY/g, year.slice(-2))
16
+ .replace(/MM/g, month)
17
+ .replace(/DD/g, day)
18
+ .replace(/HH/g, hours)
19
+ .replace(/mm/g, minutes)
20
+ .replace(/ss/g, seconds);
21
+ }
22
+ function interpolateVersionPattern(versionPattern, data) {
23
+ const commitSha = (0, git_utils_1.getLatestCommitSha)();
24
+ const substitutions = {
25
+ projectName: data.projectName ?? '',
26
+ currentDate: data.currentDate ?? new Date(),
27
+ commitSha: data.commitSha ?? commitSha,
28
+ shortCommitSha: data.shortCommitSha ?? commitSha.slice(0, 7),
29
+ };
30
+ return versionPattern.replace(tokenRegex, (match, identifier, format) => {
31
+ const value = substitutions[identifier];
32
+ if (value === undefined) {
33
+ return match; // Keep original token if no data
34
+ }
35
+ // Handle date formatting
36
+ if (identifier === 'currentDate') {
37
+ if (format) {
38
+ return formatDate(value, format);
39
+ }
40
+ else {
41
+ return value.toISOString();
42
+ }
43
+ }
44
+ return value;
45
+ });
46
+ }
@@ -0,0 +1,6 @@
1
+ import { ProjectGraphProjectNode } from '@nx/devkit';
2
+ import type { FinalConfigForProject } from 'nx/src/command-line/release/version/release-group-processor';
3
+ export declare function handleDockerVersion(projectGraphNode: ProjectGraphProjectNode, finalConfigForProject: FinalConfigForProject): Promise<{
4
+ newVersion: string;
5
+ logs: string[];
6
+ }>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleDockerVersion = handleDockerVersion;
4
+ const child_process_1 = require("child_process");
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const enquirer_1 = require("enquirer");
8
+ const version_pattern_utils_1 = require("./version-pattern-utils");
9
+ const DEFAULT_VERSION_SCHEMES = {
10
+ production: '{currentDate|YYMM.DD}.{shortCommitSha}',
11
+ hotfix: '{currentDate|YYMM.DD}.{shortCommitSha}-hotfix',
12
+ };
13
+ async function handleDockerVersion(projectGraphNode, finalConfigForProject) {
14
+ const availableVersionSchemes = finalConfigForProject.dockerOptions.versionSchemes ??
15
+ DEFAULT_VERSION_SCHEMES;
16
+ const versionScheme = await promptForNewVersion(availableVersionSchemes, projectGraphNode.name);
17
+ const newVersion = calculateNewVersion(projectGraphNode.name, versionScheme, availableVersionSchemes);
18
+ const logs = updateProjectVersion(newVersion, projectGraphNode.data.root, finalConfigForProject.dockerOptions.repositoryName, finalConfigForProject.dockerOptions.registryUrl);
19
+ return {
20
+ newVersion,
21
+ logs,
22
+ };
23
+ }
24
+ async function promptForNewVersion(versionSchemes, projectName) {
25
+ const { versionScheme } = await (0, enquirer_1.prompt)({
26
+ name: 'versionScheme',
27
+ type: 'select',
28
+ message: `What type of release would you like to make for project "${projectName}"?`,
29
+ choices: Object.keys(versionSchemes).map((vs) => ({
30
+ name: vs,
31
+ message: vs,
32
+ value: vs,
33
+ hint: (0, version_pattern_utils_1.interpolateVersionPattern)(versionSchemes[vs], { projectName }),
34
+ })),
35
+ });
36
+ return versionScheme;
37
+ }
38
+ function calculateNewVersion(projectName, versionScheme, versionSchemes) {
39
+ if (!(versionScheme in versionSchemes)) {
40
+ throw new Error(`Could not find version scheme '${versionScheme}'. Available options are: ${Object.keys(versionSchemes).join(', ')}.`);
41
+ }
42
+ return (0, version_pattern_utils_1.interpolateVersionPattern)(versionSchemes[versionScheme], {
43
+ projectName,
44
+ });
45
+ }
46
+ function updateProjectVersion(newVersion, projectRoot, repositoryName, registry) {
47
+ const isDryRun = process.env.NX_DRY_RUN && process.env.NX_DRY_RUN !== 'false';
48
+ const imageRef = getDefaultImageReference(projectRoot);
49
+ const newImageRef = getImageReference(projectRoot, repositoryName, registry);
50
+ const fullImageRef = `${newImageRef}:${newVersion}`;
51
+ if (!isDryRun) {
52
+ (0, child_process_1.execSync)(`docker tag ${imageRef} ${fullImageRef}`);
53
+ }
54
+ const logs = [`Image ${imageRef} tagged with ${fullImageRef}.`];
55
+ if (isDryRun) {
56
+ logs.push(`No changes were applied as --dry-run is enabled.`);
57
+ }
58
+ else {
59
+ (0, fs_1.writeFileSync)((0, path_1.join)(projectRoot, '.docker-version'), fullImageRef);
60
+ }
61
+ return logs;
62
+ }
63
+ function getImageReference(projectRoot, repositoryName, registry) {
64
+ let imageRef = repositoryName ?? getDefaultImageReference(projectRoot);
65
+ if (registry) {
66
+ imageRef = `${registry}/${imageRef}`;
67
+ }
68
+ return imageRef;
69
+ }
70
+ function getDefaultImageReference(projectRoot) {
71
+ return projectRoot.replace(/^[\\/]/, '').replace(/[\\/\s]+/g, '-');
72
+ }