-
Notifications
You must be signed in to change notification settings - Fork 15
/
generate-module.js
executable file
·168 lines (148 loc) · 6.24 KB
/
generate-module.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
#!/usr/bin/env node
const fse = require('fs-extra')
const path = require('path')
const babel = require('@babel/core')
const pick = require('lodash.pick')
const filenameMap = require('./filenameMap')
function checkNameClashes (icons) {
const caseNameClashes = icons.filter(
(icon) =>
icon.filename == null &&
icons.filter(
(icon2) =>
icon2.filename == null &&
icon2.name.toLowerCase() === icon.name.toLowerCase()
).length > 1
)
if (caseNameClashes.length > 0) {
throw new Error(
`The following icons have the same file name (case insensitive): ${caseNameClashes
.map((icon) => icon.name)
.join(', ')}`
)
}
}
(async () => {
const icons = Object.entries(require('@mdi/js'))
.filter(([name]) => name.indexOf('mdi') === 0)
.map(([name, path]) => ({
name: name.substr(3), // remove mdi prefix
filename: filenameMap[name.substr(3)],
svgPath: path
}))
checkNameClashes(icons)
const lightIcons = Object.entries(require('@mdi/light-js'))
.filter(([name]) => name.indexOf('mdil') === 0)
.map(([name, path]) => ({
name: name.substr(4), // remove mdil prefix
svgPath: path
}))
checkNameClashes(lightIcons)
fse.removeSync(path.join(__dirname, 'package'))
fse.mkdirpSync(path.join(__dirname, 'package', 'light'))
fse.removeSync(path.join(__dirname, 'package', 'esm'))
fse.mkdirpSync(path.join(__dirname, 'package', 'esm', 'light'))
for (const { name, filename, svgPath } of icons) {
const code = `
"use client";
import createIcon from './util/createIcon'
export default createIcon('${svgPath}', '${name}')
`
// es module
fse.writeFileSync(path.join(__dirname, 'package', 'esm', `${filename || name}.js`), babel.transform(code, {
presets: ['@babel/preset-react', ['@babel/preset-env', { modules: false }]],
compact: process.env.NODE_ENV === 'production'
}).code)
// commonjs module
fse.writeFileSync(path.join(__dirname, 'package', `${filename || name}.js`), babel.transform(code, {
presets: ['@babel/preset-react', '@babel/preset-env'],
compact: process.env.NODE_ENV === 'production'
}).code)
// typescript definition
fse.writeFileSync(path.join(__dirname, 'package', `${filename || name}.d.ts`), `export { default } from '@mui/material/SvgIcon'
`)
}
for (const { name, filename, svgPath } of lightIcons) {
const code = `
"use client";
import createIcon from '../util/createIcon'
export default createIcon('${svgPath}', '${name}')
`
// es module
fse.writeFileSync(path.join(__dirname, 'package', 'esm', 'light', `${filename || name}.js`), babel.transform(code, {
presets: ['@babel/preset-react', ['@babel/preset-env', { modules: false }]],
compact: process.env.NODE_ENV === 'production'
}).code)
// commonjs module
fse.writeFileSync(path.join(__dirname, 'package', 'light', `${filename || name}.js`), babel.transform(code, {
presets: ['@babel/preset-react', '@babel/preset-env'],
compact: process.env.NODE_ENV === 'production'
}).code)
// typescript definition
fse.writeFileSync(path.join(__dirname, 'package', 'light', `${filename || name}.d.ts`), `export { default } from '@mui/material/SvgIcon'
`)
}
const generateIndexFiles = (destination, esmDestination, icons) => {
// es module
const allExports = icons.map(({ name, filename }) => `export { default as ${name} } from './${filename || name}'`).join('\n')
fse.writeFileSync(path.join(esmDestination, 'index.js'), allExports)
// typescript index definition (looks exactly the same)
fse.writeFileSync(path.join(destination, 'index.d.ts'), allExports)
// commonjs module
fse.writeFileSync(path.join(destination, 'index.js'), babel.transform(allExports, {
plugins: ['@babel/plugin-transform-modules-commonjs'],
compact: process.env.NODE_ENV === 'production'
}).code)
}
generateIndexFiles(path.join(__dirname, 'package'), path.join(__dirname, 'package', 'esm'), icons)
generateIndexFiles(path.join(__dirname, 'package', 'light'), path.join(__dirname, 'package', 'esm', 'light'), lightIcons)
// createIcon function
fse.mkdirSync(path.join(__dirname, 'package', 'util'))
fse.mkdirSync(path.join(__dirname, 'package', 'esm', 'util'))
fse.writeFileSync(
path.join(__dirname, 'package', 'util', 'createIcon.js'),
babel.transform(fse.readFileSync(path.join(__dirname, 'src', 'util', 'createIcon.js')), {
presets: [['@babel/preset-react', { runtime: "automatic" }], '@babel/preset-env'],
compact: process.env.NODE_ENV === 'production'
}).code
)
fse.writeFileSync(
path.join(__dirname, 'package', 'esm', 'util', 'createIcon.js'),
babel.transform(fse.readFileSync(path.join(__dirname, 'src', 'util', 'createIcon.js')), {
presets: [['@babel/preset-react', { runtime: "automatic" }], ['@babel/preset-env', { modules: false }]],
compact: process.env.NODE_ENV === 'production'
}).code
)
// update readme
const mdiVersion = require(path.join(require.resolve('@mdi/js'), '..', '..', 'package.json')).version
const mdiLightVersion = require(path.join(require.resolve('@mdi/light-js'), '..', '..', 'package.json')).version
let readme = await fse.readFile(path.join(__dirname, 'README.md'), 'utf-8')
readme = readme.replace(/img\.shields\.io\/badge\/mdi-v(.+?)-blue\.svg/g, `img.shields.io/badge/mdi-v${mdiVersion}-blue.svg`)
readme = readme.replace(/img\.shields\.io\/badge\/mdi--light-v(.+?)-blue\.svg/g, `img.shields.io/badge/mdi--light-v${mdiLightVersion}-blue.svg`)
await fse.writeFile(path.join(__dirname, 'README.md'), readme, 'utf-8')
// copy other files
;[
'README.md',
'NOTICE.txt',
'LICENSE.txt',
'.npmignore'
].forEach((file) => fse.copySync(path.join(__dirname, file), path.join(__dirname, 'package', file)))
const packageJson = require('./package.json')
packageJson.name = 'mdi-material-ui'
fse.writeFileSync(path.join(__dirname, 'package', 'package.json'), JSON.stringify(pick(packageJson, [
'name',
'version',
'description',
'main',
'module',
'jsnext:main',
'sideEffects',
'repository',
'keywords',
'author',
'license',
'bugs',
'homepage',
'peerDependencies'
]), null, 2), 'utf-8')
})()