-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathcopyFilesUtils.mjs
63 lines (52 loc) · 1.99 KB
/
copyFilesUtils.mjs
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
/* eslint-disable no-console */
import path from 'path';
import fse from 'fs-extra';
import glob from 'fast-glob';
const packagePath = process.cwd();
const buildPath = path.join(packagePath, './build');
export async function includeFileInBuild(file) {
const sourcePath = path.resolve(packagePath, file);
const targetPath = path.resolve(buildPath, path.basename(file));
await fse.copy(sourcePath, targetPath);
console.log(`Copied ${sourcePath} to ${targetPath}`);
}
export async function typescriptCopy({ from, to }) {
if (!(await fse.pathExists(to))) {
console.warn(`path ${to} does not exists`);
return [];
}
const files = await glob('**/*.d.ts', { cwd: from });
const cmds = files.map((file) => fse.copy(path.resolve(from, file), path.resolve(to, file)));
return Promise.all(cmds);
}
export async function createPackageFile() {
const packageData = await fse.readFile(path.resolve(packagePath, './package.json'), 'utf8');
const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } =
JSON.parse(packageData);
const newPackageData = {
...packageDataOther,
private: false,
...(packageDataOther.main
? {
main: fse.existsSync(path.resolve(buildPath, './node/index.js'))
? './node/index.js'
: './index.js',
module: fse.existsSync(path.resolve(buildPath, './esm/index.js'))
? './esm/index.js'
: './index.js',
}
: {}),
};
const typeDefinitionsFilePath = path.resolve(buildPath, './index.d.ts');
if (await fse.pathExists(typeDefinitionsFilePath)) {
newPackageData.types = './index.d.ts';
}
const targetPath = path.resolve(buildPath, './package.json');
await fse.writeFile(targetPath, JSON.stringify(newPackageData, null, 2), 'utf8');
console.log(`Created package.json in ${targetPath}`);
return newPackageData;
}
export async function prepend(file, string) {
const data = await fse.readFile(file, 'utf8');
await fse.writeFile(file, string + data, 'utf8');
}