-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4_sync-npm-modules
74 lines (66 loc) · 4.71 KB
/
4_sync-npm-modules
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
#!/bin/bash
cat <<"EOF"
https://github.com/riyadhalnur/npm-modules-sync instructions:
1. On the machine you want to use as the source of your package list, run `npm-sync init -t <your-github-token>`
2. Note the ID of the gist returned from the previous step once it completes
3. On the machine you want to sync using the the gist, run `npm-sync init -t <your-github-token> -i <gist-id>`
4. Run `npm-sync up` subsequently on your main machine to update the list of packages
5. Run `npm-sync dl` subsequently on your synced machines to download the current list of global packages
EOF
# and note there is scant alternative unless you do it yourself https://stackoverflow.com/questions/41583514/npm-how-to-sync-global-packages-between-different-computers
# we'll need a github token. note: As a security precaution, GitHub automatically removes personal access tokens that haven't been used in a year.
# settings > Developer Settings > Personal Access Tokens > Generate new token, then just tick gist as the scope, note you won't be able to see the token ever again.
tokenFile=./sync-npm-modules-github-token.conf
source $tokenFile # gives us github-token-for-gist-access and #github_gist_id
token="$github_token_for_gist_access"
gist=$github_gist_id
[[ ! -z $token ]] && echo "github token is: $token" || (echo "can't access your github token from $tokenFile, exiting..." && exit)
[[ ! -z $gist ]] && echo "github gist can be found at: https://gist.github.com/tonywoode/$gist" || (echo "can't access your github gist from $tokenFile, exiting..." && exit)
echo "try this in browser console to pretty print the gist from that url: JSON.parse(document.getElementById('file-modules-json-LC1').textContent)"
echo ""
# install npm-modules sync if you don't have it (make sure you've installed a version of node first (try nvm install --lts)
if ! type "npm-sync" > /dev/null 2>&1; then
echo "we need to install npm-modules-sync, installing globally..."
npm install -g npm-modules-sync
fi
while true; do
echo "What shall we do now?"
echo " 1. make a one-off initial push from this machine to a gist (you'll need to manually paste the new gist id into the config file)"
echo " 2. make a one-off initial pull from the gist to this machine (his step 3)"
echo " 3. upload npm module changes to gist (his step 4) This might not work if you've been away awhile: so make a new gist with step 1"
echo " 4. download npm module changes from gist (his step 5)"
echo " 5. exit"
read -p "-> " response
case $response in
1) read -s -n 1 -p "Press any key to do an INITIAL sync to this machine (for subsequent syncs read instructions above) . . ."
echo ""
npm-sync init -t "$token"
npm uninstall -g replace-slack-text
echo "note a bug if you have installed anything from your own filesystem, you'll have to delete it from the gist manually atm"
npm install -g /Users/twoode/CODE/Scripts/unslack/
echo "Now paste that gist ID into the config file";;
2) # bear in mind I already ran a gist creation from an initial machine which made me a gist id
read -s -n 1 -p "Press any key to do an INITIAL sync to this machine (for subsequent syncs read instructions above) . . ."
echo ""
npm-sync init -t "$token" -i "$gist"
echo "note a bug if you have installed anything from your own filesystem, you'll have to delete it from the gist manually atm"
echo "I don't say much I did just finish....bye";;
3) echo "Ok, lets upload changes"
echo "we have to uninstall your locally installed non-npm modules or this will fail...we can install them again right after"
npm uninstall -g replace-slack-text
npm-sync up -t "$token" -i "$gist"
npm install -g /Users/twoode/CODE/Scripts/unslack/
echo "saving a copy of the modules installed at this point to /npm-modules-log/"
npm list -g --depth=0 > npm-modules-log/npm_installed_modules_$(hostname).txt && date >> npm-modules-log/npm_installed_modules_$(hostname).txt
;;
4) echo "Ok, lets download changes"
echo "take a copy of the modules installed at this point to /npm-modules-log name *_BEFORE.txt"
npm list -g --depth=0 > npm-modules-log/npm_installed_modules_$(hostname)_BEFORE.txt && date >> npm-modules-log/npm_installed_modules_$(hostname)_BEFORE.txt
npm-sync dl
echo "take a copy of the modules installed after the operation to /npm-modules-log (the one not named *_BEFORE.txt)"
npm list -g --depth=0 > npm-modules-log/npm_installed_modules_$(hostname).txt && date >> npm-modules-log/npm_installed_modules_$(hostname).txt
;;
5) echo "bye" && exit;;
*) echo "Try again";;
esac
done