forked from inlife/nexrender
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added action decompress for template and assets
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const AdmZip = require('adm-zip'); | ||
|
||
const decompress = (job, settings, asset, action) => { | ||
switch (action.format) { | ||
case 'zip': | ||
const zip = new AdmZip(asset.dest); | ||
zip.extractAllTo(job.workpath, action.overwrite || false); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
module.exports = (job, settings, action, type) => { | ||
if (type !== 'prerender') { | ||
return Promise.reject("'action-decompress' module should be used only in 'prerender' section") | ||
} | ||
|
||
if (['zip'].indexOf(action.format) === -1) { | ||
return Promise.reject(`'action-decompress' module doesn't support '${action.format}' format archives`) | ||
} | ||
|
||
const promises = [].concat( | ||
decompress(job, settings, job.template, action), | ||
job.assets.map(asset => decompress(job, settings, asset, action)) | ||
); | ||
|
||
return Promise | ||
.all(promises) | ||
.then(() => job) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@nexrender/action-decompress", | ||
"homepage": "https://www.nexrender.com", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"author": "Vladyslav Hrytsenko", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"adm-zip": "^0.5.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Action Decompress | ||
|
||
Decompress/extract your zip-archived template file and/or assets to the project work directory. | ||
|
||
## Installation | ||
|
||
If you are using [binary](https://github.com/inlife/nexrender/releases) version of the nexrender, | ||
there is no need to install the module, it is **included** in the binary build. | ||
|
||
``` | ||
npm i @nexrender/action-decompress -g | ||
``` | ||
|
||
## Usage | ||
|
||
When creating your render job provide this module as one of the `prerender` actions: | ||
|
||
Options: | ||
|
||
- `format` - format of the archive to decompress, currently only `zip` is supported | ||
- `overwrite` - if set to `true`, it will overwrite existing files in the project work directory on name conflict, default is `false` | ||
|
||
```json | ||
// job.json | ||
{ | ||
"actions": { | ||
"prerender": [ | ||
{ | ||
"module": "@nexrender/action-decompress", | ||
"format": "zip", | ||
"overwrite": false, | ||
} | ||
] | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const AdmZip = require('adm-zip'); | ||
const decompressAction = require('../index'); | ||
|
||
// create "template" zip file | ||
const zip = new AdmZip(); | ||
zip.addFile("template.aep", new Buffer.from("hello there 1")); | ||
zip.addFile("(Footage)/test.jpg", new Buffer.from("hello there 2")); | ||
zip.writeZip(path.join(__dirname, 'template.zip')); | ||
|
||
// create "asset" zip file | ||
const zip2 = new AdmZip(); | ||
zip2.addFile("asset.jpg", new Buffer.from("hello there 3")); | ||
zip2.writeZip(path.join(__dirname, 'asset.zip')); | ||
|
||
|
||
// create job and test with the action | ||
|
||
const job = { | ||
workpath: __dirname, | ||
template: { | ||
dest: path.join(__dirname, 'template.zip'), | ||
}, | ||
assets: [ | ||
{ | ||
dest: path.join(__dirname, 'asset.zip'), | ||
}, | ||
], | ||
}; | ||
|
||
decompressAction(job, {}, { format: 'zip' }, 'prerender') | ||
.then(() => { | ||
// ensure each file is extracted | ||
assert(fs.existsSync(path.join(__dirname, 'template.aep'))); | ||
assert(fs.existsSync(path.join(__dirname, '(Footage)', 'test.jpg'))); | ||
assert(fs.existsSync(path.join(__dirname, 'asset.jpg'))); | ||
|
||
// ensure each file has correct content | ||
assert.equal(fs.readFileSync(path.join(__dirname, 'template.aep'), 'utf8'), 'hello there 1'); | ||
assert.equal(fs.readFileSync(path.join(__dirname, '(Footage)', 'test.jpg'), 'utf8'), 'hello there 2'); | ||
assert.equal(fs.readFileSync(path.join(__dirname, 'asset.jpg'), 'utf8'), 'hello there 3'); | ||
|
||
// cleanup | ||
fs.unlinkSync(path.join(__dirname, 'template.zip')); | ||
fs.unlinkSync(path.join(__dirname, 'asset.zip')); | ||
fs.unlinkSync(path.join(__dirname, 'template.aep')); | ||
fs.unlinkSync(path.join(__dirname, '(Footage)', 'test.jpg')); | ||
fs.unlinkSync(path.join(__dirname, 'asset.jpg')); | ||
fs.rmdirSync(path.join(__dirname, '(Footage)')); | ||
|
||
console.log('All tests passed'); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); |