turf/scripts/generate-readmes
2017-11-26 21:36:46 -08:00

69 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const load = require('load-json-file');
const documentation = require('documentation');
/**
* When firing `npm run docs`:
* - inside a module, only the docs of that module will be generated
* - outside or at the root level it will generate docs for all modules
*/
const currentFolder = process.cwd().split(path.sep).pop();
const packages = currentFolder.includes('turf-') ?
[path.join(process.cwd(), 'package.json')] :
glob.sync(path.join(__dirname, '..', 'packages', 'turf-*', 'package.json'));
// Template for README Markdown
const postfix = fs.readFileSync(path.join(__dirname, 'postfix.md'), 'utf8');
const paths = {
GeoJSON: 'http://geojson.org/geojson-spec.html#geojson-objects',
GeometryCollection: 'http://geojson.org/geojson-spec.html#geometrycollection',
Point: 'http://geojson.org/geojson-spec.html#point',
Points: 'http://geojson.org/geojson-spec.html#point',
'(Multi)Point': 'http://geojson.org/geojson-spec.html#point',
'(Multi)Points': 'http://geojson.org/geojson-spec.html#point',
'(Multi)Point(s)': 'http://geojson.org/geojson-spec.html#point',
MultiPoint: 'http://geojson.org/geojson-spec.html#multipoint',
MultiPoints: 'http://geojson.org/geojson-spec.html#multipoint',
LineString: 'http://geojson.org/geojson-spec.html#linestring',
LineStrings: 'http://geojson.org/geojson-spec.html#linestring',
'(Multi)LineString': 'http://geojson.org/geojson-spec.html#linestring',
'(Multi)LineStrings': 'http://geojson.org/geojson-spec.html#linestring',
'(Multi)LineString(s)': 'http://geojson.org/geojson-spec.html#linestring',
MultiLineString: 'http://geojson.org/geojson-spec.html#multilinestring',
MultiLineStrings: 'http://geojson.org/geojson-spec.html#multilinestring',
Polygon: 'http://geojson.org/geojson-spec.html#polygon',
Polygons: 'http://geojson.org/geojson-spec.html#polygon',
'(Multi)Polygon': 'http://geojson.org/geojson-spec.html#polygon',
'(Multi)Polygons': 'http://geojson.org/geojson-spec.html#polygon',
'(Multi)Polygon(s)': 'http://geojson.org/geojson-spec.html#polygon',
MultiPolygon: 'http://geojson.org/geojson-spec.html#multipolygon',
MultiPolygons: 'http://geojson.org/geojson-spec.html#multipolygon',
Geometry: 'http://geojson.org/geojson-spec.html#geometry',
Feature: 'http://geojson.org/geojson-spec.html#feature-objects',
FeatureCollection: 'http://geojson.org/geojson-spec.html#feature-collection-objects'
};
packages.forEach(packagePath => {
const directory = path.parse(packagePath).dir;
const indexPath = path.join(directory, 'index.js');
const pckg = load.sync(packagePath);
const name = pckg.name;
// Build Documentation
documentation.build(indexPath, {shallow: true}).then(res => {
if (res === undefined) return console.warning(packagePath);
console.log('Building Docs: ' + name);
// Format Markdown
documentation.formats.md(res, {paths}).then(markdown => {
markdown = `# ${name}\n\n${markdown}${postfix.replace(/{module}/, name)}`;
fs.writeFileSync(path.join(directory, 'README.md'), markdown);
}).catch(error => console.warning(error));
}).catch(error => console.warning(error));
});