-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcopy-files.ts
84 lines (70 loc) · 2.43 KB
/
copy-files.ts
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
import fs from 'fs';
import url from 'url';
import path from 'path';
import packageData from './package.json' assert { type: 'json' };
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const distPath = path.join(__dirname, './dist');
async function copyFileToBuild(filePath: string) {
const destination = path.resolve(distPath, filePath);
await fs.promises.mkdir(path.dirname(destination), { recursive: true });
return fs.promises.copyFile(path.resolve(__dirname, filePath), destination);
}
async function prependBanner(file: fs.PathLike, string: string) {
const data = await fs.promises.readFile(file, 'utf8');
// If string was not yet prepended
if (!data.startsWith(string)) {
await fs.promises.writeFile(file, `${string}\n${data}`, 'utf8');
}
}
async function addLicense() {
const license = `/** @license MUI Chameleon v${packageData.version}
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*/`;
await Promise.all(
[
'./index.js',
'./SideBarEditor/index.js',
'./ThemeProvider/index.js',
'./es/index.js',
'./es/SideBarEditor/index.js',
'./es/ThemeProvider/index.js',
'./esm/index.js',
'./esm/SideBarEditor/index.js',
'./esm/ThemeProvider/index.js',
].map(async (file) => {
try {
await prependBanner(path.resolve(distPath, file), license);
} catch (err: any) {
if (err.code === 'ENOENT') {
console.log(`Skipped license for ${file}`);
} else {
throw err;
}
}
})
);
}
async function run() {
try {
await copyFileToBuild('LICENSE.md');
await copyFileToBuild('README.md');
await addLicense();
delete packageData.private;
delete packageData.scripts;
delete packageData.devDependencies;
delete packageData.engines;
const newPackageData = {
...packageData,
module: './esm/index.js',
main: './index.js',
types: './index.d.ts',
};
await fs.promises.writeFile(path.resolve(distPath, 'package.json'), JSON.stringify(newPackageData, null, 4));
} catch (err) {
console.error(err);
process.exit(1);
}
}
run();