Skip to content

Commit 2f03e7a

Browse files
committed
test: remove rimraf and fs-extra dependencies
1 parent 2d0d82b commit 2f03e7a

File tree

3 files changed

+82
-160
lines changed

3 files changed

+82
-160
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
"@types/postcss-preset-env": "^6.7.1",
119119
"@types/progress": "^2.0.3",
120120
"@types/resolve": "^1.17.1",
121-
"@types/rimraf": "^3.0.0",
122121
"@types/sass": "^1.16.0",
123122
"@types/semver": "^7.0.0",
124123
"@types/text-table": "^0.2.1",
@@ -204,7 +203,6 @@
204203
"raw-loader": "4.0.2",
205204
"regenerator-runtime": "0.13.7",
206205
"resolve-url-loader": "4.0.0",
207-
"rimraf": "3.0.2",
208206
"rxjs": "6.6.7",
209207
"sass": "1.34.1",
210208
"sass-loader": "12.0.0",

tests/legacy-cli/e2e/utils/fs.ts

Lines changed: 78 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,127 @@
1-
import * as fs from 'fs-extra';
2-
import {dirname} from 'path';
3-
import {stripIndents} from 'common-tags';
4-
5-
6-
export function readFile(fileName: string) {
7-
return new Promise<string>((resolve, reject) => {
8-
fs.readFile(fileName, 'utf-8', (err: any, data: string) => {
9-
if (err) {
10-
reject(err);
11-
} else {
12-
resolve(data);
13-
}
14-
});
15-
});
16-
}
1+
import { promises as fs, constants } from 'fs';
2+
import { dirname, join } from 'path';
3+
import { stripIndents } from 'common-tags';
174

18-
export function writeFile(fileName: string, content: string, options?: any) {
19-
return new Promise<void>((resolve, reject) => {
20-
fs.writeFile(fileName, content, options, (err: any) => {
21-
if (err) {
22-
reject(err);
23-
} else {
24-
resolve();
25-
}
26-
});
27-
});
5+
export function readFile(fileName: string): Promise<string> {
6+
return fs.readFile(fileName, 'utf-8');
287
}
298

30-
31-
export function deleteFile(path: string) {
32-
return new Promise<void>((resolve, reject) => {
33-
fs.unlink(path, (err) => {
34-
if (err) {
35-
reject(err);
36-
} else {
37-
resolve();
38-
}
39-
});
40-
});
9+
export function writeFile(fileName: string, content: string, options?: any): Promise<void> {
10+
return fs.writeFile(fileName, content, options);
4111
}
4212

43-
44-
export function rimraf(path: string) {
45-
return new Promise<void>((resolve, reject) => {
46-
fs.remove(path, (err?: any) => {
47-
if (err) {
48-
reject(err);
49-
} else {
50-
resolve();
51-
}
52-
});
53-
});
13+
export function deleteFile(path: string): Promise<void> {
14+
return fs.unlink(path);
5415
}
5516

56-
57-
export function moveFile(from: string, to: string) {
58-
return new Promise<void>((resolve, reject) => {
59-
fs.rename(from, to, (err) => {
60-
if (err) {
61-
reject(err);
62-
} else {
63-
resolve();
64-
}
65-
});
66-
});
17+
export function rimraf(path: string): Promise<void> {
18+
return fs.rmdir(path, { recursive: true, maxRetries: 3 });
6719
}
6820

69-
70-
export function symlinkFile(from: string, to: string, type?: string) {
71-
return new Promise<void>((resolve, reject) => {
72-
fs.symlink(from, to, type, (err) => {
73-
if (err) {
74-
reject(err);
75-
} else {
76-
resolve();
77-
}
78-
});
79-
});
21+
export function moveFile(from: string, to: string): Promise<void> {
22+
return fs.rename(from, to);
8023
}
8124

82-
export function createDir(path: string) {
83-
return _recursiveMkDir(path);
25+
export function symlinkFile(from: string, to: string, type?: string): Promise<void> {
26+
return fs.symlink(from, to, type);
8427
}
8528

86-
87-
function _recursiveMkDir(path: string): Promise<void> {
88-
if (fs.existsSync(path)) {
89-
return Promise.resolve();
90-
} else {
91-
return _recursiveMkDir(dirname(path))
92-
.then(() => fs.mkdirSync(path));
93-
}
29+
export function createDir(path: string): Promise<void> {
30+
return fs.mkdir(path, { recursive: true });
9431
}
9532

96-
export function copyFile(from: string, to: string) {
97-
return _recursiveMkDir(dirname(to))
98-
.then(() => new Promise((resolve, reject) => {
99-
const rd = fs.createReadStream(from);
100-
rd.on('error', (err: Error) => reject(err));
33+
export async function copyFile(from: string, to: string): Promise<void> {
34+
await createDir(dirname(to));
10135

102-
const wr = fs.createWriteStream(to);
103-
wr.on('error', (err: Error) => reject(err));
104-
wr.on('close', () => resolve());
105-
106-
rd.pipe(wr);
107-
}));
36+
return fs.copyFile(from, to, constants.COPYFILE_FICLONE);
10837
}
10938

110-
export function moveDirectory(from: string, to: string) {
111-
return fs.move(from, to, { overwrite: true });
112-
}
39+
export async function moveDirectory(from: string, to: string): Promise<void> {
40+
await rimraf(to);
41+
await createDir(to);
11342

43+
for (const entry of await fs.readdir(from)) {
44+
const fromEntry = join(from, entry);
45+
const toEntry = join(to, entry);
46+
if ((await fs.stat(fromEntry)).isFile()) {
47+
await copyFile(fromEntry, toEntry);
48+
} else {
49+
await moveDirectory(fromEntry, toEntry);
50+
}
51+
}
52+
}
11453

11554
export function writeMultipleFiles(fs: { [path: string]: string }) {
116-
return Promise.all(Object.keys(fs).map(fileName => writeFile(fileName, fs[fileName])));
55+
return Promise.all(Object.keys(fs).map((fileName) => writeFile(fileName, fs[fileName])));
11756
}
11857

119-
12058
export function replaceInFile(filePath: string, match: RegExp | string, replacement: string) {
121-
return readFile(filePath)
122-
.then((content: string) => writeFile(filePath, content.replace(match, replacement)));
59+
return readFile(filePath).then((content: string) =>
60+
writeFile(filePath, content.replace(match, replacement)),
61+
);
12362
}
12463

125-
12664
export function appendToFile(filePath: string, text: string, options?: any) {
127-
return readFile(filePath)
128-
.then((content: string) => writeFile(filePath, content.concat(text), options));
65+
return readFile(filePath).then((content: string) =>
66+
writeFile(filePath, content.concat(text), options),
67+
);
12968
}
13069

131-
13270
export function prependToFile(filePath: string, text: string, options?: any) {
133-
return readFile(filePath)
134-
.then((content: string) => writeFile(filePath, text.concat(content), options));
71+
return readFile(filePath).then((content: string) =>
72+
writeFile(filePath, text.concat(content), options),
73+
);
13574
}
13675

76+
export async function expectFileMatchToExist(dir: string, regex: RegExp): Promise<string> {
77+
const files = await fs.readdir(dir);
78+
const fileName = files.find((name) => regex.test(name));
13779

138-
export function expectFileMatchToExist(dir: string, regex: RegExp) {
139-
return new Promise((resolve, reject) => {
140-
const [fileName] = fs.readdirSync(dir).filter(name => name.match(regex));
141-
if (!fileName) {
142-
reject(new Error(`File ${regex} was expected to exist but not found...`));
143-
}
144-
resolve(fileName);
145-
});
80+
if (!fileName) {
81+
throw new Error(`File ${regex} was expected to exist but not found...`);
82+
}
83+
84+
return fileName;
14685
}
14786

148-
export function expectFileNotToExist(fileName: string) {
149-
return new Promise((resolve, reject) => {
150-
fs.exists(fileName, (exist) => {
151-
if (exist) {
152-
reject(new Error(`File ${fileName} was expected not to exist but found...`));
153-
} else {
154-
resolve();
155-
}
156-
});
157-
});
87+
export async function expectFileNotToExist(fileName: string): Promise<void> {
88+
try {
89+
await fs.access(fileName, constants.F_OK);
90+
} catch {
91+
return;
92+
}
93+
94+
throw new Error(`File ${fileName} was expected not to exist but found...`);
15895
}
15996

160-
export function expectFileToExist(fileName: string) {
161-
return new Promise((resolve, reject) => {
162-
fs.exists(fileName, (exist) => {
163-
if (exist) {
164-
resolve();
165-
} else {
166-
reject(new Error(`File ${fileName} was expected to exist but not found...`));
167-
}
168-
});
169-
});
97+
export async function expectFileToExist(fileName: string): Promise<void> {
98+
try {
99+
await fs.access(fileName, constants.F_OK);
100+
} catch {
101+
throw new Error(`File ${fileName} was expected to exist but not found...`);
102+
}
170103
}
171104

172105
export function expectFileToMatch(fileName: string, regEx: RegExp | string) {
173-
return readFile(fileName)
174-
.then(content => {
175-
if (typeof regEx == 'string') {
176-
if (content.indexOf(regEx) == -1) {
177-
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
106+
return readFile(fileName).then((content) => {
107+
if (typeof regEx == 'string') {
108+
if (content.indexOf(regEx) == -1) {
109+
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
178110
Content:
179111
${content}
180112
------
181113
`);
182-
}
183-
} else {
184-
if (!content.match(regEx)) {
185-
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
114+
}
115+
} else {
116+
if (!content.match(regEx)) {
117+
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
186118
Content:
187119
${content}
188120
------
189121
`);
190-
}
191122
}
192-
});
123+
}
124+
});
193125
}
194126

195127
export async function getFileSize(fileName: string) {
@@ -202,6 +134,8 @@ export async function expectFileSizeToBeUnder(fileName: string, sizeInBytes: num
202134
const fileSize = await getFileSize(fileName);
203135

204136
if (fileSize > sizeInBytes) {
205-
throw new Error(`File "${fileName}" exceeded file size of "${sizeInBytes}". Size is ${fileSize}.`);
137+
throw new Error(
138+
`File "${fileName}" exceeded file size of "${sizeInBytes}". Size is ${fileSize}.`,
139+
);
206140
}
207141
}

yarn.lock

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@
8686

8787
"@angular/dev-infra-private@https://github.com/angular/dev-infra-private-builds.git#7308e71916da94b51a9b558693c49a04bfc98beb":
8888
version "0.0.0"
89-
uid "7308e71916da94b51a9b558693c49a04bfc98beb"
90-
resolved "https://github.com/angular/dev-infra-private-builds.git#7308e71916da94b51a9b558693c49a04bfc98beb"
89+
resolved "https://github.com/angular/dev-infra-private-builds.git#ccf49db26d09093bdb3882282d99f3448cf68cba"
9190
dependencies:
9291
"@angular/benchpress" "0.2.1"
9392
"@bazel/buildifier" "^4.0.1"
@@ -113,9 +112,9 @@
113112
semver "^7.3.5"
114113
shelljs "^0.8.4"
115114
ts-node "^10.0.0"
116-
tslib "^2.2.0"
115+
tslib "^2.1.0"
117116
typed-graphqlify "^3.1.1"
118-
typescript "~4.3.2"
117+
typescript "~4.2.4"
119118
yaml "^1.10.0"
120119
yargs "^17.0.0"
121120

@@ -1670,7 +1669,7 @@
16701669
resolved "https://registry.yarnpkg.com/@types/find-cache-dir/-/find-cache-dir-3.2.0.tgz#eaaf331699dccf52c47926e4d4f8f3ed8db33f3c"
16711670
integrity sha512-+JeT9qb2Jwzw72WdjU+TSvD5O1QRPWCeRpDJV+guiIq+2hwR0DFGw+nZNbTFjMIVe6Bf4GgAKeB/6Ytx6+MbeQ==
16721671

1673-
"@types/glob@*", "@types/glob@^7.1.1":
1672+
"@types/glob@^7.1.1":
16741673
version "7.1.3"
16751674
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
16761675
integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==
@@ -1878,14 +1877,6 @@
18781877
dependencies:
18791878
"@types/node" "*"
18801879

1881-
"@types/rimraf@^3.0.0":
1882-
version "3.0.0"
1883-
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-3.0.0.tgz#b9d03f090ece263671898d57bb7bb007023ac19f"
1884-
integrity sha512-7WhJ0MdpFgYQPXlF4Dx+DhgvlPCfz/x5mHaeDQAKhcenvQP1KCpLQ18JklAqeGMYSAT2PxLpzd0g2/HE7fj7hQ==
1885-
dependencies:
1886-
"@types/glob" "*"
1887-
"@types/node" "*"
1888-
18891880
"@types/sass@^1.16.0":
18901881
version "1.16.0"
18911882
resolved "https://registry.yarnpkg.com/@types/sass/-/sass-1.16.0.tgz#b41ac1c17fa68ffb57d43e2360486ef526b3d57d"
@@ -10213,7 +10204,6 @@ [email protected], sass@^1.32.8:
1021310204

1021410205
"sauce-connect-proxy@https://saucelabs.com/downloads/sc-4.6.4-linux.tar.gz":
1021510206
version "0.0.0"
10216-
uid "992e2cb0d91e54b27a4f5bbd2049f3b774718115"
1021710207
resolved "https://saucelabs.com/downloads/sc-4.6.4-linux.tar.gz#992e2cb0d91e54b27a4f5bbd2049f3b774718115"
1021810208

1021910209
saucelabs@^1.5.0:

0 commit comments

Comments
 (0)