Skip to content

Commit

Permalink
added action decompress for template and assets
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Feb 4, 2024
1 parent 2a54223 commit aedc8e8
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/nexrender-action-decompress/index.js
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)
}
24 changes: 24 additions & 0 deletions packages/nexrender-action-decompress/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/nexrender-action-decompress/package.json
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"
}
}
36 changes: 36 additions & 0 deletions packages/nexrender-action-decompress/readme.md
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,
}
]
}
}
```
57 changes: 57 additions & 0 deletions packages/nexrender-action-decompress/test/manual.js
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);
});

0 comments on commit aedc8e8

Please sign in to comment.