detype 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/detype.js +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -233
- package/dist/index.cjs +194 -257
- package/dist/index.d.cts +34 -33
- package/dist/index.d.ts +34 -33
- package/dist/index.js +2 -12
- package/dist/transformFile-qmpi6_2q.js +190 -0
- package/package.json +11 -8
- package/dist/chunk-VYED23Y7.js +0 -250
package/detype.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import "./dist/cli";
|
|
2
|
+
import "./dist/cli.js";
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.js
CHANGED
|
@@ -1,232 +1,100 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "./chunk-VYED23Y7.js";
|
|
5
|
-
|
|
6
|
-
// src/cli-lib.ts
|
|
7
|
-
import fs from "fs";
|
|
8
|
-
import path from "path";
|
|
1
|
+
import { n as transformFile, t as removeMagicCommentsFromFile } from "./transformFile-qmpi6_2q.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
9
4
|
import fastGlob from "fast-glob";
|
|
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
|
-
const flags = [];
|
|
99
|
-
for (const arg of args2) {
|
|
100
|
-
if (arg === "--") {
|
|
101
|
-
dashDash = true;
|
|
102
|
-
} else if (dashDash || !arg.startsWith("-")) {
|
|
103
|
-
params.push(arg);
|
|
104
|
-
} else {
|
|
105
|
-
flags.push(arg);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (params.length > 2) {
|
|
109
|
-
console.error("Too many arguments");
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
let [input, output] = params;
|
|
113
|
-
if (params.length === 0 || flags.some((flag) => flag === "-h" || flag === "--help")) {
|
|
114
|
-
printUsage();
|
|
115
|
-
return params.length > 0;
|
|
116
|
-
}
|
|
117
|
-
if (flags.some((flag) => flag === "-v" || flag === "--version")) {
|
|
118
|
-
console.log(VERSION);
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
const removeMagic = flags.some(
|
|
122
|
-
(flag) => flag === "-m" || flag === "--remove-magic-comments"
|
|
123
|
-
);
|
|
124
|
-
const removeTsComments = flags.some(
|
|
125
|
-
(flag) => flag === "-t" || flag === "--remove-ts-comments"
|
|
126
|
-
);
|
|
127
|
-
if (removeMagic && removeTsComments) {
|
|
128
|
-
console.warn(
|
|
129
|
-
"--remove-ts-comments has no effect when --remove-magic-comments is used"
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
if (!removeMagic) {
|
|
133
|
-
[input, output] = args2;
|
|
134
|
-
}
|
|
135
|
-
if (!input) {
|
|
136
|
-
console.error("No input file or directory given");
|
|
137
|
-
printUsage();
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
const inputStat = await stat(input);
|
|
141
|
-
if (inputStat.isDirectory()) {
|
|
142
|
-
if (!output) {
|
|
143
|
-
console.error("No output directory given");
|
|
144
|
-
printUsage();
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
const files = (await fastGlob(unixify(input + "/**/*.{ts,tsx,vue}"))).filter((file) => !file.endsWith(".d.ts"));
|
|
148
|
-
const dirs = [...new Set(files.map((file) => path.dirname(file)))].sort();
|
|
149
|
-
await mkdir(path.normalize(output), { recursive: true });
|
|
150
|
-
for (const dir of dirs) {
|
|
151
|
-
const outDir = path.join(output, path.relative(input, dir));
|
|
152
|
-
if (outDir === output) continue;
|
|
153
|
-
await mkdir(path.normalize(outDir), { recursive: true });
|
|
154
|
-
}
|
|
155
|
-
for (const file of files) {
|
|
156
|
-
const inputDir = path.dirname(path.relative(input, file));
|
|
157
|
-
const outputName = inferName(file, path.join(output, inputDir));
|
|
158
|
-
if (removeMagic) {
|
|
159
|
-
await removeMagicCommentsFromFile(
|
|
160
|
-
path.normalize(file),
|
|
161
|
-
path.normalize(outputName)
|
|
162
|
-
);
|
|
163
|
-
} else {
|
|
164
|
-
await transformFile(path.normalize(file), path.normalize(outputName), {
|
|
165
|
-
removeTsComments
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return true;
|
|
170
|
-
}
|
|
171
|
-
if (output) {
|
|
172
|
-
const outputStat = await stat(output).catch((error) => {
|
|
173
|
-
if (error && error.code === "ENOENT") {
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
throw error;
|
|
177
|
-
});
|
|
178
|
-
if (outputStat && outputStat.isDirectory()) {
|
|
179
|
-
output = inferName(input, output);
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
if (removeMagic) {
|
|
183
|
-
console.error(
|
|
184
|
-
"Output file name is required when removing magic comments"
|
|
185
|
-
);
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
if (input.endsWith(".vue")) {
|
|
189
|
-
console.error("Output file name is required for .vue files");
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
output = inferName(input);
|
|
193
|
-
}
|
|
194
|
-
const outputDir = path.dirname(output);
|
|
195
|
-
if (outputDir) {
|
|
196
|
-
await mkdir(path.normalize(outputDir), { recursive: true });
|
|
197
|
-
}
|
|
198
|
-
if (removeMagic) {
|
|
199
|
-
await removeMagicCommentsFromFile(
|
|
200
|
-
path.normalize(input),
|
|
201
|
-
path.normalize(output)
|
|
202
|
-
);
|
|
203
|
-
} else {
|
|
204
|
-
await transformFile(path.normalize(input), path.normalize(output), {
|
|
205
|
-
removeTsComments
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
return true;
|
|
209
|
-
function inferName(input2, outputDir2) {
|
|
210
|
-
let output2;
|
|
211
|
-
const { dir, name, ext } = path.parse(input2);
|
|
212
|
-
if (removeMagic) {
|
|
213
|
-
output2 = path.join(outputDir2 ?? dir, `${name}${ext}`);
|
|
214
|
-
} else if (ext === ".ts") {
|
|
215
|
-
output2 = path.join(outputDir2 ?? dir, name + ".js");
|
|
216
|
-
} else if (ext === ".tsx") {
|
|
217
|
-
output2 = path.join(outputDir2 ?? dir, name + ".jsx");
|
|
218
|
-
} else if (ext === ".vue") {
|
|
219
|
-
output2 = path.join(outputDir2 ?? dir, name + ".vue");
|
|
220
|
-
} else {
|
|
221
|
-
throw new Error(`Unknwon file extension ${input2}`);
|
|
222
|
-
}
|
|
223
|
-
return output2;
|
|
224
|
-
}
|
|
5
|
+
//#region package.json
|
|
6
|
+
var version = "2.0.5";
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/cli-lib.ts
|
|
9
|
+
const { stat, mkdir } = fs.promises;
|
|
10
|
+
async function cli(...args) {
|
|
11
|
+
let dashDash = false;
|
|
12
|
+
const params = [];
|
|
13
|
+
const flags = [];
|
|
14
|
+
for (const arg of args) if (arg === "--") dashDash = true;
|
|
15
|
+
else if (dashDash || !arg.startsWith("-")) params.push(arg);
|
|
16
|
+
else flags.push(arg);
|
|
17
|
+
if (params.length > 2) {
|
|
18
|
+
console.error("Too many arguments");
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
let [input, output] = params;
|
|
22
|
+
if (params.length === 0 || flags.some((flag) => flag === "-h" || flag === "--help")) {
|
|
23
|
+
printUsage();
|
|
24
|
+
return params.length > 0;
|
|
25
|
+
}
|
|
26
|
+
if (flags.some((flag) => flag === "-v" || flag === "--version")) {
|
|
27
|
+
console.log(VERSION);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
const removeMagic = flags.some((flag) => flag === "-m" || flag === "--remove-magic-comments");
|
|
31
|
+
const removeTsComments = flags.some((flag) => flag === "-t" || flag === "--remove-ts-comments");
|
|
32
|
+
if (removeMagic && removeTsComments) console.warn("--remove-ts-comments has no effect when --remove-magic-comments is used");
|
|
33
|
+
if (!removeMagic) [input, output] = args;
|
|
34
|
+
if (!input) {
|
|
35
|
+
console.error("No input file or directory given");
|
|
36
|
+
printUsage();
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if ((await stat(input)).isDirectory()) {
|
|
40
|
+
if (!output) {
|
|
41
|
+
console.error("No output directory given");
|
|
42
|
+
printUsage();
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const files = (await fastGlob(unixify(input + "/**/*.{ts,tsx,vue}"))).filter((file) => !file.endsWith(".d.ts"));
|
|
46
|
+
const dirs = [...new Set(files.map((file) => path.dirname(file)))].sort();
|
|
47
|
+
await mkdir(path.normalize(output), { recursive: true });
|
|
48
|
+
for (const dir of dirs) {
|
|
49
|
+
const outDir = path.join(output, path.relative(input, dir));
|
|
50
|
+
if (outDir === output) continue;
|
|
51
|
+
await mkdir(path.normalize(outDir), { recursive: true });
|
|
52
|
+
}
|
|
53
|
+
for (const file of files) {
|
|
54
|
+
const inputDir = path.dirname(path.relative(input, file));
|
|
55
|
+
const outputName = inferName(file, path.join(output, inputDir));
|
|
56
|
+
if (removeMagic) await removeMagicCommentsFromFile(path.normalize(file), path.normalize(outputName));
|
|
57
|
+
else await transformFile(path.normalize(file), path.normalize(outputName), { removeTsComments });
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (output) {
|
|
62
|
+
const outputStat = await stat(output).catch((error) => {
|
|
63
|
+
if (error && error.code === "ENOENT") return null;
|
|
64
|
+
throw error;
|
|
65
|
+
});
|
|
66
|
+
if (outputStat && outputStat.isDirectory()) output = inferName(input, output);
|
|
67
|
+
} else {
|
|
68
|
+
if (removeMagic) {
|
|
69
|
+
console.error("Output file name is required when removing magic comments");
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (input.endsWith(".vue")) {
|
|
73
|
+
console.error("Output file name is required for .vue files");
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
output = inferName(input);
|
|
77
|
+
}
|
|
78
|
+
const outputDir = path.dirname(output);
|
|
79
|
+
if (outputDir) await mkdir(path.normalize(outputDir), { recursive: true });
|
|
80
|
+
if (removeMagic) await removeMagicCommentsFromFile(path.normalize(input), path.normalize(output));
|
|
81
|
+
else await transformFile(path.normalize(input), path.normalize(output), { removeTsComments });
|
|
82
|
+
return true;
|
|
83
|
+
function inferName(input, outputDir) {
|
|
84
|
+
let output;
|
|
85
|
+
const { dir, name, ext } = path.parse(input);
|
|
86
|
+
if (removeMagic) output = path.join(outputDir ?? dir, `${name}${ext}`);
|
|
87
|
+
else if (ext === ".ts") output = path.join(outputDir ?? dir, name + ".js");
|
|
88
|
+
else if (ext === ".tsx") output = path.join(outputDir ?? dir, name + ".jsx");
|
|
89
|
+
else if (ext === ".vue") output = path.join(outputDir ?? dir, name + ".vue");
|
|
90
|
+
else throw new Error(`Unknwon file extension ${input}`);
|
|
91
|
+
return output;
|
|
92
|
+
}
|
|
225
93
|
}
|
|
226
94
|
function printUsage() {
|
|
227
|
-
|
|
95
|
+
console.error(USAGE);
|
|
228
96
|
}
|
|
229
|
-
|
|
97
|
+
const USAGE = `Usage:
|
|
230
98
|
|
|
231
99
|
detype [-m | --remove-magic-comments] <INPUT> [OUTPUT]
|
|
232
100
|
|
|
@@ -248,14 +116,16 @@ var USAGE = `Usage:
|
|
|
248
116
|
detype [-h | --help]
|
|
249
117
|
|
|
250
118
|
Print this help and exit`;
|
|
251
|
-
|
|
119
|
+
const VERSION = version;
|
|
120
|
+
/** Unixify path */
|
|
252
121
|
function unixify(name) {
|
|
253
|
-
|
|
122
|
+
return name.replaceAll(path.sep, "/");
|
|
254
123
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
process.exit(1);
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/cli.ts
|
|
126
|
+
cli(...process.argv.slice(2)).then((success) => process.exit(success ? 0 : 1)).catch((error) => {
|
|
127
|
+
console.error(error);
|
|
128
|
+
process.exit(1);
|
|
261
129
|
});
|
|
130
|
+
//#endregion
|
|
131
|
+
export {};
|