/
index.js
101 lines (71 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"use strict";
const { existsSync } = require( "fs" );
const { join } = require( "path" );
const { color, log } = require( "@futagoza/cli-utils" );
const parseArgv = require( "./lib/parseArgv" );
const publish = require( "@futagoza/publish-package" );
const PluginError = require( "plugin-error" );
const through = require( "through2" );
const toRegex = require( "to-regex" );
/**
* Builds a Gulp plugin error for this package.
*
* @param {String} message The error message
*/
function error( message ) {
return new PluginError( "@futagoza/gulp-publish-package", message );
}
/**
* Default status logger.
*
* __NOTE:__ If `path` is the only argument, it's persumed this is normal log.
*
* @param {String} path The path of the package being published.
* @param {String} [command] Command to spawn.
* @param {String[]} [args] Arguments that will be passed to the spawned process.
* @param {{}} [runOpts] Options that will be passed to the spawned process.
*/
function defaultLogger( path, command, args, runOpts ) {
if ( ! command && ! args && ! runOpts ) {
log.info( path );
} else {
args = args.slice( 0 );
args.unshift( color.magenta( path.replace( /\\/g, "/" ) ), command );
log.info( ...args );
}
}
/**
* Will publish the NPM packages passed to it from Gulp.
*
* @param {String[]} [argv] This is `process.argv` by default.
* @param {{}} [options] The options that are appended to the command.
*/
function publishThrough( argv, options = {} ) {
if ( ! Array.isArray( argv ) ) {
options = argv || {};
argv = options.argv || process.argv.slice( 2 );
}
options = parseArgv( argv, options );
options.checkVersion = options.checkVersion || options[ "check-version" ];
if ( ! options.log ) options.log = defaultLogger;
let ignore = void 0;
if ( typeof options.only === "string" )
ignore = toRegex( options.only, {
contains: true,
negate: options.ignore !== true,
nocase: true,
} );
return through.obj( ( file, encoding, done ) => {
const path = file.path;
if ( file.isStream() ) return done( error( "Streaming not supported" ) );
if ( ! existsSync( path ) ) return done( error( "Path not found: " + path ) );
if ( ignore ) {
const { name } = require( join( path, "package.json" ) );
if ( ignore.test( name ) ) return done( null, file );
}
publish( path, options )
.catch( done )
.then( result => done( null, result ) );
} );
}
module.exports = publishThrough;