Skip to content

Commit 65c46b1

Browse files
committed
update syntax
1 parent a240c4b commit 65c46b1

File tree

12 files changed

+113
-141
lines changed

12 files changed

+113
-141
lines changed

src/begin-ready.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
'use strict'
2-
3-
const Transform = require('stream').Transform
4-
const Writable = require('stream').Writable
1+
const { Transform, Writable } = require('stream')
52
const restream = require('restream')
63

74
const BEGIN_READY_RE = /{begin(\d+)}([\s\S]*){ready\1}/g

src/execute-with-rs.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict'
21
const wrote = require('wrote')
3-
const Readable = require('stream').Readable
2+
const { Readable } = require('stream')
43

54
/**
65
* Create temp file for rs, execute exiftool command, then erase file
7-
* @param {Readable} rs a read strem
6+
* @param {Readable} rs a read stream
87
* @param {string[]} args Arguments
98
* @param {function} executeCommand function which is responsible for executing the command
109
*/

src/index.js

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
const EventEmitter = require('events')
42
const lib = require('./lib')
53
const beginReady = require('./begin-ready')
@@ -55,7 +53,7 @@ class ExiftoolProcess extends EventEmitter {
5553
* @returns {Promise.<number>} A promise to spawn exiftool in stay_open
5654
* mode, resolved with pid.
5755
*/
58-
open(encoding, options) {
56+
async open(encoding, options) {
5957
let _encoding = encoding
6058
let _options = options
6159
// if encoding is not a string and options are not given, treat it as options
@@ -65,45 +63,43 @@ class ExiftoolProcess extends EventEmitter {
6563
}
6664
this._assignEncoding(_encoding)
6765
if (this._open) {
68-
return Promise.reject(new Error('Exiftool process is already open'))
66+
throw new Error('Exiftool process is already open')
67+
}
68+
const exiftoolProcess = await lib.spawn(this._bin, _options)
69+
//console.log(`Started exiftool process %s`, process.pid);
70+
this.emit(events.OPEN, exiftoolProcess.pid)
71+
this._process = exiftoolProcess
72+
73+
this._process.on('exit', this._exitListener.bind(this))
74+
if (!lib.isReadable(this._process.stdout)) {
75+
lib.killProcess(this._process)
76+
throw new Error('Process was not spawned with a readable stdout, check stdio options.')
77+
}
78+
if (!lib.isWritable(this._process.stdin)) {
79+
lib.killProcess(this._process)
80+
throw new Error('Process was not spawned with a writable stdin, check stdio options.')
6981
}
70-
return lib.spawn(this._bin, _options)
71-
.then((exiftoolProcess) => {
72-
//console.log(`Started exiftool process %s`, process.pid);
73-
this.emit(events.OPEN, exiftoolProcess.pid)
74-
this._process = exiftoolProcess
75-
76-
this._process.on('exit', this._exitListener.bind(this))
77-
if (!lib.isReadable(this._process.stdout)) {
78-
lib.killProcess(this._process)
79-
throw new Error('Process was not spawned with a readable stdout, check stdio options.')
80-
}
81-
if (!lib.isWritable(this._process.stdin)) {
82-
lib.killProcess(this._process)
83-
throw new Error('Process was not spawned with a writable stdin, check stdio options.')
84-
}
85-
86-
// if process was spawned, stderr is readable (see lib/spawn)
87-
88-
this._process.stdout.setEncoding(this._encoding)
89-
this._process.stderr.setEncoding(this._encoding)
90-
91-
// resolve-write streams
92-
this._stdoutResolveWs = beginReady.setupResolveWriteStreamPipe(this._process.stdout)
93-
this._stderrResolveWs = beginReady.setupResolveWriteStreamPipe(this._process.stderr)
94-
95-
// handle errors so that Node does not crash
96-
this._stdoutResolveWs.on('error', console.error) // eslint-disable-line no-console
97-
this._stderrResolveWs.on('error', console.error) // eslint-disable-line no-console
98-
99-
// debug
100-
// exiftoolProcess.stdout.pipe(process.stdout)
101-
// exiftoolProcess.stderr.pipe(process.stderr)
102-
103-
this._open = true
104-
105-
return exiftoolProcess.pid
106-
})
82+
83+
// if process was spawned, stderr is readable (see lib/spawn)
84+
85+
this._process.stdout.setEncoding(this._encoding)
86+
this._process.stderr.setEncoding(this._encoding)
87+
88+
// resolve-write streams
89+
this._stdoutResolveWs = beginReady.setupResolveWriteStreamPipe(this._process.stdout)
90+
this._stderrResolveWs = beginReady.setupResolveWriteStreamPipe(this._process.stderr)
91+
92+
// handle errors so that Node does not crash
93+
this._stdoutResolveWs.on('error', console.error) // eslint-disable-line no-console
94+
this._stderrResolveWs.on('error', console.error) // eslint-disable-line no-console
95+
96+
// debug
97+
// exiftoolProcess.stdout.pipe(process.stdout)
98+
// exiftoolProcess.stderr.pipe(process.stderr)
99+
100+
this._open = true
101+
102+
return exiftoolProcess.pid
107103
}
108104

109105
_exitListener() {
@@ -120,13 +116,13 @@ class ExiftoolProcess extends EventEmitter {
120116
return this._open
121117
}
122118

123-
_executeCommand(command, args, argsNoSplit, debug) {
119+
async _executeCommand(command, args, argsNoSplit, debug) {
124120
//test this!
125121
if (!this._open) {
126-
return Promise.reject(new Error('exiftool is not open'))
122+
throw new Error('exiftool is not open')
127123
}
128124
if (this._process.signalCode === 'SIGTERM') {
129-
return Promise.reject(new Error('Could not connect to the exiftool process'))
125+
throw new Error('Could not connect to the exiftool process')
130126
}
131127

132128
const proc = debug === true ? process : this._process
@@ -159,12 +155,12 @@ class ExiftoolProcess extends EventEmitter {
159155
* @returns {Promise.<{{data, error}}>} A promise to write metadata,
160156
* resolved with data from stdout and stderr.
161157
*/
162-
writeMetadata(file, data, args, debug) {
158+
async writeMetadata(file, data, args, debug) {
163159
if (!lib.isString(file)) {
164160
throw new Error('File must be a string')
165161
}
166162
if (!lib.checkDataObject(data)) {
167-
return Promise.reject(new Error('Data argument is not an object'))
163+
throw new Error('Data argument is not an object')
168164
}
169165

170166
const writeArgs = lib.mapDataToTagArray(data)

src/lib.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict'
2-
31
const cp = require('child_process')
4-
const EOL = require('os').EOL
2+
const { EOL } = require('os')
53
const isStream = require('is-stream')
4+
const erotic = require('erotic')
65

76
function writeStdIn(proc, data, encoding) {
87
// console.log('write stdin', data)

test/fixtures/detached.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
const ExiftoolContext = require('exiftool-context')
2-
const exiftool = require('../../.')
3-
4-
const bin = ExiftoolContext.exiftoolBin
5-
const ep = new exiftool.ExiftoolProcess(bin)
1+
const { exiftoolBin: bin } = require('exiftool-context')
2+
const exiftool = require('../../src/')
63

74
if (typeof process.send !== 'function') {
85
throw new Error('This module should be spawned with an IPC channel.')
96
}
107

11-
const EXIFTOOL_DETACHED = process.env.EXIFTOOL_DETACHED === 'true'
8+
const { EXIFTOOL_DETACHED } = process.env
9+
10+
const detached = EXIFTOOL_DETACHED === 'true';
1211

13-
ep
14-
.open({ detached: EXIFTOOL_DETACHED })
15-
.then((pid) => {
12+
(async () => {
13+
try {
14+
const ep = new exiftool.ExiftoolProcess(bin)
15+
const pid = await ep.open({ detached })
1616
process.send(pid)
17-
})
18-
.catch((err) => {
17+
} catch (err) {
1918
console.log(err) // eslint-disable-line no-console
2019
process.exit(1)
21-
})
20+
}
21+
})()

test/spec/begin-ready.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const assert = require('assert')
2-
const Readable = require('stream').Readable
3-
const Writable = require('stream').Writable
4-
const Transform = require('stream').Transform
5-
const beginReady = require('../../src/begin-ready')
6-
const createBeginReadyMatchTransformStream = beginReady.createBeginReadyMatchTransformStream
7-
const createResolverWriteStream = beginReady.createResolverWriteStream
8-
const setupResolveWriteStreamPipe = beginReady.setupResolveWriteStreamPipe
2+
const { Readable, Writable } = require('stream')
3+
const {
4+
createBeginReadyMatchTransformStream,
5+
createResolverWriteStream,
6+
setupResolveWriteStreamPipe,
7+
} = require('../../src/begin-ready')
98

109
/**
1110
* Pipe Readable stream in object mode into process.stdout,
@@ -14,16 +13,16 @@ const setupResolveWriteStreamPipe = beginReady.setupResolveWriteStreamPipe
1413
* gets assigned a lot of stream listeners such as end, drain,
1514
* error, finish, unpipe, close.
1615
*/
17-
function debugObjectReadStream(rs, name) {
18-
rs.pipe(new Transform({
19-
objectMode: true,
20-
transform: (chunk, enc, next) => {
21-
const s = JSON.stringify(chunk, null, 2)
22-
console.log(`Some data from ${name} rs: `)
23-
next(null, `${s}\r\n`)
24-
},
25-
})).pipe(process.stdout)
26-
}
16+
// function debugObjectReadStream(rs, name) {
17+
// rs.pipe(new Transform({
18+
// objectMode: true,
19+
// transform: (chunk, enc, next) => {
20+
// const s = JSON.stringify(chunk, null, 2)
21+
// console.log(`Some data from ${name} rs: `)
22+
// next(null, `${s}\r\n`)
23+
// },
24+
// })).pipe(process.stdout)
25+
// }
2726

2827
const commandNumber = '376080'
2928
const commandNumber2 = '65754'
@@ -111,8 +110,7 @@ const brtsTestSuite = {
111110
})
112111
.then((res) => {
113112
assert.equal(res.length, 2)
114-
const output = res[0]
115-
const output2 = res[1]
113+
const [output, output2] = res
116114
assert.equal(output.cn, commandNumber)
117115
assert.equal(output.d, data)
118116
assert.equal(output2.cn, commandNumber2)

test/spec/codedcharacterset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const assert = require('assert')
2-
const EOL = require('os').EOL
2+
const { EOL } = require('os')
33
const context = require('exiftool-context')
44
const exiftool = require('../../src/')
55
context.globalExiftoolConstructor = exiftool.ExiftoolProcess

test/spec/detached-true.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
const makepromise = require('makepromise')
42
const ps = require('ps-node')
53
const assert = require('assert')
@@ -86,11 +84,11 @@ const findExiftoolChildAndConhost = (epPid, exiftoolDetached) => {
8684
if (!exiftoolDetached) return res
8785
// if detached, conhost is child of child exiftool,
8886
// which we are now finding
89-
const childExiftool = res[0]
87+
const [childExiftool] = res
9088
assert(childExiftool)
9189
assert(/exiftool\.exe/.test(childExiftool.command))
9290
return checkPpid(childExiftool.pid).then((conhostRes) => {
93-
const conhost = conhostRes[0]
91+
const [conhost] = conhostRes
9492
assert(conhost)
9593
assert(/conhost.exe/.test(conhost.command))
9694
const all = [].concat(conhostRes, res)
@@ -119,10 +117,7 @@ const setup = (exiftoolDetached, ctx) => {
119117
let checkPids
120118

121119
return ctx.forkNode(exiftoolDetached)
122-
.then((meta) => {
123-
const forkPid = meta.forkPid
124-
const epPid = meta.epPid
125-
120+
.then(({ forkPid, epPid }) => {
126121
const res = { forkPid, epPid }
127122

128123
if (isWindows) {

test/spec/exiftool.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const os = require('os')
1+
const { EOL } = require('os')
22
const assert = require('assert')
33
const child_process = require('child_process')
44
const context = require('exiftool-context')
55
const exiftool = require('../../src/')
66
context.globalExiftoolConstructor = exiftool.ExiftoolProcess
77

8-
const ChildProcess = child_process.ChildProcess
9-
const EOL = os.EOL
8+
const { ChildProcess } = child_process
109

1110
const exiftoolTestSuite = {
1211
context,
@@ -163,7 +162,7 @@ const exiftoolTestSuite = {
163162
.then((res) => {
164163
assert.equal(res.error, null)
165164
assert(Array.isArray(res.data))
166-
const metadata = res.data[0]
165+
const { data: [metadata] } = res
167166
const expected = {
168167
SourceFile: ctx.replaceSlashes(ctx.jpegFile),
169168
Directory: ctx.replaceSlashes(ctx.folder),
@@ -265,13 +264,13 @@ const exiftoolTestSuite = {
265264
.then((res) => {
266265
assert(Array.isArray(res.data))
267266
assert.equal(res.error, null)
268-
const meta = res.data[0]
269-
assert.equal(meta.Keywords.length, keywords.length)
270-
meta.Keywords.forEach((keyword, index) => {
267+
const { data: [metadata] } = res
268+
assert.equal(metadata.Keywords.length, keywords.length)
269+
metadata.Keywords.forEach((keyword, index) => {
271270
assert.equal(keyword, keywords[index])
272271
})
273-
assert.equal(meta.Comment, comment)
274-
assert.equal(meta.Scene, undefined) // should be removed with -all=
272+
assert.equal(metadata.Comment, comment)
273+
assert.equal(metadata.Scene, undefined) // should be removed with -all=
275274
})
276275
},
277276
},

0 commit comments

Comments
 (0)