-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
221 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {NODE_ATTRIBUTES, NODE_TEXT} from './nodes'; | ||
|
||
/** | ||
* @param {Object} object | ||
* @param {string[]} nodeNames | ||
* @param {string[]} ignoreNodes | ||
* @returns {{name: string, item: {}[]}[]} | ||
*/ | ||
export function filterNodes( | ||
object, | ||
nodeNames, | ||
ignoreNodes = [NODE_ATTRIBUTES, NODE_TEXT] | ||
) { | ||
const stack = [object]; | ||
const found = []; | ||
|
||
function isNotIgnored(key) { | ||
return ignoreNodes.includes(key) === false; | ||
} | ||
|
||
while (stack.length) { | ||
const item = stack.shift(); | ||
|
||
const keys = Object.keys(item).filter(isNotIgnored); | ||
|
||
if (!keys.length) { | ||
continue; | ||
} | ||
|
||
for (let i = 0; i < keys.length; i++) { | ||
const nodeName = keys[i]; | ||
const {[nodeName]: nextItem} = item; | ||
|
||
if (nodeNames.includes(nodeName)) { | ||
found.push({ | ||
name: nodeName, | ||
item: nextItem | ||
}); | ||
|
||
continue; | ||
} | ||
|
||
if (Array.isArray(nextItem)) { | ||
stack.push(...nextItem); | ||
|
||
continue; | ||
} | ||
|
||
if (typeof nextItem !== 'object') { | ||
continue; | ||
} | ||
|
||
stack.push(nextItem); | ||
} | ||
} | ||
|
||
return found; | ||
} |
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,90 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import {parseStringPromise} from 'xml2js'; | ||
import {NODE_ATTRIBUTES} from './nodes'; | ||
import {filterNodes} from './filter-nodes'; | ||
|
||
const readFile = util.promisify(fs.readFile); | ||
|
||
/** | ||
* @param {{attribute: {}[]}} node | ||
* @return {boolean} | ||
*/ | ||
function hasAttributes({item: [attributes]}) { | ||
return attributes && NODE_ATTRIBUTES in attributes; | ||
} | ||
|
||
function getSrcAttribute({item: [attributes]}) { | ||
return attributes[NODE_ATTRIBUTES].src; | ||
} | ||
|
||
/** | ||
* @param {string} source | ||
* @return {Promise<Set<string>>} | ||
*/ | ||
async function getFileImports(source) { | ||
const root = await parseStringPromise(source); | ||
const nodes = filterNodes(root, [ | ||
'fest:include', | ||
'fest:insert' | ||
]); | ||
|
||
return new Set( | ||
nodes.filter(hasAttributes).map(getSrcAttribute) | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} parentFile | ||
* @param {string} importedPath | ||
* @return {string} | ||
*/ | ||
function getAbsolutePath(parentFile, importedPath) { | ||
return path.resolve(path.dirname(parentFile), importedPath); | ||
} | ||
|
||
async function getAbsoluteFileImports(resourcePath, source) { | ||
const toAbsolutePath = getAbsolutePath.bind(null, resourcePath); | ||
const fileImports = await getFileImports(source); | ||
|
||
return [...fileImports].map(toAbsolutePath); | ||
} | ||
|
||
export async function getDependencies(resourcePath, source) { | ||
const dependencies = new Set(); | ||
let stack = [ | ||
[resourcePath, source] | ||
]; | ||
|
||
do { | ||
const [resourcePath, source] = stack.shift(); | ||
|
||
let absoluteFileImports = await getAbsoluteFileImports( | ||
resourcePath, | ||
source | ||
); | ||
absoluteFileImports = absoluteFileImports.filter((fileImport) => ( | ||
!dependencies.has(fileImport) | ||
)); | ||
|
||
for (const fileImport of absoluteFileImports) { | ||
dependencies.add(fileImport); | ||
|
||
if (!fileImport.endsWith('.xml')) { | ||
continue; | ||
} | ||
|
||
let source; | ||
try { | ||
source = await readFile(fileImport, 'utf8'); | ||
} catch (e) { | ||
continue; | ||
} | ||
|
||
stack.push([fileImport, source]); | ||
} | ||
} while (stack.length); | ||
|
||
return dependencies; | ||
} |
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,2 @@ | ||
export const NODE_ATTRIBUTES = '$'; | ||
export const NODE_TEXT = '_'; |
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 @@ | ||
<fest:template xmlns:fest="http://fest.mail.ru" context_name="json"> | ||
Simple include | ||
</fest:template> |
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 @@ | ||
<fest:template xmlns:fest="http://fest.mail.ru" context_name="json"> | ||
<fest:include context="json.list" src="./include.xml"/> | ||
</fest:template> |
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 @@ | ||
.foo { | ||
color: red; | ||
} |
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 @@ | ||
<fest:template xmlns:fest="http://fest.mail.ru" context_name="json"> | ||
<fest:include context="json.list" src="./nested-dependency.xml"/> | ||
<div><fest:insert src="./styles.css"/></div> | ||
</fest:template> |
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