@mikeyt23/node-cli-utils 1.1.0 → 1.2.2

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 +64 -13
  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
  }
@@ -160,7 +160,7 @@ exports.defaultSpawnOptions = {
160
160
  stdio: ['ignore', 'inherit', 'inherit']
161
161
  }
162
162
 
163
- async function createTarball(directoryToTarball, outputDirectory, tarballName) {
163
+ async function createTarball(directoryToTarball, outputDirectory, tarballName, cwd = '') {
164
164
  if (!directoryToTarball || directoryToTarball.length === 0) {
165
165
  throw new Error('directoryToTarball is required')
166
166
  }
@@ -170,25 +170,72 @@ 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
- fse.emptyDirSync(outputDirectory)
186
+ if (fs.existsSync(tarballPath)) {
187
+ fs.unlinkSync(tarballPath)
188
+ }
187
189
  }
188
190
 
189
- await tar.c({gzip: true, file: tarballPath}, [directoryToTarball])
191
+ let options = { gzip: true, file: tarballPath }
192
+
193
+ if (!!cwd) {
194
+ options.C = cwd
195
+ }
196
+
197
+ await tar.c(options, [directoryToTarball])
190
198
  }
191
199
 
200
+ async function dockerCompose(command, projectName, dockerRelativeDirectory, detached = false) {
201
+ if (!projectName || projectName.length === 0) {
202
+ throw new Error('projectName is required')
203
+ }
204
+
205
+ const dockerRelativeDir = dockerRelativeDirectory || 'docker'
206
+ const dockerWorkingDir = path.join(process.cwd(), dockerRelativeDir)
207
+
208
+ if (!fs.existsSync(dockerWorkingDir)) {
209
+ throw new Error('Docker directory does not exist: ' + dockerWorkingDir)
210
+ }
211
+
212
+ await throwIfDockerNotRunning()
213
+
214
+ const dockerSpawnOptions = { ...defaultSpawnOptions, cwd: dockerWorkingDir }
215
+
216
+ let args = ['--project-name', projectName, command]
217
+ if (detached) {
218
+ args.push('-d')
219
+ }
220
+
221
+ return waitForProcess(spawn('docker-compose', args, dockerSpawnOptions))
222
+ }
223
+
224
+ async function dockerDepsUp(projectName, dockerRelativeDirectory) {
225
+ return dockerCompose('up', projectName, dockerRelativeDirectory)
226
+ }
227
+
228
+ async function dockerDepsUpDetached(projectName, dockerRelativeDirectory) {
229
+ return dockerCompose('up', projectName, dockerRelativeDirectory, true)
230
+ }
231
+
232
+ async function dockerDepsDown(projectName, dockerRelativeDirectory) {
233
+ return dockerCompose('down', projectName, dockerRelativeDirectory)
234
+ }
235
+
236
+ async function dockerDepsStop(projectName, dockerRelativeDirectory) {
237
+ return dockerCompose('stop', projectName, dockerRelativeDirectory)
238
+ }
192
239
 
193
240
  exports.defaultSpawnOptions = defaultSpawnOptions
194
241
  exports.waitForProcess = waitForProcess
@@ -198,3 +245,7 @@ exports.throwIfDockerNotRunning = throwIfDockerNotRunning
198
245
  exports.bashIntoRunningDockerContainer = bashIntoRunningDockerContainer
199
246
  exports.dockerContainerIsRunning = dockerContainerIsRunning
200
247
  exports.createTarball = createTarball
248
+ exports.dockerDepsUp = dockerDepsUp
249
+ exports.dockerDepsUpDetached = dockerDepsUpDetached
250
+ exports.dockerDepsDown = dockerDepsDown
251
+ 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.2",
4
4
  "description": "Some node cli utility functions",
5
5
  "author": "Mike Thompson",
6
6
  "license": "MIT",