forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyFiles.js
182 lines (155 loc) · 5.72 KB
/
copyFiles.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// TODO: Unify with core
/* eslint-disable no-console */
const path = require('path');
const fse = require('fs-extra');
const glob = require('fast-glob');
const packagePath = process.cwd();
const buildPath = path.join(packagePath, './build');
const srcPath = path.join(packagePath, './src');
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}`);
}
/**
* Puts a package.json into every immediate child directory of rootDir.
* That package.json contains information about esm for bundlers so that imports
* like import Typography from '@mui/material/Typography' are tree-shakeable.
*
* It also tests that this import can be used in TypeScript by checking
* if an index.d.ts is present at that path.
* @param {object} param0
* @param {string} param0.from
* @param {string} param0.to
*/
async function createModulePackages({ from, to }) {
const directoryPackages = glob.sync('*/index.{js,ts,tsx}', { cwd: from }).map(path.dirname);
await Promise.all(
directoryPackages.map(async (directoryPackage) => {
const packageJsonPath = path.join(to, directoryPackage, 'package.json');
const topLevelPathImportsAreCommonJSModules = await fse.pathExists(
path.resolve(path.dirname(packageJsonPath), '../esm'),
);
const packageJson = {
sideEffects: false,
module: topLevelPathImportsAreCommonJSModules
? path.posix.join('../esm', directoryPackage, 'index.js')
: './index.js',
main: topLevelPathImportsAreCommonJSModules
? './index.js'
: path.posix.join('../node', directoryPackage, 'index.js'),
types: './index.d.ts',
};
const [typingsEntryExist, moduleEntryExists, mainEntryExists] = await Promise.all([
fse.pathExists(path.resolve(path.dirname(packageJsonPath), packageJson.types)),
fse.pathExists(path.resolve(path.dirname(packageJsonPath), packageJson.module)),
fse.pathExists(path.resolve(path.dirname(packageJsonPath), packageJson.main)),
fse.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)),
]);
const manifestErrorMessages = [];
if (!typingsEntryExist) {
manifestErrorMessages.push(`'types' entry '${packageJson.types}' does not exist`);
}
if (!moduleEntryExists) {
manifestErrorMessages.push(`'module' entry '${packageJson.module}' does not exist`);
}
if (!mainEntryExists) {
manifestErrorMessages.push(`'main' entry '${packageJson.main}' does not exist`);
}
if (manifestErrorMessages.length > 0) {
// TODO: AggregateError
throw new Error(`${packageJsonPath}:\n${manifestErrorMessages.join('\n')}`);
}
return packageJsonPath;
}),
);
}
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);
}
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',
}
: {}),
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;
}
async function prepend(file, string) {
const data = await fse.readFile(file, 'utf8');
await fse.writeFile(file, string + data, 'utf8');
}
async function addLicense(packageData) {
const license = `/** @license MUI v${packageData.version}
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
`;
await Promise.all(
[
'./index.js',
'./legacy/index.js',
'./modern/index.js',
'./node/index.js',
'./umd/material-ui.development.js',
'./umd/material-ui.production.min.js',
].map(async (file) => {
try {
await prepend(path.resolve(buildPath, file), license);
} catch (err) {
if (err.code === 'ENOENT') {
console.log(`Skipped license for ${file}`);
} else {
throw err;
}
}
}),
);
}
async function run() {
try {
const packageData = await createPackageFile();
const filesToCopy = [
'./README.md',
// TODO: Remove `grid` folder to flatten the structure
packageData.name.includes('grid') ? '../../../CHANGELOG.md' : '../../CHANGELOG.md',
];
const hasLicenseFileInPackage = await fse.exists(path.join(packagePath, 'LICENSE'));
if (hasLicenseFileInPackage) {
filesToCopy.push('./LICENSE');
}
await Promise.all(filesToCopy.map((file) => includeFileInBuild(file)));
await addLicense(packageData);
// TypeScript
await typescriptCopy({ from: srcPath, to: buildPath });
await createModulePackages({ from: srcPath, to: buildPath });
} catch (err) {
console.error(err);
process.exit(1);
}
}
run();