You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

psdinfo 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env node
  2. var argv = require('yargs')
  3. .option('fonts', { alias: 'f', type: 'boolean' })
  4. .option('text', { alias: 't', type: 'boolean' })
  5. .option('doc', { alias: 'd', type: 'boolean' })
  6. .option('full', { alias: 'F', type: 'boolean' })
  7. .argv
  8. var assign = require('util')._extend
  9. if (!module.parent) {
  10. if (argv._.length === 0 || argv.help || argv.h) {
  11. console.log("Usage:")
  12. console.log(" psdinfo FILE.psd [--fonts] [--text] [--doc] [--full]")
  13. process.exit(0)
  14. }
  15. run(argv._)
  16. }
  17. /*
  18. * Returns a summary of the PSD document
  19. */
  20. function getSummary (data) {
  21. return {
  22. document: {
  23. width: data.document.width,
  24. height: data.document.height
  25. }
  26. }
  27. }
  28. /*
  29. * Returns fonts in the document
  30. */
  31. function getFonts (data) {
  32. return {
  33. fonts: pluckOut(data, 'text', function (text) {
  34. return text && text.font && text.font.name
  35. })
  36. }
  37. }
  38. /*
  39. * Returns texts found in the document
  40. */
  41. function getStrings (data) {
  42. return {
  43. text: pluckOut(data, 'text', function (text) {
  44. return text && text.value
  45. })
  46. }
  47. }
  48. function pluckOut (data, parent, fn) {
  49. var out = {}
  50. walk(data, parent, function (node) {
  51. var result = fn(node)
  52. if (result) out[result] = true
  53. })
  54. return Object.keys(out).sort()
  55. }
  56. function walk (object, needle, fn) {
  57. for (var key in object) {
  58. if (key === needle) {
  59. fn(object[key])
  60. } else if (typeof object[key] === 'object') {
  61. walk(object[key], needle, fn)
  62. }
  63. }
  64. }
  65. function run (files) {
  66. if (!argv.fonts && !argv.text && !argv.doc && !argv.full) {
  67. console.error('options missing; see --help')
  68. process.exit(1)
  69. }
  70. files.forEach(function (file) {
  71. require('psd').open(file)
  72. .then(function (psd) {
  73. return psd.tree().export()
  74. })
  75. .then(function (data) {
  76. if (argv.full) return data
  77. var obj = {}
  78. if (argv.fonts) assign(obj, getFonts(data))
  79. if (argv.text) assign(obj, getStrings(data))
  80. if (argv.doc) assign(obj, getSummary(data))
  81. return obj
  82. })
  83. .then(function (data) {
  84. log(data, file)
  85. })
  86. .catch(function (err) {
  87. console.error(err.message)
  88. process.exit(1)
  89. })
  90. })
  91. }
  92. /*
  93. * Logs a block of JSON data to the console
  94. */
  95. function log (data, heading) {
  96. var repr = ''
  97. if (process.stdout.isTTY) {
  98. if (heading) repr += '\033[32m →\033[30m ' + heading + '\033[0m\n\n'
  99. repr += ' ' + require('js-yaml').dump(data).replace(/\n/g, '\n ')
  100. repr = '\n' + repr.trim()
  101. } else {
  102. repr = JSON.stringify(data, null, 2)
  103. }
  104. console.log(repr)
  105. }
  106. /*
  107. * export
  108. */
  109. exports.getFonts = getFonts
  110. exports.getStrings = getStrings
  111. exports.getSummary = getSummary