-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.js
More file actions
120 lines (106 loc) · 3.22 KB
/
Copy pathfiles.js
File metadata and controls
120 lines (106 loc) · 3.22 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const path = require("path");
const fs = require("fs");
const COPY_FILES = /^(?:CHANGELOG|changelog|README|readme|LICEN[SC]E|licen[sc]e)(?:\..*)?$/;
const CWD = process.cwd();
let destination = path.resolve(CWD, "dist");
function getFilepath(p) {
if (!path.isAbsolute(p)) {
p = path.resolve(CWD, p);
}
return new Promise((resolve, reject) => {
fs.stat(p, (err, stats) => {
if (err) return reject(err);
if (stats.isFile()) return resolve(path.relative(CWD, p));
reject(Error(`UnknownError: "${path.relative(CWD, p)}" is not a file.`));
});
});
}
/** @param {string} p path to package.json */
function readPackage(p) {
if (!path.isAbsolute(p)) p = path.resolve(CWD, p);
return new Promise((resolve, reject) => {
if (!fs.statSync(p).isFile()) {
return reject(
Error(`UnknownError: "${path.relative(CWD, p)}" is not a file`)
);
}
fs.readFile(p, (err, buff) => {
if (err) return reject(err);
resolve(buff.toString());
});
});
}
function writePackage(data) {
const filename = path.resolve(destination, "package.json");
return new Promise((resolve, reject) => {
fs.writeFile(filename, data, err => {
if (err) return reject(err);
resolve();
});
}).then(() => validateFilePointers(data));
}
function validateFilePointers(data) {
const pkg = JSON.parse(data);
const exists = rel =>
new Promise((resolve, reject) => {
fs.stat(path.resolve(destination, rel), (err, stats) => {
if (err) return reject(err);
if (stats.isFile()) return resolve("OK");
return reject("ERR");
});
});
return Promise.all([
typeof pkg["main"] == "string"
? exists(pkg["main"])
: Promise.resolve('No "main" key'),
typeof pkg["module"] == "string"
? exists(pkg["module"])
: Promise.resolve('No "module" key'),
typeof pkg["bin"] == "object"
? Promise.all(
Object.values(pkg["bin"]).map(file_path => exists(file_path))
)
: Promise.resolve('No "bin" key')
]);
}
/** @param {string} src path to CHANGELOG.md, README.md, LICENSE */
function copyFilesFrom(src) {
return new Promise((resolve, reject) => {
src = path.isAbsolute(src) ? src : path.resolve(CWD, src);
const directory = fs.statSync(src).isDirectory() ? src : path.dirname(src);
const src_files = fs
.readdirSync(directory)
.filter(value => COPY_FILES.test(value));
return Promise.all(
src_files.map(
src_file =>
new Promise((resolve, reject) => {
const filename = path.basename(src_file);
fs.copyFile(src_file, path.resolve(destination, filename), err => {
if (err) return reject(err);
resolve();
});
})
)
).then(resolve, reject);
});
}
module.exports = directoryOfBuild => {
if (!path.isAbsolute(directoryOfBuild)) {
directoryOfBuild = path.resolve(CWD, directoryOfBuild);
}
const stats = fs.statSync(directoryOfBuild);
if (stats.isDirectory()) {
destination = directoryOfBuild;
} else {
throw Error(
`TargetDirectoryError: "${directoryOfBuild}" is not a directory`
);
}
return {
getFilepath,
readPackage,
writePackage,
copyFilesFrom
};
};