@@ -35,8 +35,8 @@ const {spawnSync} = require('child_process');
35
35
const { sync : globSync } = require ( 'glob' ) ;
36
36
37
37
const ALL_IN_ONE_PACKAGE = 'material-components-web' ;
38
- const DECLARATION_FILE_PREFIX = 'mdc-' ;
39
38
const D_TS_DIRECTORY = path . resolve ( __dirname , '../build/packages' ) ;
39
+ const PACKAGES_DIRECTORY = path . resolve ( __dirname , '../packages' ) ;
40
40
const PKG_RE = / (?: m a t e r i a l \- c o m p o n e n t s \- w e b ) | (?: m d c \. [ a - z A - Z \- ] + ) / ;
41
41
42
42
const isValidCwd = (
@@ -76,7 +76,7 @@ function cleanPkgDistDirs() {
76
76
77
77
/**
78
78
* @param {string } asset filepath relative to the root directory
79
- * @returns {string } destination package name
79
+ * @return {string } destination package name
80
80
*/
81
81
function getAssetEntry ( asset ) {
82
82
const [ entryName ] = path . parse ( asset ) . name . match ( PKG_RE ) ;
@@ -90,7 +90,7 @@ function getAssetEntry(asset) {
90
90
91
91
/**
92
92
* @param {string } asset filepath relative to the root directory
93
- * @returns {Promise<void> }
93
+ * @return {Promise<void> }
94
94
*/
95
95
async function cpAsset ( asset ) {
96
96
const assetPkg = path . join ( 'packages' , getAssetEntry ( asset ) ) ;
@@ -107,53 +107,27 @@ async function cpAsset(asset) {
107
107
}
108
108
109
109
/**
110
- * Imports all files in index.d.ts and compiles a bundled .d.ts file for
111
- * UMD files.
110
+ * Imports all files in index.d.ts and compiles a bundled .d.ts file for UMD bundles.
112
111
*/
113
112
function dtsBundler ( ) {
114
113
const packageDirectories = fs . readdirSync ( D_TS_DIRECTORY ) ;
115
114
packageDirectories . forEach ( ( packageDirectory ) => {
116
- const main = path . resolve ( D_TS_DIRECTORY , packageDirectory , './index.d.ts' ) ;
115
+ const packagePath = path . join ( PACKAGES_DIRECTORY , packageDirectory ) ;
116
+ const name = JSON . parse ( fs . readFileSync ( path . join ( packagePath , 'package.json' ) , 'utf8' ) ) . name ;
117
+ const main = path . join ( D_TS_DIRECTORY , packageDirectory , './index.d.ts' ) ;
118
+ const isAllInOne = packageDirectory === ALL_IN_ONE_PACKAGE ;
119
+ const destBasename = isAllInOne ? packageDirectory : `mdc.${ toCamelCase ( packageDirectory . replace ( / ^ m d c - / , '' ) ) } ` ;
120
+ const destFilename = path . join ( packagePath , 'dist' , `${ destBasename } .d.ts` ) ;
121
+
122
+ console . log ( `Writing UMD declarations in ${ destFilename . replace ( process . cwd ( ) + '/' , '' ) } ` ) ;
117
123
dts . bundle ( {
118
- name : packageDirectory ,
124
+ name,
119
125
main,
126
+ out : destFilename ,
120
127
} ) ;
121
128
} ) ;
122
129
}
123
130
124
- /**
125
- * @param {string } asset filepath relative to the root directory
126
- * @param {string } assetPkg directory path to destination package
127
- * @returns {string } destination filepath for UMD declaration file.
128
- */
129
- function getDeclarationFileName ( asset , assetPkg ) {
130
- const packageName = path . parse ( asset ) . name . replace ( / ^ m d c - | \. d $ / g, '' ) ;
131
- const isAllInOne = packageName === ALL_IN_ONE_PACKAGE ;
132
- const destFileName = isAllInOne ? packageName : `mdc.${ toCamelCase ( packageName ) } ` ;
133
- return path . join ( assetPkg , 'dist' , `${ destFileName } .d.ts` ) ;
134
- }
135
-
136
- /**
137
- * Copies the declaration file from the /build directory to its respective
138
- * destination directory.
139
- * @param {string } asset filepath relative to the root directory
140
- * @returns {Promise<void> }
141
- */
142
- async function cpDeclarationAsset ( asset ) {
143
- const assetPkg = path . parse ( asset . split ( 'build/' ) [ 1 ] ) . dir ;
144
- if ( ! fs . existsSync ( assetPkg ) ) {
145
- return Promise . reject ( new Error ( `Non-existent asset package path ${ assetPkg } for ${ asset } ` ) ) ;
146
- }
147
-
148
- const destDir = getDeclarationFileName ( asset , assetPkg ) ;
149
- return cpFile ( asset , destDir ) . then (
150
- ( ) => console . log ( `cp ${ asset } -> ${ destDir } ` ) ,
151
- ( err ) => {
152
- throw err ;
153
- }
154
- ) ;
155
- }
156
-
157
131
/*
158
132
* 1. Cleans all /dist directories in each package
159
133
* 2. Copies generated css, js, and map files to the respective packages
@@ -167,13 +141,5 @@ Promise.all(globSync('build/*.{css,js,map}').map(cpAsset)).catch((err) => {
167
141
process . exit ( 1 ) ;
168
142
} ) ;
169
143
170
- // this method builds the files that the next lines copy to each package
144
+ // Build d.ts files for each UMD bundle and copy into each package's dist folder
171
145
dtsBundler ( ) ;
172
-
173
- Promise . all (
174
- globSync ( `build/packages/**/{${ DECLARATION_FILE_PREFIX } ,${ ALL_IN_ONE_PACKAGE } }*.d.ts` )
175
- . map ( cpDeclarationAsset )
176
- ) . catch ( ( err ) => {
177
- console . error ( 'Error encountered copying assets:' , err ) ;
178
- process . exit ( 1 ) ;
179
- } ) ;
0 commit comments