@tadaima/sekkei
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -8,35 +8,52 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var read = require('fs-readdir-recursive'); | ||
var mkdirp = require('mkdirp'); | ||
// const del = require('del'); | ||
var Document = function () { | ||
function Document(path) { | ||
_classCallCheck(this, Document); | ||
var Parser = function () { | ||
function Parser(location) { | ||
_classCallCheck(this, Parser); | ||
this._path = path; | ||
this._type = typeof this._path.match(/\.(\w+)$/)[1] === "string" ? this._path.match(/\.(\w+)$/)[1] : null; | ||
if (this._type === null) { | ||
console.error('Could not find the extension of the file at ' + path); | ||
return false; | ||
this._location = location; | ||
this._config = JSON.parse(fs.readFileSync(this._location + '/tadaima.json', 'utf8')); | ||
if (!this._config) { | ||
console.error('Error: A config was not given.'); | ||
return null; | ||
} | ||
if (!/(?:css|html)/.test(this._type)) { | ||
console.error(this._type + ' files are not supported by Sekkei. Only html and css files are supported.'); | ||
this._source = null; | ||
} else { | ||
this._source = fs.readFileSync(this._path, 'utf8'); | ||
} | ||
this.output = this._source; | ||
// Aliases | ||
this.save = { | ||
as: this.save.bind(this) | ||
this.a = { | ||
"variable": this.parseVariable.bind(this), | ||
"link": this.parseLink.bind(this), | ||
"file": this.parseFile.bind(this) | ||
}; | ||
this.all = { | ||
"variables": this.parseVariables.bind(this), | ||
"links": this.parseLinks.bind(this), | ||
"files": this.parseFiles.bind(this) | ||
}; | ||
} | ||
_createClass(Document, [{ | ||
key: 'save', | ||
value: function save() { | ||
var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._path; | ||
_createClass(Parser, [{ | ||
key: 'getFile', | ||
value: function getFile(filepath) { | ||
if (!filepath) { | ||
console.error('Error: No path was given.'); | ||
return false; | ||
} | ||
if (!fs.existsSync(this._location + '/src/' + filepath)) { | ||
console.error('Error: File does not exist.'); | ||
return false; | ||
} | ||
return fs.readFileSync(this._location + '/src/' + filepath, 'utf8'); | ||
} | ||
}, { | ||
key: 'saveFile', | ||
value: function saveFile(filepath, input) { | ||
if (!fs.existsSync(filepath)) { | ||
if (process.platform === "win32") mkdirp.sync(filepath.substring(0, filepath.lastIndexOf("\\")));else mkdirp.sync(filepath.substring(0, filepath.lastIndexOf("/"))); | ||
} | ||
try { | ||
fs.writeFileSync(path, this.output, 'utf8'); | ||
fs.writeFileSync(filepath, input, 'utf8'); | ||
} catch (err) { | ||
@@ -46,31 +63,125 @@ console.error('An error was encountered when trying to save the parsed file. Here\'s what we\'ve got: ' + err); | ||
} | ||
}]); | ||
}, { | ||
key: 'parseFile', | ||
value: function parseFile(input) { | ||
if (!input) { | ||
console.error('Error: No path or file was given.'); | ||
return false; | ||
} | ||
var type = path.extname(this._location + '/src/' + input) || null; | ||
if (!type || type != ".html" && type != ".css") { | ||
if (!type) console.error('Error: File could not be found. Skipping...');else console.error('Error: File type is not supported. Skipping...'); | ||
return false; | ||
} | ||
var file = typeof input === "string" ? this.getFile(input) : null; | ||
if (!file) { | ||
console.error('Error: Could not parse the file.'); | ||
return false; | ||
} | ||
file = this.all.variables(file); | ||
file = this.all.links(file); | ||
this.saveFile(this._location + '/out/' + input, file); | ||
return file; | ||
} | ||
}, { | ||
key: 'parseFiles', | ||
value: function parseFiles() { | ||
var cleanOutputDir = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
return Document; | ||
}(); | ||
// If flagged, clean the previous output from the directory | ||
// if (cleanOutputDir) del.sync(`${this._location}/out/*`); | ||
var Parser = function () { | ||
function Parser(docPath, config) { | ||
_classCallCheck(this, Parser); | ||
var files = this._config.files; | ||
var out = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
this._doc = new Document(docPath); | ||
this._config = config; | ||
if (this._doc._source === null) { | ||
console.error("The provided Document object is invalid. Check that it is either a HTML or CSS file."); | ||
this._doc = null; | ||
try { | ||
for (var _iterator = files[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var file = _step.value; | ||
// Get all files in current directory | ||
if (file === "*") { | ||
var subfiles = read(this._location + '/src'); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = subfiles[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var subfile = _step2.value; | ||
console.log(subfile); | ||
out.push(this.parseFile(subfile)); | ||
} | ||
// Get all files in sub directories | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} else if (/(?:(.+)\/*\*)$/.test(file)) { | ||
// Find path pre-asterisk | ||
var match = /(?:(.+)\/*\*)$/.exec(file); | ||
var _subfiles = match[1] ? read(this._location + '/src/' + match[1]) : null; | ||
if (_subfiles) { | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = _subfiles[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _subfile = _step3.value; | ||
out.push(this.parseFile(file.replace(/\*$/, _subfile))); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
out.push(this.parseFile(file)); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return out; | ||
} | ||
// Aliases | ||
this.a = { | ||
variable: this.parseVariable.bind(this), | ||
link: this.parseLink.bind(this) | ||
}; | ||
this.all = { | ||
variables: this.parseVariables.bind(this), | ||
links: this.parseLinks.bind(this) | ||
}; | ||
} | ||
_createClass(Parser, [{ | ||
}, { | ||
key: 'parseVariable', | ||
value: function parseVariable(key) { | ||
value: function parseVariable(key, input) { | ||
if (!key) { | ||
@@ -80,13 +191,13 @@ console.error("A key must be provided."); | ||
} | ||
if (this._doc._source === null) { | ||
console.error('Could not parse the variable "' + key + '" as the provided Document object is invalid.'); | ||
if (!input) { | ||
console.error('Parse Variable: An input must be provided.'); | ||
return false; | ||
} | ||
var re = new RegExp('{' + key + '}', 'g'); | ||
if (re.test(this._doc.output)) { | ||
if (re.test(input)) { | ||
var value = this._config.variables[key].value; | ||
this._doc.output = this._doc.output.replace(re, '' + value); | ||
return input.replace(re, '' + value); | ||
} else { | ||
console.error('A variable could not be found using the key ' + key + '. Skipping...'); | ||
return input; | ||
} | ||
@@ -96,3 +207,7 @@ } | ||
key: 'parseVariables', | ||
value: function parseVariables() { | ||
value: function parseVariables(input) { | ||
if (!input) { | ||
console.error('Parse Variables: An input must be provided.'); | ||
return false; | ||
} | ||
if (!this._config.variables) { | ||
@@ -104,8 +219,9 @@ console.error("Could not import variables from the provided config. Skipping..."); | ||
for (var variable in variables) { | ||
this.parseVariable(variable); | ||
input = this.parseVariable(variable, input); | ||
} | ||
return input; | ||
} | ||
}, { | ||
key: 'parseLink', | ||
value: function parseLink(key) { | ||
value: function parseLink(key, input) { | ||
if (!key) { | ||
@@ -115,9 +231,8 @@ console.error("A key must be provided."); | ||
} | ||
if (this._doc._source === null) { | ||
console.error('Could not parse the link "' + key + '" as the provided Document object is invalid.'); | ||
if (!input) { | ||
console.error('Parse Link: An input must be provided.'); | ||
return false; | ||
} | ||
var re = new RegExp('{link name:(' + key + ')(?:(?: class:(.+))|)}', "gmi"); | ||
if (re.test(this._doc.output)) { | ||
if (re.test(input)) { | ||
// Overwrite the RegExp object to prevent an issue that causes the first | ||
@@ -137,11 +252,11 @@ // match to be ignored. This seems to be do with the test in the above statement. | ||
var href = this._config.links[key].href; | ||
var match = void 0; | ||
while (match = re.exec(this._doc.output)) { | ||
while (match = re.exec(input)) { | ||
var classes = match[2] ? match[2].replace(/[,\s]/g, ' ') : ""; | ||
var _re = new RegExp('{link name:(' + key + ')(?:(?: class:(.+))|)}', "i"); | ||
this._doc.output = this._doc.output.replace(_re, '<a href=\'' + href + '\' class=\'' + classes + '\'>' + title + '</a>'); | ||
return input.replace(_re, '<a href=\'' + href + '\' class=\'' + classes + '\'>' + title + '</a>'); | ||
} | ||
} else { | ||
console.error('A link could not be found using the key ' + key + '. Skipping...'); | ||
return input; | ||
} | ||
@@ -151,3 +266,7 @@ } | ||
key: 'parseLinks', | ||
value: function parseLinks() { | ||
value: function parseLinks(input) { | ||
if (!input) { | ||
console.error('Parse Links: An input must be provided.'); | ||
return false; | ||
} | ||
if (!this._config.links) { | ||
@@ -159,4 +278,5 @@ console.error("Could not import links from the provided config. Skipping..."); | ||
for (var link in links) { | ||
this.parseLink(link); | ||
input = this.parseLink(link, input); | ||
} | ||
return input; | ||
} | ||
@@ -169,4 +289,3 @@ }]); | ||
module.exports = { | ||
Parser: Parser, | ||
Document: Document | ||
Parser: Parser | ||
}; |
{ | ||
"name": "@tadaima/sekkei", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "API for the parsing of Tadaima startpages", | ||
@@ -21,2 +21,5 @@ "main": "sekkei.js", | ||
"chai": "^4.1.2", | ||
"del": "^3.0.0", | ||
"fs-readdir-recursive": "^1.1.0", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^5.2.0" | ||
@@ -23,0 +26,0 @@ }, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
12155
264
6
3
1
+ Addeddel@^3.0.0
+ Addedfs-readdir-recursive@^1.1.0
+ Addedmkdirp@^0.5.1
+ Addedarray-union@1.0.2(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addeddel@3.0.0(transitive)
+ Addedfs-readdir-recursive@1.1.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobby@6.1.0(transitive)
+ Addedis-path-cwd@1.0.0(transitive)
+ Addedis-path-in-cwd@1.0.1(transitive)
+ Addedis-path-inside@1.0.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedp-map@1.2.0(transitive)
+ Addedpath-is-inside@1.0.2(transitive)
+ Addedpify@2.3.03.0.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedrimraf@2.7.1(transitive)