aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvaporstack <andrew@vaporstack.org>2018-02-12 13:42:11 -0500
committervaporstack <andrew@vaporstack.org>2018-02-12 13:42:11 -0500
commitda2e1452ebfa3c1b012866bfafe78a162ac0c68b (patch)
treecaa9a9b39a7412c7612441a019c35b970c0c5c7d
parentdf33ba6c26516b75be9da4ea2f66b3243fe1bb99 (diff)
Proposal for schema change and a couple of trivial enhancements
-rw-r--r--.gitignore2
-rw-r--r--index.js63
2 files changed, 54 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ca0973
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+
diff --git a/index.js b/index.js
index 495a544..22a488f 100644
--- a/index.js
+++ b/index.js
@@ -1,8 +1,15 @@
fs = require('fs');
+path = require('path');
var normalizedPath = require("path").join(__dirname, "themes");
var html = "# Themes\nThis collection of themes are meant to be used with [Marabu](https://github.com/hundredrabbits/Marabu), [Ronin](https://github.com/hundredrabbits/Ronin), [Left](https://github.com/hundredrabbits/Left), [Donsol](https://github.com/hundredrabbits/Donsol) and [Dotgrid](https://github.com/hundredrabbits/Dotgrid).\n\n<img src='https://raw.githubusercontent.com/hundredrabbits/Themes/master/PREVIEW.jpg' width='600'/>\n\n## Install\nTo install a theme, simply drag the `thm` file onto the application window.\nYou are welcome to submit your own themes to this collection!\n\n"
+// opt in to upgrade the schema change
+// would require to do 'theme = theme.data' in client apps
+// idea is tradeoff for authorship and versioning, potentially multiple codepaths
+// if, in the future, more colors are ever added
+var generate_v2 = false;
+
function build_svg(n,theme)
{
var name = n.split(".")[0];
@@ -24,7 +31,7 @@ function build_svg(n,theme)
fs.writeFile("assets/"+name+".svg", html, function(err) {
if(err) {return console.log(err);}
console.log("Saved "+name)
- });
+ });
}
function build_theme(n,theme)
@@ -33,14 +40,13 @@ function build_theme(n,theme)
return `## [${name}](themes/${name}.thm)\n![${name}](assets/${name}.svg)\n\n`
}
-function is_json(text)
+function safe_parse_json(text)
{
try{
- JSON.parse(text);
- return true;
+ return JSON.parse(text);
}
catch (error){
- return false;
+ return null;
}
}
@@ -49,17 +55,52 @@ function generate(html)
fs.writeFile("README.md", html, function(err) {
if(err) {return console.log(err);}
console.log("Done.")
- });
+ });
+}
+
+function upgrade_with_defaults(theme)
+{
+
+ if ( !theme.hasOwnProperty("meta"))
+ {
+ //assume v1, provide default meta
+ return {"meta":
+ {
+ "author": "unknown",
+ "version": 2,
+ "revision": 1
+ },
+ "data": theme
+ }
+
+ }else{
+ // v2 and up
+ return theme;
+ }
}
require("fs").readdirSync(normalizedPath).forEach(function(file_name) {
- fs.readFile('themes/'+file_name, 'utf8', function (err,data){
+ fs.readFile('themes'+path.sep+file_name, 'utf8', function (err, data){
if(err) { return console.log(err); }
- if(is_json(data)){
- var theme = JSON.parse(data)
- build_svg(file_name,theme)
- html += build_theme(file_name,theme)
+
+ var theme = safe_parse_json(data);
+ if (!theme ){ return console.log(err); }
+ build_svg(file_name, theme)
+ html += build_theme(file_name, theme)
+
+
+ if( generate_v2 )
+ {
+ var v2dir = 'themes-v2';
+
+ if ( !fs.existsSync(v2dir))
+ {
+ fs.mkdirSync(v2dir);
+ }
+ theme = upgrade_with_defaults(theme);
+ fs.writeFileSync(v2dir + path.sep + file_name, JSON.stringify(theme, null, 2));
}
+
});
});