v1.0 First Commit
This commit is contained in:
commit
ff2bc6ac3a
5 changed files with 257 additions and 0 deletions
116
.gitignore
vendored
Normal file
116
.gitignore
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
|
||||
# Created by https://www.gitignore.io/api/osx,node,windows
|
||||
|
||||
### Dont Ship any SVGs
|
||||
.svg
|
||||
|
||||
### Node ###
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Typescript v1 declaration files
|
||||
typings/
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
|
||||
### OSX ###
|
||||
*.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# End of https://www.gitignore.io/api/osx,node,windows
|
40
README.md
Normal file
40
README.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# svgdir2sprite
|
||||
|
||||
Creates a spritesheet from a directory of svgs. Can return the results directly or write to a spritesheet svg file. Node based and powered by [svg2sprite](https://github.com/mrmlnc/svg2sprite).
|
||||
|
||||
## As a Module
|
||||
|
||||
`npm install --save svgdir2sprite`
|
||||
|
||||
### Return SVG Spritesheet as String
|
||||
|
||||
``` javascript
|
||||
const svgdir2sprite = require('svgdir2sprite');
|
||||
|
||||
svgdir2sprite('./src/svgs') // Async Promise
|
||||
.then((svgContent) => console.log(svgContent));
|
||||
```
|
||||
|
||||
### Write SVG Spritesheet to File
|
||||
|
||||
``` javascript
|
||||
const svgdir2sprite = require('svgdir2sprite');
|
||||
|
||||
svgdir2sprite('./src/svgs', './build/spritesheet.svg');
|
||||
```
|
||||
|
||||
## From the CLI
|
||||
|
||||
`npm install -g blabber-comic`
|
||||
|
||||
### Return SVG Spritesheet to the Console
|
||||
|
||||
``` bash
|
||||
svgdir2sprite ./src/svgs
|
||||
```
|
||||
|
||||
### Write SVG Spritesheet to File
|
||||
|
||||
``` bash
|
||||
svgdir2sprite ./src/svgs ./build/spritesheet.svg
|
||||
```
|
24
cli.js
Normal file
24
cli.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
const svgdir2sprite = require('./');
|
||||
const argv = require('yargs')
|
||||
.demandCommand(1, 'Warning: Please set a source directory for loose svg\'s')
|
||||
.example('$0 ./svgs', ':: Output to console')
|
||||
.example('$0 ./svgs ./build/file.svg', ':: Output to file')
|
||||
.alias('help', 'h')
|
||||
.help()
|
||||
.version()
|
||||
.wrap(process.stdout.columns)
|
||||
.argv;
|
||||
|
||||
let [src, dest] = argv._;
|
||||
|
||||
svgdir2sprite(src, dest)
|
||||
.then((svgContents) => {
|
||||
if (dest) {
|
||||
console.log('Spritesheet successfully generated as ' + dest);
|
||||
} else {
|
||||
console.log(svgContents);
|
||||
}
|
||||
});
|
50
index.js
Normal file
50
index.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
const fs = require('fs');
|
||||
const async = require('async');
|
||||
const svgSprite = require('svg2sprite');
|
||||
const sprite = svgSprite.collection();
|
||||
|
||||
const isSVG = (filename) => filename.split('.').pop() === 'svg';
|
||||
const iconName = (filename) => filename.split('.')[0];
|
||||
|
||||
function saveSpritesheet(path, spritesheet) {
|
||||
if(path.indexOf('.svg') === -1) throw 'Error: Please specify a filename ending with .svg';
|
||||
|
||||
fs.writeFile(path, spritesheet, (error) => { if (error) throw error });
|
||||
}
|
||||
|
||||
function readSprite(filename, file) {
|
||||
sprite.add(iconName(filename), `<svg>${file}</svg>`);
|
||||
}
|
||||
|
||||
function allSpritesRead(pathBuild) {
|
||||
let spritesheet = sprite.compile();
|
||||
|
||||
if (pathBuild) {
|
||||
saveSpritesheet(pathBuild, spritesheet);
|
||||
}
|
||||
|
||||
return spritesheet;
|
||||
}
|
||||
|
||||
function generate(pathSrc, pathBuild) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readdir(pathSrc, (error, filenames) => {
|
||||
if (error) reject(error);
|
||||
|
||||
async.eachSeries(filenames.filter(isSVG), (filename, cb) => {
|
||||
fs.readFile(pathSrc + filename, (error, fileData) => {
|
||||
if (error) reject(error);
|
||||
|
||||
readSprite(filename, fileData);
|
||||
cb(fileData);
|
||||
});
|
||||
}, (error) => {
|
||||
if (error) reject(error);
|
||||
|
||||
resolve(allSpritesRead(pathBuild));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = generate;
|
27
package.json
Normal file
27
package.json
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "svgdir2sprite",
|
||||
"version": "1.0.0",
|
||||
"description": "Creates a spritesheet from a directory of svgs.",
|
||||
"repository": "sharpshark28/svgdir2sprite",
|
||||
"main": "index.js",
|
||||
"bin": {
|
||||
"svgdir2sprite": "./cli.js"
|
||||
},
|
||||
"keywords": [
|
||||
"svg",
|
||||
"symbol",
|
||||
"sprite",
|
||||
"spritesheet",
|
||||
"generator",
|
||||
"icon",
|
||||
"icons"
|
||||
],
|
||||
"author": "Joe Wroten <sharpshark28@gmail.com> (http://joewroten.com/)",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"async": "^2.3.0",
|
||||
"fs": "0.0.1-security",
|
||||
"svg2sprite": "^2.0.1",
|
||||
"yargs": "^7.1.0"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue