-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathcopy-files.js
141 lines (121 loc) · 4.09 KB
/
copy-files.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
/* eslint-disable no-console */
const path = require('path');
const fse = require('fs-extra');
const glob = require('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 '@material-ui/core/Typography' are tree-shakeable.
*
* It also tests that an this import can be used in TypeScript by checking
* if an index.d.ts is present at that path.
*
* @param {string} rootDir
*/
async function createModulePackages({ from, to }) {
const directoryPackages = glob.sync('*/index.js', { cwd: from }).map(path.dirname);
await Promise.all(
directoryPackages.map(async (directoryPackage) => {
const packageJson = {
sideEffects: false,
module: path.join('../esm', directoryPackage, 'index.js'),
typings: './index.d.ts',
};
const packageJsonPath = path.join(to, directoryPackage, 'package.json');
const [typingsExist] = await Promise.all([
fse.exists(path.join(to, directoryPackage, 'index.d.ts')),
fse.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)),
]);
if (!typingsExist) {
throw new Error(`index.d.ts for ${directoryPackage} is missing`);
}
return packageJsonPath;
}),
);
}
async function typescriptCopy({ from, to }) {
if (!(await fse.exists(to))) {
console.warn(`path ${to} does not exists`);
return [];
}
const files = glob.sync('**/*.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,
main: './index.js',
module: './esm/index.js',
typings: './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 Material-UI 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',
'./esm/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();
await Promise.all(
[
// use enhanced readme from workspace root for `@material-ui/core`
packageData.name === '@material-ui/core' ? '../../README.md' : './README.md',
'../../CHANGELOG.md',
'../../LICENSE',
].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();