-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from license2e/add-merge
Add $merge feature
- Loading branch information
Showing
12 changed files
with
225 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const Promise = require('bluebird'); | ||
const fs = require('fs'); | ||
|
||
|
||
function extract(filePath, parser) { | ||
return new Promise((resolve, reject) => { | ||
if (!filePath || filePath === undefined) { | ||
return reject('Requires a file path to process.'); | ||
} | ||
let fileData = ''; | ||
let data = {}; | ||
try { | ||
fileData = fs.readFileSync(filePath, 'utf-8'); | ||
if (fileData === '') { | ||
throw new Error('Empty file, nothing to process.'); | ||
} | ||
data = parser(fileData); | ||
} catch (err) { | ||
return reject(err.message); | ||
} | ||
return resolve(JSON.stringify(data)); | ||
}); | ||
} | ||
|
||
module.exports = extract; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const Promise = require('bluebird'); | ||
const path = require('path'); | ||
|
||
function transform(dataStr, key, filePath, process) { | ||
return new Promise((resolve, reject) => { | ||
let dataString = dataStr; | ||
const refMatches = dataString.match(/{"\$ref":"(.*?)"}/g); | ||
const baseDir = path.dirname(filePath); | ||
if (refMatches && refMatches.length > 0) { | ||
const refFileList = []; | ||
refMatches.forEach((matchKey) => { | ||
const refFile = matchKey.match(/{"\$ref":"(.*)"}/)[1]; | ||
const refFilePath = path.resolve(`${baseDir}/${refFile}`); | ||
refFileList.push(process(refFilePath, matchKey)); | ||
}); | ||
return Promise.all(refFileList) | ||
.then((results) => { | ||
const mergeMatches = dataString.match(/{"\$merge":\[(.*?)\]}/g); | ||
if (mergeMatches && mergeMatches.length > 0) { | ||
mergeMatches.forEach((mergeMatchKey) => { | ||
const localMatches = []; | ||
const localRefMatches = mergeMatchKey.match(/{"\$ref":"(.*?)"}/g); | ||
if (!localRefMatches || localRefMatches.length === 0) { | ||
reject('Malformed merge setting, please check the input file.'); | ||
return; | ||
} | ||
localRefMatches.forEach((refMatchKey) => { | ||
const j = results.length; | ||
for (let i = 0; i < j; i += 1) { | ||
const result = results[i]; | ||
if (result.key === refMatchKey) { | ||
const end = (result.dataString.length - 2); | ||
localMatches.push(result.dataString.substr(1, end)); | ||
break; | ||
} | ||
} | ||
}); | ||
const localMatchesStr = `{${localMatches.join(',')}}`; | ||
dataString = dataString.replace(mergeMatchKey, localMatchesStr); | ||
return; | ||
}); | ||
} else { | ||
results.forEach((result) => { | ||
dataString = dataString.replace(result.key, result.dataString); | ||
}); | ||
} | ||
return resolve({ | ||
dataString, | ||
key, | ||
}); | ||
}); | ||
} | ||
const mergeMatches = dataString.match(/{"\$merge":\[(.*?)\]}/g); | ||
if (mergeMatches && mergeMatches.length > 0) { | ||
return reject('Malformed merge setting, please check the input file.'); | ||
} | ||
return resolve({ | ||
dataString, | ||
key, | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = transform; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[test] | ||
$merge[]=$ref:./file.yaml | ||
$merge[]=$ref:./file-refs.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test: | ||
$merge: | ||
- $ref: ./file.yaml | ||
- $ref: ./file-refs.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.