forked from Theshedman/password-validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-dist.writer.js
49 lines (41 loc) · 1.01 KB
/
package-dist.writer.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
/* eslint-disable @typescript-eslint/explicit-function-return-type */
const fsPromise = import("fs");
class PackageDistWriter {
toCjsDirectory() {
fsPromise
.then(fs => this.writePackageJsonToDist(fs, "commonjs", ""))
.catch(error => {
console.log(`Error importing fs module: ${error}`);
});
return this;
}
toEsmDirectory() {
fsPromise
.then(fs => this.writePackageJsonToDist(fs, "module", ""))
.catch(error => {
console.log(`Error importing fs module: ${error}`);
});
return this;
}
writePackageJsonToDist(fs, type) {
const directory = type === "module" ? "esm" : "cjs";
fs.writeFile(
`./dist/${directory}/package.json`,
`
{
"type": "${type}"
}
`,
"utf8",
err => {
this.handleFileError(err);
},
);
}
handleFileError(err) {
if (err) {
throw new Error(`Error reading file from disk: ${err}`);
}
}
}
new PackageDistWriter().toCjsDirectory().toEsmDirectory();