-
Notifications
You must be signed in to change notification settings - Fork 29
/
cli.js
executable file
·94 lines (83 loc) · 3.14 KB
/
cli.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env node
var repl = require('repl')
var path = require('path')
var os = require('os')
var colors = require('colors')
var replHistory = require('repl.history')
var vm = require('vm')
var exec = require('child_process').exec
var loadPackages = require('./index')
const TRYMODULE_PATH = process.env.TRYMODULE_PATH || path.resolve((os.homedir()), '.trymodule')
const TRYMODULE_HISTORY_PATH = process.env.TRYMODULE_HISTORY_PATH || path.resolve(TRYMODULE_PATH, 'repl_history')
const flags = []
const packages = {} // data looks like [moduleName, as]
const makeVariableFriendly = str => str.replace(/-|\./g, '_')
process.argv.slice(2).forEach(arg => {
if (arg[0] === '-') {
// matches '--clear', etc
flags.push(arg)
} else if (arg.indexOf('=') > -1) {
// matches 'lodash=_', etc
const i = arg.indexOf('=')
const module = arg.slice(0, i) // 'lodash'
const as = arg.slice(i + 1) // '_'
packages[module] = makeVariableFriendly(as) // ['lodash', '_']
} else {
// assume it's just a regular module name: 'lodash', 'express', etc
packages[arg] = makeVariableFriendly(arg) // call it the module's name
}
})
if (!flags.length && !Object.keys(packages).length) {
throw new Error('You need to provide some arguments!')
}
const logGreen = (msg) => console.log(colors.green(msg))
const hasFlag = (flag) => flags.indexOf(flag) > -1
const addPackageToObject = (obj, pkg) => {
logGreen(`Package '${pkg.name}' was loaded and assigned to '${pkg.as}' in the current scope`)
obj[pkg.as] = pkg.package
return obj
}
if (hasFlag('--clear')) {
console.log(`Removing folder ${TRYMODULE_PATH + '/node_modules'}`)
exec('rm -r ' + TRYMODULE_PATH + '/node_modules', (err, stdout, stderr) => {
if (!err) {
logGreen('Cache successfully cleared!')
process.exit(0)
} else {
throw new Error('Could not remove cache! Error ' + err)
}
})
} else {
logGreen('Gonna start a REPL with packages installed and loaded for you')
// Extract
loadPackages(packages, TRYMODULE_PATH).then((packages) => {
const contextPackages = packages.reduce((context, pkg) => {
return addPackageToObject(context, pkg)
}, {})
console.log('REPL started...')
if (!process.env.TRYMODULE_NONINTERACTIVE) {
var replServer = repl.start({
prompt: '> ',
eval: function evalWithPromises (cmd, context, filename, callback) {
const script = new vm.Script(cmd)
var result = script.runInContext(replServer.context)
// Some libraries use non-native Promise implementations
// (ie lib$es6$promise$promise$$Promise)
if (result instanceof Promise || (typeof result === 'object' && typeof result.then === 'function')) {
console.log('Returned a Promise. waiting for result...')
result.then(function (val) {
callback(null, val)
})
.catch(function (err) {
callback(err)
})
} else {
callback(null, result)
}
}
})
replHistory(replServer, TRYMODULE_HISTORY_PATH)
replServer.context = Object.assign(replServer.context, contextPackages)
}
})
}