gulp-rev-all
Advanced tools
Comparing version 3.0.0 to 4.0.0
61
index.js
@@ -1,8 +0,8 @@ | ||
var Through = require("through2"); | ||
var Revisioner = require("./revisioner"); | ||
var PluginError = require("plugin-error"); | ||
import transformStream from "easy-transform-stream"; | ||
import Revisioner from "./revisioner.js"; | ||
import PluginError from "plugin-error"; | ||
var PLUGIN_NAME = "gulp-rev-all"; | ||
module.exports = { | ||
export default { | ||
revision: function (options) { | ||
@@ -12,10 +12,6 @@ var revisioner = new Revisioner(options); | ||
// Feed the RevAll Revisioner with all the files in the stream, don't emit them until all of them have been processed | ||
return Through.obj( | ||
function (file, enc, callback) { | ||
return transformStream({ objectMode: true }, | ||
function (file) { | ||
if (file.isStream()) { | ||
this.emit( | ||
"error", | ||
new PluginError(PLUGIN_NAME, "Streams not supported!") | ||
); | ||
return callback(); | ||
throw new PluginError(PLUGIN_NAME, "Streams not supported!"); | ||
} | ||
@@ -28,13 +24,6 @@ | ||
file.revisioner = revisioner; | ||
callback(); | ||
}, | ||
function (callback) { | ||
function () { | ||
revisioner.run(); | ||
var files = revisioner.files; | ||
for (var filename in files) { | ||
this.push(files[filename]); | ||
} | ||
callback(); | ||
return Object.values(revisioner.files); | ||
} | ||
@@ -48,4 +37,4 @@ ); | ||
// Drop any existing files off the stream, push the generated version file | ||
return Through.obj( | ||
function (file, enc, callback) { | ||
return transformStream({ objectMode: true }, | ||
function (file) { | ||
if (!revisioner) { | ||
@@ -56,15 +45,9 @@ revisioner = file.revisioner; | ||
// Drop any existing files off the stream | ||
callback(); | ||
}, | ||
function (callback) { | ||
function () { | ||
if (!revisioner) { | ||
this.emit( | ||
"error", | ||
new PluginError(PLUGIN_NAME, "revision() must be called first!") | ||
); | ||
return callback(); | ||
throw new PluginError(PLUGIN_NAME, "revision() must be called first!"); | ||
} | ||
this.push(revisioner.versionFile()); | ||
callback(); | ||
return [revisioner.versionFile()]; | ||
} | ||
@@ -78,20 +61,14 @@ ); | ||
// Drop any existing files off the stream, push the generated manifest file | ||
return Through.obj( | ||
function (file, enc, callback) { | ||
return transformStream({ objectMode: true }, | ||
function (file) { | ||
if (!revisioner) { | ||
revisioner = file.revisioner; | ||
} | ||
callback(); | ||
}, | ||
function (callback) { | ||
function () { | ||
if (!revisioner) { | ||
this.emit( | ||
"error", | ||
new PluginError(PLUGIN_NAME, "revision() must be called first!") | ||
); | ||
return callback(); | ||
throw new PluginError(PLUGIN_NAME, "revision() must be called first!"); | ||
} | ||
this.push(revisioner.manifestFile()); | ||
callback(); | ||
return [revisioner.manifestFile()]; | ||
} | ||
@@ -98,0 +75,0 @@ ); |
{ | ||
"name": "gulp-rev-all", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn.098f6bcd.css, also re-writes references in each file to new reved name.", | ||
"main": "index.js", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"dependencies": { | ||
"chalk": "^4.1.0", | ||
"fancy-log": "^1.3.2", | ||
"isbinaryfile": "^4.0.6", | ||
"merge": "^2.1.1", | ||
"plugin-error": "^1.0.1", | ||
"through2": "^4.0.2", | ||
"vinyl": "^2.2.1" | ||
"chalk": "^5.2.0", | ||
"easy-transform-stream": "^1.0.0", | ||
"fancy-log": "^2.0.0", | ||
"isbinaryfile": "^5.0.0", | ||
"plugin-error": "^2.0.1", | ||
"vinyl": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "7.24.0", | ||
"eslint": "^8.29.0", | ||
"event-stream": "^4.0.1", | ||
"gulp": "^4.0.2", | ||
"mocha": "^8.3.2", | ||
"mocha": "^10.2.0", | ||
"prettier": "^2.2.1", | ||
@@ -58,4 +58,4 @@ "should": "^13.2.3" | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=16" | ||
} | ||
} |
@@ -40,4 +40,4 @@ # gulp-rev-all [![NPM version](https://img.shields.io/npm/v/gulp-rev-all.svg)](https://www.npmjs.com/package/gulp-rev-all) [![Run tests](https://github.com/nfroidure/svg-pathdata/actions/workflows/test.yml/badge.svg)](https://github.com/nfroidure/svg-pathdata/actions/workflows/test.yml) [![Dependency Status](https://img.shields.io/david/smysnk/gulp-rev-all.svg)](https://david-dm.org/smysnk/gulp-rev-all) | ||
```js | ||
var gulp = require("gulp"); | ||
var RevAll = require("gulp-rev-all"); | ||
import gulp from "gulp"; | ||
import RevAll from "gulp-rev-all"; | ||
@@ -50,6 +50,6 @@ gulp.task("default", function () { | ||
```js | ||
var gulp = require("gulp"); | ||
var RevAll = require("gulp-rev-all"); | ||
var awspublish = require("gulp-awspublish"); | ||
var cloudfront = require("gulp-cloudfront"); | ||
import gulp from "gulp"; | ||
import RevAll from "gulp-rev-all"; | ||
import awspublish from "gulp-awspublish"; | ||
import cloudfront from "gulp-cloudfront"; | ||
@@ -94,4 +94,4 @@ var aws = { | ||
```js | ||
var gulp = require("gulp"); | ||
var RevAll = require("gulp-rev-all"); | ||
import gulp from "gulp"; | ||
import RevAll from "gulp-rev-all"; | ||
@@ -123,4 +123,4 @@ gulp.task("default", function () { | ||
```js | ||
var gulp = require("gulp"); | ||
var RevAll = require("gulp-rev-all"); | ||
import gulp from "gulp"; | ||
import RevAll from "gulp-rev-all"; | ||
@@ -127,0 +127,0 @@ gulp.task("default", function () { |
@@ -1,326 +0,328 @@ | ||
var Vinyl = require("vinyl"); | ||
var fancyLog = require("fancy-log"); | ||
var chalk = require("chalk"); | ||
var Merge = require("merge"); | ||
var Path = require("path"); | ||
var Tool = require("./tool"); | ||
import Vinyl from "vinyl"; | ||
import fancyLog from "fancy-log"; | ||
import chalk from "chalk"; | ||
import Path from "path"; | ||
import { | ||
get_reference_representations_absolute, | ||
get_reference_representations_relative, | ||
get_relative_path, | ||
is_binary_file, | ||
join_path, | ||
join_path_url, | ||
md5, | ||
path_without_ext, | ||
} from "./tool.js"; | ||
var Revisioner = (function () { | ||
"use strict"; | ||
var Revisioner = function (options) { | ||
var defaults = { | ||
hashLength: 8, | ||
dontGlobal: [/^\/favicon.ico$/g], | ||
dontRenameFile: [], | ||
dontUpdateReference: [], | ||
dontSearchFile: [], | ||
fileNameVersion: "rev-version.json", | ||
fileNameManifest: "rev-manifest.json", | ||
prefix: "", | ||
referenceToRegexs: referenceToRegexs, | ||
annotator: annotator, | ||
replacer: replacer, | ||
debug: false, | ||
includeFilesInManifest: [".css", ".js"], | ||
}; | ||
var Revisioner = function (options) { | ||
var defaults = { | ||
hashLength: 8, | ||
dontGlobal: [/^\/favicon.ico$/g], | ||
dontRenameFile: [], | ||
dontUpdateReference: [], | ||
dontSearchFile: [], | ||
fileNameVersion: "rev-version.json", | ||
fileNameManifest: "rev-manifest.json", | ||
prefix: "", | ||
referenceToRegexs: referenceToRegexs, | ||
annotator: annotator, | ||
replacer: replacer, | ||
debug: false, | ||
includeFilesInManifest: [".css", ".js"], | ||
}; | ||
this.options = Merge(defaults, options); | ||
this.options = { ...defaults, ... options }; | ||
// File pool, any file passed into the Revisioner is stored in this object | ||
this.files = {}; | ||
this.filesTemp = []; | ||
// File pool, any file passed into the Revisioner is stored in this object | ||
this.files = {}; | ||
this.filesTemp = []; | ||
// Stores the combined hash of all processed files, used to create the version file | ||
this.hashCombined = ""; | ||
// Stores the combined hash of all processed files, used to create the version file | ||
this.hashCombined = ""; | ||
// Stores the before : after path of assets, used to create the manifset file | ||
this.manifest = {}; | ||
// Stores the before : after path of assets, used to create the manifset file | ||
this.manifest = {}; | ||
// Enable / Disable logger based on supplied options | ||
this.log = this.options.debug ? fancyLog : function () {}; | ||
// Enable / Disable logger based on supplied options | ||
this.log = this.options.debug ? fancyLog : function () {}; | ||
// Make tools available client side callbacks supplied in options | ||
this.Tool = Tool; | ||
var nonFileNameChar = "[^a-zA-Z0-9\\.\\-\\_\\/]"; | ||
var qoutes = "'|\""; | ||
var nonFileNameChar = "[^a-zA-Z0-9\\.\\-\\_\\/]"; | ||
var qoutes = "'|\""; | ||
function referenceToRegexs(reference) { | ||
var escapedRefPathBase = path_without_ext(reference.path).replace( | ||
/([^0-9a-z])/gi, | ||
"\\$1" | ||
); | ||
var escapedRefPathExt = Path.extname(reference.path).replace( | ||
/([^0-9a-z])/gi, | ||
"\\$1" | ||
); | ||
function referenceToRegexs(reference) { | ||
var escapedRefPathBase = Tool.path_without_ext(reference.path).replace( | ||
/([^0-9a-z])/gi, | ||
"\\$1" | ||
); | ||
var escapedRefPathExt = Path.extname(reference.path).replace( | ||
/([^0-9a-z])/gi, | ||
"\\$1" | ||
); | ||
var regExp, | ||
regExps = []; | ||
var isJSReference = reference.path.match(/\.js$/); | ||
var regExp, | ||
regExps = []; | ||
var isJSReference = reference.path.match(/\.js$/); | ||
// Extensionless javascript file references has to to be qouted | ||
if (isJSReference) { | ||
regExp = | ||
"(" + qoutes + ")(" + escapedRefPathBase + ")()(" + qoutes + "|$)"; | ||
regExps.push(new RegExp(regExp, "g")); | ||
} | ||
// Expect left and right sides of the reference to be a non-filename type character, escape special regex chars | ||
// Extensionless javascript file references has to to be qouted | ||
if (isJSReference) { | ||
regExp = | ||
"(" + | ||
nonFileNameChar + | ||
")(" + | ||
escapedRefPathBase + | ||
")(" + | ||
escapedRefPathExt + | ||
")(" + | ||
nonFileNameChar + | ||
"|$)"; | ||
"(" + qoutes + ")(" + escapedRefPathBase + ")()(" + qoutes + "|$)"; | ||
regExps.push(new RegExp(regExp, "g")); | ||
return regExps; | ||
} | ||
function annotator(contents) { | ||
return [{ contents: contents }]; | ||
} | ||
// Expect left and right sides of the reference to be a non-filename type character, escape special regex chars | ||
regExp = | ||
"(" + | ||
nonFileNameChar + | ||
")(" + | ||
escapedRefPathBase + | ||
")(" + | ||
escapedRefPathExt + | ||
")(" + | ||
nonFileNameChar + | ||
"|$)"; | ||
regExps.push(new RegExp(regExp, "g")); | ||
function replacer(fragment, replaceRegExp, newReference) { | ||
fragment.contents = fragment.contents.replace( | ||
replaceRegExp, | ||
"$1" + newReference + "$3$4" | ||
); | ||
} | ||
}; | ||
return regExps; | ||
} | ||
Revisioner.prototype.versionFile = function () { | ||
var out = { | ||
hash: this.hashCombined, | ||
timestamp: new Date(), | ||
}; | ||
function annotator(contents) { | ||
return [{ contents: contents }]; | ||
} | ||
var file = new Vinyl({ | ||
cwd: this.pathCwd, | ||
base: this.pathBase, | ||
path: Path.join(this.pathBase, this.options.fileNameVersion), | ||
contents: Buffer.from(JSON.stringify(out, null, 2)), | ||
revisioner: this, | ||
}); | ||
function replacer(fragment, replaceRegExp, newReference) { | ||
fragment.contents = fragment.contents.replace( | ||
replaceRegExp, | ||
"$1" + newReference + "$3$4" | ||
); | ||
} | ||
}; | ||
file.revisioner = this; | ||
return file; | ||
Revisioner.prototype.versionFile = function () { | ||
var out = { | ||
hash: this.hashCombined, | ||
timestamp: new Date(), | ||
}; | ||
Revisioner.prototype.manifestFile = function () { | ||
var file = new Vinyl({ | ||
cwd: this.pathCwd, | ||
base: this.pathBase, | ||
path: Path.join(this.pathBase, this.options.fileNameManifest), | ||
contents: Buffer.from(JSON.stringify(this.manifest, null, 2)), | ||
}); | ||
var file = new Vinyl({ | ||
cwd: this.pathCwd, | ||
base: this.pathBase, | ||
path: Path.join(this.pathBase, this.options.fileNameVersion), | ||
contents: Buffer.from(JSON.stringify(out, null, 2)), | ||
revisioner: this, | ||
}); | ||
file.revisioner = this; | ||
return file; | ||
}; | ||
file.revisioner = this; | ||
return file; | ||
}; | ||
/** | ||
* Used to feed files into the Revisioner, sets up the original filename and hash. | ||
*/ | ||
Revisioner.prototype.processFile = function (file) { | ||
if (!this.pathCwd) { | ||
this.pathCwd = file.cwd; | ||
} | ||
Revisioner.prototype.manifestFile = function () { | ||
var file = new Vinyl({ | ||
cwd: this.pathCwd, | ||
base: this.pathBase, | ||
path: Path.join(this.pathBase, this.options.fileNameManifest), | ||
contents: Buffer.from(JSON.stringify(this.manifest, null, 2)), | ||
}); | ||
// Chnage relative paths to absolute | ||
if (!file.base.match(/^(\/|[a-z]:)/i)) { | ||
file.base = Tool.join_path(file.cwd, file.base); | ||
} | ||
file.revisioner = this; | ||
return file; | ||
}; | ||
// Normalize the base common to all the files | ||
if (!this.pathBase) { | ||
this.pathBase = file.base; | ||
} else if (file.base.indexOf(this.pathBase) === -1) { | ||
var levelsBase = this.pathBase.split(/[/|\\]/); | ||
var levelsFile = file.base.split(/[/|\\]/); | ||
/** | ||
* Used to feed files into the Revisioner, sets up the original filename and hash. | ||
*/ | ||
Revisioner.prototype.processFile = function (file) { | ||
if (!this.pathCwd) { | ||
this.pathCwd = file.cwd; | ||
} | ||
var common = []; | ||
for (var level = 0, length = levelsFile.length; level < length; level++) { | ||
if ( | ||
level < levelsBase.length && | ||
level < levelsFile.length && | ||
levelsBase[level] === levelsFile[level] | ||
) { | ||
common.push(levelsFile[level]); | ||
continue; | ||
} | ||
} | ||
// Chnage relative paths to absolute | ||
if (!file.base.match(/^(\/|[a-z]:)/i)) { | ||
file.base = join_path(file.cwd, file.base); | ||
} | ||
if (common[common.length - 1] !== "") { | ||
common.push(""); | ||
// Normalize the base common to all the files | ||
if (!this.pathBase) { | ||
this.pathBase = file.base; | ||
} else if (file.base.indexOf(this.pathBase) === -1) { | ||
var levelsBase = this.pathBase.split(/[/|\\]/); | ||
var levelsFile = file.base.split(/[/|\\]/); | ||
var common = []; | ||
for (var level = 0, length = levelsFile.length; level < length; level++) { | ||
if ( | ||
level < levelsBase.length && | ||
level < levelsFile.length && | ||
levelsBase[level] === levelsFile[level] | ||
) { | ||
common.push(levelsFile[level]); | ||
continue; | ||
} | ||
this.pathBase = common.join("/"); | ||
} | ||
// Set original values before any processing occurs | ||
file.revPathOriginal = file.revOrigPath = file.path; | ||
file.revFilenameExtOriginal = Path.extname(file.path); | ||
file.revFilenameOriginal = Path.basename( | ||
file.path, | ||
file.revFilenameExtOriginal | ||
); | ||
file.revHashOriginal = this.Tool.md5(file.contents); | ||
file.revContentsOriginal = file.contents; | ||
if (common[common.length - 1] !== "") { | ||
common.push(""); | ||
} | ||
this.pathBase = common.join("/"); | ||
} | ||
this.filesTemp.push(file); | ||
}; | ||
// Set original values before any processing occurs | ||
file.revPathOriginal = file.revOrigPath = file.path; | ||
file.revFilenameExtOriginal = Path.extname(file.path); | ||
file.revFilenameOriginal = Path.basename( | ||
file.path, | ||
file.revFilenameExtOriginal | ||
); | ||
file.revHashOriginal = md5(file.contents); | ||
file.revContentsOriginal = file.contents; | ||
/** | ||
* Resolves references, renames files, updates references. To be called after all the files | ||
* have been fed into the Revisioner (ie. At the end of the file stream) | ||
*/ | ||
Revisioner.prototype.run = function () { | ||
this.hashCombined = ""; | ||
this.filesTemp.push(file); | ||
}; | ||
// Go through and correct the base path now that we have proccessed all the files coming in | ||
for (var i = 0, length = this.filesTemp.length; i < length; i++) { | ||
this.filesTemp[i].base = this.pathBase; | ||
var path = this.Tool.get_relative_path( | ||
this.pathBase, | ||
this.filesTemp[i].path | ||
); | ||
this.files[path] = this.filesTemp[i]; | ||
} | ||
/** | ||
* Resolves references, renames files, updates references. To be called after all the files | ||
* have been fed into the Revisioner (ie. At the end of the file stream) | ||
*/ | ||
Revisioner.prototype.run = function () { | ||
this.hashCombined = ""; | ||
// Resolve references to other files | ||
for (path in this.files) { | ||
this.resolveReferences(this.files[path]); | ||
} | ||
// Go through and correct the base path now that we have proccessed all the files coming in | ||
for (var i = 0, length = this.filesTemp.length; i < length; i++) { | ||
this.filesTemp[i].base = this.pathBase; | ||
var path = get_relative_path( | ||
this.pathBase, | ||
this.filesTemp[i].path | ||
); | ||
this.files[path] = this.filesTemp[i]; | ||
} | ||
// Resolve and set revisioned filename based on hash + reference hashes and ignore rules | ||
for (path in this.files) { | ||
this.revisionFilename(this.files[path]); | ||
} | ||
// Resolve references to other files | ||
for (path in this.files) { | ||
this.resolveReferences(this.files[path]); | ||
} | ||
// Consolidate the concatinated hash of all the files, into a single hash for the version file | ||
this.hashCombined = this.Tool.md5(this.hashCombined); | ||
// Resolve and set revisioned filename based on hash + reference hashes and ignore rules | ||
for (path in this.files) { | ||
this.revisionFilename(this.files[path]); | ||
} | ||
// Update references to revisioned filenames | ||
for (path in this.files) { | ||
this.updateReferences(this.files[path]); | ||
} | ||
}; | ||
// Consolidate the concatinated hash of all the files, into a single hash for the version file | ||
this.hashCombined = md5(this.hashCombined); | ||
/** | ||
* Go through each file in the file pool, search for references to any other file in the pool. | ||
*/ | ||
Revisioner.prototype.resolveReferences = function (fileResolveReferencesIn) { | ||
var contents = String(fileResolveReferencesIn.revContentsOriginal); | ||
fileResolveReferencesIn.revReferencePaths = {}; | ||
fileResolveReferencesIn.revReferenceFiles = {}; | ||
var referenceGroupRelative = []; | ||
var referenceGroupAbsolute = []; | ||
fileResolveReferencesIn.referenceGroupsContainer = { | ||
relative: referenceGroupRelative, | ||
absolute: referenceGroupAbsolute, | ||
}; | ||
// Update references to revisioned filenames | ||
for (path in this.files) { | ||
this.updateReferences(this.files[path]); | ||
} | ||
}; | ||
// Don't try and resolve references in binary files or files that have been blacklisted | ||
if ( | ||
this.Tool.is_binary_file(fileResolveReferencesIn) || | ||
!this.shouldSearchFile(fileResolveReferencesIn) | ||
) { | ||
return; | ||
} | ||
/** | ||
* Go through each file in the file pool, search for references to any other file in the pool. | ||
*/ | ||
Revisioner.prototype.resolveReferences = function (fileResolveReferencesIn) { | ||
var contents = String(fileResolveReferencesIn.revContentsOriginal); | ||
fileResolveReferencesIn.revReferencePaths = {}; | ||
fileResolveReferencesIn.revReferenceFiles = {}; | ||
var referenceGroupRelative = []; | ||
var referenceGroupAbsolute = []; | ||
fileResolveReferencesIn.referenceGroupsContainer = { | ||
relative: referenceGroupRelative, | ||
absolute: referenceGroupAbsolute, | ||
}; | ||
// For the current file (fileResolveReferencesIn), look for references to any other file in the project | ||
for (var path in this.files) { | ||
// Organize them by relative vs absolute reference types | ||
var fileCurrentReference = this.files[path]; | ||
var references; | ||
// Don't try and resolve references in binary files or files that have been blacklisted | ||
if ( | ||
is_binary_file(fileResolveReferencesIn) || | ||
!this.shouldSearchFile(fileResolveReferencesIn) | ||
) { | ||
return; | ||
} | ||
references = this.Tool.get_reference_representations_relative( | ||
fileCurrentReference, | ||
fileResolveReferencesIn | ||
); | ||
for (var i = 0, length = references.length; i < length; i++) { | ||
referenceGroupRelative.push({ | ||
file: this.files[path], | ||
path: references[i], | ||
}); | ||
} | ||
// For the current file (fileResolveReferencesIn), look for references to any other file in the project | ||
for (var path in this.files) { | ||
// Organize them by relative vs absolute reference types | ||
var fileCurrentReference = this.files[path]; | ||
var references; | ||
references = this.Tool.get_reference_representations_absolute( | ||
fileCurrentReference, | ||
fileResolveReferencesIn | ||
); | ||
for (i = 0, length = references.length; i < length; i++) { | ||
referenceGroupAbsolute.push({ | ||
file: this.files[path], | ||
path: references[i], | ||
}); | ||
} | ||
references = get_reference_representations_relative( | ||
fileCurrentReference, | ||
fileResolveReferencesIn | ||
); | ||
for (var i = 0, length = references.length; i < length; i++) { | ||
referenceGroupRelative.push({ | ||
file: this.files[path], | ||
path: references[i], | ||
}); | ||
} | ||
// Priority relative references higher than absolute | ||
for (var referenceType in fileResolveReferencesIn.referenceGroupsContainer) { | ||
var referenceGroup = | ||
fileResolveReferencesIn.referenceGroupsContainer[referenceType]; | ||
references = get_reference_representations_absolute( | ||
fileCurrentReference, | ||
fileResolveReferencesIn | ||
); | ||
for (i = 0, length = references.length; i < length; i++) { | ||
referenceGroupAbsolute.push({ | ||
file: this.files[path], | ||
path: references[i], | ||
}); | ||
} | ||
} | ||
for ( | ||
var referenceIndex = 0, referenceGroupLength = referenceGroup.length; | ||
referenceIndex < referenceGroupLength; | ||
referenceIndex++ | ||
) { | ||
var reference = referenceGroup[referenceIndex]; | ||
var regExps = this.options.referenceToRegexs(reference); | ||
// Priority relative references higher than absolute | ||
for (var referenceType in fileResolveReferencesIn.referenceGroupsContainer) { | ||
var referenceGroup = | ||
fileResolveReferencesIn.referenceGroupsContainer[referenceType]; | ||
for (var j = 0; j < regExps.length; j++) { | ||
if (contents.match(regExps[j])) { | ||
// Only register this reference if we don't have one already by the same path | ||
if (!fileResolveReferencesIn.revReferencePaths[reference.path]) { | ||
fileResolveReferencesIn.revReferenceFiles[reference.file.path] = | ||
reference.file; | ||
fileResolveReferencesIn.revReferencePaths[reference.path] = { | ||
regExps: [regExps[j]], | ||
file: reference.file, | ||
path: reference.path, | ||
}; | ||
this.log( | ||
"gulp-rev-all:", | ||
"Found", | ||
referenceType, | ||
"reference [", | ||
chalk.magenta(reference.path), | ||
"] -> [", | ||
chalk.green(reference.file.path), | ||
"] in [", | ||
chalk.blue(fileResolveReferencesIn.revPathOriginal), | ||
"]" | ||
); | ||
} else if ( | ||
for ( | ||
var referenceIndex = 0, referenceGroupLength = referenceGroup.length; | ||
referenceIndex < referenceGroupLength; | ||
referenceIndex++ | ||
) { | ||
var reference = referenceGroup[referenceIndex]; | ||
var regExps = this.options.referenceToRegexs(reference); | ||
for (var j = 0; j < regExps.length; j++) { | ||
if (contents.match(regExps[j])) { | ||
// Only register this reference if we don't have one already by the same path | ||
if (!fileResolveReferencesIn.revReferencePaths[reference.path]) { | ||
fileResolveReferencesIn.revReferenceFiles[reference.file.path] = | ||
reference.file; | ||
fileResolveReferencesIn.revReferencePaths[reference.path] = { | ||
regExps: [regExps[j]], | ||
file: reference.file, | ||
path: reference.path, | ||
}; | ||
this.log( | ||
"gulp-rev-all:", | ||
"Found", | ||
referenceType, | ||
"reference [", | ||
chalk.magenta(reference.path), | ||
"] -> [", | ||
chalk.green(reference.file.path), | ||
"] in [", | ||
chalk.blue(fileResolveReferencesIn.revPathOriginal), | ||
"]" | ||
); | ||
} else if ( | ||
fileResolveReferencesIn.revReferencePaths[reference.path].file | ||
.revPathOriginal === reference.file.revPathOriginal | ||
) { | ||
// Append the other regexes to account for inconsitent use | ||
fileResolveReferencesIn.revReferencePaths[ | ||
reference.path | ||
].regExps.push(regExps[j]); | ||
} else { | ||
this.log( | ||
"gulp-rev-all:", | ||
"Possible ambiguous reference detected [", | ||
chalk.red( | ||
fileResolveReferencesIn.revReferencePaths[reference.path].path | ||
), | ||
" (", | ||
fileResolveReferencesIn.revReferencePaths[reference.path].file | ||
.revPathOriginal === reference.file.revPathOriginal | ||
) { | ||
// Append the other regexes to account for inconsitent use | ||
fileResolveReferencesIn.revReferencePaths[ | ||
reference.path | ||
].regExps.push(regExps[j]); | ||
} else { | ||
this.log( | ||
"gulp-rev-all:", | ||
"Possible ambiguous reference detected [", | ||
chalk.red( | ||
fileResolveReferencesIn.revReferencePaths[reference.path].path | ||
), | ||
" (", | ||
fileResolveReferencesIn.revReferencePaths[reference.path].file | ||
.revPathOriginal, | ||
")] <-> [", | ||
chalk.red(reference.path), | ||
"(", | ||
chalk.red(reference.file.revPathOriginal), | ||
")]" | ||
); | ||
} | ||
.revPathOriginal, | ||
")] <-> [", | ||
chalk.red(reference.path), | ||
"(", | ||
chalk.red(reference.file.revPathOriginal), | ||
")]" | ||
); | ||
} | ||
@@ -330,241 +332,239 @@ } | ||
} | ||
}; | ||
} | ||
}; | ||
/** | ||
* Calculate hash based contents and references. | ||
* hash = hash(file hash + hash(hash references 1 + hash reference N)..) | ||
*/ | ||
Revisioner.prototype.calculateHash = function (file, stack) { | ||
stack = stack || []; | ||
var hash = file.revHashOriginal; | ||
/** | ||
* Calculate hash based contents and references. | ||
* hash = hash(file hash + hash(hash references 1 + hash reference N)..) | ||
*/ | ||
Revisioner.prototype.calculateHash = function (file, stack) { | ||
stack = stack || []; | ||
var hash = file.revHashOriginal; | ||
stack.push(file); | ||
stack.push(file); | ||
// Resolve hash for child references | ||
if (Object.keys(file.revReferenceFiles).length > 0) { | ||
for (var key in file.revReferenceFiles) { | ||
// Prevent infinite loops caused by circular references, don't recurse if we've already encountered this file | ||
if (stack.indexOf(file.revReferenceFiles[key]) === -1) { | ||
hash += this.calculateHash(file.revReferenceFiles[key], stack); | ||
} | ||
// Resolve hash for child references | ||
if (Object.keys(file.revReferenceFiles).length > 0) { | ||
for (var key in file.revReferenceFiles) { | ||
// Prevent infinite loops caused by circular references, don't recurse if we've already encountered this file | ||
if (stack.indexOf(file.revReferenceFiles[key]) === -1) { | ||
hash += this.calculateHash(file.revReferenceFiles[key], stack); | ||
} | ||
} | ||
// This file's hash should change if any of its references will be prefixed. | ||
if ( | ||
this.options.prefix && | ||
Object.keys(file.referenceGroupsContainer.absolute).length | ||
) { | ||
hash += this.options.prefix; | ||
} | ||
// Consolidate many hashes into one | ||
hash = this.Tool.md5(hash); | ||
// This file's hash should change if any of its references will be prefixed. | ||
if ( | ||
this.options.prefix && | ||
Object.keys(file.referenceGroupsContainer.absolute).length | ||
) { | ||
hash += this.options.prefix; | ||
} | ||
return hash; | ||
}; | ||
// Consolidate many hashes into one | ||
hash = md5(hash); | ||
} | ||
/** | ||
* Revision filename based on internal contents + references. | ||
*/ | ||
Revisioner.prototype.revisionFilename = function (file) { | ||
var filename = file.revFilenameOriginal; | ||
var ext = file.revFilenameExtOriginal; | ||
return hash; | ||
}; | ||
file.revHash = this.calculateHash(file); | ||
/** | ||
* Revision filename based on internal contents + references. | ||
*/ | ||
Revisioner.prototype.revisionFilename = function (file) { | ||
var filename = file.revFilenameOriginal; | ||
var ext = file.revFilenameExtOriginal; | ||
// Allow the client to transform the final filename | ||
if (this.options.transformFilename) { | ||
filename = this.options.transformFilename.call(this, file, file.revHash); | ||
} else { | ||
filename = | ||
filename + "." + file.revHash.substr(0, this.options.hashLength) + ext; | ||
} | ||
file.revHash = this.calculateHash(file); | ||
file.revFilename = filename; | ||
// file.revFilenameNoExt = Tool.path_without_ext(file.revFilename); | ||
// Allow the client to transform the final filename | ||
if (this.options.transformFilename) { | ||
filename = this.options.transformFilename.call(this, file, file.revHash); | ||
} else { | ||
filename = | ||
filename + "." + file.revHash.substr(0, this.options.hashLength) + ext; | ||
} | ||
if (this.shouldFileBeRenamed(file)) { | ||
file.path = this.Tool.join_path(Path.dirname(file.path), filename); | ||
} | ||
file.revFilename = filename; | ||
// file.revFilenameNoExt = Tool.path_without_ext(file.revFilename); | ||
// Maintain the combined hash used in version file | ||
this.hashCombined += file.revHash; | ||
if (this.shouldFileBeRenamed(file)) { | ||
file.path = join_path(Path.dirname(file.path), filename); | ||
} | ||
// Maintain the manifset file | ||
var pathOriginal = this.Tool.get_relative_path( | ||
this.pathBase, | ||
file.revPathOriginal, | ||
true | ||
); | ||
var pathRevisioned = this.Tool.get_relative_path( | ||
file.base, | ||
file.path, | ||
true | ||
); | ||
// Add only specific file types to the manifest file | ||
if (this.options.includeFilesInManifest.indexOf(ext) !== -1) { | ||
this.manifest[pathOriginal] = pathRevisioned; | ||
} | ||
// Maintain the combined hash used in version file | ||
this.hashCombined += file.revHash; | ||
file.revPath = pathRevisioned; | ||
}; | ||
// Maintain the manifset file | ||
var pathOriginal = get_relative_path( | ||
this.pathBase, | ||
file.revPathOriginal, | ||
true | ||
); | ||
var pathRevisioned = get_relative_path( | ||
file.base, | ||
file.path, | ||
true | ||
); | ||
// Add only specific file types to the manifest file | ||
if (this.options.includeFilesInManifest.indexOf(ext) !== -1) { | ||
this.manifest[pathOriginal] = pathRevisioned; | ||
} | ||
/** | ||
* Update the contents of a file with the revisioned filenames of its references. | ||
*/ | ||
Revisioner.prototype.updateReferences = function (file) { | ||
// Don't try and update references in binary files or blacklisted files | ||
if (this.Tool.is_binary_file(file) || !this.shouldSearchFile(file)) { | ||
return; | ||
} | ||
file.revPath = pathRevisioned; | ||
}; | ||
var contents = String(file.revContentsOriginal); | ||
var annotatedContent = this.options.annotator( | ||
contents, | ||
file.revPathOriginal | ||
/** | ||
* Update the contents of a file with the revisioned filenames of its references. | ||
*/ | ||
Revisioner.prototype.updateReferences = function (file) { | ||
// Don't try and update references in binary files or blacklisted files | ||
if (is_binary_file(file) || !this.shouldSearchFile(file)) { | ||
return; | ||
} | ||
var contents = String(file.revContentsOriginal); | ||
var annotatedContent = this.options.annotator( | ||
contents, | ||
file.revPathOriginal | ||
); | ||
for (var pathReference in file.revReferencePaths) { | ||
var reference = file.revReferencePaths[pathReference]; | ||
// Replace regular filename with revisioned version | ||
var referencePath = reference.path.substr( | ||
0, | ||
reference.path.length - | ||
(reference.file.revFilenameOriginal.length + | ||
reference.file.revFilenameExtOriginal.length) | ||
); | ||
var pathReferenceReplace = referencePath + reference.file.revFilename; | ||
for (var pathReference in file.revReferencePaths) { | ||
var reference = file.revReferencePaths[pathReference]; | ||
// Replace regular filename with revisioned version | ||
var referencePath = reference.path.substr( | ||
0, | ||
reference.path.length - | ||
(reference.file.revFilenameOriginal.length + | ||
reference.file.revFilenameExtOriginal.length) | ||
if (this.options.transformPath) { | ||
// Transform path using client supplied transformPath callback, | ||
pathReferenceReplace = this.options.transformPath.call( | ||
this, | ||
pathReferenceReplace, | ||
reference.path, | ||
reference.file, | ||
file | ||
); | ||
var pathReferenceReplace = referencePath + reference.file.revFilename; | ||
} else if (this.options.prefix && pathReferenceReplace[0] === "/") { | ||
// Append with user supplied prefix | ||
pathReferenceReplace = join_path_url( | ||
this.options.prefix, | ||
pathReferenceReplace | ||
); | ||
} | ||
if (this.options.transformPath) { | ||
// Transform path using client supplied transformPath callback, | ||
pathReferenceReplace = this.options.transformPath.call( | ||
this, | ||
pathReferenceReplace, | ||
reference.path, | ||
reference.file, | ||
file | ||
); | ||
} else if (this.options.prefix && pathReferenceReplace[0] === "/") { | ||
// Append with user supplied prefix | ||
pathReferenceReplace = this.Tool.join_path_url( | ||
this.options.prefix, | ||
pathReferenceReplace | ||
); | ||
} | ||
if (this.shouldUpdateReference(reference.file)) { | ||
// The extention should remain constant so we dont add extentions to references without extentions | ||
var noExtReplace = path_without_ext(pathReferenceReplace); | ||
if (this.shouldUpdateReference(reference.file)) { | ||
// The extention should remain constant so we dont add extentions to references without extentions | ||
var noExtReplace = Tool.path_without_ext(pathReferenceReplace); | ||
for (var i = 0; i < annotatedContent.length; i++) { | ||
for (var j = 0; j < reference.regExps.length; j++) { | ||
this.options.replacer( | ||
annotatedContent[i], | ||
reference.regExps[j], | ||
noExtReplace, | ||
reference.file | ||
); | ||
} | ||
for (var i = 0; i < annotatedContent.length; i++) { | ||
for (var j = 0; j < reference.regExps.length; j++) { | ||
this.options.replacer( | ||
annotatedContent[i], | ||
reference.regExps[j], | ||
noExtReplace, | ||
reference.file | ||
); | ||
} | ||
} | ||
} | ||
} | ||
contents = annotatedContent | ||
.map(function (annotation) { | ||
return annotation.contents; | ||
}) | ||
.join(""); | ||
file.contents = Buffer.from(contents); | ||
}; | ||
contents = annotatedContent | ||
.map(function (annotation) { | ||
return annotation.contents; | ||
}) | ||
.join(""); | ||
file.contents = Buffer.from(contents); | ||
}; | ||
/** | ||
* Determines if a file should be renamed based on dontRenameFile supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldFileBeRenamed = function (file) { | ||
var filename = this.Tool.get_relative_path(file.base, file.revPathOriginal); | ||
/** | ||
* Determines if a file should be renamed based on dontRenameFile supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldFileBeRenamed = function (file) { | ||
var filename = get_relative_path(file.base, file.revPathOriginal); | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
} | ||
for (i = this.options.dontRenameFile.length; i--; ) { | ||
regex = | ||
this.options.dontRenameFile[i] instanceof RegExp | ||
? this.options.dontRenameFile[i] | ||
: new RegExp(this.options.dontRenameFile[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (i = this.options.dontRenameFile.length; i--; ) { | ||
regex = | ||
this.options.dontRenameFile[i] instanceof RegExp | ||
? this.options.dontRenameFile[i] | ||
: new RegExp(this.options.dontRenameFile[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Determines if a particular reference should be updated across assets based on dontUpdateReference supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldUpdateReference = function (file) { | ||
var filename = this.Tool.get_relative_path(file.base, file.revPathOriginal); | ||
/** | ||
* Determines if a particular reference should be updated across assets based on dontUpdateReference supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldUpdateReference = function (file) { | ||
var filename = get_relative_path(file.base, file.revPathOriginal); | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
} | ||
for (i = this.options.dontUpdateReference.length; i--; ) { | ||
regex = | ||
this.options.dontUpdateReference[i] instanceof RegExp | ||
? this.options.dontUpdateReference[i] | ||
: new RegExp(this.options.dontUpdateReference[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (i = this.options.dontUpdateReference.length; i--; ) { | ||
regex = | ||
this.options.dontUpdateReference[i] instanceof RegExp | ||
? this.options.dontUpdateReference[i] | ||
: new RegExp(this.options.dontUpdateReference[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
return true; | ||
}; | ||
/** | ||
* Determines if a particular reference should be updated across assets based on dontUpdateReference supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldSearchFile = function (file) { | ||
var filename = this.Tool.get_relative_path(file.base, file.revPathOriginal); | ||
/** | ||
* Determines if a particular reference should be updated across assets based on dontUpdateReference supplied in options. | ||
*/ | ||
Revisioner.prototype.shouldSearchFile = function (file) { | ||
var filename = get_relative_path(file.base, file.revPathOriginal); | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (var i = this.options.dontGlobal.length; i--; ) { | ||
var regex = | ||
this.options.dontGlobal[i] instanceof RegExp | ||
? this.options.dontGlobal[i] | ||
: new RegExp(this.options.dontGlobal[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
} | ||
for (i = this.options.dontSearchFile.length; i--; ) { | ||
regex = | ||
this.options.dontSearchFile[i] instanceof RegExp | ||
? this.options.dontSearchFile[i] | ||
: new RegExp(this.options.dontSearchFile[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
for (i = this.options.dontSearchFile.length; i--; ) { | ||
regex = | ||
this.options.dontSearchFile[i] instanceof RegExp | ||
? this.options.dontSearchFile[i] | ||
: new RegExp(this.options.dontSearchFile[i] + "$", "ig"); | ||
if (filename.match(regex)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
return true; | ||
}; | ||
return Revisioner; | ||
})(); | ||
module.exports = Revisioner; | ||
export default Revisioner; |
110
test.js
@@ -1,11 +0,17 @@ | ||
var RevAll = require("./index"); | ||
var Tool = require("./tool"); | ||
var Path = require("path"); | ||
var gulp = require("gulp"); | ||
var Vinyl = require("vinyl"); | ||
var es = require("event-stream"); | ||
var crypto = require("crypto"); | ||
import RevAll from "./index.js"; | ||
import { | ||
get_reference_representations_absolute, | ||
get_reference_representations_relative, | ||
get_relative_path, | ||
join_path, | ||
join_path_url, | ||
} from "./tool.js"; | ||
import Path from "path"; | ||
import gulp from "gulp"; | ||
import Vinyl from "vinyl"; | ||
import es from "event-stream"; | ||
import crypto from "crypto"; | ||
require("should"); | ||
require("mocha"); | ||
import "should"; | ||
import "mocha"; | ||
@@ -558,3 +564,3 @@ describe("gulp-rev-all", function () { | ||
transformPath: function (reved) { | ||
return this.Tool.join_path_url( | ||
return join_path_url( | ||
"//images.example.com/", | ||
@@ -964,3 +970,3 @@ reved.replace("img/", "") | ||
it("should correct slashes", function () { | ||
Tool.join_path("d:\\first\\second", "images.png").should.equal( | ||
join_path("d:\\first\\second", "images.png").should.equal( | ||
"/first/second/images.png" | ||
@@ -971,3 +977,3 @@ ); | ||
it("should not add starting slash", function () { | ||
Tool.join_path("first\\second", "images.png").should.equal( | ||
join_path("first\\second", "images.png").should.equal( | ||
"first/second/images.png" | ||
@@ -980,3 +986,3 @@ ); | ||
it("should correct slashes", function () { | ||
Tool.join_path("/first/second", "images.png").should.equal( | ||
join_path("/first/second", "images.png").should.equal( | ||
"/first/second/images.png" | ||
@@ -987,3 +993,3 @@ ); | ||
it("should not add starting slash", function () { | ||
Tool.join_path("first/second", "images.png").should.equal( | ||
join_path("first/second", "images.png").should.equal( | ||
"first/second/images.png" | ||
@@ -997,9 +1003,9 @@ ); | ||
it("should only truncate paths that overap with the base", function () { | ||
Tool.get_relative_path("/base/", "sub/index.html").should.equal( | ||
get_relative_path("/base/", "sub/index.html").should.equal( | ||
"sub/index.html" | ||
); | ||
Tool.get_relative_path("/base/", "/sub/index.html").should.equal( | ||
get_relative_path("/base/", "/sub/index.html").should.equal( | ||
"/sub/index.html" | ||
); | ||
Tool.get_relative_path("/base/", "/base/sub/index.html").should.equal( | ||
get_relative_path("/base/", "/base/sub/index.html").should.equal( | ||
"/sub/index.html" | ||
@@ -1011,7 +1017,7 @@ ); | ||
it("should correct slashes", function () { | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"c:\\base", | ||
"c:\\base\\sub\\index.html" | ||
).should.equal("/sub/index.html"); | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"c:\\base\\", | ||
@@ -1023,3 +1029,3 @@ "c:\\base\\sub\\index.html" | ||
it("should remove starting slash", function () { | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"d:\\base", | ||
@@ -1029,3 +1035,3 @@ "d:\\base\\sub\\index.html", | ||
).should.equal("sub/index.html"); | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"d:\\base\\", | ||
@@ -1038,3 +1044,3 @@ "d:\\base\\sub\\index.html", | ||
it("should work on base", function () { | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"e:\\base\\sub", | ||
@@ -1044,3 +1050,3 @@ "e:\\base\\sub\\index.html", | ||
).should.equal("index.html"); | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"e:\\base\\sub\\", | ||
@@ -1055,6 +1061,6 @@ "e:\\base\\sub\\index.html", | ||
it("should correct slashes", function () { | ||
Tool.get_relative_path("/base/", "/base/sub/index.html").should.equal( | ||
get_relative_path("/base/", "/base/sub/index.html").should.equal( | ||
"/sub/index.html" | ||
); | ||
Tool.get_relative_path("/base", "/base/sub/index.html").should.equal( | ||
get_relative_path("/base", "/base/sub/index.html").should.equal( | ||
"/sub/index.html" | ||
@@ -1065,3 +1071,3 @@ ); | ||
it("should remove starting slash", function () { | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"/base/", | ||
@@ -1071,3 +1077,3 @@ "/base/sub/index.html", | ||
).should.equal("sub/index.html"); | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"/base", | ||
@@ -1080,3 +1086,3 @@ "/base/sub/index.html", | ||
it("should work on base", function () { | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"/base/sub/", | ||
@@ -1086,3 +1092,3 @@ "/base/sub/index.html", | ||
).should.equal("index.html"); | ||
Tool.get_relative_path( | ||
get_relative_path( | ||
"/base/sub", | ||
@@ -1115,3 +1121,3 @@ "/base/sub/index.html", | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1142,3 +1148,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1171,3 +1177,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1198,3 +1204,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1226,3 +1232,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1256,3 +1262,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1282,3 +1288,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1308,3 +1314,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1334,3 +1340,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1362,3 +1368,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1388,3 +1394,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1414,3 +1420,3 @@ file | ||
var references = Tool.get_reference_representations_relative( | ||
var references = get_reference_representations_relative( | ||
fileReference, | ||
@@ -1444,3 +1450,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1471,3 +1477,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1500,3 +1506,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1527,3 +1533,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1556,3 +1562,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1583,3 +1589,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1610,3 +1616,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1637,3 +1643,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1666,3 +1672,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1693,3 +1699,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1720,3 +1726,3 @@ file | ||
var references = Tool.get_reference_representations_absolute( | ||
var references = get_reference_representations_absolute( | ||
fileReference, | ||
@@ -1723,0 +1729,0 @@ file |
313
tool.js
@@ -1,195 +0,180 @@ | ||
var Path = require("path"); | ||
var crypto = require("crypto"); | ||
var isBinaryFileSync = require("isbinaryfile").isBinaryFileSync; | ||
import Path from "path"; | ||
import crypto from "crypto"; | ||
import { isBinaryFileSync } from "isbinaryfile"; | ||
module.exports = (function () { | ||
"use strict"; | ||
export function path_without_ext(path) { | ||
var ext = Path.extname(path); | ||
return path.substr(0, path.length - ext.length); | ||
} | ||
var path_without_ext = function (path) { | ||
var ext = Path.extname(path); | ||
return path.substr(0, path.length - ext.length); | ||
}; | ||
export function dirname_with_sep(path) { | ||
return Path.dirname(path).replace(/\\/g, "/") + "/"; | ||
} | ||
var dirname_with_sep = function (path) { | ||
return Path.dirname(path).replace(/\\/g, "/") + "/"; | ||
}; | ||
export function join_path_url(prefix, path) { | ||
prefix = prefix.replace(/\/$/, ""); | ||
path = path.replace(/^\//, ""); | ||
return [prefix, path].join("/"); | ||
} | ||
var join_path_url = function (prefix, path) { | ||
prefix = prefix.replace(/\/$/, ""); | ||
path = path.replace(/^\//, ""); | ||
return [prefix, path].join("/"); | ||
}; | ||
/** | ||
* Joins a directory and a filename, replaces Windows forward-slash with a backslash. | ||
*/ | ||
export function join_path(directory, filename) { | ||
return Path.join(directory, filename) | ||
.replace(/^[a-z]:\\/i, "/") | ||
.replace(/\\/g, "/"); | ||
} | ||
/** | ||
* Joins a directory and a filename, replaces Windows forward-slash with a backslash. | ||
*/ | ||
var join_path = function (directory, filename) { | ||
return Path.join(directory, filename) | ||
.replace(/^[a-z]:\\/i, "/") | ||
.replace(/\\/g, "/"); | ||
}; | ||
/** | ||
* Given a base path and resource path, will return resource path relative to the base. | ||
* Also replaces Windows forward-slash with a backslash. | ||
*/ | ||
export function get_relative_path(base, path, noStartingSlash) { | ||
if (base === path) { | ||
return ""; | ||
} | ||
/** | ||
* Given a base path and resource path, will return resource path relative to the base. | ||
* Also replaces Windows forward-slash with a backslash. | ||
*/ | ||
var get_relative_path = function (base, path, noStartingSlash) { | ||
if (base === path) { | ||
return ""; | ||
} | ||
// Sanitize inputs, convert windows to posix style slashes, ensure trailing slash for base | ||
base = | ||
base | ||
.replace(/^[a-z]:/i, "") | ||
.replace(/\\/g, "/") | ||
.replace(/\/$/g, "") + "/"; | ||
path = path.replace(/^[a-z]:/i, "").replace(/\\/g, "/"); | ||
// Sanitize inputs, convert windows to posix style slashes, ensure trailing slash for base | ||
base = | ||
base | ||
.replace(/^[a-z]:/i, "") | ||
.replace(/\\/g, "/") | ||
.replace(/\/$/g, "") + "/"; | ||
path = path.replace(/^[a-z]:/i, "").replace(/\\/g, "/"); | ||
// Only truncate paths that overap with the base | ||
if (base === path.substr(0, base.length)) { | ||
path = "/" + path.substr(base.length); | ||
} | ||
// Only truncate paths that overap with the base | ||
if (base === path.substr(0, base.length)) { | ||
path = "/" + path.substr(base.length); | ||
var modifyStartingSlash = noStartingSlash !== undefined; | ||
if (modifyStartingSlash) { | ||
if (path[0] === "/" && noStartingSlash) { | ||
path = path.substr(1); | ||
} else if (path[0] !== "/" && !noStartingSlash) { | ||
path = "/" + path; | ||
} | ||
} | ||
var modifyStartingSlash = noStartingSlash !== undefined; | ||
if (modifyStartingSlash) { | ||
if (path[0] === "/" && noStartingSlash) { | ||
path = path.substr(1); | ||
} else if (path[0] !== "/" && !noStartingSlash) { | ||
path = "/" + path; | ||
} | ||
} | ||
return path; | ||
} | ||
return path; | ||
}; | ||
export function md5(buf) { | ||
return crypto.createHash("md5").update(buf).digest("hex"); | ||
} | ||
var md5 = function (buf) { | ||
return crypto.createHash("md5").update(buf).digest("hex"); | ||
}; | ||
export function is_binary_file(file) { | ||
return isBinaryFileSync(file.contents, file.contents.length); | ||
} | ||
var is_binary_file = function (file) { | ||
return isBinaryFileSync(file.contents, file.contents.length); | ||
}; | ||
/** | ||
* Given a file (context) and a file reference, return all the possible representations of paths to get from | ||
* the context to the reference file. | ||
* | ||
*/ | ||
export function get_reference_representations_relative( | ||
fileCurrentReference, | ||
file | ||
) { | ||
var representations = []; | ||
/** | ||
* Given a file (context) and a file reference, return all the possible representations of paths to get from | ||
* the context to the reference file. | ||
* | ||
*/ | ||
var get_reference_representations_relative = function ( | ||
fileCurrentReference, | ||
file | ||
// Scenario 2: Current file is the same directory or lower than the reference | ||
// (ie. file.path and the reference file.path are the same) | ||
// | ||
// file.base = /user/project | ||
// file.path = /user/project/second/current_file.html | ||
// fileCurrentReference.path = /user/project/second/index.html | ||
if ( | ||
dirname_with_sep(fileCurrentReference.path).indexOf( | ||
dirname_with_sep(file.path) | ||
) === 0 | ||
) { | ||
var representations = []; | ||
// index.html | ||
representations.push( | ||
get_relative_path( | ||
Path.dirname(file.path), | ||
fileCurrentReference.revPathOriginal, | ||
true | ||
) | ||
); | ||
// Scenario 2: Current file is the same directory or lower than the reference | ||
// (ie. file.path and the reference file.path are the same) | ||
// | ||
// file.base = /user/project | ||
// file.path = /user/project/second/current_file.html | ||
// fileCurrentReference.path = /user/project/second/index.html | ||
if ( | ||
dirname_with_sep(fileCurrentReference.path).indexOf( | ||
dirname_with_sep(file.path) | ||
) === 0 | ||
) { | ||
// index.html | ||
representations.push( | ||
// ./index.html (reference: relative) | ||
representations.push( | ||
"." + | ||
get_relative_path( | ||
Path.dirname(file.path), | ||
fileCurrentReference.revPathOriginal, | ||
true | ||
false | ||
) | ||
); | ||
); | ||
} | ||
// ./index.html (reference: relative) | ||
representations.push( | ||
"." + | ||
get_relative_path( | ||
Path.dirname(file.path), | ||
fileCurrentReference.revPathOriginal, | ||
false | ||
) | ||
); | ||
} | ||
// Scenario 3: Current file is in a different child directory than the reference | ||
// (ie. file.path and the reference file.path are different, not in root directory) | ||
// | ||
// file.base = /user/project | ||
// file.path = /user/project/first/index.html | ||
// fileCurrentReference.path = /user/project/second/index.html | ||
// Scenario 3: Current file is in a different child directory than the reference | ||
// (ie. file.path and the reference file.path are different, not in root directory) | ||
// | ||
// file.base = /user/project | ||
// file.path = /user/project/first/index.html | ||
// fileCurrentReference.path = /user/project/second/index.html | ||
if ( | ||
dirname_with_sep(file.path) !== | ||
dirname_with_sep(fileCurrentReference.path) && | ||
dirname_with_sep(fileCurrentReference.path).indexOf( | ||
dirname_with_sep(file.path) | ||
) === -1 | ||
) { | ||
var pathCurrentReference = dirname_with_sep( | ||
get_relative_path( | ||
fileCurrentReference.base, | ||
fileCurrentReference.revPathOriginal | ||
) | ||
); | ||
var pathFile = dirname_with_sep( | ||
get_relative_path(file.base, file.revPathOriginal) | ||
); | ||
// ../second/index.html | ||
var relPath = Path.relative(pathFile, pathCurrentReference); | ||
relPath = relPath.replace(/\\/g, "/"); | ||
representations.push( | ||
join_path(relPath, Path.basename(fileCurrentReference.revPathOriginal)) | ||
); | ||
} | ||
return representations; | ||
}; | ||
/** | ||
* Given a file (context) and a file reference, return all the possible representations of paths to get from | ||
* the context to the reference file. | ||
* | ||
*/ | ||
var get_reference_representations_absolute = function (fileCurrentReference) { | ||
var representations = []; | ||
var representation; | ||
// Scenario 1: Current file is anywhere | ||
// /view/index.html (reference: absolute) | ||
representations.push( | ||
if ( | ||
dirname_with_sep(file.path) !== | ||
dirname_with_sep(fileCurrentReference.path) && | ||
dirname_with_sep(fileCurrentReference.path).indexOf( | ||
dirname_with_sep(file.path) | ||
) === -1 | ||
) { | ||
var pathCurrentReference = dirname_with_sep( | ||
get_relative_path( | ||
fileCurrentReference.base, | ||
fileCurrentReference.revPathOriginal, | ||
false | ||
fileCurrentReference.revPathOriginal | ||
) | ||
); | ||
var pathFile = dirname_with_sep( | ||
get_relative_path(file.base, file.revPathOriginal) | ||
); | ||
// Without starting slash, only if it contains a directory | ||
// view/index.html (reference: absolute, without slash prefix) | ||
representation = get_relative_path( | ||
// ../second/index.html | ||
var relPath = Path.relative(pathFile, pathCurrentReference); | ||
relPath = relPath.replace(/\\/g, "/"); | ||
representations.push( | ||
join_path(relPath, Path.basename(fileCurrentReference.revPathOriginal)) | ||
); | ||
} | ||
return representations; | ||
} | ||
/** | ||
* Given a file (context) and a file reference, return all the possible representations of paths to get from | ||
* the context to the reference file. | ||
* | ||
*/ | ||
export function get_reference_representations_absolute(fileCurrentReference) { | ||
var representations = []; | ||
var representation; | ||
// Scenario 1: Current file is anywhere | ||
// /view/index.html (reference: absolute) | ||
representations.push( | ||
get_relative_path( | ||
fileCurrentReference.base, | ||
fileCurrentReference.revPathOriginal, | ||
true | ||
); | ||
if (representation.indexOf("/")) { | ||
representations.push(representation); | ||
} | ||
false | ||
) | ||
); | ||
return representations; | ||
}; | ||
// Without starting slash, only if it contains a directory | ||
// view/index.html (reference: absolute, without slash prefix) | ||
representation = get_relative_path( | ||
fileCurrentReference.base, | ||
fileCurrentReference.revPathOriginal, | ||
true | ||
); | ||
if (representation.indexOf("/")) { | ||
representations.push(representation); | ||
} | ||
return { | ||
get_relative_path: get_relative_path, | ||
md5: md5, | ||
is_binary_file: is_binary_file, | ||
path_without_ext: path_without_ext, | ||
join_path: join_path, | ||
join_path_url: join_path_url, | ||
get_reference_representations_relative: get_reference_representations_relative, | ||
get_reference_representations_absolute: get_reference_representations_absolute, | ||
}; | ||
})(); | ||
return representations; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6
Yes
810301
2204
+ Addedeasy-transform-stream@^1.0.0
+ Addedbare-events@2.5.0(transitive)
+ Addedchalk@5.3.0(transitive)
+ Addedeasy-transform-stream@1.0.1(transitive)
+ Addedfancy-log@2.0.0(transitive)
+ Addedfast-fifo@1.3.2(transitive)
+ Addedisbinaryfile@5.0.4(transitive)
+ Addedplugin-error@2.0.1(transitive)
+ Addedqueue-tick@1.0.1(transitive)
+ Addedreplace-ext@2.0.0(transitive)
+ Addedstreamx@2.20.2(transitive)
+ Addedteex@1.0.1(transitive)
+ Addedtext-decoder@1.2.1(transitive)
+ Addedvinyl@3.0.0(transitive)
- Removedmerge@^2.1.1
- Removedthrough2@^4.0.2
- Removedansi-gray@0.1.1(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedarr-diff@4.0.0(transitive)
- Removedarr-union@3.1.0(transitive)
- Removedassign-symbols@1.0.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedclone-buffer@1.0.0(transitive)
- Removedcloneable-readable@1.1.3(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedextend-shallow@3.0.2(transitive)
- Removedfancy-log@1.3.3(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-extendable@1.0.1(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisbinaryfile@4.0.10(transitive)
- Removedisobject@3.0.1(transitive)
- Removedmerge@2.1.1(transitive)
- Removedparse-node-version@1.0.1(transitive)
- Removedplugin-error@1.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.83.6.2(transitive)
- Removedreplace-ext@1.0.1(transitive)
- Removedsafe-buffer@5.1.25.2.1(transitive)
- Removedstring_decoder@1.1.11.3.0(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedthrough2@4.0.2(transitive)
- Removedtime-stamp@1.1.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedvinyl@2.2.1(transitive)
Updatedchalk@^5.2.0
Updatedfancy-log@^2.0.0
Updatedisbinaryfile@^5.0.0
Updatedplugin-error@^2.0.1
Updatedvinyl@^3.0.0