forked from material-components/material-components-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp-pkgs.js
155 lines (139 loc) · 5.32 KB
/
cp-pkgs.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
/**
* @license
* Copyright 2016 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @fileoverview Copies built assets from our build directory to each of their respective package's
* dist/ folder.
*/
const cpFile = require('cp-file');
const dts = require('dts-bundle');
const fs = require('fs');
const path = require('path');
const toSlugCase = require('to-slug-case');
const {spawnSync} = require('child_process');
const {sync: globSync} = require('glob');
const ALL_IN_ONE_PACKAGE = 'material-components-web';
const PACKAGES_DIRECTORY = path.resolve(__dirname, '../packages');
const PKG_RE = /(?:material\-components\-web)|(?:mdc\.[a-zA-Z\-]+)/;
const isValidCwd = (
path.basename(process.cwd()) === ALL_IN_ONE_PACKAGE &&
fs.existsSync('packages') &&
fs.existsSync('build')
);
if (!isValidCwd) {
console.error(
'Invalid CWD. Please ensure you are running this from the root of the repo, and that you have run `npm run dist`',
);
process.exit(1);
}
/**
* @param {string} dashedName A dash-separated package name. E.g., "mdc-linear-progress".
* @return {string} dashedName converted to camelCase. E.g., "mdcLinearProgress".
*/
function toCamelCase(dashedName) {
return dashedName.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
}
/**
* Cleans the /dist directory of each package.
*/
function cleanPkgDistDirs() {
const statuses = globSync('packages/*/dist').map(
(distPath) => spawnSync('rm', ['-r', distPath], {stdio: 'inherit'}).status);
if (statuses.find((status) => status > 0)) {
console.error('Failed to clean package dist folders prior to copying');
process.exit(1);
}
}
/**
* @param {string} asset filepath relative to the root directory
* @return {string} destination package name
*/
function getAssetEntry(asset) {
const [entryName] = path.parse(asset).name.match(PKG_RE);
const [MDC, componentName] = entryName.split('.');
const dealingWithSubpackage = Boolean(MDC && componentName);
if (!dealingWithSubpackage) {
return entryName;
}
return [MDC, toSlugCase(componentName)].join('-');
}
/**
* @param {string} asset filepath relative to the root directory
* @return {Promise<void>}
*/
async function cpAsset(asset) {
const assetPkg = path.join('packages', getAssetEntry(asset));
if (!fs.existsSync(assetPkg)) {
return Promise.reject(new Error(`Non-existent asset package path ${assetPkg} for ${asset}`));
}
const destDir = path.join(assetPkg, 'dist', path.basename(asset));
return cpFile(asset, destDir).then(
() => console.log(`cp ${asset} -> ${destDir}`),
(err) => {
throw err;
},
);
}
/**
* Imports all files in index.d.ts and compiles a bundled .d.ts file for UMD bundles.
*/
function dtsBundler() {
const packageDirectories = fs.readdirSync(PACKAGES_DIRECTORY);
packageDirectories.forEach((packageDirectory) => {
const main = path.join(PACKAGES_DIRECTORY, packageDirectory, './index.d.ts');
fs.access(main, fs.constants.F_OK, (error) => {
if (error) {
return;
}
const isAllInOne = packageDirectory === ALL_IN_ONE_PACKAGE;
if (!isAllInOne) {
// Only the all-in-one package should generate a d.ts bundle
return;
}
// d.ts file exists
const packagePath = path.join(PACKAGES_DIRECTORY, packageDirectory);
const name = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json'), 'utf8')).name;
const destBasename
= isAllInOne ? packageDirectory : `mdc.${toCamelCase(packageDirectory.replace(/^mdc-/, ''))}`;
const destFilename = path.join(packagePath, 'dist', `${destBasename}.d.ts`);
console.log(`Writing UMD declarations in ${destFilename.replace(process.cwd() + '/', '')}`);
dts.bundle({
name,
main,
out: destFilename,
});
});
});
}
/*
* 1. Cleans all /dist directories in each package
* 2. Copies generated css, js, and map files to the respective packages
* 3. Bundles the declaration files into one file for the UMD index.js file
* 4. Copies the generated declaration file from step 3, into the respective pacakge
*/
cleanPkgDistDirs();
Promise.all(globSync('build/*.{css,js,map}').map(cpAsset)).catch((err) => {
console.error('Error encountered copying assets:', err);
process.exit(1);
});
// Build d.ts files for each UMD bundle and copy into each package's dist folder
dtsBundler();