This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeToFileConverter.ts
191 lines (177 loc) · 6.05 KB
/
TreeToFileConverter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { ConverterOptionsInterface } from '../options/ConverterOptionsInterface'
import { RootNodeInterface, SockoNodeInterface, SockoNodeType } from 'socko-api'
import * as fs from 'fs'
import * as path from 'path'
import { getLogger, Logger } from 'loglevel'
import { createHash } from 'crypto'
import { mkdirs } from 'fs-extra'
import Bluebird = require('bluebird')
/**
* A converter to convert a socko-api SockoNodeInterface tree into a file tree
*/
export class TreeToFileConverter {
private _options: ConverterOptionsInterface
private _log: Logger
/**
* Build the converter with the given options
* @param {ConverterOptionsInterface} options the converter options to use
*/
constructor (options: ConverterOptionsInterface) {
this._options = options
this._log = getLogger('socko-converter-file:converter:TreeToFileConverter')
}
/**
* Dump the given socko-api RootNodeInterface as a file tree
* @param {RootNodeInterface} root
* @return {Bluebird<void>}
*/
public convert (root: RootNodeInterface): Bluebird<void> {
return this._convert(root)
}
/**
* The recursive converter method
* @param {SockoNodeInterface} node the current node to convert
* @return {Bluebird<void>}
* @private
*/
private _convert (node: SockoNodeInterface): Bluebird<void> {
let pathResolver
this._log.debug('Fetching path to work on')
if (node.type === SockoNodeType.Root) {
pathResolver = Bluebird.resolve(this._options.outputPath)
} else {
pathResolver = node.getPathNodes()
.then(
pathComponents => {
let pathNames: Array<string> = pathComponents.map(component => component.name)
pathNames.shift()
pathNames.unshift(this._options.outputPath)
return Bluebird.resolve(path.join.apply(null, pathNames))
}
)
}
return pathResolver
.then(
nodePath => {
this._log.debug(`Working at path ${nodePath}`)
if (node.type === SockoNodeType.Output) {
this._log.debug('This is a content node')
return this._processFile(node, nodePath)
} else {
this._log.debug('Creating path')
return this._processDirectory(node, nodePath)
}
}
)
}
/**
* Convert a socko-api BranchNodeInterface into a directory
* @param {SockoNodeInterface} node current BranchNodeInterface node to process
* @param {string} nodePath output path to use
* @return {Bluebird<void>}
* @private
*/
private _processDirectory (node: SockoNodeInterface, nodePath: string): Bluebird<void> {
return Bluebird.fromCallback(mkdirs.bind(null, nodePath))
.catch(
(error: any) => {
return error.code === 'EEXIST'
},
() => {
this._log.debug('Path already exists. Ignoring that.')
return Bluebird.resolve()
}
)
.then(
() => {
this._log.debug('Scanning children...')
return Bluebird.reduce<SockoNodeInterface, void>(
node.getChildren() as Array<SockoNodeInterface>,
(total, child) => {
return this._convert(child)
},
null
)
}
)
.thenReturn()
}
/**
* Convert a socko-api OutputNodeInterface into a file
*
* @param {SockoNodeInterface} node node to process
* @param {string} nodePath output path to use
* @return {Bluebird<void>}
* @private
*/
private _processFile (node: SockoNodeInterface, nodePath: string): Bluebird<void> {
return node.readContent()
.then(
content => {
let options = {}
if (typeof content === 'string') {
this._log.debug(`The retrieved content is a string. Using encoding ${this._options.outputEncoding}`)
options = {
encoding: this._options.outputEncoding
}
}
let writeFileCommand = Bluebird.fromCallback(
fs.writeFile.bind(
null,
nodePath,
content,
options
)
)
if (this._options.checkBeforeOverwrite) {
this._log.debug('Checking, if new content differs from existing content')
return Bluebird.fromCallback(
fs.stat.bind(null, nodePath)
)
.catch(
(error: any) => {
return error.code === 'ENOENT'
},
() => {
this._log.debug('File does not already exist. Writing file')
return writeFileCommand
}
)
.then(
() => {
this._log.debug('Hashing new content')
let newHash = createHash(this._options.hashingAlgorithm)
newHash.update(content)
this._log.debug('Reading original content')
let readOptions = {}
if (typeof content === 'string') {
readOptions = {
encoding: this._options.inputEncoding
}
}
return Bluebird.fromCallback(
fs.readFile.bind(null, nodePath, readOptions)
)
.then(
originalContent => {
this._log.debug('Hashing original content')
let originalHash = createHash(this._options.hashingAlgorithm)
originalHash.update(originalContent)
if (originalHash.digest() !== newHash.digest()) {
this._log.debug('Content differs. Write new file.')
return writeFileCommand
} else {
this._log.debug('Content matches. Skipping file.')
return Bluebird.resolve()
}
}
)
}
)
} else {
return writeFileCommand
}
}
)
}
}