This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
genmanifest.js
133 lines (122 loc) · 3.71 KB
/
genmanifest.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
const path = require('path')
const fs = require('fs')
const walk = require('walk')
function main () {
const projRoot = path.resolve(__dirname)
const inputRoot = getInputDirPath()
let ramlPaths = listRamls(inputRoot)
ramlPaths = sortRAMLPaths(inputRoot, ramlPaths)
ramlPaths = makePathsRelative(projRoot, ramlPaths)
generateManifest(projRoot, ramlPaths)
}
// Gets absolute path of input folder
function getInputDirPath () {
let dirPath = process.argv[2]
if (!fs.existsSync(dirPath)) {
console.error(`'${dirPath}' not found`)
return
}
dirPath = path.resolve(dirPath)
if (!fs.lstatSync(dirPath).isDirectory()) {
console.error(`'${dirPath}' is not a directory`)
return
}
return dirPath
}
// Lists RAML files under directory path
function listRamls (dirPath) {
let files = []
const options = {
listeners: {
file: (root, fileStats, next) => {
if (fileStats.name.indexOf('.raml') >= 0) {
files.push(path.join(root, fileStats.name))
}
next()
}
}
}
walk.walkSync(dirPath, options)
return files
}
// Sorts string ramlPaths according to features definition order
// in RAML 1.0 spec
function sortRAMLPaths (ramlsRoot, ramlPaths) {
const pathObjs = extendWithPriority(ramlsRoot, ramlPaths)
const sortedPathObjs = pathObjs.sort((obj1, obj2) => {
return obj1.priority - obj2.priority
})
return sortedPathObjs.map((obj) => {
return obj.path
})
}
// Makes RAML fiels paths relative to a directory
function makePathsRelative (dirPath, ramlPaths) {
return ramlPaths.map((pth) => {
return path.relative(dirPath, pth)
})
}
// Turns plain RAML paths into objects of type {priority: INT, path: STRING}
// E.g.:
// > let featuresPriority = ['methodresponses', 'overlays']
// > extendWithPriority('/foo/bar', '/foo/bar/Overlays/some/file.raml')
// > {priority: 2, path: '/foo/bar/Overlays/some/file.raml'}
// > extendWithPriority('/foo/bar', '/foo/bar/qweqwe/some/file.raml')
// > {priority: 3, path: '/foo/bar/qweqwe/some/file.raml'}
//
// Override this to change logic of picking priority.
function extendWithPriority (ramlsRoot, ramlPaths) {
// Features listed in order they appear in RAML 1.0 spec.
let featuresPriority = [
'root',
'types',
'resources',
'methods',
'responses',
'methodresponses',
'resourcetypes',
'traits',
'templatefunctions',
'securityschemes',
'annotations',
'fragments',
'libraries',
'overlays'
]
return ramlPaths.map((pth) => {
const piece = getFirstPathPiece(ramlsRoot, pth)
let priority = featuresPriority.findIndex((el) => {
return el === piece
})
// Feature name not found. Adding it to the list allows sorting not
// found features.
if (priority === -1) {
featuresPriority.push(piece)
priority = featuresPriority.length - 1
}
priority += 1 // Make 1-based
return {path: pth, priority: priority}
})
}
// Gets relative folder 'root' name of RAML file path.
// E.g.:
// > const ramlsRoot = '/foo/bar'
// > const ramlPath = '/foo/bar/MethodResponses/some/file.raml'
// > getFirstPathPiece(ramlsRoot, ramlPath)
// > 'methodresponses'
function getFirstPathPiece (ramlsRoot, ramlPath) {
const relPath = path.relative(ramlsRoot, ramlPath)
return relPath.split(path.sep)[0].toLowerCase()
}
// Generates and writes manifest file
function generateManifest (dirPath, ramlPaths) {
const data = {
description: 'RAML files listed in order corresponding RAML ' +
'feature appears in RAML 1.0 spec',
filePaths: ramlPaths
}
const manifestPath = path.join(dirPath, 'manifest.json')
console.log(`Writing manifest file to ${manifestPath}`)
fs.writeFileSync(manifestPath, JSON.stringify(data, null, 4))
}
main()