1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | const fs = require("fs");
|
8 | const path = require("path");
|
9 | const promisify = require("pify");
|
10 | const tasks_1 = require("@microsoft.azure/tasks");
|
11 | class 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 | }
|
18 | exports.PathNotFoundException = PathNotFoundException;
|
19 | class 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 | }
|
26 | exports.PathIsNotFileException = PathIsNotFileException;
|
27 | class 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 | }
|
34 | exports.PathIsNotDirectoryException = PathIsNotDirectoryException;
|
35 | class 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 | }
|
42 | exports.UnableToRemoveException = UnableToRemoveException;
|
43 | class 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 | }
|
50 | exports.UnableToMakeDirectoryException = UnableToMakeDirectoryException;
|
51 | exports.exists = path => new Promise((r, j) => fs.stat(path, (err, stats) => err ? r(false) : r(true)));
|
52 | exports.readdir = promisify(fs.readdir);
|
53 | exports.close = promisify(fs.close);
|
54 | exports.writeFile = (filename, content) => Promise.resolve(fs.writeFileSync(filename, content));
|
55 | exports.lstat = promisify(fs.lstat);
|
56 | const fs_rmdir = promisify(fs.rmdir);
|
57 | const unlink = promisify(fs.unlink);
|
58 | const fs_mkdir = promisify(fs.mkdir);
|
59 | const fs_open = promisify(fs.open);
|
60 | const fs_close = promisify(fs.close);
|
61 | async 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 | }
|
80 | exports.mkdir = mkdir;
|
81 | const fs_readFile = promisify(fs.readFile);
|
82 | async function readFile(filename) {
|
83 | return fs_readFile(filename, "utf-8");
|
84 | }
|
85 | exports.readFile = readFile;
|
86 | async function isDirectory(dirPath) {
|
87 | try {
|
88 | if (await exports.exists(dirPath)) {
|
89 | return (await exports.lstat(dirPath)).isDirectory();
|
90 | }
|
91 | }
|
92 | catch (e) {
|
93 |
|
94 | }
|
95 | return false;
|
96 | }
|
97 | exports.isDirectory = isDirectory;
|
98 | async function isFile(filePath) {
|
99 | try {
|
100 | if (await exports.exists(filePath)) {
|
101 | return !(await exports.lstat(filePath)).isDirectory();
|
102 | }
|
103 | }
|
104 | catch (e) {
|
105 |
|
106 | }
|
107 | return false;
|
108 | }
|
109 | exports.isFile = isFile;
|
110 | async function rmdir(dirPath) {
|
111 |
|
112 | if (!await exports.exists(dirPath)) {
|
113 | return;
|
114 | }
|
115 |
|
116 | if (!await isDirectory(dirPath)) {
|
117 | throw new PathIsNotDirectoryException(dirPath);
|
118 | }
|
119 |
|
120 | if (process.cwd() === path.normalize(dirPath)) {
|
121 | process.chdir(`${dirPath}/..`);
|
122 | }
|
123 |
|
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 |
|
133 | awaiter.Await(rmdir(p));
|
134 | }
|
135 | else {
|
136 |
|
137 | awaiter.Await(unlink(p).catch(() => { }));
|
138 | }
|
139 | }
|
140 | catch (e) {
|
141 |
|
142 | }
|
143 | }
|
144 | }
|
145 | finally {
|
146 |
|
147 | await awaiter.Wait();
|
148 | }
|
149 | }
|
150 | try {
|
151 |
|
152 | await fs_rmdir(dirPath);
|
153 | }
|
154 | catch (e) {
|
155 |
|
156 | if (await isDirectory(dirPath)) {
|
157 |
|
158 | throw new UnableToRemoveException(dirPath);
|
159 | }
|
160 | }
|
161 | }
|
162 | exports.rmdir = rmdir;
|
163 | async function rmFile(filePath) {
|
164 |
|
165 | if (!exports.exists(filePath)) {
|
166 | return;
|
167 | }
|
168 |
|
169 | if (await isDirectory(filePath)) {
|
170 | throw new PathIsNotFileException(filePath);
|
171 | }
|
172 | try {
|
173 |
|
174 | await unlink(filePath);
|
175 | }
|
176 | catch (e) {
|
177 |
|
178 | if (await exports.exists(filePath)) {
|
179 |
|
180 | throw new UnableToRemoveException(filePath);
|
181 | }
|
182 | }
|
183 | }
|
184 | exports.rmFile = rmFile;
|
185 |
|
\ | No newline at end of file |