-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.js
executable file
·129 lines (115 loc) · 3.96 KB
/
command.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
const program = require('commander');
const execSh = require('exec-sh');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { prompt } = require('inquirer');
const { jsonToMap, mapToJson, display } = require('./utils');
if (process.env.HOME || process.env.HOMEPATH) {
var HOME = path.resolve(process.env.HOME || process.env.HOMEPATH, '.cmsh');
var STORE_PATH = path.resolve(HOME, 'store.json');
} else {
console.error(chalk`{bgRed ERR} {bgYellow Initialization} Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!`);
console.error(chalk`{bgRed ERR} {bgYellow Initialization} Trying to use Environment variable USER (Linux) or USERPROFILE (Windows)`);
if(process.env.USER) {
var HOME = path.resolve('/home', process.env.USER, '.cmsh');
var STORE_PATH = path.resolve(HOME, 'store.json');
} else if (process.env.USERPROFILE) {
var HOME = path.resolve(process.env.USERPROFILE, '.cmsh');
var STORE_PATH = path.resolve(HOME, 'store.json');
} else {
console.error(chalk`{bgRed ERR} {bgYellow Initialization} Please set environment variable HOME (Linux) or HOMEPATH (Windows).`);
}
}
if (!fs.existsSync(HOME)) {
try {
require('mkdirp').sync(HOME);
} catch (e) {
console.log(chalk`{bgRed ERR} Something went wrong while initialization!`);
}
}
program
.version('1.0.0')
.description('CMD-Shortener');
program
.command('define <shorthand> <command>')
.alias('d')
.description('Define shorthand')
.action((shorthand, command) => {
fs.readFile(STORE_PATH, 'utf8', function (err, content) {
if (err) {
if (err.code === 'ENOENT') {
content = "{}";
} else {
return console.log(chalk`{bgRed ERR} Something went wrong while saving your shorthands!`);
}
}
let commands = jsonToMap(content);
if (!commands.has(shorthand)) {
commands.set(shorthand, command);
content = mapToJson(commands);
fs.writeFile(STORE_PATH, content, 'utf8', function (err) {
if (err) {
return console.log(chalk`{bgRed ERR} Something went wrong while saving your shorthands!`);
}
});
} else {
let question = [{
type: 'confirm',
name: 'overwrite',
message: `You have already defined "${shorthand}" shorthand, Are you sure you want to overwrite it?`
}];
prompt(question).then((answer) => {
if (!answer) {
return;
}
commands.set(shorthand, command);
content = mapToJson(commands);
fs.writeFile(STORE_PATH, content, 'utf8', function (err) {
if (err) {
return console.log(chalk`{bgRed ERR} Something went wrong while saving your shorthands!`);
}
});
});
}
});
});
program
.command('run <shorthand>')
.alias('r')
.description('Execute command using shorthand')
.action((shorthand) => {
fs.readFile(STORE_PATH, 'utf8', function (err, content) {
if (err) {
if (err.code === 'ENOENT') {
content = "{}";
} else {
return console.log(chalk`{bgRed ERR} Something went wrong while getting your shorthands!`);
}
}
let commands = jsonToMap(content);
if (commands.has(shorthand)) {
execSh(commands.get(shorthand));
} else {
console.log(chalk`{bgYellow WARN} shorthand:${shorthand} is not defined!\n You can define it by hitting {bold cmsh d ${shorthand} <command>}.`)
}
});
});
program
.command('list')
.alias('l')
.description('List all defined shorthands')
.action(() => {
fs.readFile(STORE_PATH, 'utf8', function (err, content) {
if (err) {
if (err.code === 'ENOENT') {
content = "{}";
} else {
return console.log(chalk`{bgRed ERR} Something went wrong while getting your shorthands!`);
}
}
display(content);
});
});
program.parse(process.argv);