This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (61 loc) · 2.01 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
let { updater } = require('@architect/utils')
let inventory = require('@architect/inventory')
let { spawnSync: child } = require('child_process')
let { join } = require('path')
let { existsSync } = require('fs')
/**
* Add to your Architect project manifest file:
*
* @macros
* architect/macro-node-prune
*
* That's it, zero config!
*/
module.exports = async function pruner (arc, cloudformation) {
let { inv } = await inventory({})
let { lambdaSrcDirs } = inv
let quiet = process.env.ARC_QUIET
let update = updater('Pruner')
for (let pathToCode of lambdaSrcDirs) {
if (existsSync(pathToCode)) {
let start = Date.now()
let cwd = process.cwd()
pathToCode = pathToCode.startsWith(cwd)
? pathToCode
: join(cwd, pathToCode)
let cmd = join(cwd, 'node_modules', '@architect', 'macro-node-prune', 'prune.sh')
let options = { cwd: pathToCode, shell: true }
let spawn = child(cmd, [], options)
let output = spawn.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(
pathToCode.replace(cwd, ''),
...pretty
)
}
if (spawn.status !== 0 || spawn.error) {
let error = spawn.error ? spawn.error : ''
throw (`Prune error, exited ${spawn.status}`, error)
}
}
}
return cloudformation
}