forked from tracespace/tracespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (146 loc) · 4.38 KB
/
index.js
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
'use strict'
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const runParallel = require('run-parallel')
const runWaterfall = require('run-waterfall')
const server = require('./server')
const gerberFilenames = require('./gerber-filenames.json')
const GLOB_BOARD_MANIFEST = path.join(__dirname, 'boards/**/manifest.json')
const GLOB_SPEC_GERBER = path.join(__dirname, 'gerbers/**/*.@(gbr|drl|svg)')
module.exports = {
gerberFilenames,
getBoards,
getGerberSpecs,
server,
}
function getBoards(done) {
runWaterfall(
[
next => glob(GLOB_BOARD_MANIFEST, next),
(manifestPaths, next) =>
runParallel(
manifestPaths.map(manifest => next => readManifest(manifest, next)),
next
),
],
done
)
}
getBoards.sync = function getBoardsSync() {
const manifests = glob.sync(GLOB_BOARD_MANIFEST)
return manifests.map(manifest => readManifest.sync(manifest))
}
function getGerberSpecs(done) {
runWaterfall(
[
next => glob(GLOB_SPEC_GERBER, next),
(gerberPaths, next) =>
runParallel(
gerberPaths.map(gerber => next => readFile(gerber, {}, next)),
next
),
(gerberSpecs, next) => next(null, collectSpecs(gerberSpecs)),
],
done
)
}
getGerberSpecs.sync = function() {
const paths = glob.sync(GLOB_SPEC_GERBER)
const specs = paths.map(filepath => readFile.sync(filepath, {}))
return collectSpecs(specs)
}
function readManifest(manifestPath, done) {
const name = path.basename(path.dirname(manifestPath))
runWaterfall(
[next => readFile(manifestPath, {name}, next), addLayersToManifest],
done
)
}
readManifest.sync = function readManifestSync(manifestPath) {
const name = path.basename(path.dirname(manifestPath))
const manifest = readFile.sync(manifestPath, {name})
return addLayersToManifest.sync(manifest)
}
function getLayerFilepath(manifest, layer) {
return path.join(path.dirname(manifest.filepath), layer.name)
}
function addLayersToManifest(manifest, done) {
runWaterfall(
[
next =>
runParallel(
manifest.layers.map(layer => next =>
readFile(getLayerFilepath(manifest, layer), layer, next)
),
next
),
(layers, next) => next(null, Object.assign(manifest, {layers})),
],
done
)
}
addLayersToManifest.sync = function addLayersToManifestSync(manifest) {
const layers = manifest.layers.map(layer =>
readFile.sync(getLayerFilepath(manifest, layer), layer)
)
return Object.assign(manifest, {layers})
}
function readFile(filepath, props, done) {
runWaterfall(
[
next => fs.readFile(filepath, 'utf8', next),
(source, next) => next(null, makeFileResult(filepath, props, source)),
],
done
)
}
readFile.sync = function readFileSync(filepath, props) {
const source = fs.readFileSync(filepath, 'utf8')
return makeFileResult(filepath, props, source)
}
function makeFileResult(filepath, props, source) {
const dirname = path.dirname(filepath)
const category = path.basename(dirname)
const extname = path.extname(filepath)
const filename = path.basename(filepath)
const name = path.basename(filepath, extname)
const file = {category, filepath, filename, name}
const result =
extname.toLowerCase() === '.json' ? JSON.parse(source) : {source}
return Object.assign(file, result, props)
}
function collectSpecs(gerberSpecs) {
const {suites, suitesByName} = gerberSpecs.reduce(
(result, spec) => {
const {category, name} = spec
if (path.extname(spec.filepath).toLowerCase() === '.svg') {
spec = {expected: spec.source}
}
if (!result.suitesByName[category]) {
result.suites.push(category)
result.suitesByName[category] = {
name: category,
specs: [name],
specsByName: {[name]: spec},
}
} else if (result.suitesByName[category].specs.indexOf(name) < 0) {
result.suitesByName[category].specs.push(name)
result.suitesByName[category].specsByName[name] = spec
} else {
result.specsByName = Object.assign(
result.suitesByName[category].specsByName[name],
spec
)
}
return result
},
{suites: [], suitesByName: {}}
)
return suites.map(name => ({
name,
specs: suitesByName[name].specs.map(
specName => suitesByName[name].specsByName[specName]
),
}))
}