File tree Expand file tree Collapse file tree 4 files changed +53
-10
lines changed
gulp/utils/localpack-code Expand file tree Collapse file tree 4 files changed +53
-10
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,17 @@ const defineCli = function() {
1313}
1414
1515const CONFIG = {
16+ cwd : {
17+ string : true ,
18+ alias : 'c' ,
19+ requiresArg : true ,
20+ describe : 'Current directory' ,
21+ } ,
1622 output : {
1723 string : true ,
1824 alias : 'o' ,
1925 requiresArg : true ,
20- describe : 'Where to unpack the package (default: {packageRoot }/localpack/)' ,
26+ describe : 'Where to unpack the package (default: {cwd }/localpack/)' ,
2127 } ,
2228}
2329
Original file line number Diff line number Diff line change 1+ 'use strict'
2+
3+ const { cwd : currentCwd } = require ( 'process' )
4+ const assert = require ( 'assert' )
5+ const { stat } = require ( 'fs' )
6+ const { promisify } = require ( 'util' )
7+
8+ const pStat = promisify ( stat )
9+
10+ // Retrieve current directory, used to compute `packageRoot` and default
11+ // `output`
12+ // Can be changed with `options.cwd`
13+ const getCwd = async function ( { cwd } ) {
14+ if ( cwd === undefined ) {
15+ return currentCwd ( )
16+ }
17+
18+ await validateCwd ( { cwd } )
19+
20+ return cwd
21+ }
22+
23+ const validateCwd = async function ( { cwd } ) {
24+ assert ( typeof cwd === 'string' , "option 'cwd' must be a string" )
25+ assert ( cwd !== '' , "option 'cwd' must not be an empty string" )
26+
27+ try {
28+ const cwdStat = await pStat ( cwd )
29+ assert ( cwdStat . isDirectory ( ) , "option 'cwd' must not be a directory" )
30+ } catch ( error ) {
31+ throw new Error ( `option 'cwd' is invalid: ${ error . message } ` )
32+ }
33+ }
34+
35+ module . exports = {
36+ getCwd,
37+ }
Original file line number Diff line number Diff line change 11// eslint-disable-next-line filenames/match-exported
22'use strict'
33
4+ const { getCwd } = require ( './cwd' )
45const { getPackageRoot } = require ( './root' )
56const { getTempDir, cleanTempDir } = require ( './temp' )
67const { unpack } = require ( './unpack' )
78const { addDevChecks } = require ( './dev_checks' )
89
910// Runs `npm pack` then unpack it to `opts.output`
10- const localpack = async function ( { output } = { } ) {
11+ const localpack = async function ( { cwd, output } = { } ) {
12+ const cwdA = await getCwd ( { cwd } )
13+
1114 const [ packageRoot , tempDir ] = await Promise . all ( [
12- getPackageRoot ( ) ,
15+ getPackageRoot ( cwdA ) ,
1316 getTempDir ( ) ,
1417 ] )
1518
16- const outputA = getOutput ( { packageRoot , output } )
19+ const outputA = getOutput ( { cwd : cwdA , output } )
1720
1821 await unpack ( { packageRoot, tempDir, output : outputA } )
1922
@@ -24,10 +27,7 @@ const localpack = async function({ output } = {}) {
2427}
2528
2629// Retrieve where package is unpacked
27- const getOutput = function ( {
28- packageRoot,
29- output = `${ packageRoot } /${ DEFAULT_OUTPUT } ` ,
30- } ) {
30+ const getOutput = function ( { cwd, output = `${ cwd } /${ DEFAULT_OUTPUT } ` } ) {
3131 return output
3232}
3333
Original file line number Diff line number Diff line change @@ -6,8 +6,8 @@ const pkgDir = require('pkg-dir')
66const moize = require ( 'moize' ) . default
77
88// Retrieve package root directory
9- const getPackageRoot = async function ( ) {
10- const packageRoot = await pkgDir ( )
9+ const getPackageRoot = async function ( cwd ) {
10+ const packageRoot = await pkgDir ( cwd )
1111
1212 assert (
1313 packageRoot !== null ,
You can’t perform that action at this time.
0 commit comments