Comparing version 0.0.7 to 0.0.8
@@ -5,2 +5,3 @@ var Promise = require('bluebird'); | ||
var chokidar = require('chokidar'); | ||
var util = require('hexo-util'); | ||
@@ -11,2 +12,3 @@ var dirname = pathFn.dirname; | ||
var rEOL = new RegExp(EOL, 'g'); | ||
var escapeRegex = util.escape.regex; | ||
@@ -412,2 +414,43 @@ var statAsync = Promise.promisify(fs.stat); | ||
function _findUnusedPath(path, files){ | ||
var extname = pathFn.extname(path); | ||
var basename = pathFn.basename(path, extname); | ||
var regex = new RegExp('^' + escapeRegex(basename) + '(?:-(\\d+))?' + escapeRegex(extname) + '$'); | ||
var item = ''; | ||
var num = -1; | ||
var match, matchNum; | ||
for (var i = 0, len = files.length; i < len; i++){ | ||
item = files[i]; | ||
if (!regex.test(item)) continue; | ||
match = item.match(regex); | ||
matchNum = match[1] ? parseInt(match[1], 10) : 0; | ||
if (matchNum > num){ | ||
num = matchNum; | ||
} | ||
} | ||
return join(dirname(path), basename + '-' + (num + 1) + extname); | ||
} | ||
function ensurePath(path, callback){ | ||
return exists(path).then(function(exist){ | ||
if (!exist) return path; | ||
return readdirAsync(dirname(path)).then(function(files){ | ||
return _findUnusedPath(path, files); | ||
}); | ||
}).nodeify(callback); | ||
} | ||
function ensurePathSync(path){ | ||
if (!fs.existsSync(path)) return path; | ||
var files = fs.readdirSync(dirname(path)); | ||
return _findUnusedPath(path, files); | ||
} | ||
// appendFile | ||
@@ -449,2 +492,6 @@ exports.appendFile = appendFile; | ||
// ensurePath | ||
exports.ensurePath = ensurePath; | ||
exports.ensurePathSync = ensurePathSync; | ||
// exists | ||
@@ -451,0 +498,0 @@ exports.exists = exists; |
{ | ||
"name": "hexo-fs", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "File system module for Hexo.", | ||
@@ -34,3 +34,4 @@ "main": "lib/fs", | ||
"chokidar": "^0.12.0", | ||
"graceful-fs": "^3.0.4" | ||
"graceful-fs": "^3.0.4", | ||
"hexo-util": "0.0.2" | ||
}, | ||
@@ -37,0 +38,0 @@ "devDependencies": { |
126
README.md
# fs | ||
[![Build Status](https://travis-ci.org/hexojs/fs.svg?branch=master)](https://travis-ci.org/hexojs/fs) [![NPM version](https://badge.fury.io/js/hexo-fs.svg)](http://badge.fury.io/js/hexo) [![Coverage Status](https://img.shields.io/coveralls/hexojs/fs.svg)](https://coveralls.io/r/hexojs/fs?branch=master) [![Build status](https://ci.appveyor.com/api/projects/status/wift3c57kei3ylq4/branch/master?svg=true)](https://ci.appveyor.com/project/tommy351/fs/branch/master) | ||
[![Build Status](https://travis-ci.org/hexojs/fs.svg?branch=master)](https://travis-ci.org/hexojs/fs) [![NPM version](https://badge.fury.io/js/hexo-fs.svg)](http://badge.fury.io/js/hexo-fs) [![Coverage Status](https://img.shields.io/coveralls/hexojs/fs.svg)](https://coveralls.io/r/hexojs/fs?branch=master) [![Build status](https://ci.appveyor.com/api/projects/status/wift3c57kei3ylq4/branch/master?svg=true)](https://ci.appveyor.com/project/tommy351/fs/branch/master) | ||
@@ -16,3 +16,3 @@ File system module for [Hexo]. | ||
``` bash | ||
$ npm install hexo-fs | ||
$ npm install hexo-fs --save | ||
``` | ||
@@ -26,2 +26,124 @@ | ||
> Some methods in the original fs module are not listed below, but they're available in hexo-fs. | ||
### exists(path, [callback]) | ||
Test whether or not the given `path` exists by checking with the file system. | ||
### existsSync(path) | ||
Synchronous version of `fs.exists`. | ||
### mkdirs(path, [callback]) | ||
Creates a directory and its parent directories if they does not exist. | ||
### mkdirsSync(path) | ||
Synchronous version of `fs.mkdirs`. | ||
### writeFile(path, data, [options], [callback]) | ||
Writes data to a file. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`encoding` | File encoding | utf8 | ||
`mode` | Mode | 438 (0666 in octal) | ||
`flag` | Flag | w | ||
### writeFileSync(path, data, [options]) | ||
Synchronous version of `fs.writeFile`. | ||
### appendFile(path, data, [options], [callback]) | ||
Appends data to a file. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`encoding` | File encoding | utf8 | ||
`mode` | Mode | 438 (0666 in octal) | ||
`flag` | Flag | w | ||
### appendFileSync(path, data, [options]) | ||
Synchronous version of `fs.appendFile`. | ||
### copyFile(src, dest, [callback]) | ||
Copies a file from `src` to `dest`. | ||
### copyDir(src, dest, [options], [callback]) | ||
Copies a directory from `src` to `dest`. It returns an array of copied files. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`ignoreHidden` | Ignore hidden files | true | ||
`ignorePattern` | Ignore files which pass the regular expression | | ||
### listDir(path, [options], [callback]) | ||
Lists files in a directory. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`ignoreHidden` | Ignore hidden files | true | ||
`ignorePattern` | Ignore files which pass the regular expression | | ||
### listDirSync(path, [options]) | ||
Synchronous version of `fs.listDir`. | ||
### readFile(path, [options], [callback]) | ||
Reads the entire contents of a file. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`encoding` | File encoding | utf8 | ||
`flag` | Flag | r | ||
`escape` | Escape UTF BOM and line ending in the content | true | ||
### readFileSync(path, [options]) | ||
Synchronous version of `fs.readFile`. | ||
### emptyDir(path, [options], [callback]) | ||
Deletes all files in a directory. It returns an array of deleted files. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`ignoreHidden` | Ignore hidden files | true | ||
`ignorePattern` | Ignore files which pass the regular expression | | ||
`exclude` | Ignore files in the array | | ||
### emptyDirSync(path, [options]) | ||
Synchronous version of `fs.emptyDir`. | ||
### rmdir(path, [callback]) | ||
Removes a directory and all files in it. | ||
### rmdirSync(path) | ||
Synchronous version of `fs.rmdir`. | ||
### watch(path, [options], [callback]) | ||
Watches changes of a file or a directory. | ||
See [Chokidar API](https://github.com/paulmillr/chokidar#api) for more info. | ||
### ensurePath(path, [callback]) | ||
Ensures the given path is available to use or appends a number to the path. | ||
### ensurePathSync(path) | ||
Synchronous version of `fs.ensurePath`. | ||
## License | ||
@@ -28,0 +150,0 @@ |
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
20435
482
153
4
+ Addedhexo-util@0.0.2
+ Addedhexo-util@0.0.2(transitive)
+ Addedhighlight.js@8.9.1(transitive)
+ Addedinflection@1.13.4(transitive)