-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·204 lines (159 loc) · 5.55 KB
/
cli.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
// @ts-check
const chalk = require("chalk");
const parseArgs = require("minimist");
const { version: packageVersion } = require("../package.json");
const { runner } = require("../dist/index");
const isJestTestEnv = process.env.JEST_TEST_ENV === "true";
const version = isJestTestEnv ? "<MOCKED>" : packageVersion;
const help =
reindent(`
${chalk.bold(
"codestamp"
)} - Stamp and verify your files and contents [version ${version}]
A language-agnostic tool for signing and verifying the integrity of your
files. It's most useful for guarding codegen'd files against unintentional
manual edits, but it also can be used for signing individual files.
${chalk.bold("Usage")}
$ codestamp target_file [options]
${chalk.bold("Options")}
-w, --write Rewrite the file in-place. Without this flag, codestamp
[boolean] runs in verification mode: it prints the diff to
\`stderr\` and \`exit(1)\` when the stamp is invalid.
-d, --deps One or more file paths or globs. The stamp hash is
[comma-separated computed from the target file's content and all
string] dependencies.
Make sure to quote the globs to let codestamp expand
the globs, rather than your shell.
Examples:
$ codestamp target.ts --deps 'data/foo.json'
$ codestamp target.ts --deps 'data/foo.json,types/*.ts'
$ codestamp target.ts --deps 'data/**/*.json,types/*.ts'
-t, --template A template string for placing the stamp. codestamp will
[string] replace \`%STAMP%\` with the stamp, and \`%CONTENT%\`
with the rest of content.
Use the Node.js API to dynamically place the stamp.
Examples:
$ codestamp target.py -t '# @codegen %STAMP%\\n%CONTENT%'
-h, --help This help guide.
${chalk.bold("Docs")}
${chalk.underline("https://github.com/keyz/codestamp")}
${chalk.bold("Example")}
$ ./your-script-that-generates-types --from ffi.rs,data.json
# Generates \`types.ts\` from \`ffi.rs\` and \`data.json\`
$ cat types.ts
type FFI = ...
$ codestamp types.ts --deps ffi.rs,data.json
${chalk.green(
"+ /* @generated CodeStamp<<c1aa4ff2ac747d1192773354ad64d122>> */"
)}
type FFI = ...
$ codestamp types.ts --deps ffi.rs,data.json --write
CodeStamp: 🔏 Stamped \`types.ts\`.
$ codestamp types.ts --deps ffi.rs,data.json
CodeStamp: ✅ Verified \`types.ts\`.
$ codestamp types.ts --deps ffi.rs,data.json --write
CodeStamp: ✅ Verified \`types.ts\`.
# If you updated \`ffi.rs\` but forgot to run the codegen script...
$ codestamp types.ts --deps ffi.rs,data.json
${chalk.red(
"- /* @generated CodeStamp<<c1aa4ff2ac747d1192773354ad64d122>> */"
)}
${chalk.green(
"+ /* @generated CodeStamp<<64adca472a2638d8c915fb5d83c688f7>> */"
)}
type FFI = ...
$ echo $?
1
`) + "\n";
main().catch((error) => {
console.error(error);
process.exit(1);
});
async function main() {
const argv = parseArgs(process.argv.slice(2), {
boolean: ["write", "help", "version"],
string: ["deps", "template"],
alias: {
w: "write",
d: "deps",
t: "template",
h: "help",
v: "version",
},
});
if (argv.version) {
console.log(version);
return;
}
if (argv.help) {
console.log(help);
return;
}
if (argv._.length !== 1) {
const errorMessage = reindent(`
CodeStamp Error: Missing required argument \`target_file\`.
Run \`codestamp --help\` to see the quick guide and examples.
`);
console.error(errorMessage);
process.exit(1);
}
const targetFilePath = String(argv._[0]);
const shouldWrite = Boolean(argv.write);
if (argv.deps === "") {
const errorMessage = reindent(`
CodeStamp Error: Received empty value for option \`-d, --deps\`.
Run \`codestamp --help\` to see the quick guide and examples.
`);
console.error(errorMessage);
process.exit(1);
}
if (argv.template === "") {
const errorMessage = reindent(`
CodeStamp Error: Received empty value for option \`-t, --template\`.
Run \`codestamp --help\` to see the quick guide and examples.
`);
console.error(errorMessage);
process.exit(1);
}
const rawDeps = argv.deps || "";
const rawTemplate = argv.template || "";
const dependencyGlobList = rawDeps.split(",").filter(Boolean);
const initialStampPlacer = Boolean(rawTemplate)
? undoUnescape(rawTemplate)
: undefined;
const result = await runner({
targetFilePath,
shouldWrite,
dependencyGlobList,
initialStampPlacer,
silent: false,
});
if (result.shouldFatalIfDesired) {
process.exit(1);
}
}
/**
* @param {string} input
* @returns {string}
*/
function reindent(input) {
const lineList = input.split("\n");
const secondLineIndent = (lineList[1] || "").match(/^\s*/)[0].length;
if (secondLineIndent === 0) {
return input;
}
const regex = new RegExp(`^[ \\t]{${secondLineIndent}}`, "gm");
return input.replace(regex, "").trim();
}
/**
* @param {string} input
* @returns {string}
*/
function undoUnescape(input) {
try {
return JSON.parse(`"${input}"`);
} catch (_) {
return input;
}
}