diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a451dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +*.log \ No newline at end of file diff --git a/bin/native-css.js b/bin/native-css.js new file mode 100644 index 0000000..37ce8ed --- /dev/null +++ b/bin/native-css.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +(function() { + var nativeCSS = require('../lib/index.js'); +})(); diff --git a/docs/default.md b/docs/default.md new file mode 100644 index 0000000..909c320 --- /dev/null +++ b/docs/default.md @@ -0,0 +1,2 @@ +Hey young padawan, please type a valid command. +>> To see all commands, use: native-css [ -h, --help ] diff --git a/docs/help.md b/docs/help.md new file mode 100644 index 0000000..01ad4c4 --- /dev/null +++ b/docs/help.md @@ -0,0 +1,13 @@ + +Usage: native-css + +Description: Convert pure CSS to react native style object + +More info: https://github.com/raphamorim/native-css + +Commands: + native-css convert CSS file to react native style + +Options: + -v, --version output version number + -h, --help output usage information diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..a9de72d --- /dev/null +++ b/lib/index.js @@ -0,0 +1,15 @@ +'use strict'; + +var nativeCSS = require('./native-css'), + verify = require('../src').verify, + commands = process.argv; + +// Version +if (verify(['-v', '--version'])) + return console.log(nativeCSS.version()); + +// Help +else if (verify(['-h', '--help'])) + return console.log(nativeCSS.help()); + +return nativeCSS.convert(commands[2]); \ No newline at end of file diff --git a/lib/native-css.js b/lib/native-css.js new file mode 100644 index 0000000..bf21aba --- /dev/null +++ b/lib/native-css.js @@ -0,0 +1,69 @@ +'use strict'; + +var packageJson = require('../package.json'), + src = require('../src'); + +var nativeCSS = function() {}; + +nativeCSS.prototype.default = function() { + return src.readFile('/../docs/default.md') +} + +nativeCSS.prototype.version = function() { + return ('native-css version: ' + packageJson.version) +} + +nativeCSS.prototype.help = function() { + return src.readFile('/../docs/help.md') +} + +nativeCSS.prototype.convertProperties = function(reference, property) { + var properties = {}; + + property = property.replace(new RegExp(reference + '{', 'g'), '') + .replace(new RegExp('}', 'g'), '') + + property = property.split(';'); + + property.forEach(function(item) { + if (item != '') { + var entry = item.split(':'); + properties[entry[0]] = entry[1]; + } + }) + + return properties; +} + +nativeCSS.prototype.convert = function(cssFile) { + var path = process.cwd() + '/' + cssFile; + + console.log(path); + + if (!(require('fs').existsSync(path))) + return console.log('Ooops!\nError: CSS file not found!'); + + var self = this, + css = src.readFile(path); + css = css.replace(/\s/g, ''); + + var classNames = css.match(/(?:#\w+\s+)?\.[\w-]+(?:\s+\w+\s*\.\w+|\s+\w+)?/ig), + classNamesData = ''; + + var classes = []; + var result = {}; + + classNames.forEach(function(item) { + classNamesData = css + .substring(css.indexOf(item),css.indexOf('}')+1); + + css = css.replace(classNamesData, ''); + classes.push(classNamesData); + + result[item] = self.convertProperties(item, classNamesData); + }); + + console.log(result) +} + +module.exports = new nativeCSS(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..83cfad9 --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "native-css", + "version": "1.0.0", + "description": "Convert pure CSS to react native style object", + "main": "index.js", + "bin": { + "native-css": "native-css.js" + }, + "directories": { + "doc": "docs" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/raphamorim/native-css" + }, + "keywords": [ + "react", + "css", + "react", + "native", + "convert" + ], + "author": "Raphael Amorim", + "license": "ISC", + "bugs": { + "url": "https://github.com/raphamorim/native-css/issues" + }, + "homepage": "https://github.com/raphamorim/native-css" +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..82a37b8 --- /dev/null +++ b/readme.md @@ -0,0 +1,17 @@ +# native-css + +Convert pure CSS to react native style object. + +## Contributing + +Don't be shy, send a Pull Request! Here is how: + +1. Fork it! +2. Create your feature branch: `git checkout -b my-new-feature` +3. Commit your changes: `git commit -m 'Add some feature'` +4. Push to the branch: `git push origin my-new-feature` +5. Submit a pull request :D + +## About + +**License:** MIT ® [Raphael Amorim](https://github.com/raphamorim) diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..c95943b --- /dev/null +++ b/src/index.js @@ -0,0 +1,20 @@ +var fs = require('fs'), + input = process.argv[2]; + +exports.readFile = function(path) { + return fs.readFileSync(path, 'utf8'); +} + +exports.verify = function(args) { + if (typeof args === 'object') { + if (args.indexOf(input) == -1) + return false; + + return true; + } else { + if (args != input) + return false; + + return true; + } +};