Skip to content

Commit 7965776

Browse files
committed
Make sure output directories exist before writing a file
1 parent 631478c commit 7965776

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

cli/asc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const path = require("path");
1919
const utf8 = require("@protobufjs/utf8");
2020
const colorsUtil = require("./util/colors");
2121
const optionsUtil = require("./util/options");
22+
const mkdirp = require("./util/mkdirp");
2223
const EOL = process.platform === "win32" ? "\r\n" : "\n";
2324

2425
// Emscripten adds an `uncaughtException` listener to Binaryen that results in an additional
@@ -704,6 +705,7 @@ exports.main = function main(argv, options, callback) {
704705
try {
705706
stats.writeCount++;
706707
stats.writeTime += measure(() => {
708+
mkdirp(path.dirname(filename));
707709
if (typeof contents === "string") {
708710
fs.writeFileSync(filename, contents, { encoding: "utf8" } );
709711
} else {

cli/util/mkdirp.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Options {
2+
mode?: number;
3+
}
4+
declare function mkdirp(path: string, options?: Options): string | null;
5+
export = mkdirp;

cli/util/mkdirp.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2010 James Halliday (mail@substack.net)
3+
4+
This project is free software released under the MIT/X11 license:
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
25+
var path = require("path");
26+
var fs = require("fs");
27+
var _0777 = parseInt("0777", 8);
28+
29+
module.exports = function mkdirp(p, opts, made) {
30+
if (!opts || typeof opts !== "object") {
31+
opts = { mode: opts };
32+
}
33+
var mode = opts.mode;
34+
if (mode === undefined) {
35+
mode = _0777 & (~process.umask());
36+
}
37+
if (!made) made = null;
38+
p = path.resolve(p);
39+
try {
40+
fs.mkdirSync(p, mode);
41+
made = made || p;
42+
} catch (err0) {
43+
switch (err0.code) {
44+
case "ENOENT":
45+
made = mkdirp(path.dirname(p), opts, made);
46+
mkdirp(p, opts, made);
47+
break;
48+
default:
49+
var stat;
50+
try {
51+
stat = fs.statSync(p);
52+
} catch (err1) {
53+
throw err0;
54+
}
55+
if (!stat.isDirectory()) throw err0;
56+
break;
57+
}
58+
}
59+
return made;
60+
};

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)