diff --git a/packages/nexrender-action-mogrt/index.js b/packages/nexrender-action-mogrt/index.js index e709500f..208eb89e 100644 --- a/packages/nexrender-action-mogrt/index.js +++ b/packages/nexrender-action-mogrt/index.js @@ -1,5 +1,6 @@ const path = require('path'); const url = require('url'); +const Mogrt = require('./mogrt'); module.exports = async (job, settings, options, type) => { settings.logger = settings.logger ?? console; @@ -38,7 +39,6 @@ module.exports = async (job, settings, options, type) => { return job; } - const { Mogrt } = await import('mogrt'); const mogrt = new Mogrt(job.template.dest); await mogrt.init(); diff --git a/packages/nexrender-action-mogrt/mogrt.js b/packages/nexrender-action-mogrt/mogrt.js new file mode 100644 index 00000000..047ef326 --- /dev/null +++ b/packages/nexrender-action-mogrt/mogrt.js @@ -0,0 +1,92 @@ +const StreamZip = require("node-stream-zip") +const tempy = require("tempy"); +const path = require("node:path"); + +function flattenStrings(obj, doNotCopy) { + if (!doNotCopy) obj = JSON.parse(JSON.stringify(obj)); + for (let [k, v] of Object.entries(obj)) { + if (typeof v === 'object') { + if (!Array.isArray(v) && 'strDB' in v) { + if (v.strDB.length) { + obj[k] = Object.values(v.strDB)[0].str; + } else { + obj[k] = ''; + } + } else { + obj[k] = flattenStrings(v, true); + } + } + } + return obj; +} + +class Mogrt { + constructor(filename) { + this.filename = filename; + this.initialized = false; + this._manifest = null; + } + + _getZip() { + return new StreamZip.async({ file: this.filename }); + } + + isAfterEffects() { + if (!this.initialized) throw Error('Must initialise with .init() first'); + return this._manifest['authorApp'] === 'aefx'; + } + + isPremiere() { + if (!this.initialized) throw Error('Must initialise with .init() first'); + return this._manifest['authorApp'] === 'ppro'; + } + + getEssentialFields(flattened) { + if (!this.initialized) throw Error('Must initialise with .init() first'); + flattened = typeof flattened === 'undefined' || flattened; + let { clientControls } = this._manifest; + if (flattened) { + clientControls = flattenStrings(clientControls); + } else { + clientControls = JSON.parse(JSON.stringify(clientControls)); + } + return clientControls; + } + + async extractTo(toPath) { + const zip = this._getZip(); + const entries = []; + await tempy.file.task(async (tempPath) => { + await zip.extract('project.aegraphic', tempPath); + const aegraphicZip = new StreamZip.async({ file: tempPath }); + aegraphicZip.on('entry', entry => entries.push(path.join(toPath, entry.name))); + await aegraphicZip.extract(null, toPath); + await Promise.all([ + aegraphicZip.close(), + zip.close() + ]); + }); + return entries; + } + + async getManifest(flattened) { + flattened = typeof flattened === 'undefined' || flattened; + if (this._manifest === null) { + const zip = this._getZip(); + this._manifest = JSON.parse((await zip.entryData('definition.json')).toString()); + await zip.close(); + } + if (flattened) { + return flattenStrings(this._manifest) + } else { + return this._manifest; + } + } + + async init() { + await this.getManifest(false); + this.initialized = true; + } +} + +module.exports = Mogrt; diff --git a/packages/nexrender-action-mogrt/package.json b/packages/nexrender-action-mogrt/package.json index d4c83ef1..0643457f 100644 --- a/packages/nexrender-action-mogrt/package.json +++ b/packages/nexrender-action-mogrt/package.json @@ -11,6 +11,7 @@ "access": "public" }, "dependencies": { - "mogrt": "^0.0.2" + "node-stream-zip": "^1.15.0", + "tempy": "^1.0.1" } } diff --git a/packages/nexrender-action-mogrt/test.js b/packages/nexrender-action-mogrt/test.js index a84cfb98..04001774 100644 --- a/packages/nexrender-action-mogrt/test.js +++ b/packages/nexrender-action-mogrt/test.js @@ -2,6 +2,7 @@ const extension = require('./index.js'); const url = require('node:url'); const path = require('node:path'); const assert = require('assert'); +const tempy = require('tempy'); describe("action/mogrt",() => { const defaultParameters = { @@ -26,9 +27,8 @@ describe("action/mogrt",() => { let parameters; beforeEach(async () => { - const { temporaryDirectory } = await import('tempy'); parameters = JSON.parse(JSON.stringify(defaultParameters)); - parameters.job.workpath = temporaryDirectory(); + parameters.job.workpath = tempy.directory(); }) it('self-adds to postdownload', async () => { diff --git a/packages/nexrender-action-mogrt/test/test.manual.js b/packages/nexrender-action-mogrt/test/test.manual.js index d1ffc819..75e51f61 100644 --- a/packages/nexrender-action-mogrt/test/test.manual.js +++ b/packages/nexrender-action-mogrt/test/test.manual.js @@ -2,11 +2,11 @@ process.env.NEXRENDER_ENABLE_AELOG_PROJECT_FOLDER = 1; const path = require('path'); const url = require('node:url'); -const { render } = require("@nexrender/core"); +const { render } = require("../../nexrender-core"); const job = { template: { - src: url.pathToFileURL(path.join(__dirname, '__tests__', './assets/ae.mogrt')).href, + src: url.pathToFileURL(path.join(__dirname, './assets/ae.mogrt')).href, composition: 'ignored', // continueOnMissing: true, }, @@ -22,8 +22,8 @@ const job = { actions: { predownload: [ { - module: require.resolve('./index.js'), - essentialParameters: { + module: require.resolve('../index.js'), + params: { 'Group Test': { 'Image': 'ref-image-layer' }