turf/scripts/create-new-module
2017-11-19 22:03:05 -05:00

222 lines
5.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const meow = require('meow');
const camelcase = require('camelcase');
const decamelize = require('decamelize');
const cli = meow(`
Usage:
$ ./scripts/create-new-module <module>
Examples:
$ ./scripts/create-new-module clone
`);
if (!cli.input.length) cli.showHelp();
let name = cli.input[0];
// Normalize module name
// turf-clone => clone
name = name.replace(/turf-/, '');
const camelcaseName = camelcase(name);
const decamelizeName = decamelize(name);
// Create Folder
const folderPath = path.join(__dirname, '..', 'packages', `turf-${decamelizeName}`);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
fs.mkdirSync(path.join(folderPath, 'test'));
fs.mkdirSync(path.join(folderPath, 'test/in'));
fs.mkdirSync(path.join(folderPath, 'test/out'));
}
// Create index.js
fs.writeFileSync(path.join(folderPath, 'index.js'), `/**
* <DESCRIPTION>
*
* @name ${camelcaseName}
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
* @returns {Boolean} true/false
* @example
* <SIMPLE EXAMPLE>
*/
function ${camelcaseName}(feature1, feature2) {
return true;
};
export default ${camelcaseName};
`);
// Create package.json
fs.writeFileSync(path.join(folderPath, 'package.json'), `{
"name": "@turf/${decamelizeName}",
"version": "5.0.0",
"description": "turf ${decamelizeName} module",
"main": "main",
"module": "index",
"jsnext:main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"main.js"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"bench": "node -r @std/esm bench.js"
},
"repository": {
"type": "git",
"url": "git://github.com/Turfjs/turf.git"
},
"keywords": [
"turf",
"${name}"
],
"author": "Turf Authors",
"contributors": [
"YOUR NAME <@GITHUB NAME>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@std/esm": "*",
"benchmark": "*",
"rollup": "*",
"write-json-file": "*",
"load-json-file": "*",
"tape": "*"
},
"dependencies": {
},
"@std/esm": {
"esm": "js",
"cjs": true
}
}
`);
// Create LICENSE
const license = path.join(__dirname, '..', 'packages', 'turf', 'LICENSE');
fs.copySync(license, path.join(folderPath, 'LICENSE'));
// Create index.d.ts
fs.writeFileSync(path.join(folderPath, 'index.d.ts'), `import { Feature, GeometryObject } from '@turf/helpers'
/**
* http://turfjs.org/docs/#${name.toLocaleLowerCase().replace('-', '')}
*/
export default function (
feature1: Feature<any> | GeometryObject,
feature2: Feature<any> | GeometryObject
): boolean
`);
// Create bench.js
fs.writeFileSync(path.join(folderPath, 'bench.js'), `const path = require('path');
const glob = require('glob');
const load = require('load-json-file');
const Benchmark = require('benchmark');
const ${camelcaseName} = require('./');
/**
* Benchmark Results
*
* <Place results here>
*/
const suite = new Benchmark.Suite('turf-${decamelizeName}');
glob.sync(path.join(__dirname, 'test', 'in', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
const [feature1, feature2] = geojson.features;
console.time(name);
${camelcaseName}(feature1, feature2);
console.timeEnd(name);
suite.add(name, () => ${camelcaseName}(feature1, feature2));
});
suite
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();
`);
// Create test.js
fs.writeFileSync(path.join(folderPath, 'test.js'), `const fs = require('fs');
const test = require('tape');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const ${camelcaseName} = require('./');
const directories = {
in: path.join(__dirname, 'test', 'in') + path.sep,
out: path.join(__dirname, 'test', 'out') + path.sep
};
const fixtures = fs.readdirSync(directories.in).map(filename => {
return {
filename,
name: path.parse(filename).name,
geojson: load.sync(directories.in + filename)
};
});
test('turf-${decamelizeName}', t => {
for (const {filename, name, geojson} of fixtures) {
const [feature1, feature2] = geojson.features;
const results = ${camelcaseName}(feature1, feature2);
if (process.env.REGEN) write.sync(directories.out + filename, results);
t.deepEqual(results, load.sync(directories.out + filename), name);
}
t.end();
});
`);
// Create README.md
fs.writeFileSync(path.join(folderPath, 'README.md'), `# @turf/${decamelizeName}
# ${camelcaseName}
<DESCRIPTION>
**Parameters**
<PARAMETERS>
**Examples**
<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->
---
This module is part of the [Turfjs project](http://turfjs.org/), an open source
module collection dedicated to geographic algorithms. It is maintained in the
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
PRs and issues.
### Installation
Install this module individually:
\`\`\`sh
$ npm install @turf/${decamelizeName}
\`\`\`
Or install the Turf module that includes it as a function:
\`\`\`sh
$ npm install @turf/turf
\`\`\`
`);