UNPKG

6.37 kBJavaScriptView Raw
1"use strict";
2/*---------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 *--------------------------------------------------------------------------------------------*/
6Object.defineProperty(exports, "__esModule", { value: true });
7const fs = require("fs");
8const path = require("path");
9const promisify = require("pify");
10const tasks_1 = require("@microsoft.azure/tasks");
11class PathNotFoundException extends tasks_1.Exception {
12 constructor(path, exitCode = 1) {
13 super(`File '${path}' not found.`, exitCode);
14 this.exitCode = exitCode;
15 Object.setPrototypeOf(this, PathNotFoundException.prototype);
16 }
17}
18exports.PathNotFoundException = PathNotFoundException;
19class PathIsNotFileException extends tasks_1.Exception {
20 constructor(path, exitCode = 1) {
21 super(`File '${path}' is not a file.`, exitCode);
22 this.exitCode = exitCode;
23 Object.setPrototypeOf(this, PathIsNotFileException.prototype);
24 }
25}
26exports.PathIsNotFileException = PathIsNotFileException;
27class PathIsNotDirectoryException extends tasks_1.Exception {
28 constructor(path, exitCode = 1) {
29 super(`File '${path}' is not a directory.`, exitCode);
30 this.exitCode = exitCode;
31 Object.setPrototypeOf(this, PathIsNotFileException.prototype);
32 }
33}
34exports.PathIsNotDirectoryException = PathIsNotDirectoryException;
35class UnableToRemoveException extends tasks_1.Exception {
36 constructor(path, exitCode = 1) {
37 super(`Unable to remove '${path}'.`, exitCode);
38 this.exitCode = exitCode;
39 Object.setPrototypeOf(this, UnableToRemoveException.prototype);
40 }
41}
42exports.UnableToRemoveException = UnableToRemoveException;
43class UnableToMakeDirectoryException extends tasks_1.Exception {
44 constructor(path, exitCode = 1) {
45 super(`Unable to create directory '${path}'.`, exitCode);
46 this.exitCode = exitCode;
47 Object.setPrototypeOf(this, UnableToMakeDirectoryException.prototype);
48 }
49}
50exports.UnableToMakeDirectoryException = UnableToMakeDirectoryException;
51exports.exists = path => new Promise((r, j) => fs.stat(path, (err, stats) => err ? r(false) : r(true)));
52exports.readdir = promisify(fs.readdir);
53exports.close = promisify(fs.close);
54exports.writeFile = (filename, content) => Promise.resolve(fs.writeFileSync(filename, content)); // for some reason writeFile only produced empty files
55exports.lstat = promisify(fs.lstat);
56const fs_rmdir = promisify(fs.rmdir);
57const unlink = promisify(fs.unlink);
58const fs_mkdir = promisify(fs.mkdir);
59const fs_open = promisify(fs.open);
60const fs_close = promisify(fs.close);
61async function mkdir(dirPath) {
62 if (!await isDirectory(dirPath)) {
63 const p = path.normalize(dirPath + "/");
64 const parent = path.dirname(dirPath);
65 if (!await isDirectory(parent)) {
66 if (p != parent) {
67 await mkdir(parent);
68 }
69 }
70 try {
71 await fs_mkdir(p);
72 }
73 catch (e) {
74 if (!await isDirectory(p)) {
75 throw new UnableToMakeDirectoryException(p);
76 }
77 }
78 }
79}
80exports.mkdir = mkdir;
81const fs_readFile = promisify(fs.readFile);
82async function readFile(filename) {
83 return fs_readFile(filename, "utf-8");
84}
85exports.readFile = readFile;
86async function isDirectory(dirPath) {
87 try {
88 if (await exports.exists(dirPath)) {
89 return (await exports.lstat(dirPath)).isDirectory();
90 }
91 }
92 catch (e) {
93 // don't throw!
94 }
95 return false;
96}
97exports.isDirectory = isDirectory;
98async function isFile(filePath) {
99 try {
100 if (await exports.exists(filePath)) {
101 return !(await exports.lstat(filePath)).isDirectory();
102 }
103 }
104 catch (e) {
105 // don't throw!
106 }
107 return false;
108}
109exports.isFile = isFile;
110async function rmdir(dirPath) {
111 // if it's not there, do nothing.
112 if (!await exports.exists(dirPath)) {
113 return;
114 }
115 //if it's not a directory, that's bad.
116 if (!await isDirectory(dirPath)) {
117 throw new PathIsNotDirectoryException(dirPath);
118 }
119 // make sure this isn't the current directory.
120 if (process.cwd() === path.normalize(dirPath)) {
121 process.chdir(`${dirPath}/..`);
122 }
123 // make sure the folder is empty first.
124 const files = await exports.readdir(dirPath);
125 if (files.length) {
126 const awaiter = new tasks_1.OutstandingTaskAwaiter();
127 try {
128 for (const file of files) {
129 try {
130 const p = path.join(dirPath, file);
131 if (await isDirectory(p)) {
132 // folders are recursively rmdir'd
133 awaiter.Await(rmdir(p));
134 }
135 else {
136 // files and symlinks are unlink'd
137 awaiter.Await(unlink(p).catch(() => { }));
138 }
139 }
140 catch (e) {
141 // uh... can't.. ok.
142 }
143 }
144 }
145 finally {
146 // after all the entries are done
147 await awaiter.Wait();
148 }
149 }
150 try {
151 // if this fails for some reason, check if it's important.
152 await fs_rmdir(dirPath);
153 }
154 catch (e) {
155 // is it gone? that's all we really care about.
156 if (await isDirectory(dirPath)) {
157 // directory did not delete
158 throw new UnableToRemoveException(dirPath);
159 }
160 }
161}
162exports.rmdir = rmdir;
163async function rmFile(filePath) {
164 // not there? no problem
165 if (!exports.exists(filePath)) {
166 return;
167 }
168 // not a file? that's not cool.
169 if (await isDirectory(filePath)) {
170 throw new PathIsNotFileException(filePath);
171 }
172 try {
173 // files and symlinks are unlink'd
174 await unlink(filePath);
175 }
176 catch (e) {
177 // is it gone? that's all we really care about.
178 if (await exports.exists(filePath)) {
179 // directory did not delete
180 throw new UnableToRemoveException(filePath);
181 }
182 }
183}
184exports.rmFile = rmFile;
185//# sourceMappingURL=file-io.js.map
\No newline at end of file