-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
117 lines (97 loc) · 2.53 KB
/
create.js
File metadata and controls
117 lines (97 loc) · 2.53 KB
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
#!/usr/bin/env node
// I wish this didn’t have to exist https://fediverse.zachleat.com/@zachleat/112615813950390962
import { parseArgs } from "node:util";
import path from "node:path";
import fs from "node:fs";
import chalk from "kleur";
import readline from "node:readline";
const SINGLE_QUOTE = "'";
const DOUBLE_QUOTE = '"';
async function getStdIn() {
return new Promise((resolve, reject) => {
let rl = readline.createInterface({ input: process.stdin });
let timer = setTimeout(() => {
reject();
}, 100);
rl.on("line", line => {
resolve(line);
clearTimeout(timer);
});
rl.on("end", () => {
reject();
});
});
}
function stripQuotes(content) {
let trimmed = content.trim();
if(trimmed.startsWith(SINGLE_QUOTE) && trimmed.endsWith(SINGLE_QUOTE) || trimmed.startsWith(DOUBLE_QUOTE) && trimmed.endsWith(DOUBLE_QUOTE)) {
return trimmed.slice(1, -1);
}
return content;
}
function isExistingDir(filepath) {
return fs.existsSync(filepath) && fs.statSync(filepath).isDirectory();
}
function hasFilename(filepath) {
if(filepath.endsWith(path.sep) || filepath.endsWith("/")) {
return false;
}
let parsed = path.parse(filename);
if(!parsed.base) {
return false;
}
return true;
}
let { positionals, values } = parseArgs({
allowPositionals: true,
strict: false,
options: {
encoding: {
type: "string",
default: "utf8",
},
quiet: {
type: "boolean",
default: false,
},
},
});
// TODO check not a parent directory
let { quiet, encoding } = values;
let [ filename, content ] = positionals;
let src;
if(!content) {
try {
content = await getStdIn();
src = "stdin";
} catch(e) {
// do nothing
}
// for Windows cmd.exe
content = stripQuotes(content);
}
// Input checking
if(!filename || !content || !hasFilename(filename) || isExistingDir(filename)) {
console.error("Incorrect usage, expected one of:");
console.error(" npx @11ty/create file_path 'File Content'");
console.error(" echo 'File Content' | npx @11ty/create file_path");
process.exit(1);
}
// Create parent dirs
let dirs = filename.split(path.sep);
// On Windows, work with forward and back slashes
if(dirs.length <= 1 && path.sep !== "/" && filename.includes("/")) {
dirs = filename.split("/");
}
if(dirs.length > 1 ) {
dirs.pop();
fs.mkdirSync(dirs.join(path.sep), {
recursive: true
});
}
// Write file
fs.writeFileSync(filename, content, { encoding });
// Output
if(!quiet) {
console.log( `${chalk.gray('[11ty/create]')} Writing ${filename} ${chalk.gray(`(${(content.length/1000).toFixed(3)}kb)${src ? ` (${src})` : ""}`)}` );
}