turf/scripts/create-new-module

234 lines
5.4 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").default;
const decamelize = require("decamelize").default;
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>
*/
export default function ${camelcaseName}(feature1, feature2) {
return true;
};
`
);
// Create package.json
fs.writeFileSync(
path.join(folderPath, "package.json"),
`{
"name": "@turf/${decamelizeName}",
"version": "5.0.0",
"description": "turf ${decamelizeName} module",
"main": "main.js",
"module": "main.es.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"main.js",
"main.es.js"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js && echo '{"type":"module"}' > dist/es/package.json",
"test": "node -r @std/esm test.js",
"bench": "node -r @std/esm bench.js",
"docs": "node ../../scripts/generate-readmes"
},
"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": "^2.1.4",
"rollup": "^2.79.1",
"write-json-file": "^5.0.0",
"load-json-file": "^7.0.1",
"tape": "^5.7.0"
},
"dependencies": {
"@turf/helpers": "*"
},
"@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"),
`import Benchmark from 'benchmark';
import ${camelcaseName} from '.';
import glob from 'glob';
import path from 'path';
import load from 'load-json-file';
/**
* 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 = loadJsonFileSync(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)))
.run();
`
);
// Create test.js
fs.writeFileSync(
path.join(folderPath, "test.js"),
`import test from 'tape';
import glob from 'glob';
import path from 'path';
import load from 'load-json-file';
import write from 'write-json-file';
import ${camelcaseName} from '.';
test('turf-${decamelizeName}', t => {
glob.sync(path.join(__dirname, 'test', 'in', '*.json')).forEach(filepath => {
// Define params
const {name} = path.parse(filepath);
const geojson = loadJsonFileSync(filepath);
// etc.
const results = [];
const out = filepath.replace(path.join('test', 'in'), path.join('test', 'out'));
if (process.env.REGEN) writeJsonFileSync(out, results);
t.deepEqual(results, loadJsonFileSync(out), 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
\`\`\`
`
);