userscript-utils
Advanced tools
+2
-2
| { | ||
| "name": "userscript-utils", | ||
| "version": "1.0.0-beta.1", | ||
| "version": "1.0.0-beta.2", | ||
| "description": "Useful tools for developing userscripts", | ||
@@ -26,2 +26,3 @@ "preferGlobal": true, | ||
| "async": "^2.0.0-alpha.0", | ||
| "minimist": "^1.0", | ||
| "get-stdin": "^5.0" | ||
@@ -51,3 +52,2 @@ }, | ||
| "chai": "^3.5", | ||
| "minimist": "^1.2", | ||
| "rimraf": "^2.5", | ||
@@ -54,0 +54,0 @@ "mocha": "^2.5" |
+153
-3
@@ -18,2 +18,5 @@ Useful tools for developing userscripts - in both CLI and API modes (CLI uses asynchronous calls while API can use either). | ||
| - [API examples](#api-examples) | ||
| - [Extracting the .meta.js metablock and writing it to a new file](#extracting-the-metajs-metablock-and-writing-it-to-a-new-file) | ||
| - [Extracting the full metadata block](#extracting-the-full-metadata-block) | ||
| - [Combining with UglifyJS](#extracting-the-full-metadata-block) | ||
| - [Grunt task example](#grunt-task-example) | ||
@@ -84,3 +87,3 @@ - [Notes before running tests](#notes-before-running-tests) | ||
| ## API usage | ||
| See [generated JSDoc](https://cdn.rawgit.com/Alorel/userscript-utils/1.0.0-beta.1/docs/index.html). | ||
| See [generated JSDoc](https://cdn.rawgit.com/Alorel/userscript-utils/1.0.0-beta.2/docs/index.html). | ||
@@ -119,10 +122,157 @@ # Examples | ||
| ## API examples | ||
| ### Extracting the `.meta.js` metablock and writing it to a new file: | ||
| ```js | ||
| var utils = require('userscript-utils').getUpdateMetablock, | ||
| fs = require('fs'), | ||
| inputFile = "foo.user.js", | ||
| outputFile = "foo.meta.js", | ||
| //For fs.writeFile | ||
| innerCallback = function (e) { | ||
| if (e) { | ||
| throw e; | ||
| } | ||
| }, | ||
| //For userscript-utils | ||
| outerCallback = function (e, o) { | ||
| if (e) { | ||
| throw e; | ||
| } else { | ||
| fs.writeFile(outputFile, o, 'utf8', innerCallback); | ||
| } | ||
| }; | ||
| [todo] | ||
| //Bare minimum | ||
| utils.fromFile(inputFile, outerCallback); | ||
| //Include @updateURL | ||
| utils.fromFile(inputFile, outerCallback, true); | ||
| //Include @downloadURL | ||
| utils.fromFile(inputFile, outerCallback, false, true); | ||
| //Include @updateURL @downloadURL | ||
| utils.fromFile(inputFile, outerCallback, true, true); | ||
| // For synchronous mode simply omit the callback argument and replate the method with "fromFileSync": | ||
| try { | ||
| utils.fromFileSync(inputFile); | ||
| } catch (e) { | ||
| //handle | ||
| } | ||
| //And you can just as easily do the same if you have the file contents as a string: | ||
| utils.fromString(stringContainingFileContents, outerCallback); | ||
| try { | ||
| utils.fromStringSync(stringContainingFileContents); | ||
| } catch (e) { | ||
| //handle | ||
| } | ||
| ``` | ||
| ### Extracting the full metadata block | ||
| ```js | ||
| var utils = require('userscript-utils').getMetablock, | ||
| fs = require('fs'), | ||
| inputFile = "foo.user.js", | ||
| outputFile = "foo.meta.js", | ||
| //For fs.writeFile | ||
| innerCallback = function (e) { | ||
| if (e) { | ||
| throw e; | ||
| } | ||
| }, | ||
| //For userscript-utils | ||
| outerCallback = function (e, o) { | ||
| if (e) { | ||
| throw e; | ||
| } else { | ||
| fs.writeFile(outputFile, o, 'utf8', innerCallback); | ||
| } | ||
| }; | ||
| //Async | ||
| utils.fromFile(inputFile, outerCallback); | ||
| // Sync | ||
| try { | ||
| utils.fromFileSync(inputFile); | ||
| } catch (e) { | ||
| //handle | ||
| } | ||
| //And you can just as easily do the same if you have the file contents as a string: | ||
| utils.fromString(stringContainingFileContents, outerCallback); | ||
| try { | ||
| utils.fromStringSync(stringContainingFileContents); | ||
| } catch (e) { | ||
| //handle | ||
| } | ||
| ``` | ||
| ### Combining with [UglifyJS](https://www.npmjs.com/package/uglify-js) | ||
| ```js | ||
| var utils = require('userscript-utils'), | ||
| minify = require('uglify-js').minify, | ||
| fs = require('fs'), | ||
| inFile = "foo.user.js", | ||
| outFile = "foo.min.user.js"; | ||
| //Get our metablock | ||
| utils.getMetablock.fromFile(inFile, function (e, metablock) { | ||
| if (e) { | ||
| throw e; | ||
| } else { | ||
| // Minify the JS | ||
| var minified = minify(inFile).code; | ||
| //Open our file for writing | ||
| fs.open(outFile, 'w', function (e, fd) { | ||
| if (e) { | ||
| throw e; | ||
| } else { | ||
| //Write our file | ||
| fs.write(fd, metablock + "\n" + minified, function (e) { | ||
| try { | ||
| if (e) { | ||
| throw e; | ||
| } | ||
| } finally { | ||
| fs.close(fd, function (e) { | ||
| if (e) { | ||
| throw e; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| ``` | ||
| ## Grunt task example | ||
| You'll need [grunt-run](https://www.npmjs.com/package/grunt-run): | ||
| ```sh | ||
| npm i --save-dev grunt-run; | ||
| ``` | ||
| Add a `run` task to your `Gruntfile.js` under any name (e.g. *foo*) containing a CLI command to execute, e.g.: | ||
| ```js | ||
| module.exports = function (grunt) { | ||
| // Initializing configuration objects | ||
| grunt.initConfig({ | ||
| run: { | ||
| foo: { | ||
| exec: 'userscript-utils get-updateblock -i foo.user.js -o foo.meta.js' | ||
| } | ||
| } | ||
| }); | ||
| [todo] | ||
| grunt.loadNpmTasks('grunt-run'); | ||
| }; | ||
| ``` | ||
| Run the task: | ||
| ```sh | ||
| grunt run:foo; | ||
| ``` | ||
| # Notes before running tests | ||
| If you cloned this repository and want to run tests be sure to run `npm link` beforehand otherwise you'll get errors! |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
49962
9.63%5
-16.67%275
120%3
50%+ Added
+ Added