forked from TeamWertarbyte/mdi-material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-module.js
executable file
·90 lines (78 loc) · 3.03 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
#!/usr/bin/env node
const fse = require('fs-extra')
const path = require('path')
const pascalCase = require('pascal-case')
const babel = require('babel-core')
const pick = require('lodash.pick')
;(async () => {
const icons = Object.entries(require('@mdi/js'))
.filter(([name]) => name.indexOf('mdi') === 0)
.map(([name, path]) => ({
name: pascalCase(name.substr(3)), // remove mdi prefix
svgPath: path
}))
fse.removeSync(path.join(__dirname, 'package'))
fse.mkdirpSync(path.join(__dirname, 'package'))
for (const { name, svgPath } of icons) {
const code = `import createIcon from './util/createIcon'
export default createIcon('${svgPath}')
`
// commonjs module syntax
fse.writeFileSync(path.join(__dirname, 'package', `${name}.js`), babel.transform(code, {
presets: ['es2015', 'react', 'stage-0'],
compact: process.env.NODE_ENV === 'production'
}).code)
// typescript definition
fse.writeFileSync(path.join(__dirname, 'package', `${name}.d.ts`), `export { default } from '@material-ui/core/SvgIcon'
`)
}
// es2015 module syntax
const allExports = icons.map(({ name }) => `export { default as ${name} } from './${name}'`).join('\n')
fse.writeFileSync(path.join(__dirname, 'package', 'index.es.js'), allExports)
// typescript index definition (looks exactly the same)
fse.writeFileSync(path.join(__dirname, 'package', 'index.d.ts'), allExports)
// commonjs module
fse.writeFileSync(path.join(__dirname, 'package', 'index.js'), babel.transform(allExports, {
plugins: ['transform-es2015-modules-commonjs'],
compact: process.env.NODE_ENV === 'production'
}).code)
// createIcon function
fse.mkdirSync(path.join(__dirname, 'package', 'util'))
fse.writeFileSync(
path.join(__dirname, 'package', 'util', 'createIcon.js'),
babel.transform(fse.readFileSync(path.join(__dirname, 'src', 'util', 'createIcon.js')), {
presets: ['es2015', 'react', 'stage-0'],
compact: process.env.NODE_ENV === 'production'
}).code
)
// update readme
const mdiVersion = require(path.join(require.resolve('@mdi/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`)
await fse.writeFile(path.join(__dirname, 'README.md'), readme, 'utf-8')
// copy other files
;[
'README.md',
'NOTICE',
'LICENSE',
'.npmignore'
].forEach((file) => fse.copySync(path.join(__dirname, file), path.join(__dirname, 'package', file)))
const packageJson = require('./package.json')
packageJson.name = '@mcesystems/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')
})()