asset-assistant
Advanced tools
Comparing version 0.0.7 to 0.0.8
{ | ||
"name": "asset-assistant", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"keywords": [ | ||
@@ -28,3 +28,9 @@ "util", | ||
"main": "src/LoadAssets.js", | ||
"files": "src/*" | ||
"files": "src/*", | ||
"scripts": { | ||
"test": "ava --verbose" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.25.0" | ||
} | ||
} |
@@ -1,10 +0,11 @@ | ||
const jsdom = require("jsdom"); | ||
const jsdom = require('jsdom'); | ||
const { JSDOM } = jsdom; | ||
const fs = require("fs"); | ||
const fs = require('fs'); | ||
const relative = require('relative'); | ||
const glob = require('glob'); | ||
class InjectHeader{ | ||
constructor(filename){ | ||
this.filename = filename; | ||
let src = fs.readFileSync(filename, 'utf8'); | ||
constructor(htmlFilename){ | ||
this.htmlFilename = htmlFilename; | ||
let src = fs.readFileSync(htmlFilename, 'utf8'); | ||
this.dom = new JSDOM(src); | ||
@@ -19,2 +20,6 @@ this.document = this.dom.window.document; | ||
setSilent(){ | ||
this.silent = true; | ||
} | ||
logDebug(string){ | ||
@@ -24,24 +29,44 @@ if (this.debug) console.log(string); | ||
hasCSS(cssFilename){ | ||
let filename = relative(this.filename, cssFilename).replace("\\", "/"); | ||
let query = this.document.querySelectorAll(`link[href="${filename}"]`); | ||
this.logDebug(`hasCSS ${cssFilename} as ${filename}: ${query.length !== 0}`); | ||
log(string){ | ||
if (!this.silent) console.log(string); | ||
} | ||
/** | ||
* Return true if the target file contains this .css style directive. | ||
* @param {type} filename | ||
* @returns {Boolean} | ||
*/ | ||
hasCSS(filename){ | ||
let queryString = `link[href="${filename}"]`; | ||
this.logDebug(queryString); | ||
let query = this.document.querySelectorAll(queryString); | ||
this.logDebug(`hasCSS ${filename}: ${query.length !== 0}`); | ||
return query.length !== 0; | ||
} | ||
hasJS(jsFilename){ | ||
let filename = relative(this.filename, jsFilename).replace("\\", "/"); | ||
let query = this.document.querySelectorAll(`script[src="${filename}"]`); | ||
this.logDebug(`hasJS ${jsFilename} as ${filename}: ${query.length !== 0}`); | ||
/** | ||
* Return true if the target file contins this .js script directive. | ||
* @param {type} filename | ||
* @returns {Boolean} | ||
*/ | ||
hasJS(filename){ | ||
let queryString = `script[src="${filename}"]`; | ||
this.logDebug(queryString); | ||
let query = this.document.querySelectorAll(queryString); | ||
this.logDebug(`hasJS ${filename} : ${query.length !== 0}`); | ||
return query.length !== 0; | ||
} | ||
injectJS(jsFilename){ | ||
let filename = relative(this.filename, jsFilename).replace("\\", "/"); | ||
/** | ||
* Inject a .js file into the target. | ||
* @param {type} filename | ||
* @returns {Boolean} | ||
*/ | ||
injectJS(filename){ | ||
this.logDebug(`injectJS ${filename}`); | ||
let tab = this.document.createTextNode("\t"); | ||
this.document.head.appendChild(tab); | ||
let ele = this.document.createElement("script"); | ||
let newline = this.document.createTextNode("\n"); | ||
let ele = this.document.createElement("script"); | ||
ele.setAttribute("src", filename); | ||
@@ -51,17 +76,22 @@ ele.setAttribute("type", "text/javascript"); | ||
ele.setAttribute("data-injected", "true"); | ||
this.document.head.appendChild(tab); | ||
this.document.head.appendChild(ele); | ||
let nl = this.document.createTextNode("\n"); | ||
this.document.head.appendChild(nl); | ||
this.document.head.appendChild(newline); | ||
return true; | ||
} | ||
injectCSS(cssFilename){ | ||
let filename = relative(this.filename, cssFilename).replace("\\", "/"); | ||
/** | ||
* Inject a .css file into the target. | ||
* @param {type} filename | ||
* @returns {Boolean} | ||
*/ | ||
injectCSS(filename){ | ||
this.logDebug(`injectCSS ${filename}`); | ||
let tab = this.document.createTextNode("\t"); | ||
this.document.head.appendChild(tab); | ||
let ele = this.document.createElement("link"); | ||
let newline = this.document.createTextNode("\n"); | ||
let ele = this.document.createElement("link"); | ||
ele.setAttribute("href", filename); | ||
@@ -71,6 +101,7 @@ ele.setAttribute("rel", "stylesheet"); | ||
ele.setAttribute("data-injected", "true"); | ||
this.document.head.appendChild(tab); | ||
this.document.head.appendChild(ele); | ||
this.document.head.appendChild(newline); | ||
let nl = this.document.createTextNode("\n"); | ||
this.document.head.appendChild(nl); | ||
return true; | ||
@@ -86,4 +117,33 @@ } | ||
} | ||
injectPath(path){ | ||
let injected = []; | ||
let filenames = glob.sync(path, {nodir: true}); | ||
if (filenames.length === 0){ | ||
this.log(`*Warning* path '${path}' empty`); | ||
} | ||
for (let filename of filenames) { | ||
filename = relative(this.htmlFilename, filename).replace("\\", "/"); | ||
if (filename.endsWith(".js")){ | ||
if (!this.hasJS(filename)){ | ||
this.logDebug(`inject ${filename}`); | ||
this.injectJS(filename); | ||
injected.push(filename); | ||
} | ||
} | ||
else if (filename.endsWith(".css")){ | ||
if (!this.hasCSS(filename)){ | ||
this.logDebug(`inject ${filename}`); | ||
this.injectCSS(filename); | ||
injected.push(filename); | ||
} | ||
} | ||
} | ||
return injected; | ||
} | ||
} | ||
module.exports = InjectHeader; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30822
21
584
1