@mikeyt23/node-cli-utils 1.1.0 → 1.2.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.
Files changed (2) hide show
  1. package/index.js +55 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2,7 +2,7 @@ const fs = require('fs')
2
2
  const fse = require('fs-extra')
3
3
  const fsp = require('fs').promises
4
4
  const which = require('which')
5
- const {spawn, spawnSync} = require('child_process')
5
+ const { spawn, spawnSync } = require('child_process')
6
6
  const path = require('path')
7
7
  const tar = require('tar')
8
8
 
@@ -10,7 +10,7 @@ const defaultSpawnOptions = {
10
10
  shell: true,
11
11
  stdio: ['ignore', 'inherit', 'inherit']
12
12
  }
13
- const spawnOptionsWithInput = {...defaultSpawnOptions, stdio: 'inherit'}
13
+ const spawnOptionsWithInput = { ...defaultSpawnOptions, stdio: 'inherit' }
14
14
 
15
15
  function waitForProcess(childProcess) {
16
16
  return new Promise((resolve, reject) => {
@@ -41,7 +41,7 @@ async function throwIfDockerNotRunning() {
41
41
  throw Error('docker command not found')
42
42
  }
43
43
 
44
- let childProcess = spawnSync('docker', ['info'], {encoding: 'utf8'})
44
+ let childProcess = spawnSync('docker', ['info'], { encoding: 'utf8' })
45
45
  if (childProcess.error) {
46
46
  throw childProcess.error
47
47
  }
@@ -53,7 +53,7 @@ async function throwIfDockerNotRunning() {
53
53
  async function bashIntoRunningDockerContainer(containerNamePartial, entryPoint = 'bash') {
54
54
  await throwIfDockerNotRunning()
55
55
 
56
- let childProcess = spawnSync('docker', ['container', 'ls'], {encoding: 'utf8'})
56
+ let childProcess = spawnSync('docker', ['container', 'ls'], { encoding: 'utf8' })
57
57
  if (childProcess.error) {
58
58
  throw childProcess.error
59
59
  }
@@ -81,7 +81,7 @@ async function bashIntoRunningDockerContainer(containerNamePartial, entryPoint =
81
81
  async function dockerContainerIsRunning(containerNamePartial) {
82
82
  await throwIfDockerNotRunning()
83
83
 
84
- let childProcess = spawnSync('docker', ['container', 'ls'], {encoding: 'utf8'})
84
+ let childProcess = spawnSync('docker', ['container', 'ls'], { encoding: 'utf8' })
85
85
  if (childProcess.error) {
86
86
  throw childProcess.error
87
87
  }
@@ -170,25 +170,64 @@ async function createTarball(directoryToTarball, outputDirectory, tarballName) {
170
170
  if (!tarballName || tarballName.length === 0) {
171
171
  throw new Error('tarballName is required')
172
172
  }
173
-
173
+
174
174
  const tarballPath = path.join(outputDirectory, tarballName)
175
-
175
+
176
176
  console.log('directory to create tarball from: ' + directoryToTarball)
177
- console.log('output will be:' + tarballPath)
178
-
177
+ console.log('output will be: ' + tarballPath)
178
+
179
179
  if (!fs.existsSync(directoryToTarball)) {
180
180
  throw new Error('error: dirToTarball directory does not exist')
181
181
  }
182
-
182
+
183
183
  if (!fs.existsSync(outputDirectory)) {
184
184
  fs.mkdirSync(outputDirectory)
185
185
  } else {
186
186
  fse.emptyDirSync(outputDirectory)
187
187
  }
188
-
189
- await tar.c({gzip: true, file: tarballPath}, [directoryToTarball])
188
+
189
+ await tar.c({ gzip: true, file: tarballPath }, [directoryToTarball])
190
+ }
191
+
192
+ async function dockerCompose(command, projectName, dockerRelativeDirectory, detached = false) {
193
+ if (!projectName || projectName.length === 0) {
194
+ throw new Error('projectName is required')
195
+ }
196
+
197
+ const dockerRelativeDir = dockerRelativeDirectory || 'docker'
198
+ const dockerWorkingDir = path.join(process.cwd(), dockerRelativeDir)
199
+
200
+ if (!fs.existsSync(dockerWorkingDir)) {
201
+ throw new Error('Docker directory does not exist: ' + dockerWorkingDir)
202
+ }
203
+
204
+ await throwIfDockerNotRunning()
205
+
206
+ const dockerSpawnOptions = { ...defaultSpawnOptions, cwd: dockerWorkingDir }
207
+
208
+ let args = ['--project-name', projectName, command]
209
+ if (detached) {
210
+ args.push('-d')
211
+ }
212
+
213
+ return waitForProcess(spawn('docker-compose', args, dockerSpawnOptions))
214
+ }
215
+
216
+ async function dockerDepsUp(projectName, dockerRelativeDirectory) {
217
+ return dockerCompose('up', projectName, dockerRelativeDirectory)
218
+ }
219
+
220
+ async function dockerDepsUpDetached(projectName, dockerRelativeDirectory) {
221
+ return dockerCompose('up', projectName, dockerRelativeDirectory, true)
190
222
  }
191
223
 
224
+ async function dockerDepsDown(projectName, dockerRelativeDirectory) {
225
+ return dockerCompose('down', projectName, dockerRelativeDirectory)
226
+ }
227
+
228
+ async function dockerDepsStop(projectName, dockerRelativeDirectory) {
229
+ return dockerCompose('stop', projectName, dockerRelativeDirectory)
230
+ }
192
231
 
193
232
  exports.defaultSpawnOptions = defaultSpawnOptions
194
233
  exports.waitForProcess = waitForProcess
@@ -198,3 +237,7 @@ exports.throwIfDockerNotRunning = throwIfDockerNotRunning
198
237
  exports.bashIntoRunningDockerContainer = bashIntoRunningDockerContainer
199
238
  exports.dockerContainerIsRunning = dockerContainerIsRunning
200
239
  exports.createTarball = createTarball
240
+ exports.dockerDepsUp = dockerDepsUp
241
+ exports.dockerDepsUpDetached = dockerDepsUpDetached
242
+ exports.dockerDepsDown = dockerDepsDown
243
+ exports.dockerDepsStop = dockerDepsStop
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikeyt23/node-cli-utils",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Some node cli utility functions",
5
5
  "author": "Mike Thompson",
6
6
  "license": "MIT",