-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.js
executable file
·108 lines (104 loc) · 3.73 KB
/
app.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
#!/usr/bin/env node
"use strict";
var fs = require("fs");
var nopt = require("nopt");
var path = require("path");
var glob = require("glob");
var mkdirp = require("mkdirp");
var jsdocFlow = require("./index");
var opts = nopt({
"file": path,
"directory": path,
"outdir": path,
"copy": Boolean,
"skip": Boolean
}, {
"f": "--file",
"d": "--directory",
"o": "--outdir",
"c": "--copy",
"s": "--skip"
});
if (opts.file) {
console.log(
jsdocFlow(fs.readFileSync(opts.file, "utf8"))
);
}
else if (opts.directory && opts.outdir) {
var absDirectory = path.resolve(opts.directory);
var absOutDirectory = path.resolve(opts.outdir);
// make directory if not exists
try {
fs.mkdirSync(absOutDirectory);
}
catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
// loop all files in absDirectory which have either any file ext or .js
var fileExt = opts.copy ? "*" : "js";
glob(absDirectory + "/**/*." + fileExt, function(err, files) {
if (err) {
console.error(err);
process.exit(1);
return;
}
files.forEach(function(fpath) {
// snip the absolute part to get the directory structure for outdir
// Also make sure we're speaking the same slashes
var relativeDir = fpath.replace(absDirectory.replace(/\\/g, "/"), "");
var outFilePath = path.join(absOutDirectory, relativeDir);
// make directories after stripping filename
mkdirp.sync(path.dirname(outFilePath));
if (opts.copy && path.extname(fpath) !== ".js") {
// copy it over. We don't know how big this is so use streams
var rs = fs.createReadStream(fpath);
rs.on("error", function(err) {
console.error(err);
process.exit(1);
});
var ws = fs.createWriteStream(outFilePath);
ws.on("error", function(err) {
console.error(err);
process.exit(1);
});
rs.pipe(ws);
return;
}
console.log(fpath);
var input = fs.readFileSync(fpath, "utf8");
var output;
try {
output = jsdocFlow(input);
}
catch(err) {
// if we fail to convert and --skip is enabled, then just copy
// the input file as-is.
if (opts.skip) {
console.log(" Failed to parse: " + err);
output = input;
}
else {
throw err;
}
}
fs.writeFileSync(outFilePath, output);
});
});
}
else {
console.log("Convert JSDoc comments in a file to Flow annotations");
console.log("Usage:");
console.log(" flow-jsdoc -f FILEPATH");
console.log(" flow-jsdoc -d PATH -o PATH [-c]");
console.log("Options:");
console.log(" -f, --file FILEPATH The .js file with JSDoc to convert. Prints to stdout.");
console.log(" -d, --directory PATH The directory with .js files to convert. Will inspect recursively.");
console.log(" -o, --outdir PATH Required if -d is set. The output directory to dump flow-annotated files to.");
console.log(" -c, --copy Set to copy other file extensions across to outdir.");
console.log(" -s, --skip Set to copy over .js files which cannot be parsed correctly (e.g. invalid ES6) to outdir.");
console.log("File Usage:\n flow-jsdoc -f path/to/file.js");
console.log("Directory Usage:\n flow-jsdoc -d path/to/dir -o out/dir -c");
process.exit(0);
}