Skip to content

Commit 1e2d8c7

Browse files
committed
Reorganize localpack
1 parent 59cd57f commit 1e2d8c7

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

gulp/utils/localpack-code/main.js

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

4-
const { resolve } = require('path')
5-
const { unlink, rename } = require('fs')
6-
const { promisify } = require('util')
7-
8-
const execa = require('execa')
9-
const tar = require('tar')
10-
const { remove } = require('fs-extra')
11-
12-
const pUnlink = promisify(unlink)
13-
const pRename = promisify(rename)
14-
154
const { getPackageRoot } = require('./root')
165
const { getTempDir, cleanTempDir } = require('./temp')
6+
const { unpack } = require('./unpack')
177
const { addDevChecks } = require('./dev_checks')
188

199
// Runs `npm pack` then unpack it to `opts.output`
@@ -27,11 +17,13 @@ const localpack = async function({ output } = {}) {
2717

2818
await unpack({ packageRoot, tempDir, output: outputA })
2919

30-
await addDevChecks({ output: outputA })
31-
32-
await cleanTempDir({ tempDir })
20+
await Promise.all([
21+
addDevChecks({ output: outputA }),
22+
cleanTempDir({ tempDir }),
23+
])
3324
}
3425

26+
// Retrieve where package is unpacked
3527
const getOutput = function({
3628
packageRoot,
3729
output = `${packageRoot}/${DEFAULT_OUTPUT}`,
@@ -41,28 +33,4 @@ const getOutput = function({
4133

4234
const DEFAULT_OUTPUT = 'localpack'
4335

44-
// Runs `npm pack` and unpack it to `output`
45-
const unpack = async function({ packageRoot, tempDir, output }) {
46-
// We use `npm pack` instead of `require('./npm/lib/pack')`:
47-
// - to use the same `npm` version as the one globally installed,
48-
// so it mirrors what will be published
49-
// - to make code less likely to change with npm internal changes
50-
// However this means this next line is 4 times slower (because it creates
51-
// a new process)
52-
const { stdout } = await execa.shell(
53-
`npm pack --silent --no-update-notifier ${packageRoot}`,
54-
{ stderr: 'inherit', cwd: tempDir },
55-
)
56-
const tarball = resolve(tempDir, stdout)
57-
58-
await tar.x({ file: tarball, cwd: tempDir })
59-
60-
await pUnlink(tarball)
61-
62-
// Is silent when `output` does not exist
63-
await remove(output)
64-
65-
await pRename(`${tempDir}/package`, output)
66-
}
67-
6836
module.exports = localpack

gulp/utils/localpack-code/temp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { promisify } = require('util')
77
const pRmdir = promisify(rmdir)
88
const pMkdir = promisify(mkdir)
99

10-
// `tempDir` is `/{tmpdir()}/localpack/RANDOM_ID`
10+
// `tempDir` is `/{tmpdir()}/localpack-RANDOM_ID`
1111
const getTempDir = async function() {
1212
const osTempDir = tmpdir()
1313
const randomId = getRandomId()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const { resolve } = require('path')
4+
const { unlink, rename } = require('fs')
5+
const { promisify } = require('util')
6+
7+
const execa = require('execa')
8+
const tar = require('tar')
9+
const { remove } = require('fs-extra')
10+
11+
const pUnlink = promisify(unlink)
12+
const pRename = promisify(rename)
13+
14+
// Runs `npm pack` and unpack it to `output`
15+
const unpack = async function({ packageRoot, tempDir, output }) {
16+
await Promise.all([
17+
tempUnpack({ packageRoot, tempDir }),
18+
// Is silent when `output` does not exist
19+
remove(output),
20+
])
21+
22+
await pRename(`${tempDir}/package`, output)
23+
}
24+
25+
const tempUnpack = async function({ packageRoot, tempDir }) {
26+
// We use `npm pack` instead of `require('./npm/lib/pack')`:
27+
// - to use the same `npm` version as the one globally installed,
28+
// so it mirrors what will be published
29+
// - to make code less likely to change with npm internal changes
30+
// However this means this next line is 4 times slower (because it creates
31+
// a new process)
32+
const { stdout } = await execa.shell(
33+
`npm pack --silent --no-update-notifier ${packageRoot}`,
34+
{ stderr: 'inherit', cwd: tempDir },
35+
)
36+
const tarball = resolve(tempDir, stdout)
37+
38+
await tar.x({ file: tarball, cwd: tempDir })
39+
40+
await pUnlink(tarball)
41+
}
42+
43+
module.exports = {
44+
unpack,
45+
}

0 commit comments

Comments
 (0)