Skip to content

Commit 69c56d7

Browse files
author
Victor Bjelkholm
committed
Implement --clear
1 parent 048f907 commit 69c56d7

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

functional_test.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,29 @@ check_should_error()
3131
fi
3232
}
3333

34+
# Install one package
3435
run_test "Can install package 'colors'" "./index.js colors"
3536
test -d $TEST_PATH/node_modules/colors
3637
check_failure "node_modules/colors was not installed"
3738

39+
# Install two packages
3840
run_test "Can install packages 'colors' & 'lodash'" "./index.js colors lodash"
3941
test -d $TEST_PATH/node_modules/colors
4042
check_failure "node_modules/colors was not installed"
4143
test -d $TEST_PATH/node_modules/lodash
4244
check_failure "node_modules/lodash was not installed"
4345

46+
# Can't install packages that doesn't exists
4447
run_test "Cannot install missing package 'coloursssss'" "./index.js coloursssss"
4548
test -d $TEST_PATH/node_modules/coloursssss
4649
check_should_error "node_modules/coloursssss was installed"
4750
echo "NOTE: Above error is normal and is fine, we're testing that we cannot install missing packages"
4851

52+
# Clear cache
53+
run_test "Can clear the cache" "./index.js --clear"
54+
test -d $TEST_PATH/node_modules
55+
check_should_error "node_modules existed!"
56+
4957
if [ $FAILED -eq 0 ]
5058
then
5159
echo "\n\nALL TESTS PASSED!"

index.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var npmi = require('npmi')
44
var path = require('path')
55
var colors = require('colors')
66
var replHistory = require('repl.history')
7+
var exec = require('child_process').exec
78

89
const TRYMODULE_PATH = process.env.TRYMODULE_PATH || path.resolve((process.env.HOME || process.env.USERPROFILE), '.trymodule')
910
const TRYMODULE_HISTORY_PATH = process.env.TRYMODULE_HISTORY_PATH || path.resolve(TRYMODULE_PATH, 'repl_history')
@@ -53,22 +54,32 @@ const addPackageToObject = (obj, pkg) => {
5354
return obj
5455
}
5556

56-
logGreen('Gonna start a REPL with packages installed and loaded for you')
57-
58-
const packages_to_install = takePackageArguments(process.argv)
59-
60-
const promises_for_installation = packages_to_install.map((package_name) => loadPackage(package_name))
57+
if (process.argv[2] === '--clear') {
58+
console.log(`Removing folder ${TRYMODULE_PATH + '/node_modules'}`)
59+
exec('rm -r ' + TRYMODULE_PATH + '/node_modules', (err, stdout, stderr) => {
60+
if (!err) {
61+
logGreen('Cache successfully cleared!')
62+
process.exit(0)
63+
} else {
64+
throw new Error('Could not remove cache! Error ' + err)
65+
}
66+
})
67+
} else {
68+
logGreen('Gonna start a REPL with packages installed and loaded for you')
69+
const packages_to_install = takePackageArguments(process.argv)
70+
const promises_for_installation = packages_to_install.map((package_name) => loadPackage(package_name))
6171

62-
Promise.all(promises_for_installation).then((packages) => {
63-
const context_packages = packages.reduce((context, pkg) => {
64-
return addPackageToObject(context, pkg)
65-
}, {})
66-
console.log('REPL started...')
67-
if (!process.env.TRYMODULE_NONINTERACTIVE) {
68-
var replServer = repl.start({
69-
prompt: '> '
70-
})
71-
replHistory(replServer, TRYMODULE_HISTORY_PATH)
72-
replServer.context = Object.assign(replServer.context, context_packages)
73-
}
74-
})
72+
Promise.all(promises_for_installation).then((packages) => {
73+
const context_packages = packages.reduce((context, pkg) => {
74+
return addPackageToObject(context, pkg)
75+
}, {})
76+
console.log('REPL started...')
77+
if (!process.env.TRYMODULE_NONINTERACTIVE) {
78+
var replServer = repl.start({
79+
prompt: '> '
80+
})
81+
replHistory(replServer, TRYMODULE_HISTORY_PATH)
82+
replServer.context = Object.assign(replServer.context, context_packages)
83+
}
84+
})
85+
}

0 commit comments

Comments
 (0)