-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (66 loc) · 1.86 KB
/
Copy pathindex.js
File metadata and controls
69 lines (66 loc) · 1.86 KB
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
let { updater } = require('@architect/utils')
let { spawnSync: spawn } = require('child_process')
let { join } = require('path')
let { existsSync } = require('fs')
/**
* Add to your Architect project manifest file:
*
* @plugins
* architect/plugin-node-prune
*
* That's it, zero config!
*/
async function pruner ({ inventory }) {
let { lambdaSrcDirs } = inventory.inv
for (let src of lambdaSrcDirs) {
if (existsSync(src)) prune(src)
}
return
}
function prune (pathToPrune) {
let quiet = process.env.ARC_QUIET
let update = updater('Pruner')
let start = Date.now()
let cwd = process.cwd()
pathToPrune = pathToPrune.startsWith(cwd)
? pathToPrune
: join(cwd, pathToPrune)
let cmd = join(cwd, 'node_modules', '@architect', 'plugin-node-prune', 'prune.sh')
let options = { cwd: pathToPrune, shell: true }
let child = spawn(cmd, [], options)
let output = child.stdout
if (!quiet && output) {
// Format response
output = output.toString().split('\n')
let fmt = size => {
if (size >= 1000) return `${size / 1000}MB`
return `${size}KB`
}
let beforeSize = fmt(output[0])
let afterSize = fmt(output[1])
let beforeFiles = output[2]
let afterFiles = output[3]
let prunedFiles = beforeFiles - afterFiles
let prunedSize = fmt(output[0] - output[1])
let pretty = [
`Before ... ${beforeSize} in ${beforeFiles} files`,
`After .... ${afterSize} in ${afterFiles} files`,
`Found .... ${prunedFiles} unnecessary node_modules files`,
`Pruned ... ${prunedSize} in ${Date.now() - start}ms`,
]
update.status(
pathToPrune.replace(cwd, ''),
...pretty
)
}
if (child.status !== 0 || child.error) {
let error = child.error ? child.error : ''
throw Error(`Prune error, exited ${child.status}`, error)
}
}
module.exports = {
deploy: {
start: pruner
},
prune,
}