-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkscript.js
executable file
·164 lines (143 loc) · 4.79 KB
/
mkscript.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const config = {
// End each new script with a blank line after the headers
endWithBlankline: true,
// Set +x on each created file
// This doesn't fail on Windows, but doesn't do anything either
setExecutable: true,
// Create symbolic link after the new script is created
// Loudly complains if set to true on Windows
symlink: {
create: true,
location: "~/.local/bin",
},
// The top few lines for each type of supported script
headers: {
py: [
"#!/usr/bin/env python3",
"# -*- coding: utf-8 -*-",
],
rb: [
"#!/usr/bin/env ruby",
"# encoding: UTF-8",
],
js: [
"#!/usr/bin/env node",
"\"use strict\";"
],
sh: [
"#!/bin/bash", // /usr/local/bin/bash on *bsd
],
}
}
/**
* Create a file named scriptName, fill the first few lines
* with data from headers[], and make it executable.
*
* @param {String} scriptName: name of the file to create.
* @returns {Boolean} true on file written.
*/
function makeScript(scriptName) {
// Make sure file doesn't exist
fs.stat(scriptName, (err, stat) => {
if (err) {
// Get specified extension
const scriptExt = path.extname(scriptName).substr(1);
// See if that type of script is supported
if (!config.headers.hasOwnProperty(scriptExt)) {
console.error(`${process.argv0}: Unknown filetype ${scriptName}`);
return false;
}
// Does directory exist?
fs.stat(path.dirname(scriptName), (err, stat) => {
if (err) {
console.error(`${process.argv0}: ${path.dirname(scriptName)} doesn't exist`);
return false;
}
else {
// Is it writable?
fs.access(path.dirname(scriptName), fs.constants.W_OK, (err) => {
if (err) {
console.error(`${process.argv0}: directory not writable`);
return false;
}
else
writeFile(scriptName);
});
}
});
}
else {
console.error(`${process.argv0}: File '${scriptName}' exists already`);
return false;
}
});
// Actually write the file
function writeFile(name) {
const ext = path.extname(name).substr(1);
let data = config.headers[ext].join("\n");
data += "\n";
if (config.endWithBlankline) {
data += "\n";
}
fs.writeFile(name, data, err => {
if (!err && config.setExecutable) {
fs.chmod(name, "755", err => {
if (!err && config.symlink.create) {
createSymlink(name);
}
});
}
});
}
// Create symlink
function createSymlink(name) {
if (process.platform === "win32") {
console.error(`${process.argv0}: Stubbornly refusing to create symlink ${name} on Windows"`);
}
else {
// Get absolute path of newly created script
const fullPath = path.join(path.resolve(path.dirname(name)), name);
// Remove extension from filename
const nameSansExt = path.basename(name, path.extname(name));
// Get path of where to put the symlink
const linkPath = path.join(
config.symlink.location.replace("~", process.env["HOME"]), // expand tilde
path.basename(name, path.extname(name)) // remove extension
)
fs.symlink(fullPath, linkPath, (err) => {
if (err) {
console.error(`${process.argv0}: error: ${err.message}`);
return false;
}
else {
console.log(`${process.argv0}: Created ${linkPath} -> ${fullPath}`);
return true;
}
});
}
}
}
/**
* Print usage information
*/
function usage() {
console.error(`Usage: ${process.argv0} <script_name.sh>`);
return -1;
}
/**
* Program entry point: Lazily parse command line and either
* call makeScript() or call usage().
*/
if (typeof module !== "undefined" && !module.parent) {
if (process.argv.length < 3)
process.exit(usage());
const nodePath = process.argv.shift();
const thisPath = process.argv.shift();
let scriptName = "";
while (scriptName = process.argv.shift())
makeScript(scriptName);
}