Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prepublish script #942

Merged
merged 10 commits into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ console.log(simpleIcons['Google+']);
*/
```

Alternatively you can import the needed icons individually.
This is useful if you are e.g. compiling your code with webpack and therefore have to be mindful of your package size.
Copy link
Contributor

Choose a reason for hiding this comment

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

webpack could be a link, just like "JSDelivr" and "Unpkg"


```js
const googleplus = require('simple-icons/icons/googleplus');

console.log(googleplus);
/*
{
title: 'Google+',
hex: 'DC4E41',
source: 'https://developers.google.com/+/branding-guidelines',
svg: '<svg aria-labelledby="simpleicons-googleplus-icon" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">...</svg>'
}
*/
```

## Third Party Extensions

### WordPress
Expand Down
18 changes: 0 additions & 18 deletions index.js

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
},
"scripts": {
"jsonlint": "jsonlint _data/simple-icons.json -q -V .jsonlintschema",
"svglint": "svglint icons/* --ci"
"svglint": "svglint icons/* --ci",
"prepublishOnly": "node scripts/prepublish.js",
"postpublish": "rm icons/*.js && rm index.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe rm icons/*.js index.js also works, why did you write it like this?

}
}
31 changes: 31 additions & 0 deletions scripts/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node
/**
* @fileoverview
* Compiles our icons into static .js files that can be imported in the browser
* and are tree-shakeable.
* The static .js files go in icons/{filename}.js.
* Also generates an index.js that exports all icons by title, but is not tree-shakeable
*/

const dataFile = "../_data/simple-icons.json";
const indexFile = `${__dirname}/../index.js`;
const iconsDir = `${__dirname}/../icons`;
const data = require(dataFile);
const fs = require('fs');
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick, but other then this you're always using double quotes


const { titleToFilename } = require("./utils");

const icons = {};
data.icons.forEach(i => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename the variable i to icon for readability

const filename = titleToFilename(i.title);
i.svg = fs.readFileSync(`${iconsDir}/${filename}.svg`, "utf8");
icons[i.title] = i;
// write the static .js file for the icon
fs.writeFileSync(
`${iconsDir}/${filename}.js`,
`module.exports = ${JSON.stringify(i)};`
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change this to module.exports=${JSON.stringify(i)}; (removing the space) to reduce the output size?

);
});

// write our generic index.js
fs.writeFileSync(indexFile, `module.exports = ${JSON.stringify(icons)};`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, change this to module.exports=${JSON.stringify(icons)}; (removing the space) to reduce the output size?

14 changes: 14 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
/**
* Converts a brand title into a filename (not a full path)
* @param {String} title The title to convert
*/
titleToFilename: title => (
title.toLowerCase()
.replace(/\+/g, "plus")
.replace(/^\./, "dot-")
.replace(/\.$/, "-dot")
.replace(/\./g, "-dot-")
.replace(/[ !’]/g, '')
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick, but other then this you're always using double quotes

)
}