svg list build
This commit is contained in:
+3
-1
@@ -1,3 +1,5 @@
|
||||
/.idea
|
||||
/node_modules
|
||||
/coverage
|
||||
/coverage
|
||||
/build
|
||||
/svg_list.json
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
/node_modules
|
||||
/node_modules
|
||||
/build
|
||||
@@ -4,6 +4,19 @@
|
||||
# fa-diagrams
|
||||
## (WIP) SVG diagrams built from Font-Awesome icons
|
||||
|
||||
## Build from sources
|
||||
|
||||
You will need subversion installed (used for precise folder fetching in GitHub)
|
||||
|
||||
```
|
||||
git clone https://github.com/klemek/fa-diagrams.git
|
||||
cd fa-diagrams
|
||||
npm install
|
||||
node build.sh
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
How to use (theorically)
|
||||
```javascript
|
||||
const diag = require('fa-diagrams');
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const svn = require('node-svn-ultimate');
|
||||
const rimraf = require('rimraf');
|
||||
|
||||
const FA_SVG_FOLDER_URL = 'https://github.com/FortAwesome/Font-Awesome.git/trunk/svgs';
|
||||
|
||||
const buildDir = path.join(__dirname, 'build');
|
||||
|
||||
/**
|
||||
* Get all files path inside a given folder path
|
||||
* @param dir
|
||||
* @param cb
|
||||
*/
|
||||
const getFileTree = (dir, cb) => {
|
||||
let list = [];
|
||||
let remaining = 0;
|
||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
items.forEach((item) => {
|
||||
if (item.isDirectory()) {
|
||||
remaining++;
|
||||
getFileTree(path.join(dir, item.name), (err, out) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
list.push(...out);
|
||||
remaining--;
|
||||
if (remaining === 0)
|
||||
cb(null, list);
|
||||
});
|
||||
} else {
|
||||
list.push(path.join(dir, item.name));
|
||||
}
|
||||
});
|
||||
if (remaining === 0)
|
||||
cb(null, list);
|
||||
});
|
||||
};
|
||||
|
||||
console.log('Building fa-diagrams svg list');
|
||||
|
||||
if (fs.existsSync(buildDir)) {
|
||||
console.log('\tCleaning build directory...');
|
||||
rimraf.sync(buildDir);
|
||||
} else {
|
||||
console.log('\tCreating build directory...');
|
||||
}
|
||||
|
||||
fs.mkdirSync(buildDir);
|
||||
|
||||
console.log('\tFetching Font-Awesome SVGs from repository...');
|
||||
|
||||
const svgDir = path.join(buildDir, 'svgs');
|
||||
|
||||
svn.commands.checkout(FA_SVG_FOLDER_URL, svgDir, (err) => {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
rimraf.sync(path.join(svgDir, '.svn'));
|
||||
|
||||
console.log('\tReading SVGs...');
|
||||
|
||||
getFileTree(svgDir, (err, list) => {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
const svgs = {};
|
||||
|
||||
let count = 0;
|
||||
|
||||
list.filter(x => /\.svg$/.test(x)).forEach(svgFile => {
|
||||
const data = fs.readFileSync(svgFile, {encoding: 'utf8'});
|
||||
const match1 = data.match(/viewBox="0 0 (\d+) 512"/);
|
||||
if (!match1)
|
||||
return console.warn(`\t\tInvalid viewBox match for "${svgFile}"`);
|
||||
const match2 = data.match(/path d="([^"]+)"/);
|
||||
if (!match2)
|
||||
return console.warn(`\t\tInvalid path for "${svgFile}"`);
|
||||
|
||||
const type = path.basename(path.dirname(svgFile));
|
||||
const name = path.basename(svgFile, '.svg');
|
||||
|
||||
if (!svgs[type])
|
||||
svgs[type] = {};
|
||||
|
||||
svgs[type][name] = {
|
||||
path: match2[1],
|
||||
width: match1[1]
|
||||
};
|
||||
|
||||
count++;
|
||||
});
|
||||
|
||||
console.log(`\t${count} SVGs loaded`);
|
||||
Object.keys(svgs).forEach(type => {
|
||||
console.log(`\t\t${type}: ${Object.keys(svgs[type]).length} SVGs`);
|
||||
});
|
||||
|
||||
const argIndex = process.argv.indexOf('--output');
|
||||
const output = (argIndex === -1 || !process.argv[argIndex + 1]) ? path.join(buildDir, 'svg_list.json') : process.argv[argIndex + 1];
|
||||
|
||||
fs.writeFileSync(output, JSON.stringify(svgs), {encoding: 'utf8'});
|
||||
|
||||
console.log(`\tSVG list saved at "${output}"`);
|
||||
});
|
||||
});
|
||||
Generated
+845
-217
File diff suppressed because it is too large
Load Diff
+15
-7
@@ -2,9 +2,14 @@
|
||||
"name": "fa-diagrams",
|
||||
"version": "1.0.0",
|
||||
"description": "SVG diagrams built from Font-Awesome icons",
|
||||
"main": "index.js",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"/src",
|
||||
"/svg_list.json"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "jest --silent"
|
||||
"test": "jest --silent",
|
||||
"prepublishOnly": "node build.js --output svg_list.json"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -21,15 +26,18 @@
|
||||
"url": "https://github.com/Klemek/fa-diagrams/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Klemek/fa-diagrams#readme",
|
||||
"dependencies": {
|
||||
"coveralls": "^3.0.4",
|
||||
"jest": "^24.8.0",
|
||||
"jshint": "^2.10.2"
|
||||
},
|
||||
"dependencies": {},
|
||||
"jest": {
|
||||
"collectCoverageFrom": [
|
||||
"src/**/*.js",
|
||||
"!/node_modules/"
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"coveralls": "^3.0.4",
|
||||
"jest": "^24.8.0",
|
||||
"jshint": "^2.10.2",
|
||||
"node-svn-ultimate": "^1.2.0",
|
||||
"rimraf": "^2.6.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
const path = require('path');
|
||||
|
||||
const loadList = () => {
|
||||
const paths = [
|
||||
path.join(__dirname, '..', 'build', 'svg_list.json'),
|
||||
path.join(__dirname, '..', 'svg_list.json'),
|
||||
];
|
||||
for (let p = 0; p < paths.length; p++) {
|
||||
try {
|
||||
return require(paths[p]);
|
||||
} catch (err) {
|
||||
//ignored
|
||||
}
|
||||
}
|
||||
console.error('fa-diagrams: SVG list was not found at the following paths:\n\t* ' + paths.join('\n\t* '));
|
||||
return {};
|
||||
};
|
||||
|
||||
const list = loadList();
|
||||
|
||||
const self = {
|
||||
list: list
|
||||
};
|
||||
|
||||
module.exports = self;
|
||||
Reference in New Issue
Block a user