Skip to content

Commit 4c8c0b1

Browse files
committed
Add localpack --cwd option
1 parent efb1926 commit 4c8c0b1

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

gulp/utils/localpack-code/cli/top.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ const defineCli = function() {
1313
}
1414

1515
const 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

gulp/utils/localpack-code/cwd.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

gulp/utils/localpack-code/main.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// eslint-disable-next-line filenames/match-exported
22
'use strict'
33

4+
const { getCwd } = require('./cwd')
45
const { getPackageRoot } = require('./root')
56
const { getTempDir, cleanTempDir } = require('./temp')
67
const { unpack } = require('./unpack')
78
const { 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

gulp/utils/localpack-code/root.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const pkgDir = require('pkg-dir')
66
const 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,

0 commit comments

Comments
 (0)