Skip to content

Commit

Permalink
create a prepublish script that generates the properties.js file
Browse files Browse the repository at this point in the history
This file will lazy-load properties but also specify the filename in the
require explicitly so that it plays better with browserify.

Should solve #11
  • Loading branch information
chad3814 committed May 13, 2015
1 parent ef912ac commit 6f95725
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
lib/properties.js
32 changes: 1 addition & 31 deletions lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,36 +209,6 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
}
});

var LazyDefinition = function (property, modulePath) {
this.get = function () {
var definition = require(modulePath).definition;
Object.defineProperty(this, property, definition);
return this[property];
};

this.set = function (v) {
var definition = require(modulePath).definition;
Object.defineProperty(this, property, definition);
this[property] = v;
};

this.enumerable = true;
this.configurable = true;
};

/*
*
* http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties
*/
var property_files = fs.readdirSync(__dirname + '/properties');
property_files.forEach(function (property) {
var dashed;
if (property.substr(-3) === '.js') {
property = path.basename(property, '.js');
dashed = camelToDashed(property);
Object.defineProperty(CSSStyleDeclaration.prototype, property, new LazyDefinition(property, './properties/' + property));
Object.defineProperty(CSSStyleDeclaration.prototype, dashed, new LazyDefinition(property, './properties/' + property));
}
});
require('./properties')(CSSStyleDeclaration.prototype);

exports.CSSStyleDeclaration = CSSStyleDeclaration;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"nodeunit": "~0.8.0"
},
"scripts": {
"test": "nodeunit tests"
"test": "./scripts/run_tests.sh",
"prepublish": "node ./scripts/generateTestFiles.js"
},
"licenses": [
{
Expand Down
58 changes: 58 additions & 0 deletions scripts/generate_properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var fs = require('fs');
var path = require('path');

var camelToDashed = require('../lib/parsers').camelToDashed;

var property_files = fs.readdirSync(path.resolve(__dirname, '../lib/properties'));
var out_file = fs.createWriteStream(path.resolve(__dirname, '../lib/properties.js'), {encoding: 'utf-8'});

out_file.write('\'use strict\';\n\n// autogenerated\n\n');
out_file.write('/*\n *\n * http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties\n */\n\n');
out_file.write('module.exports = function (prototype) {\n');

property_files.forEach(function (property) {
var dashed;
if (property.substr(-3) === '.js') {
property = path.basename(property, '.js');
dashed = camelToDashed(property);
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', {\n');
out_file.write(' get: function () {\n');
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
out_file.write(' return this.' + property + ';\n');
out_file.write(' },\n');
out_file.write(' set: function (v) {\n');
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
out_file.write(' this.' + property + ' = v;\n');
out_file.write(' },\n');
out_file.write(' enumerable: true,\n');
out_file.write(' configurable: true\n');
out_file.write(' });\n');
if (property !== dashed) {
out_file.write(' Object.defineProperty(prototype, \'' + dashed + '\', {\n');
out_file.write(' get: function () {\n');
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
out_file.write(' return this.' + property + ';\n');
out_file.write(' },\n');
out_file.write(' set: function (v) {\n');
out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n');
out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n');
out_file.write(' this.' + property + ' = v;\n');
out_file.write(' },\n');
out_file.write(' enumerable: true,\n');
out_file.write(' configurable: true\n');
out_file.write(' });\n');
}
}
});

out_file.write('};\n');
out_file.end(function (err) {
if (err) {
throw err;
}
});
4 changes: 4 additions & 0 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

node ./scripts/generate_properties.js
nodeunit tests

0 comments on commit 6f95725

Please sign in to comment.