Skip to content

Instantly share code, notes, and snippets.

@dfreedm
Last active August 26, 2015 05:32
Show Gist options
  • Save dfreedm/4da910206252bb07aa83 to your computer and use it in GitHub Desktop.
Save dfreedm/4da910206252bb07aa83 to your computer and use it in GitHub Desktop.
Rough Draft of a Push Manifest Generator

Usage

node index.js /path/to/app/folder index.html

Output

An flat list of HTML Imports, Scripts, and Styles for putting into a Push manifest

/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
// jshint node: true
'use strict';
var fs = require('fs');
var hyd = require('hydrolysis');
var dom5 = require('dom5');
var url = require('url');
var path = require('path');
var basePath = process.argv[2];
var inputPath = process.argv[3];
if (!inputPath || !basePath) {
console.error('Need input path!');
process.exit(1);
}
basePath = path.resolve(basePath);
inputPath = path.resolve(path.resolve(basePath, inputPath));
if (fs.statSync(inputPath).isDirectory()) {
inputPath = path.join(inputPath, 'index.html');
}
var loader = new hyd.Loader();
loader.addResolver(new hyd.FSResolver({
root: basePath,
basePath: '/'
}));
var EXTERNAL = /^(?:https?:)?\/\//;
loader.addResolver(new hyd.NoopResolver(EXTERNAL));
var analyzer = new hyd.Analyzer(false, loader);
function treeToList(tree, accum) {
if (!accum) {
accum = [];
}
accum.push(tree.href);
for(var i = 0; i < tree.imports.length; i++) {
}
}
function styleToUrl(href, style) {
var src = dom5.getAttribute(style, 'href');
if (EXTERNAL.test(src)) {
return;
}
if (src) {
return url.resolve(href, src);
}
}
function scriptToUrl(href, script) {
var src = dom5.getAttribute(script, 'src');
if (EXTERNAL.test(src)) {
return;
}
if (src) {
return url.resolve(href, src);
}
}
function treeToUrls(tree, accum) {
if (!accum) {
accum = [];
}
if (!tree) {
return accum;
}
if (!tree.href) {
return accum;
}
accum.push(tree.href);
tree.imports.forEach(function(im) {
if (im.href) {
treeToUrls(im, accum);
}
});
tree.html.script.forEach(function(script) {
var u = scriptToUrl(tree.href, script);
if (u) {
accum.push(u);
}
});
tree.html.style.forEach(function(style) {
var u = styleToUrl(tree.href, style);
if (u) {
accum.push(u);
}
});
return accum;
}
inputPath = path.join('/', path.relative(basePath, inputPath));
//console.log(inputPath);
analyzer.metadataTree(inputPath).then(function(tree) {
var list = treeToUrls(tree).slice(1).reverse();
console.log(list);
}).catch(function(error) {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment