grunt-swn-cachebuster 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.jshintrc CHANGED
@@ -1,13 +1,13 @@
1
- {
2
- "curly": true,
3
- "eqeqeq": true,
4
- "immed": true,
5
- "latedef": true,
6
- "newcap": true,
7
- "noarg": true,
8
- "sub": true,
9
- "undef": true,
10
- "boss": true,
11
- "eqnull": true,
12
- "node": true
13
- }
1
+ {
2
+ "curly": true,
3
+ "eqeqeq": true,
4
+ "immed": true,
5
+ "latedef": true,
6
+ "newcap": true,
7
+ "noarg": true,
8
+ "sub": true,
9
+ "undef": true,
10
+ "boss": true,
11
+ "eqnull": true,
12
+ "node": true
13
+ }
package/Gruntfile.js CHANGED
@@ -1,73 +1,73 @@
1
- /*
2
- * grunt-swn-cachebuster
3
- * https://github.com/Swaven/grunt-swn-cachebuster
4
- *
5
- * Copyright (c) 2017 Swaven
6
- * Licensed under the MIT license.
7
- */
8
-
9
- 'use strict';
10
-
11
- module.exports = function(grunt) {
12
-
13
- // Project configuration.
14
- grunt.initConfig({
15
- jshint: {
16
- all: [
17
- 'Gruntfile.js',
18
- 'tasks/*.js',
19
- '<%= nodeunit.tests %>'
20
- ],
21
- options: {
22
- jshintrc: '.jshintrc'
23
- }
24
- },
25
-
26
- // Before generating any new files, remove any previously-created files.
27
- clean: {
28
- tests: ['tmp']
29
- },
30
-
31
- // Configuration to be run (and then tested).
32
- cachebuster: {
33
- default_options: {
34
- options: {
35
- },
36
- files: {
37
- 'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
38
- }
39
- },
40
- custom_options: {
41
- options: {
42
- separator: ': ',
43
- punctuation: ' !!!'
44
- },
45
- files: {
46
- 'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
47
- }
48
- }
49
- },
50
-
51
- // Unit tests.
52
- nodeunit: {
53
- tests: ['test/*_test.js']
54
- }
55
-
56
- });
57
-
58
- // Actually load this plugin's task(s).
59
- grunt.loadTasks('tasks');
60
-
61
- // These plugins provide necessary tasks.
62
- grunt.loadNpmTasks('grunt-contrib-jshint');
63
- grunt.loadNpmTasks('grunt-contrib-clean');
64
- grunt.loadNpmTasks('grunt-contrib-nodeunit');
65
-
66
- // Whenever the "test" task is run, first clean the "tmp" dir, then run this
67
- // plugin's task(s), then test the result.
68
- grunt.registerTask('test', ['clean', 'cachebuster', 'nodeunit']);
69
-
70
- // By default, lint and run all tests.
71
- grunt.registerTask('default', ['jshint', 'test']);
72
-
73
- };
1
+ /*
2
+ * grunt-swn-cachebuster
3
+ * https://github.com/Swaven/grunt-swn-cachebuster
4
+ *
5
+ * Copyright (c) 2017 Swaven
6
+ * Licensed under the MIT license.
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ module.exports = function(grunt) {
12
+
13
+ // Project configuration.
14
+ grunt.initConfig({
15
+ jshint: {
16
+ all: [
17
+ 'Gruntfile.js',
18
+ 'tasks/*.js',
19
+ '<%= nodeunit.tests %>'
20
+ ],
21
+ options: {
22
+ jshintrc: '.jshintrc'
23
+ }
24
+ },
25
+
26
+ // Before generating any new files, remove any previously-created files.
27
+ clean: {
28
+ tests: ['tmp']
29
+ },
30
+
31
+ // Configuration to be run (and then tested).
32
+ cachebuster: {
33
+ default_options: {
34
+ options: {
35
+ },
36
+ files: {
37
+ 'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
38
+ }
39
+ },
40
+ custom_options: {
41
+ options: {
42
+ separator: ': ',
43
+ punctuation: ' !!!'
44
+ },
45
+ files: {
46
+ 'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
47
+ }
48
+ }
49
+ },
50
+
51
+ // Unit tests.
52
+ nodeunit: {
53
+ tests: ['test/*_test.js']
54
+ }
55
+
56
+ });
57
+
58
+ // Actually load this plugin's task(s).
59
+ grunt.loadTasks('tasks');
60
+
61
+ // These plugins provide necessary tasks.
62
+ grunt.loadNpmTasks('grunt-contrib-jshint');
63
+ grunt.loadNpmTasks('grunt-contrib-clean');
64
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
65
+
66
+ // Whenever the "test" task is run, first clean the "tmp" dir, then run this
67
+ // plugin's task(s), then test the result.
68
+ grunt.registerTask('test', ['clean', 'cachebuster', 'nodeunit']);
69
+
70
+ // By default, lint and run all tests.
71
+ grunt.registerTask('default', ['jshint', 'test']);
72
+
73
+ };
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2017 Swaven
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Swaven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,62 +1,55 @@
1
- # grunt-swn-cachebuster
2
-
3
- > Appends timestamp param to import urls
4
-
5
- ## Getting Started
6
- This plugin requires Grunt `>=0.4.5`
7
-
8
- If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
9
-
10
- ```shell
11
- npm install grunt-swn-cachebuster --save-dev
12
- ```
13
-
14
- Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
15
-
16
- ```js
17
- grunt.loadNpmTasks('grunt-swn-cachebuster');
18
- ```
19
-
20
- ## The "cachebuster" task
21
-
22
- ### Overview
23
-
24
- This task looks into source files, and edits all html and js file urls to append a timestamp query string parameter. It ignores urls that start with */bower_components*.
25
-
26
- In your project's Gruntfile, add a section named `cachebuster` to the data object passed into `grunt.initConfig()`.
27
-
28
- ```js
29
- grunt.initConfig({
30
- cachebuster: {
31
- your_target: {
32
- // Target-specific file lists and/or options go here.
33
- },
34
- },
35
- });
36
- ```
37
-
38
- ### Usage Examples
39
-
40
- ```js
41
- grunt.initConfig({
42
- cachebuster: {
43
- all: {
44
- expand: true,
45
- cwd: 'assets/',
46
- src: ['*.html', 'elements/**/*.html', '!build/', 'elements/swn-app/swn-app.js'],
47
- dest: 'assets/build/'
48
- },
49
- },
50
- });
51
- ```
52
-
53
- ## Contributing
54
- In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
55
-
56
- ## Release History
57
-
58
- * 2017-09-18: [v0.2.0](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.2.0): error handling
59
- * 2017-09-18: [v0.1.3](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.3): add some tests
60
- * 2017-06-28: [v0.1.2](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.2): fix regex
61
- * 2017-06-27: [v0.1.1](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.1): more robust regex
62
- * 2017-06-27: [v0.1.0](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.0): first release
1
+ # grunt-swn-cachebuster
2
+
3
+ > Appends timestamp param to import urls
4
+
5
+ ## Getting Started
6
+
7
+ This plugin requires **Node.js `>=22`** and **Grunt `^1.6.1`**.
8
+
9
+ If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
10
+
11
+ ```shell
12
+ npm install grunt-swn-cachebuster --save-dev
13
+ ```
14
+
15
+ Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
16
+
17
+ ```js
18
+ grunt.loadNpmTasks('grunt-swn-cachebuster');
19
+ ```
20
+
21
+ ## The "cachebuster" task
22
+
23
+ ### Overview
24
+
25
+ This task looks into source files, and edits all html and js file urls to append a timestamp query string parameter. It ignores urls that start with */bower_components*.
26
+
27
+ In your project's Gruntfile, add a section named `cachebuster` to the data object passed into `grunt.initConfig()`.
28
+
29
+ ```js
30
+ grunt.initConfig({
31
+ cachebuster: {
32
+ your_target: {
33
+ // Target-specific file lists and/or options go here.
34
+ },
35
+ },
36
+ });
37
+ ```
38
+
39
+ ### Usage Examples
40
+
41
+ ```js
42
+ grunt.initConfig({
43
+ cachebuster: {
44
+ all: {
45
+ expand: true,
46
+ cwd: 'assets/',
47
+ src: ['*.html', 'elements/**/*.html', '!build/', 'elements/swn-app/swn-app.js'],
48
+ dest: 'assets/build/'
49
+ },
50
+ },
51
+ });
52
+ ```
53
+
54
+ ## Contributing
55
+ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
@@ -1,13 +1,14 @@
1
- 'use strict'
2
-
3
- const rx = /(\/?bower_components)?(\/.*\.(html|js))('|"|`)/gi, // find all dependencies except bower components
4
- ts = Date.now() // timestamp we'll add to all dependencies urls
5
-
6
- module.exports = exports = function replace(content){
7
- if (typeof content !== 'string')
8
- throw new Error('Content is not a string')
9
-
10
- return content.replace(rx, ($0, $1, $2, $3, $4) => {
11
- return $1 ? $0 : `${$2}?v=${ts}${$4}`
12
- })
13
- }
1
+ 'use strict'
2
+
3
+ const rx = /(\/?bower_components)?(\/.*\.(html|js))('|"|`)/gi, // find all dependencies
4
+ ts = Date.now() // timestamp we'll add to all dependencies urls
5
+
6
+ module.exports = exports = function replace(content){
7
+ if (typeof content !== 'string')
8
+ throw new Error(`Content is not a string`)
9
+
10
+ return content.replace(rx, ($0, $1, $2, $3, $4) => {
11
+ // ignore urls to bower_components
12
+ return $1 ? $0 : `${$2}?v=${ts}${$4}`
13
+ })
14
+ }
package/package.json CHANGED
@@ -1,42 +1,45 @@
1
- {
2
- "name": "grunt-swn-cachebuster",
3
- "description": "Appends timestamp param to import urls",
4
- "version": "0.2.0",
5
- "homepage": "https://github.com/Swaven/grunt-swn-cachebuster",
6
- "author": {
7
- "name": "Swaven",
8
- "email": "antoine@swaven.com"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/Swaven/grunt-swn-cachebuster.git"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/Swaven/grunt-swn-cachebuster/issues"
16
- },
17
- "licenses": [
18
- {
19
- "type": "MIT",
20
- "url": "https://github.com/Swaven/grunt-swn-cachebuster/blob/master/LICENSE-MIT"
21
- }
22
- ],
23
- "engines": {
24
- "node": ">=6.10.2"
25
- },
26
- "scripts": {
27
- "test": "mocha"
28
- },
29
- "devDependencies": {
30
- "chai": "^4.1.2",
31
- "grunt": "~0.4.5",
32
- "grunt-contrib-clean": "^0.5.0",
33
- "grunt-contrib-jshint": "^0.9.2",
34
- "grunt-contrib-nodeunit": "^0.3.3"
35
- },
36
- "keywords": [
37
- "gruntplugin"
38
- ],
39
- "dependencies": {
40
- "mkdirp": "^0.5.1"
41
- }
42
- }
1
+ {
2
+ "name": "grunt-swn-cachebuster",
3
+ "description": "Appends timestamp param to import urls",
4
+ "version": "0.3.0",
5
+ "homepage": "https://github.com/Swaven/grunt-swn-cachebuster",
6
+ "author": {
7
+ "name": "Swaven",
8
+ "email": "antoine@swaven.com"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/Swaven/grunt-swn-cachebuster.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/Swaven/grunt-swn-cachebuster/issues"
16
+ },
17
+ "licenses": [
18
+ {
19
+ "type": "MIT",
20
+ "url": "https://github.com/Swaven/grunt-swn-cachebuster/blob/master/LICENSE-MIT"
21
+ }
22
+ ],
23
+ "engines": {
24
+ "node": ">=22.0.0"
25
+ },
26
+ "scripts": {
27
+ "test": "mocha"
28
+ },
29
+ "overrides": {
30
+ "js-yaml": "~3.14.2",
31
+ "lodash": "~4.18.1",
32
+ "minimatch": "^3.1.3 || ^5.1.8 || ^9.0.9"
33
+ },
34
+ "devDependencies": {
35
+ "chai": "^4.1.2",
36
+ "grunt": "^1.6.1",
37
+ "grunt-contrib-clean": "^2.0.1",
38
+ "grunt-contrib-jshint": "^3.2.0",
39
+ "grunt-contrib-nodeunit": "^5.0.0",
40
+ "mocha": "^11.0.0"
41
+ },
42
+ "keywords": [
43
+ "gruntplugin"
44
+ ]
45
+ }
@@ -1,70 +1,65 @@
1
- 'use strict'
2
-
3
- /*
4
- * grunt-swn-cachebuster
5
- * https://github.com/Swaven/grunt-swn-cachebuster
6
- *
7
- * Copyright (c) 2017 Swaven
8
- * Licensed under the MIT license.
9
- */
10
-
11
- const fs = require('fs'),
12
- path = require('path'),
13
- mkdirp = require('mkdirp'),
14
- replace = require('./replace.js')
15
-
16
- module.exports = function(grunt){
17
-
18
-
19
- grunt.registerMultiTask('cachebuster', 'Appends timestamp param to import urls', function(){
20
- const done = this.async(),
21
- options = this.options(),
22
- self = this
23
-
24
- var proms = []
25
-
26
- // get all files' content and dependencies
27
- for (let i = 0; i < this.files.length; i++){
28
- proms.push(updateLinks(this.files[i]))
29
- }
30
-
31
- Promise.all(proms)
32
- .then(() => {
33
- done(true)
34
- })
35
- })
36
-
37
- // reads file, updates all dependencies urls and write at destination
38
- function updateLinks(fileInfo){
39
- return new Promise((resolve, reject) => {
40
- var filepath = fileInfo.src[0]
41
- fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
42
- if (err){
43
- grunt.fail.warn(`Error reading ${filepath}: ${err}`)
44
- return reject()
45
- }
46
- // add timestamp query string to all matches
47
- try{
48
- let result = replace(data)
49
- return resolve(result)
50
- }
51
- catch(err){
52
- grunt.file.warn(`Error replacing ${filepath}: ${err}`)
53
- }
54
- })
55
- })
56
- .then(data => {
57
- return new Promise((resolve, reject) => {
58
- mkdirp.sync(path.dirname(fileInfo.dest))
59
- fs.writeFile(fileInfo.dest, data, 'utf8', (err, data) => {
60
- if (err){
61
- grunt.fail.warn(`Error writing ${fileInfo.dest}: ${err}`)
62
- return reject()
63
- }
64
- return resolve()
65
- })
66
- })
67
- })
68
-
69
- }
70
- }
1
+ 'use strict'
2
+
3
+ /*
4
+ * grunt-swn-cachebuster
5
+ * https://github.com/Swaven/grunt-swn-cachebuster
6
+ *
7
+ * Copyright (c) 2017 Swaven
8
+ * Licensed under the MIT license.
9
+ */
10
+
11
+ const fs = require('fs'),
12
+ path = require('path'),
13
+ replace = require('../lib/replace.js'),
14
+ util = require('util'),
15
+ lstat = util.promisify(fs.lstat),
16
+ readFile = util.promisify(fs.readFile),
17
+ writeFile = util.promisify(fs.writeFile)
18
+
19
+ module.exports = function(grunt){
20
+
21
+
22
+ grunt.registerMultiTask('cachebuster', 'Appends timestamp param to import urls', function(){
23
+ const done = this.async(),
24
+ options = this.options(),
25
+ self = this
26
+
27
+ var proms = []
28
+
29
+ // get all files' content and dependencies
30
+ for (let i = 0; i < this.files.length; i++){
31
+ proms.push(updateLinks(this.files[i]))
32
+ }
33
+
34
+ Promise.all(proms)
35
+ .then(() => {
36
+ done(true)
37
+ })
38
+ })
39
+
40
+ // reads file, updates all dependencies urls and write at destination
41
+ async function updateLinks(fileInfo){
42
+ var filepath = fileInfo.src[0]
43
+
44
+ try{
45
+ let stats = await lstat(filepath)
46
+
47
+ // not a file: ignore
48
+ if (!stats.isFile()){
49
+ // console.log(`${filepath} is not a file, ignore`)
50
+ return Promise.resolve()
51
+ }
52
+
53
+ let data = await readFile(filepath, {encoding: 'utf8'}),
54
+ result = replace(data)
55
+
56
+ // write update content to output file
57
+ fs.mkdirSync(path.dirname(fileInfo.dest), { recursive: true })
58
+ await writeFile(fileInfo.dest, result, 'utf8')
59
+ return Promise.resolve()
60
+ }
61
+ catch(err){
62
+ grunt.fail.warn(`${filepath}: ${err.message}`)
63
+ }
64
+ }
65
+ }
package/test/test.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  const assert = require('chai').assert
3
3
 
4
- var replace = require('../tasks/replace.js')
4
+ var replace = require('../lib/replace.js')
5
5
 
6
6
  describe('Replace', () => {
7
7
  it('ignore bower_components', () => {
package/.npmignore DELETED
@@ -1,3 +0,0 @@
1
- node_modules
2
- npm-debug.log
3
- tmp