UNPKG

1.84 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md.
4 */
5
6'use strict';
7
8const path = require( 'path' );
9const PassThrough = require( 'stream' ).PassThrough;
10const through = require( 'through2' );
11
12const stream = {
13 /**
14 * Creates a simple duplex stream.
15 *
16 * @param {Function} [callback] A callback which will be executed with each chunk.
17 * The callback can return a Promise to perform async actions before other chunks are accepted.
18 * @returns {Stream}
19 */
20 noop( callback ) {
21 if ( !callback ) {
22 return new PassThrough( { objectMode: true } );
23 }
24
25 return through( { objectMode: true }, ( chunk, encoding, throughCallback ) => {
26 const callbackResult = callback( chunk );
27
28 if ( callbackResult instanceof Promise ) {
29 callbackResult
30 .then( () => {
31 throughCallback( null, chunk );
32 } )
33 .catch( err => {
34 throughCallback( err );
35 } );
36 } else {
37 throughCallback( null, chunk );
38 }
39 } );
40 },
41
42 /**
43 * Checks whether a file is a test file.
44 *
45 * @param {Vinyl} file
46 * @returns {Boolean}
47 */
48 isTestFile( file ) {
49 // TODO this should be based on bender configuration (config.tests.*.paths).
50 if ( !file.relative.startsWith( 'tests' + path.sep ) ) {
51 return false;
52 }
53
54 const dirFrags = file.relative.split( path.sep );
55
56 return !dirFrags.some( dirFrag => {
57 return dirFrag.startsWith( '_' ) && dirFrag != '_utils-tests';
58 } );
59 },
60
61 /**
62 * Checks whether a file is a source file.
63 *
64 * @param {Vinyl} file
65 * @returns {Boolean}
66 */
67 isSourceFile( file ) {
68 return !stream.isTestFile( file );
69 },
70
71 /**
72 * Checks whether a file is a JS file.
73 *
74 * @param {Vinyl} file
75 * @returns {Boolean}
76 */
77 isJSFile( file ) {
78 return file.path.endsWith( '.js' );
79 }
80};
81
82module.exports = stream;