@uscreen.de/dev-service 0.13.1 → 0.14.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.
package/src/utils.js CHANGED
@@ -1,13 +1,14 @@
1
1
  'use strict'
2
2
 
3
+ import { exec, spawn } from 'node:child_process'
4
+ import path from 'node:path'
5
+ import process from 'node:process'
6
+ import { fileURLToPath } from 'node:url'
7
+ import chalk from 'chalk'
3
8
  import fs from 'fs-extra'
4
- import path from 'path'
5
- import { fileURLToPath } from 'url'
6
9
  import { readPackageSync } from 'read-pkg'
7
- import chalk from 'chalk'
8
- import { exec, spawn } from 'child_process'
9
10
 
10
- import { root, COMPOSE_DIR } from './constants.js'
11
+ import { COMPOSE_DIR, root } from './constants.js'
11
12
 
12
13
  const __filename = fileURLToPath(import.meta.url)
13
14
  const __dirname = path.dirname(__filename)
@@ -19,15 +20,18 @@ export const readPackageJson = () => {
19
20
  let packageJson = null
20
21
  try {
21
22
  packageJson = readPackageSync({ cwd: root })
22
- } catch (e) {}
23
+ }
24
+ catch {}
23
25
 
24
- if (!packageJson) throw Error('Missing or invalid package.json')
26
+ if (!packageJson) {
27
+ throw new Error('Missing or invalid package.json')
28
+ }
25
29
 
26
30
  const services = packageJson.services || []
27
31
  const name = packageJson.name || path.basename(root)
28
32
 
29
33
  if (services.length === 0) {
30
- throw Error('No services defined')
34
+ throw new Error('No services defined')
31
35
  }
32
36
 
33
37
  return { packageJson, services, name }
@@ -36,8 +40,8 @@ export const readPackageJson = () => {
36
40
  /**
37
41
  * Escape string for use in docker
38
42
  */
39
- export const escape = (name) =>
40
- name.replace(/^[^a-zA-Z0-9]*/, '').replace(/[^a-zA-Z0-9-]/g, '-')
43
+ export const escape = name =>
44
+ name.replace(/^[^a-z0-9]*/i, '').replace(/[^a-z0-9-]/gi, '-')
41
45
 
42
46
  /**
43
47
  * Checks if compose directory exists and contains files
@@ -50,26 +54,46 @@ export const checkComposeDir = () => {
50
54
  * Get all compose files from compose directory
51
55
  */
52
56
  export const getComposeFiles = () =>
53
- fs.readdirSync(COMPOSE_DIR).filter((f) => f !== '.gitignore')
57
+ fs.readdirSync(COMPOSE_DIR).filter(f => f !== '.gitignore')
58
+
59
+ /**
60
+ * Detect compose command: prefer 'docker compose' plugin, fall back to 'docker-compose'
61
+ */
62
+ let _composeCmd = null
63
+
64
+ export const getComposeCommand = () => {
65
+ if (!_composeCmd) {
66
+ _composeCmd = new Promise((resolve) => {
67
+ exec('docker compose version', (err) => {
68
+ resolve(err ? 'docker-compose' : 'docker compose')
69
+ })
70
+ })
71
+ }
72
+ return _composeCmd
73
+ }
54
74
 
55
75
  /**
56
76
  * read path to compose file/folder via `docker inspect <containerId>`
57
77
  */
58
- const getComposePath = (containerId) =>
78
+ const getComposePath = containerId =>
59
79
  new Promise((resolve, reject) => {
60
- exec(`docker inspect ${containerId}`, function (err, stdout, stderr) {
61
- if (err) reject(err)
80
+ exec(`docker inspect ${containerId}`, (err, stdout, stderr) => {
81
+ if (err) {
82
+ reject(err)
83
+ }
62
84
 
63
85
  const errMessage = stderr.toString().trim()
64
- if (errMessage) reject(Error(errMessage))
86
+ if (errMessage) {
87
+ reject(new Error(errMessage))
88
+ }
65
89
 
66
90
  const [data] = JSON.parse(stdout)
67
91
 
68
- const result =
69
- data &&
70
- data.Config &&
71
- data.Config.Labels &&
72
- data.Config.Labels['com.docker.compose.project.working_dir']
92
+ const result
93
+ = data
94
+ && data.Config
95
+ && data.Config.Labels
96
+ && data.Config.Labels['com.docker.compose.project.working_dir']
73
97
 
74
98
  resolve(result)
75
99
  })
@@ -80,17 +104,21 @@ const getComposePath = (containerId) =>
80
104
  */
81
105
  export const getComposePaths = () =>
82
106
  new Promise((resolve, reject) => {
83
- exec('docker ps -q', function (err, stdout, stderr) {
84
- if (err) reject(err)
107
+ exec('docker ps -q', (err, stdout, stderr) => {
108
+ if (err) {
109
+ reject(err)
110
+ }
85
111
 
86
112
  const errMessage = stderr.toString().trim()
87
- if (errMessage) reject(Error(errMessage))
113
+ if (errMessage) {
114
+ reject(new Error(errMessage))
115
+ }
88
116
 
89
- const containers = stdout.split(/\n/).filter((r) => r)
117
+ const containers = stdout.split(/\n/).filter(r => r)
90
118
 
91
119
  Promise.all(containers.map(getComposePath))
92
120
  .then((ps) => {
93
- const paths = Array.from(new Set(ps)).filter((p) => p)
121
+ const paths = Array.from(new Set(ps)).filter(p => p)
94
122
 
95
123
  resolve(paths)
96
124
  })
@@ -132,8 +160,10 @@ export const run = (command, parameters = [], cwd = null, stdio = [0, 1, 2]) =>
132
160
  stdio
133
161
  })
134
162
  c.on('close', (code) => {
135
- if (code === 0) return resolve(code)
136
- const e = Error(`Running "${command}" returns exit code ${code}`)
163
+ if (code === 0) {
164
+ return resolve(code)
165
+ }
166
+ const e = new Error(`Running "${command}" returns exit code ${code}`)
137
167
  e.code = code
138
168
  reject(e)
139
169
  })
@@ -151,7 +181,7 @@ export const docker = async (...params) => {
151
181
  */
152
182
  export const compose = async (...params) => {
153
183
  if (!checkComposeDir()) {
154
- throw Error('No services found. Try running `service install`')
184
+ throw new Error('No services found. Try running `service install`')
155
185
  }
156
186
 
157
187
  const { name } = await readPackageJson()
@@ -166,5 +196,10 @@ export const compose = async (...params) => {
166
196
 
167
197
  ps.push(...params)
168
198
 
199
+ const cmd = await getComposeCommand()
200
+
201
+ if (cmd === 'docker compose') {
202
+ return run('docker', ['compose', ...ps], COMPOSE_DIR)
203
+ }
169
204
  return run('docker-compose', ps, COMPOSE_DIR)
170
205
  }