Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native fs #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Add version number to js/css/image in HTML
**config**

{

/**
* Global version value
* default: %MDS%
*/
'value' : '%MDS%',

/**
* MODE: REPLACE
* eg:
Expand All @@ -34,76 +34,76 @@ Add version number to js/css/image in HTML
* [/regexp/ig, '%MD5%']]
*/
'replaces' : [

/**
* {String|Regexp} Replace Keyword/Rules to global value (config.value)
*/
'#{VERSION_REPlACE}#',

/**
* {Array}
* Replace keyword to custom value
* if just have keyword, the value will use the global value (config.value).
*/
*/
[/#{VERSION_REPlACE}#/g, '%TS%']
],


/**
* MODE: APPEND
* Can coexist and replace, after execution to replace
*/
'append' : {

/**
* Parameter
*/
'key' : '_v',

/**
* Whether to overwrite the existing parameters
* default: 0 (don't overwrite)
* If the parameter already exists, as a "custom", covering not executed.
* If you need to cover, please set to 1
*/
'cover' : 0,

/**
* Appended to the position (specify type)
* {String|Array|Object}
* If you set to 'all', will apply to all type, rules will use the global setting.
* If an array or object, will use your custom rules.
* others will passing.
*
*
* eg:
* 'js'
* ['js']
* {type:'js'}
* ['css', '%DATE%']
*/
'to' : [

/**
* {String} Specify type, the value is the global value
*/
'css',

/**
* {Array}
* Specify type, keyword and cover rules will use the global
* setting, If you need more details, please use the object
* Specify type, keyword and cover rules will use the global
* setting, If you need more details, please use the object
* configure.
*
* argument 0 necessary, otherwise passing.
* argument 1 optional, the value will use the global value
*/
['image', '%TS%'],

/**
* {Object}
* Use detailed custom rules to replace, missing items will
* Use detailed custom rules to replace, missing items will
* be taken in setting the global completion

* type is necessary, otherwise passing.
*/
{
Expand All @@ -116,7 +116,7 @@ Add version number to js/css/image in HTML
}
]
},

/**
* Output to config file
*/
Expand Down Expand Up @@ -149,6 +149,10 @@ Add version number to js/css/image in HTML

## Change log ##

#### = 0.3.0 = ####
- Drop fsPath dependency, due to having a known security issue.
- Replace fsPath with native fs functions

##### = 0.2.0 = #####
- Add a configure prop to "Object" model at "options.append.to", set the attribute what you want to match
```javascript
Expand Down
49 changes: 30 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/**
* config : {
*
*
* // VALUE, default: '%MDS%'
* 'value' : '%MDS%',
*
*
* // REPLACE
* 'replaces' : [
* // if not an array, replace to global value (config.value)
* /#{VERSION}#/g,
* [/#{VERSION_REPlACE}#/g, '%TS%']
* ],
*
*
* // APPEND
* 'append' : {
*
*
* // keyword
* 'key' : '_v',
*
*
* // Whether to overwrite the existing parameters
* // - default: 0 (don't replace)
* 'cover' : 0,
*
*
* // Append to:ALL('all') or any specific types(ARRAY),
* // others will passing.
* 'to' : [
*
*
* // (STRING) If this option is a string, apply global replace rules
* 'css',
*
*
* // (OBJECT) With custom rules to be replaced, the
* // missing items will take the global settings in
* // the completion
Expand All @@ -38,12 +38,12 @@
* 'value' : '%DATE%',
* 'cover' : 1
* },
*
*
* // (ARRAY) More simple than the object, Just specify
* // the type and value
* ['image', '%TS%']
* },
*
*
* // Output to config file
* 'output' : {
* 'file' : 'version.json'
Expand All @@ -64,8 +64,6 @@
'use strict';

var mapStream = require('map-stream');
var fsPath = require('fs-path');

var md5 = require('./lib/md5');
var randomString = require('./lib/randomString');
var leadZero = require('./lib/leadZero');
Expand All @@ -74,6 +72,8 @@ var renderingURL = require('./lib/renderingURL');
var queryToJson = require('./lib/queryToJson');
var jsonToQuery = require('./lib/jsonToQuery');

const fsp = require('fs').promises;

function version(v) {

if (typeof v === 'undefined') {
Expand Down Expand Up @@ -220,7 +220,7 @@ module.exports = function (options) {

if (Array.isArray(sts) && sts.length && this['files']) {
var files = this.files;

sts = sts.filter(function(element) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
Expand All @@ -235,7 +235,7 @@ module.exports = function (options) {
}
}
}

return false;
});
}
Expand Down Expand Up @@ -263,15 +263,26 @@ module.exports = function (options) {
return content;
}

/**
* Helper function to write asynchronously the version into the file
*/
async function writeVersion( file, content ) {
try {
await fs.promises.mkdir(
path.dirname( file ), {recursive: true})
.then(
await fsp.writeFile( file, JSON.stringify( content, null, 4 ) )
);
} catch (error){
console.log('[gulp-version-number] Output to file: ' + options.output.file);
}
}

/**
* output a json version file
*/
if (options.output && options.output.file) {
fsPath.writeFile(options.output.file, JSON.stringify(versionNumberList, null, 4), function (err) {
if (err)
throw err;
console.log('[gulp-version-number] Output to file: ' + options.output.file);
});
writeVersion( options.output.file, versionNumberList )
}

return mapStream(function (file, cb) {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gulp-version-number",
"description": "Add version number to css/js/image... in HTML file.",
"version": "0.2.4",
"version": "0.3.0",
"author": "Shinate <[email protected]>",
"bugs": {
"url": "https://github.com/shinate/gulp-version-number/issues"
Expand All @@ -13,7 +13,7 @@
"homepage": "https://github.com/shinate/gulp-version-number/",
"repository": "https://github.com/shinate/gulp-version-number.git",
"engines": {
"node": ">= 0.9"
"node": ">= 12"
},
"keywords": [
"gulp",
Expand All @@ -23,7 +23,6 @@
],
"license": "MIT",
"dependencies": {
"fs-path": "^0.0.24",
"map-stream": "^0.0.7"
},
"devDependencies": {
Expand Down