Skip to content

Commit c553e12

Browse files
authored
chore: update npm distribution (#36)
* chore: update `package.json` Add homepage & bugs url, add `files` prop. Sort keys using `sort-package-json` CLI. * chore: remove obsolete `.npmignore` * chore: add license file * chore: prevent lockfile creation * refactor: build step generates JSON output
1 parent 91a3999 commit c553e12

File tree

8 files changed

+57
-45
lines changed

8 files changed

+57
-45
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
index.js
1+
icons.json
22
node_modules
33

4-
.now
4+
.now

.npmignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2021 by Amio
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

build.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const fs = require('fs')
1+
'use strict'
2+
3+
const { readdirSync, readFileSync, writeFileSync } = require('fs')
24
const { join, parse } = require('path')
35

46
const noneSquareIconWidths = {
@@ -12,33 +14,31 @@ const noneSquareIconWidths = {
1214
zeit: 15
1315
}
1416

15-
const genIcons = (iconFolder) => {
16-
const icons = {}
17-
18-
fs.readdirSync(join(__dirname, iconFolder)).forEach(filename => {
19-
const imageType = {
20-
'.svg': 'svg+xml',
21-
'.png': 'png'
22-
}[parse(filename).ext]
23-
24-
if (!imageType) return
17+
const generateIcons = iconFolder => readdirSync(iconFolder).reduce((icons, filename) => {
18+
const { name, ext } = parse(filename)
19+
const imageType = {
20+
'.svg': 'svg+xml',
21+
'.png': 'png'
22+
}[ext]
2523

26-
const key = parse(filename).name
27-
const iconFile = join(__dirname, iconFolder, filename)
28-
const svgSource = fs.readFileSync(iconFile, 'utf8')
29-
const b64 = Buffer.from(svgSource).toString('base64')
24+
if (!imageType) return icons
3025

31-
icons[key] = {
32-
base64: `data:image/${imageType};base64,${b64}`,
33-
width: noneSquareIconWidths[key] || 13,
34-
height: 13
35-
}
36-
})
26+
const iconFile = join(iconFolder, filename)
27+
const svgSource = readFileSync(iconFile, 'utf8')
28+
const b64 = Buffer.from(svgSource).toString('base64')
3729

30+
icons[name] = {
31+
base64: `data:image/${imageType};base64,${b64}`,
32+
width: noneSquareIconWidths[name] || 13,
33+
height: 13
34+
}
3835
return icons
39-
}
36+
}, {})
37+
38+
const inputDir = join(__dirname, 'icons')
39+
const outFile = join(__dirname, 'icons.json')
4040

41-
const icons = genIcons('icons')
41+
const icons = generateIcons(inputDir)
4242
const json = JSON.stringify(icons, null, 2)
4343

44-
fs.writeFileSync('index.js', `module.exports = ${json}`)
44+
writeFileSync(outFile, json)

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./icons.json')

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
{
22
"name": "badgen-icons",
3-
"description": "Badgen's icons",
43
"version": "0.18.0",
5-
"repository": "badgen/badgen-icons",
6-
"author": "Amio <[email protected]>",
4+
"description": "Badgen's icons",
5+
"homepage": "https://github.com/badgen/badgen-icons#readme",
6+
"bugs": {
7+
"url": "https://github.com/badgen/badgen-icons/issues"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/badgen/badgen-icons.git"
12+
},
713
"license": "ISC",
14+
"author": "Amio <[email protected]>",
815
"main": "index.js",
16+
"files": [
17+
"icons.json",
18+
"icons"
19+
],
920
"scripts": {
1021
"build": "node build.js",
1122
"prepack": "npm run build"

preview.html

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,10 @@
8080
}
8181
})
8282

83-
function exportFromString (str) {
84-
const _globals = 'const __dirname = "";'
85-
const _module = { exports: {} };
86-
const _fn = new Function('module', 'exports', _globals + str);
87-
_fn(_module, _module.exports);
88-
return _module.exports
89-
}
90-
91-
function requireModule (url) {
92-
return fetch(url).then(async res => {
93-
return exportFromString(await res.text())
94-
})
95-
}
83+
const fetchJSON = (url, init) => fetch(url, init).then(resp => resp.json());
9684

9785
// render badge gallery
98-
requireModule('../index.js').then(icons => {
86+
fetchJSON('../icons.json').then(icons => {
9987
preview.innerHTML = badgen({
10088
status: 'icon',
10189
subject: '',

0 commit comments

Comments
 (0)