Skip to content

Commit

Permalink
chore: update npm distribution (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vladimyr committed Jan 25, 2021
1 parent 91a3999 commit c553e12
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
index.js
icons.json
node_modules

.now
.now
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2021 by Amio

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 changes: 24 additions & 24 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs')
'use strict'

const { readdirSync, readFileSync, writeFileSync } = require('fs')
const { join, parse } = require('path')

const noneSquareIconWidths = {
Expand All @@ -12,33 +14,31 @@ const noneSquareIconWidths = {
zeit: 15
}

const genIcons = (iconFolder) => {
const icons = {}

fs.readdirSync(join(__dirname, iconFolder)).forEach(filename => {
const imageType = {
'.svg': 'svg+xml',
'.png': 'png'
}[parse(filename).ext]

if (!imageType) return
const generateIcons = iconFolder => readdirSync(iconFolder).reduce((icons, filename) => {
const { name, ext } = parse(filename)
const imageType = {
'.svg': 'svg+xml',
'.png': 'png'
}[ext]

const key = parse(filename).name
const iconFile = join(__dirname, iconFolder, filename)
const svgSource = fs.readFileSync(iconFile, 'utf8')
const b64 = Buffer.from(svgSource).toString('base64')
if (!imageType) return icons

icons[key] = {
base64: `data:image/${imageType};base64,${b64}`,
width: noneSquareIconWidths[key] || 13,
height: 13
}
})
const iconFile = join(iconFolder, filename)
const svgSource = readFileSync(iconFile, 'utf8')
const b64 = Buffer.from(svgSource).toString('base64')

icons[name] = {
base64: `data:image/${imageType};base64,${b64}`,
width: noneSquareIconWidths[name] || 13,
height: 13
}
return icons
}
}, {})

const inputDir = join(__dirname, 'icons')
const outFile = join(__dirname, 'icons.json')

const icons = genIcons('icons')
const icons = generateIcons(inputDir)
const json = JSON.stringify(icons, null, 2)

fs.writeFileSync('index.js', `module.exports = ${json}`)
writeFileSync(outFile, json)
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./icons.json')
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"name": "badgen-icons",
"description": "Badgen's icons",
"version": "0.18.0",
"repository": "badgen/badgen-icons",
"author": "Amio <amio.cn@gmail.com>",
"description": "Badgen's icons",
"homepage": "https://github.com/badgen/badgen-icons#readme",
"bugs": {
"url": "https://github.com/badgen/badgen-icons/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/badgen/badgen-icons.git"
},
"license": "ISC",
"author": "Amio <amio.cn@gmail.com>",
"main": "index.js",
"files": [
"icons.json",
"icons"
],
"scripts": {
"build": "node build.js",
"prepack": "npm run build"
Expand Down
16 changes: 2 additions & 14 deletions preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,10 @@
}
})

function exportFromString (str) {
const _globals = 'const __dirname = "";'
const _module = { exports: {} };
const _fn = new Function('module', 'exports', _globals + str);
_fn(_module, _module.exports);
return _module.exports
}

function requireModule (url) {
return fetch(url).then(async res => {
return exportFromString(await res.text())
})
}
const fetchJSON = (url, init) => fetch(url, init).then(resp => resp.json());

// render badge gallery
requireModule('../index.js').then(icons => {
fetchJSON('../icons.json').then(icons => {
preview.innerHTML = badgen({
status: 'icon',
subject: '',
Expand Down

1 comment on commit c553e12

@vercel
Copy link

@vercel vercel bot commented on c553e12 Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.