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

Options.stylesheet does not accept javascript #78

Open
elmpp opened this issue Sep 25, 2019 · 2 comments
Open

Options.stylesheet does not accept javascript #78

elmpp opened this issue Sep 25, 2019 · 2 comments

Comments

@elmpp
Copy link

elmpp commented Sep 25, 2019

Hi there,

I'm seeing an error when trying to output javascript styles.

I have tried this with both gm and jimp compositors.

error message:

ENOENT: no such file or directory, open 'javascript'
error Command failed with exit code 1.

config:

{
    src: [path.resolve(__dirname, './assets/spriteable/horizontal/*')],
    spritePath: path.resolve(__dirname, 'assets/__generated__/spritesheet/allbookies-hz.png'),
    stylesheetPath: path.resolve(__dirname, 'assets/__generated__/spritesheet-style/allbookies-hz.js'),
    stylesheet: 'javascript',
    compositor: 'gm',
    compositorOptions: {
      compressionLevel: 6,
    },
  }

package.json:

{
  "name": "org-image",
  "version": "0.86.0",
  "description": "image processing stuff",
  "license": "ISC",
  "main": "lib/org-image.js",
  "scripts": {
    "build": "tsc",
    "postinstall": "cd ../.. && yarn postinstall",
    "postuninstall": "cd ../.. && yarn postinstall",
    "transpile": "npm run build",
    "transpile:watch": "npm run build -- --watch",
    "type-check": "tsc --noEmit"
  },
  "devDependencies": {
    "@types/node-sprite-generator": "^0.10.1",
    "node-sprite-generator": "^0.10.2",
    "ts-node": "8.2.0"
  },
  "bin": {
    "spritesheet": "scripts/spritesheet.sh"
  }
}
@navjeetfgmnt
Copy link

@elmpp did you find a sol to this?

@timohausmann
Copy link

Same, there is no "javascript" template in this repository.
I found two workarounds, these examples will create a json format, adjust the output to your likings.

Workaround 1: write custom output function

I think the easiest way is to set options.stylesheet to a custom function:

var nsg = require('node-sprite-generator');
var fs = require('fs');

nsg({
    src: [
        'faces-128/*.jpg'
    ],
    spritePath: 'faces-128/all.png',
    stylesheetPath: 'faces-128/all.json',
    stylesheet: function(layout, stylesheetPath, spritePath, options, callback) {
      const data = layout.images.map((img) => {
        const {x, y, width, height, path} = img;
        return { x, y, width, height, path }
      })

      fs.writeFileSync(stylesheetPath, JSON.stringify(data, null, 2))
    },
    compositor: 'jimp'
}, function (err) {
    console.log('Sprite generated!');
});

Workaround 2: add missing templates

You can also create the missing templates.

create new file node-sprite-generator/lib/stylesheet/javascript.js:

'use strict';

var getTemplatedStylesheet = require('./templatedStylesheet'),
    path = require('path');

module.exports = getTemplatedStylesheet(path.join(__dirname, '/templates/javascript.tpl'));

create new file node-sprite-generator/lib/stylesheet/templates/javascript.tpl:

[<% layout.images.forEach(function (image) { %>
  {
    "path": "<%= image.path %>",
    "x": <%= image.x %>,
    "y": <%= image.y %>,
    "width": <%= image.width %>,
    "height": <%= image.height %>
  }, <% }); %>
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants