Skip to content

Commit 69629d5

Browse files
committed
Refactor mdast(1) to externalise to-vfile
1 parent c710ebc commit 69629d5

File tree

4 files changed

+7
-50
lines changed

4 files changed

+7
-50
lines changed

lib/cli/file-set-pipeline/stdin.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@
1212
* Dependencies.
1313
*/
1414

15-
var path = require('path');
1615
var debug = require('debug')('mdast:cli:file-set-pipeline:stdin');
17-
var VFile = require('vfile');
16+
var toVFile = require('to-vfile');
1817
var concat = require('concat-stream');
1918

20-
/*
21-
* Methods.
22-
*/
23-
24-
var basename = path.basename;
25-
var extname = path.extname;
26-
var dirname = path.dirname;
27-
2819
/*
2920
* Constants.
3021
*/
@@ -70,21 +61,13 @@ function stdin(program, callback) {
7061
process.stdin.pipe(concat({
7162
'encoding': 'string'
7263
}, function (value) {
73-
var filePath = program.filePath;
74-
var extension = filePath && extname(filePath);
75-
var file = new VFile(filePath ? {
76-
'directory': dirname(filePath),
77-
'filename': basename(filePath, extension),
78-
'extension': extension.slice(1)
79-
} : {});
64+
var file = toVFile(program.filePath || '');
8065
var space = file.namespace('mdast:cli');
8166

8267
debug('Read from stdin');
8368

8469
file.contents = value;
85-
8670
file.quiet = true;
87-
8871
space.exists = true;
8972
space.isFile = true;
9073
space.providedByUser = true;

lib/cli/file-set.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@
1212
* Dependencies.
1313
*/
1414

15-
var path = require('path');
1615
var ware = require('ware');
17-
var VFile = require('vfile');
16+
var toVFile = require('to-vfile');
1817
var filePipeline = require('./file-pipeline');
1918

20-
/*
21-
* Methods.
22-
*/
23-
24-
var basename = path.basename;
25-
var extname = path.extname;
26-
var dirname = path.dirname;
27-
2819
/**
2920
* Utility invoked when a single file has completed it's
3021
* pipeline, invoking `fileSet.done` when all files are
@@ -166,16 +157,9 @@ function add(file) {
166157
var paths = self.originalPaths;
167158
var originalPath;
168159
var context;
169-
var extension;
170160

171161
if (typeof file === 'string') {
172-
extension = extname(file);
173-
174-
file = new VFile({
175-
'directory': dirname(file),
176-
'filename': basename(file, extension),
177-
'extension': extension.slice(1)
178-
});
162+
file = toVFile(file);
179163
}
180164

181165
originalPath = file.filePath();

lib/cli/traverser.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var fs = require('fs');
1818
var path = require('path');
1919
var debug = require('debug')('mdast:cli:traverse');
2020
var minimatch = require('minimatch');
21-
var VFile = require('vfile');
21+
var toVFile = require('to-vfile');
2222
var Ignore = require('./ignore');
2323

2424
/*
@@ -30,9 +30,6 @@ var stat = fs.statSync;
3030
var exists = fs.existsSync;
3131
var resolve = path.resolve;
3232
var join = path.join;
33-
var basename = path.basename;
34-
var extname = path.extname;
35-
var dirname = path.dirname;
3633
var concat = [].concat;
3734

3835
/*
@@ -92,18 +89,10 @@ function Traverse(options) {
9289
* @return {File}
9390
*/
9491
function toFile(filePath) {
95-
var extension = extname(filePath);
96-
var name = basename(filePath, extension);
9792
var real = exists(filePath);
98-
var directory = dirname(filePath);
93+
var file = toVFile(filePath);
9994
var space;
10095

101-
var file = new VFile({
102-
'directory': directory,
103-
'filename': name,
104-
'extension': extension.slice(1)
105-
});
106-
10796
space = file.namespace('mdast:cli');
10897

10998
file.quiet = true;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"minimatch": "^2.0.0",
3232
"repeat-string": "^1.5.0",
3333
"text-table": "^0.2.0",
34+
"to-vfile": "^1.0.0",
3435
"trim": "^0.0.1",
3536
"trim-trailing-lines": "^1.0.0",
3637
"unified": "^1.0.0",

0 commit comments

Comments
 (0)