Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev gibbok issue 1 #2

Merged
merged 7 commits into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Use this tool to move your interactive animations from stylesheets to JavaScript

## Great! So how do I use it?

- Install keyframes-tool using `npm install keyframes-tool` or adding it in your `package.json` as: `"devDependencies": { "keyframes-tool": "^1.0.2" }` and run `npm install`.
- Install keyframes-tool using `npm install keyframes-tool` or adding it in your `package.json` as: `"devDependencies": { "keyframes-tool": "^1.0.3" }` and run `npm install`.
- Run command line in your keyframes-tool directory and enter `node keyframes-tool ./input.css ./output.json`,
where as first argument `./input.css` is the CSS source file to process and the second argument `./output.json` is the destination file with the converted result.
Paths should be relative to `keyframes-tool.js` file location.
Expand Down Expand Up @@ -44,42 +44,50 @@ Output file `output.json`:

```json
{
"flash": [
{
"opacity": "1",
"offset": "0"
},
{
"opacity": "0",
"offset": "0.25"
},
{
"opacity": "1",
"offset": "0.5"
},
{
"opacity": "0",
"offset": "0.75"
},
{
"opacity": "1",
"offset": "1"
}
],
"pulse": [
{
"transform": "scale3d(1, 1, 1)",
"offset": "0"
},
{
"transform": "scale3d(1.05, 1.05, 1.05)",
"offset": "0.5"
},
{
"transform": "scale3d(1, 1, 1)",
"offset": "1"
}
]
"flash": [
{
"opacity": "1",
"offset": "0",
"easing": "ease"
},
{
"opacity": "0",
"offset": "0.25",
"easing": "ease"
},
{
"opacity": "1",
"offset": "0.5",
"easing": "ease"
},
{
"opacity": "0",
"offset": "0.75",
"easing": "ease"
},
{
"opacity": "1",
"offset": "1",
"easing": "ease"
}
],
"pulse": [
{
"transform": "scale3d(1, 1, 1)",
"offset": "0",
"easing": "ease"
},
{
"transform": "scale3d(1.05, 1.05, 1.05)",
"offset": "0.5",
"easing": "ease"
},
{
"transform": "scale3d(1, 1, 1)",
"offset": "1",
"easing": "ease"
}
]
}
```
- Use the result as embedded data in your JavaScript as [shown in this example](http://codepen.io/gibbok/pen/ENpqZO), alternatively you could load the JSON data using Ajax.
50 changes: 31 additions & 19 deletions keyframes-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ let fileIn,
/**
* Check software requirements.
*/
let prerequisiteCheck = () => {
let checkPrerequisites = () => {
return new Promise((fulfill, reject) => {
try {
// check node version
let getNodeVersion = (strVersion) => {
let getNodeVersion = strVersion => {
let numberPattern = /\d+/g;
return numVersion = Number(strVersion.match(numberPattern).join(''))
},
Expand All @@ -33,7 +33,7 @@ let prerequisiteCheck = () => {
};

/**
* Get arguments passed Node.js using terminal.
* Get arguments passed by Node.js from terminal.
*/
let getNodeArguments = () => {
return new Promise((fulfill, reject) => {
Expand Down Expand Up @@ -79,9 +79,9 @@ let readInputFile = () => {
}

/**
* Parse content of CSS input file and creates an AST tree.
* Parse content of CSS input file and create an AST tree.
*/
let parse = (data) => {
let parse = data => {
return new Promise((fulfill, reject) => {
try {
let parsedData = css.parse(data.toString(), { silent: false });
Expand All @@ -95,7 +95,7 @@ let parse = (data) => {
/**
* Validate AST tree content.
*/
let validate = (data) => {
let validate = data => {
return new Promise((fulfill, reject) => {
try {
let isStylesheet = data.type === 'stylesheet',
Expand Down Expand Up @@ -123,9 +123,9 @@ let validate = (data) => {
/**
* Process AST tree content and a new data structure valid for Web Animation API KeyframeEffect.
* The following code uses Ramda.js for traversing a complex AST tree,
* an alternative version is visible at http://codepen.io/gibbok/pen/PbRrxp
* an alternative and simplified version is visible at http://codepen.io/gibbok/pen/PbRrxp
*/
let processAST = (data) => {
let processAST = data => {
return new Promise((fulfill, reject) => {
try {
let processKeyframe = (vals, declarations) => [
Expand Down Expand Up @@ -171,7 +171,7 @@ let processAST = (data) => {
// get only object whose `type` property is `keyframes`
R.filter(R.propEq('type', 'keyframes')),
// map each item in `keyframes` collection
// to an object {name: keyframe.name, content: [contentOfkeyframes] }
// to an object `{name: keyframe.name, content: [contentOfkeyframes] }`
R.map((keyframe) => ({
name: keyframe.name,
content: getContentOfKeyframes(keyframe.keyframes)
Expand All @@ -188,19 +188,31 @@ let processAST = (data) => {
let orderByOffset = R.map(R.pipe(R.sortBy(R.prop('offset'))));

// convert hyphenated properties to camelCase
let convertToCamelCase = (data) => {
let convertToCamelCase = data => {
let mapKeys = R.curry((fn, obj) =>
R.fromPairs(R.map(R.adjust(fn, 0), R.toPairs(obj)))
),
camelCase = (str) => str.replace(/[-_]([a-z])/g, (m) => m[1].toUpperCase())
return R.map(R.map(mapKeys(camelCase)), data)
};

// convert `animationTimingFunction` to `easing` for compatibility with web animations api
// and assign `easing` default value to `ease` when `animation-timing-function` from css file is not provided
let convertToEasing = data => {
const convert = data => {
const ease = R.prop('animationTimingFunction', data) || 'ease';
return R.dissoc('animationTimingFunction', R.assoc('easing', ease, data));
};
let result = R.map(R.map(convert))(data);
return result;
};

// process
let process = R.pipe(
transformAST,
orderByOffset,
convertToCamelCase
convertToCamelCase,
convertToEasing
);
let result = process(data);
fulfill(result);
Expand All @@ -213,7 +225,7 @@ let processAST = (data) => {
/**
* Write JSON output file.
*/
let writeOutputFile = (data) => {
let writeOutputFile = data => {
return new Promise((fulfill, reject) => {
data = JSON.stringify(data);
fs.writeFile(fileOut, data, (err) => {
Expand All @@ -230,21 +242,21 @@ let writeOutputFile = (data) => {
* Initiate conversion process.
*/
let init = () => {
prerequisiteCheck().then(() => {
checkPrerequisites().then(() => {
return getNodeArguments();
}).then(() => {
return readInputFile();
}).then((data) => {
}).then(data => {
return parse(data);
}).then((data) => {
}).then(data => {
return validate(data);
}).then((data) => {
}).then(data => {
return processAST(data);
}).then((data) => {
}).then(data => {
return writeOutputFile(data);
}).then((data) => {
}).then(data => {
console.log('success: file created at: ' + fileOut);
}).catch(function (err) {
}).catch(err => {
console.log('error: ' + err);
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keyframes-tool",
"version": "1.0.2",
"version": "1.0.3",
"description": "Keyframes-tool is a NodeJs command line tool which convert CSS Animations to a keyframes object suitable for Web Animations API.",
"main": "keyframes-tool.js",
"keywords": [
Expand Down