@addev-be/framework-utils 0.19.4 → 0.20.0

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.
@@ -0,0 +1,69 @@
1
+ import path from 'path';
2
+ import { spawn } from 'child_process';
3
+
4
+ /**
5
+ * Builds a Docker image from a specified local directory.
6
+ * @param {string} directory - The path to the directory containing the Dockerfile.
7
+ * @param {string} tag - Image tag (e.g., 'my-image:latest').
8
+ * @returns {Promise<void>} Resolves when the build is complete.
9
+ */
10
+ export async function buildDockerImage(directory, tag, registryUrl) {
11
+ return new Promise((resolve, reject) => {
12
+ const args = [
13
+ 'build',
14
+ '.',
15
+ '-t',
16
+ `${registryUrl ? registryUrl + '/' : ''}${tag}`,
17
+ ];
18
+ const buildProcess = spawn('docker', args, {
19
+ cwd: path.resolve(directory),
20
+ stdio: 'inherit',
21
+ });
22
+
23
+ buildProcess.on('close', (code) => {
24
+ if (code === 0) {
25
+ resolve();
26
+ } else {
27
+ reject(new Error(`Docker build failed with exit code ${code}`));
28
+ }
29
+ });
30
+
31
+ buildProcess.on('error', (err) => {
32
+ reject(err);
33
+ });
34
+ });
35
+ }
36
+
37
+ /**
38
+ * Pushes a Docker image to a Docker server.
39
+ * @param {string} tag - The image tag to push (e.g., 'my-image:latest' or 'myregistry.example.com/my-image:latest').
40
+ * @param {string} [registryUrl] - Optional custom registry URL (e.g., 'myregistry.example.com').
41
+ * @returns {Promise<void>} Resolves when the push is complete.
42
+ */
43
+ export async function pushDockerImage(tag, registryUrl) {
44
+ return new Promise((resolve, reject) => {
45
+ let imageTag = tag;
46
+ // If a custom registry URL is provided and not already in the tag, prepend it
47
+ if (registryUrl && !tag.startsWith(registryUrl)) {
48
+ // Remove trailing slash from registryUrl if present
49
+ const cleanRegistryUrl = registryUrl.replace(/\/$/, '');
50
+ imageTag = `${cleanRegistryUrl}/${tag}`;
51
+ }
52
+ const args = ['push', imageTag];
53
+ const pushProcess = spawn('docker', args, {
54
+ stdio: 'inherit',
55
+ });
56
+
57
+ pushProcess.on('close', (code) => {
58
+ if (code === 0) {
59
+ resolve();
60
+ } else {
61
+ reject(new Error(`Docker push failed with exit code ${code}`));
62
+ }
63
+ });
64
+
65
+ pushProcess.on('error', (err) => {
66
+ reject(err);
67
+ });
68
+ });
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@addev-be/framework-utils",
3
- "version": "0.19.4",
3
+ "version": "0.20.0",
4
4
  "type": "module",
5
5
  "export": {
6
6
  "node": {