From 9eb33cf7c38c4e2c3f8bf93aaaa9bc2429e72c7f Mon Sep 17 00:00:00 2001 From: AleBles Date: Mon, 4 Nov 2019 10:34:52 +0100 Subject: [PATCH] * added winston for logging * added posibility for getting image from config * cleaned up build dir and console output --- CHANGELOG.md | 10 ++ README.md | 10 +- dist/h5-texture-unpacker.js | 52 +-------- package-lock.json | 227 +++++++++++++++++++++++++++++------- package.json | 10 +- rollup.config.ts | 12 +- src/h5-texture-unpacker.ts | 50 ++++++-- tsconfig.json | 4 +- 8 files changed, 261 insertions(+), 114 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e16fa0..1e9a135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## [0.0.2] - 2019-11-04 +### Added +- Winston logging with a verbose check +- check to see if img exists +- Can now read img file from json config + +### Fixed +- error reporting image size equals zero is nog caught +- cleaned up dist folder + ## [0.0.1] - 2019-11-01 ### Added - Initial setup and release diff --git a/README.md b/README.md index 2c917d1..ff3ab6c 100644 --- a/README.md +++ b/README.md @@ -12,24 +12,24 @@ ale@computer:~$ npm install @azerion/h5-texture-unpacker -g Now you'll be able to use the command: ``` ale@computer:~$ unpack -Usage: unpack [imageFile] [jsonFile] [outputDir] +Usage: unpack -j [jsonFile] -o [outputDir] ``` Usage ----- ``` -ale@NL0NTB032:/media/ale/dev/tools/h5-texture-unpacker$ unpack -Usage: unpack [imageFile] [jsonFile] [outputDir] +ale@computer:~$ unpack +Usage: unpack -j [jsonFile] -o [outputDir] Options: --version Show version number [boolean] - --imageFile, -i The input texture image [string] [required] + --imageFile, -i The input texture image [string] [default: ""] --jsonFile, -j The input JSON configuration [string] [required] --outputDir, -o The output folder [string] [required] + --verbose, -v Show log messages [boolean] [default: false] -h, --help Show help [boolean] copyright Azerion 2019 - ``` TODO diff --git a/dist/h5-texture-unpacker.js b/dist/h5-texture-unpacker.js index 7dbf969..106d70b 100644 --- a/dist/h5-texture-unpacker.js +++ b/dist/h5-texture-unpacker.js @@ -1,51 +1,3 @@ #!/usr/bin/env node -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var Sharp = require('sharp'); -var path = require('path'); -var fs = require('fs'); -var yargs = require('yargs'); -var basePath = path.join(__dirname, '../'); -var argv = yargs - .usage('Usage: $0 [imageFile] [jsonFile] [outputDir]') - .option('imageFile', { - alias: 'i', - demandOption: true, - describe: 'The input texture image', - type: 'string' -}) - .option('jsonFile', { - alias: 'j', - demandOption: true, - describe: 'The input JSON configuration', - type: 'string' -}) - .option('outputDir', { - alias: 'o', - demandOption: true, - describe: 'The output folder', - type: 'string' -}) - .help('h') - .alias('h', 'help') - .epilog('copyright Azerion 2019') - .argv; -var imgUrl = path.join(argv.i); -var jsonUrl = path.join(argv.j); -var outputDir = path.join(argv.o, '/'); -var json = JSON.parse(fs.readFileSync(jsonUrl)); -if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); -} -json.frames.forEach(function (source) { - Sharp(imgUrl).extract({ - width: source.frame.w, - height: source.frame.h, - left: source.frame.x, - top: source.frame.y - }).toFile(path.join(outputDir, '/', source.filename + '.png')).then(function (fileInfo) { - console.log('File: ', source.filename, ' written'); - }).catch(function (err) { - console.log('Unable to write file ', source.filename, ':', err); - }); -}); + +var e=require("sharp"),i=require("path"),t=require("fs"),r=require("yargs"),n=require("winston"),o=(new Date).toISOString(),a=n.format.printf((function(e){return o+"-"+e.level+": "+JSON.stringify(e.message,null,4)+"\n"})),s=n.createLogger({transports:[new n.transports.Console({format:n.format.combine(n.format.colorize(),a)})]}),l=(i.join(__dirname,"../"),r.usage("Usage: $0 -j [jsonFile] -o [outputDir]").option("imageFile",{alias:"i",demandOption:!1,default:"",describe:"The input texture image",type:"string"}).option("jsonFile",{alias:"j",demandOption:!0,describe:"The input JSON configuration",type:"string"}).option("outputDir",{alias:"o",demandOption:!0,describe:"The output folder",type:"string"}).option("verbose",{alias:"v",default:!1,describe:"Show log messages",type:"boolean"}).help("h").alias("h","help").epilog("copyright Azerion 2019").argv),f=i.join(l.i),p=i.join(l.j),m=i.join(l.o,"/"),u=JSON.parse(t.readFileSync(p));s.level=!0===l.verbose?"info":"warn",""===l.i&&(f=i.join(i.parse(p).dir,u.meta.image)),t.existsSync(f)||(s.error("Could not find image at specified path!"),process.exit(1)),t.existsSync(m)||t.mkdirSync(m),u.frames.forEach((function(t){0!==t.frame.w&&0!==t.frame.h?e(f).extract({width:t.frame.w,height:t.frame.h,left:t.frame.x,top:t.frame.y}).toFile(i.join(m,"/",t.filename+".png")).then((function(e){s.info("File: "+t.filename+" written")})).catch((function(e){s.info("Unable to write file , "+t.filename+":, "+e)})):s.warn("Unable to write file , "+t.filename+": Source width/height is 0")})); diff --git a/package-lock.json b/package-lock.json index 0c155fc..3bf6890 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@azerion/h5-texture-unpacker", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -752,9 +752,10 @@ "dev": true }, "@types/node": { - "version": "10.17.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.2.tgz", - "integrity": "sha512-sAh60KDol+MpwOr1RTK0+HgBEYejKsxdpmrOS1Wts5bI03dLzq8F7T0sRXDKeaEK8iWDlGfdzxrzg6vx/c5pNA==" + "version": "10.17.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.3.tgz", + "integrity": "sha512-QZ9CjUB3QoA3f2afw3utKlfRPhpmufB7jC2+oDhLWnXqoyx333fhKSQDLQu2EK7OE0a15X67eYiRAaJsHXrpMA==", + "dev": true }, "@types/semver": { "version": "6.2.0", @@ -766,6 +767,7 @@ "version": "0.23.0", "resolved": "https://registry.npmjs.org/@types/sharp/-/sharp-0.23.0.tgz", "integrity": "sha512-lZxg80QILInYfuh1RIqd4+s+zprqs8Z7w7Pd5fNDlQdlgND5wb9bM/S5TqElwCUxegL2rafuBstu89aPhcl4IQ==", + "dev": true, "requires": { "@types/node": "*" } @@ -1599,11 +1601,35 @@ "simple-swizzle": "^0.2.2" } }, + "colornames": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/colornames/-/colornames-1.1.1.tgz", + "integrity": "sha1-+IiQMGhcfE/54qVZ9Qd+t2qBb5Y=" + }, "colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", - "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", - "dev": true + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" + }, + "colorspace": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/colorspace/-/colorspace-1.1.2.tgz", + "integrity": "sha512-vt+OoIP2d76xLhjwbBaucYlNSpPsrJWPlBTtwCpQKIu6/CSMutyzX93O/Do0qzpH3YoHEes8YEFXyZ797rEhzQ==", + "requires": { + "color": "3.0.x", + "text-hex": "1.0.x" + }, + "dependencies": { + "color": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/color/-/color-3.0.0.tgz", + "integrity": "sha512-jCpd5+s0s0t7p3pHQKpnJ0TpQKKdleP71LWcA0aqiljpiuAkOSUFN/dyH8ZwF0hRmFlrIuRhufds1QyEP9EB+w==", + "requires": { + "color-convert": "^1.9.1", + "color-string": "^1.5.2" + } + } + } }, "combined-stream": { "version": "1.0.8", @@ -2158,6 +2184,16 @@ "integrity": "sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I=", "dev": true }, + "diagnostics": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/diagnostics/-/diagnostics-1.1.1.tgz", + "integrity": "sha512-8wn1PmdunLJ9Tqbx+Fx/ZEuHfJf4NKSN2ZBj7SJC/OWRWha843+WsTjqMe1B5E3p28jqBlp+mJ2fPVxPyNgYKQ==", + "requires": { + "colorspace": "1.1.x", + "enabled": "1.0.x", + "kuler": "1.0.x" + } + }, "diff": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", @@ -2233,6 +2269,14 @@ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==" }, + "enabled": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/enabled/-/enabled-1.0.2.tgz", + "integrity": "sha1-ll9lE9LC0cX0ZStkouM5ZGf8L5M=", + "requires": { + "env-variable": "0.0.x" + } + }, "end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -2241,6 +2285,11 @@ "once": "^1.4.0" } }, + "env-variable": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/env-variable/-/env-variable-0.0.5.tgz", + "integrity": "sha512-zoB603vQReOFvTg5xMl9I1P2PnHsHQQKTEowsKKD7nseUfJq6UWzK+4YtlWUO1nhiQUxe6XMkk+JleSZD1NZFA==" + }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -2575,6 +2624,11 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "fast-safe-stringify": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz", + "integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==" + }, "fb-watchman": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.0.tgz", @@ -2584,6 +2638,11 @@ "bser": "^2.0.0" } }, + "fecha": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fecha/-/fecha-2.3.3.tgz", + "integrity": "sha512-lUGBnIamTAwk4znq5BcqsDaxSmZ9nDVJaij6NvRt/Tg4R69gERA+otPKbS86ROw9nxVMw2/mp1fnaiWqbs6Sdg==" + }, "figures": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz", @@ -4093,8 +4152,7 @@ "is-stream": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", - "dev": true + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" }, "is-symbol": { "version": "1.0.2", @@ -5063,6 +5121,14 @@ "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", "dev": true }, + "kuler": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kuler/-/kuler-1.0.1.tgz", + "integrity": "sha512-J9nVUucG1p/skKul6DU3PUZrhs0LPulNaeUOox0IyXDi8S4CztTHs1gQphhuZmzXG7VOQSf6NJfKuzteQLv9gQ==", + "requires": { + "colornames": "^1.1.1" + } + }, "lcov-parse": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/lcov-parse/-/lcov-parse-0.0.10.tgz", @@ -5253,8 +5319,7 @@ "lodash": { "version": "4.17.14", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.14.tgz", - "integrity": "sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==", - "dev": true + "integrity": "sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==" }, "lodash._reinterpolate": { "version": "3.0.0", @@ -5368,6 +5433,18 @@ } } }, + "logform": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/logform/-/logform-2.1.2.tgz", + "integrity": "sha512-+lZh4OpERDBLqjiwDLpAWNQu6KMjnlXH2ByZwCuSqVPJletw0kTWJf5CgSNAUKn1KUkv3m2cUz/LK8zyEy7wzQ==", + "requires": { + "colors": "^1.2.1", + "fast-safe-stringify": "^2.0.4", + "fecha": "^2.3.3", + "ms": "^2.1.1", + "triple-beam": "^1.3.0" + } + }, "longest": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-2.0.1.tgz", @@ -5615,8 +5692,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "mute-stream": { "version": "0.0.7", @@ -5877,6 +5953,11 @@ "wrappy": "1" } }, + "one-time": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/one-time/-/one-time-0.0.4.tgz", + "integrity": "sha1-+M33eISCb+Tf+T46nMN7HkSAdC4=" + }, "onetime": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/onetime/-/onetime-2.0.1.tgz", @@ -6257,6 +6338,43 @@ "revalidator": "0.1.x", "utile": "0.3.x", "winston": "2.1.x" + }, + "dependencies": { + "async": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz", + "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=", + "dev": true + }, + "winston": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/winston/-/winston-2.1.1.tgz", + "integrity": "sha1-PJNJ0ZYgf9G9/51LxD73JRDjoS4=", + "dev": true, + "requires": { + "async": "~1.0.0", + "colors": "1.0.x", + "cycle": "1.0.x", + "eyes": "0.1.x", + "isstream": "0.1.x", + "pkginfo": "0.3.x", + "stack-trace": "0.0.x" + }, + "dependencies": { + "colors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", + "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=", + "dev": true + }, + "pkginfo": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.3.1.tgz", + "integrity": "sha1-Wyn2qB9wcXFC4J52W76rl7T4HiE=", + "dev": true + } + } + } } }, "prompts": { @@ -6728,6 +6846,16 @@ "@types/node": "*" } }, + "rollup-plugin-add-shebang": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/rollup-plugin-add-shebang/-/rollup-plugin-add-shebang-0.3.1.tgz", + "integrity": "sha512-tKONSgKoVw9Om1cp1CnAlPQ9nsHBzu8fInKObX3zT5KZVoAJtslD1aBL84lJuKLeh+L28dB26CBBeYT+doTMLg==", + "dev": true, + "requires": { + "magic-string": "^0.25.3", + "rollup-pluginutils": "^2.8.1" + } + }, "rollup-plugin-commonjs": { "version": "9.3.4", "resolved": "https://registry.npmjs.org/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.3.4.tgz", @@ -7383,8 +7511,7 @@ "stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", - "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=", - "dev": true + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" }, "stack-utils": { "version": "1.0.2", @@ -7692,6 +7819,11 @@ "integrity": "sha512-wiBrwC1EhBelW12Zy26JeOUkQ5mRu+5o8rpsJk5+2t+Y5vE7e842qtZDQ2g1NpX/29HdyFeJ4nSIhI47ENSxlQ==", "dev": true }, + "text-hex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", + "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" + }, "throat": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/throat/-/throat-4.1.0.tgz", @@ -7822,6 +7954,11 @@ "integrity": "sha1-n5up2e+odkw4dpi8v+sshI8RrbM=", "dev": true }, + "triple-beam": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.3.0.tgz", + "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" + }, "ts-jest": { "version": "23.10.5", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-23.10.5.tgz", @@ -8269,40 +8406,50 @@ } }, "winston": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/winston/-/winston-2.1.1.tgz", - "integrity": "sha1-PJNJ0ZYgf9G9/51LxD73JRDjoS4=", - "dev": true, + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/winston/-/winston-3.2.1.tgz", + "integrity": "sha512-zU6vgnS9dAWCEKg/QYigd6cgMVVNwyTzKs81XZtTFuRwJOcDdBg7AU0mXVyNbs7O5RH2zdv+BdNZUlx7mXPuOw==", "requires": { - "async": "~1.0.0", - "colors": "1.0.x", - "cycle": "1.0.x", - "eyes": "0.1.x", - "isstream": "0.1.x", - "pkginfo": "0.3.x", - "stack-trace": "0.0.x" + "async": "^2.6.1", + "diagnostics": "^1.1.1", + "is-stream": "^1.1.0", + "logform": "^2.1.1", + "one-time": "0.0.4", + "readable-stream": "^3.1.1", + "stack-trace": "0.0.x", + "triple-beam": "^1.3.0", + "winston-transport": "^4.3.0" }, "dependencies": { "async": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz", - "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=", - "dev": true - }, - "colors": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", - "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=", - "dev": true + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.3.tgz", + "integrity": "sha512-zflvls11DCy+dQWzTW2dzuilv8Z5X/pjfmZOWba6TNIVDm+2UDaJmXSOXlasHKfNBs8oo3M0aT50fDEWfKZjXg==", + "requires": { + "lodash": "^4.17.14" + } }, - "pkginfo": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.3.1.tgz", - "integrity": "sha1-Wyn2qB9wcXFC4J52W76rl7T4HiE=", - "dev": true + "readable-stream": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.4.0.tgz", + "integrity": "sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } } } }, + "winston-transport": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/winston-transport/-/winston-transport-4.3.0.tgz", + "integrity": "sha512-B2wPuwUi3vhzn/51Uukcao4dIduEiPOcOt9HJ3QeaXgkJ5Z7UwpBzxS4ZGNHtrxrUvTwemsQiSys0ihOf8Mp1A==", + "requires": { + "readable-stream": "^2.3.6", + "triple-beam": "^1.2.0" + } + }, "word-wrap": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", diff --git a/package.json b/package.json index 9b944d2..20445c0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@azerion/h5-texture-unpacker", "author": "Azerion", - "version": "0.0.1", + "version": "0.0.2", "description": "Simple CLI tool that allows you to unpack your existing Texture atlasses provided that you have the JSON file available", "contributors": [ { @@ -27,7 +27,7 @@ "scripts": { "lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", "prebuild": "rimraf dist", - "build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src", + "build": "rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src", "start": "rollup -c rollup.config.ts -w", "test": "jest --coverage", "test:watch": "jest --coverage --watch", @@ -86,7 +86,8 @@ "@commitlint/cli": "^8.2.0", "@commitlint/config-conventional": "^7.6.0", "@types/jest": "^23.3.2", - "@types/node": "^10.17.2", + "@types/node": "^10.17.3", + "@types/sharp": "^0.23.0", "@types/yargs": "^13.0.3", "colors": "^1.4.0", "commitizen": "^4.0.3", @@ -103,6 +104,7 @@ "replace-in-file": "^3.4.4", "rimraf": "^2.7.1", "rollup": "^0.67.0", + "rollup-plugin-add-shebang": "^0.3.1", "rollup-plugin-commonjs": "^9.3.4", "rollup-plugin-node-resolve": "^3.4.0", "rollup-plugin-preserve-shebangs": "^0.1.2", @@ -119,8 +121,8 @@ "typescript": "^3.6.3" }, "dependencies": { - "@types/sharp": "^0.23.0", "sharp": "^0.23.2", + "winston": "^3.2.1", "yargs": "^14.2.0" } } diff --git a/rollup.config.ts b/rollup.config.ts index 72204d3..d97b3fe 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -3,7 +3,7 @@ import commonjs from 'rollup-plugin-commonjs'; import typescript from 'rollup-plugin-typescript2'; import serve from 'rollup-plugin-serve'; import { terser } from 'rollup-plugin-terser' -import { preserveShebangs } from 'rollup-plugin-preserve-shebangs'; +import shebang from 'rollup-plugin-add-shebang'; const pkg = require('./package.json'); @@ -14,12 +14,12 @@ const isWatching = process.argv.findIndex((element) => element === '-w') !== -1; let basePlugins = []; if (isWatching) { - basePlugins = [ + basePlugins.push( serve({ contentBase: ['./', './example'], port: 8080 }) - ]; + ); } export default { input: `src/${libraryName}.ts`, @@ -32,7 +32,6 @@ export default { include: 'src/**', }, plugins: basePlugins.concat([ - preserveShebangs(), // Compile TypeScript files typescript({useTsconfigDeclarationDir: false, objectHashIgnoreUnknownHack: true}), // Allow node_modules resolution, so you can use 'external' to control @@ -42,7 +41,10 @@ export default { // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs) commonjs(), terser({ - include: [/^.+\.min\.js$/], + include: [/^.+\.js$/], + }), + shebang({ + include: `dist/${libraryName}.js` }) ]) }; diff --git a/src/h5-texture-unpacker.ts b/src/h5-texture-unpacker.ts index 719deaa..c78c620 100644 --- a/src/h5-texture-unpacker.ts +++ b/src/h5-texture-unpacker.ts @@ -1,12 +1,22 @@ -#!/usr/bin/env node - import { OutputInfo } from 'sharp' const Sharp = require('sharp') const path = require('path') const fs = require('fs') const yargs = require('yargs') +const winston = require('winston') +let date = new Date().toISOString() +const logFormat = winston.format.printf(function(info: any) { + return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n` +}) +const logger = winston.createLogger({ + transports: [ + new winston.transports.Console({ + format: winston.format.combine(winston.format.colorize(), logFormat) + }) + ] +}) const basePath = path.join(__dirname, '../') declare interface IMatrix { @@ -41,12 +51,12 @@ declare interface IJSONAtlas { scale: number } } - const argv = yargs - .usage('Usage: $0 [imageFile] [jsonFile] [outputDir]') + .usage('Usage: $0 -j [jsonFile] -o [outputDir]') .option('imageFile', { alias: 'i', - demandOption: true, + demandOption: false, + default: '', describe: 'The input texture image', type: 'string' }) @@ -62,20 +72,44 @@ const argv = yargs describe: 'The output folder', type: 'string' }) + .option('verbose', { + alias: 'v', + default: false, + describe: 'Show log messages', + type: 'boolean' + }) .help('h') .alias('h', 'help') .epilog('copyright Azerion 2019').argv -const imgUrl: string = path.join(argv.i) +let imgUrl: string = path.join(argv.i) const jsonUrl: string = path.join(argv.j) const outputDir: string = path.join(argv.o, '/') const json: IJSONAtlas = JSON.parse(fs.readFileSync(jsonUrl)) +logger.level = argv.verbose === true ? 'info' : 'warn' + +if (argv.i === '') { + //Image path empty string, let's see if we can get the image from the json + imgUrl = path.join(path.parse(jsonUrl).dir, json.meta.image) +} + +if (!fs.existsSync(imgUrl)) { + //Let's check if the image file is found + logger.error('Could not find image at specified path!') + process.exit(1) +} + if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir) } json.frames.forEach((source: IFrame) => { + if (source.frame.w === 0 || source.frame.h === 0) { + logger.warn(`Unable to write file , ${source.filename}: Source width/height is 0`) + + return + } Sharp(imgUrl) .extract({ width: source.frame.w, @@ -85,9 +119,9 @@ json.frames.forEach((source: IFrame) => { }) .toFile(path.join(outputDir, '/', source.filename + '.png')) .then((fileInfo: OutputInfo) => { - console.log('File: ', source.filename, ' written') + logger.info(`File: ${source.filename} written`) }) .catch((err: any) => { - console.log('Unable to write file ', source.filename, ':', err) + logger.info(`Unable to write file , ${source.filename}:, ${err}`) }) }) diff --git a/tsconfig.json b/tsconfig.json index 1c1272d..d24e464 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "es2017", "dom" ], -// "allowJs": true, + "allowJs": true, "strict": true, "sourceMap": false, "declaration": false, @@ -17,7 +17,7 @@ "experimentalDecorators": true, "emitDecoratorMetadata": true, "esModuleInterop": true, - "outDir": "dist", + "outDir": "dist/lib", "typeRoots": [ "node_modules/@types" ]