diff --git a/README.md b/README.md index fd34c4ae33512d..a822c69c47a5e9 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,9 @@ Your package should have this structure: | tsconfig.json | This allows you to run `tsc` within the package. | | tslint.json | Enables linting. | -Generate these by running `npm run new-package -- new-package-name`. +Generate these by running `npm run new-package -- --name my-package-name --template module`. +(Other templates are `module-class`, `module-function`, `module-plugin`, `global`, `global-plugin`, and `global-modifying-module`. +This just wraps [dts-gen](https://github.com/Microsoft/dts-gen), so it supports all options from that.) You may edit the `tsconfig.json` to add new files, to add `"target": "es6"` (needed for async functions), to add to `"lib"`, or to add the `"jsx"` compiler option. diff --git a/package.json b/package.json index a9b9625069cfcd..1dad46394d943f 100644 --- a/package.json +++ b/package.json @@ -16,12 +16,13 @@ }, "scripts": { "compile-scripts": "tsc -p scripts", - "new-package": "node scripts/new-package.js", + "new-package": "dts-gen --dt", "not-needed": "node scripts/not-needed.js", "lint": "node scripts/lint.js", "test": "node node_modules/types-publisher/bin/tester/test.js --run-from-definitely-typed --nProcesses 1" }, "devDependencies": { - "types-publisher": "Microsoft/types-publisher#production" + "types-publisher": "Microsoft/types-publisher#production", + "dts-gen": "latest" } } diff --git a/scripts/new-package.js b/scripts/new-package.js deleted file mode 100644 index 360fdff85f9437..00000000000000 --- a/scripts/new-package.js +++ /dev/null @@ -1,121 +0,0 @@ -/// -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -const http_1 = require("http"); -const fs_1 = require("fs"); -const path = require("path"); -const newPackageName = process.argv[2]; -if (!newPackageName) { - throw new Error("Usage: node scripts/new-package.js new-package-name"); -} -run().catch(e => { - console.error(e); - process.exit(1); -}); -function run() { - return __awaiter(this, void 0, void 0, function* () { - const files = [ - ["index.d.ts", yield getIndex()], - [`${newPackageName}-tests.ts`, ""], - ["tsconfig.json", `${JSON.stringify(getTSConfig(), undefined, 4)}\n`], - ["tslint.json", '{ "extends": "../tslint.json" }\n'], - ]; - try { - yield pify1(fs_1.mkdir, newPackageName); - yield Promise.all(files.map(([name, text]) => write(name, text))); - } - catch (e) { - if (e.code === "EEXIST") { - console.warn(`Module “${newPackageName}” already exists!`); - } - else { - console.error(`Error creating module files: ${e}\nCleaning Up.`); - yield Promise.all(files.map(([name]) => rm(name))); - yield pify1(fs_1.rmdir, newPackageName); - } - process.exit(1); - } - }); -} -function getIndex() { - return __awaiter(this, void 0, void 0, function* () { - let version = "x.x"; - let project = "https://github.com/baz/foo (Does not have to be to GitHub, but prefer linking to a source code repository rather than to a project website.)"; - try { - const reg = JSON.parse(yield loadString(`http://registry.npmjs.org/${newPackageName}`)); - const { latest } = reg["dist-tags"]; - const { homepage } = reg.versions[latest]; - version = latest.split(".").slice(0, 2).join("."); - if (homepage !== undefined) - project = homepage; - } - catch (e) { - console.warn(`Warning: could not retrieve version/homepage information: ${e.message}`); - } - return `// Type definitions for ${newPackageName} ${version} -// Project: ${project} -// Definitions by: My Self -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - -Fill the header in! -`; - }); -} -function getTSConfig() { - return { - "compilerOptions": { - "module": "commonjs", - "target": "es6", - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - `${newPackageName}-tests.ts` - ] - }; -} -function write(name, content) { - return new Promise((resolve, reject) => { - fs_1.writeFile(path.join(newPackageName, name), content, err => err ? reject(err) : resolve()); - }); -} -function rm(name) { - return pify1(fs_1.unlink, path.join(newPackageName, name)).catch((e) => { - if (e.code === "ENOENT") - return; - throw e; - }); -} -function pify1(fn, arg) { - return new Promise((resolve, reject) => { - fn(arg, err => err ? reject(err) : resolve()); - }); -} -function loadString(url) { - return new Promise((resolve, reject) => { - http_1.get(url, (res) => { - if (res.statusCode !== 200) { - return reject(new Error(`HTTP Error ${res.statusCode}: ${http_1.STATUS_CODES[res.statusCode || 500]} for ${url}`)); - } - let rawData = ""; - res.on("data", chunk => rawData += chunk); - res.on("end", () => resolve(rawData)); - }).on("error", e => reject(e)); - }); -} diff --git a/scripts/new-package.ts b/scripts/new-package.ts deleted file mode 100644 index 6e9384c896475e..00000000000000 --- a/scripts/new-package.ts +++ /dev/null @@ -1,131 +0,0 @@ -/// - -import { get, STATUS_CODES } from "http" -import { mkdir, writeFile, unlink, rmdir } from "fs" -import * as path from "path" - -interface Package { - name: string - description: string - version: string - homepage?: string -} - -interface Registry { - name: string - description: string - "dist-tags": { latest: string } - versions: { [version: string]: Package } -} - -const newPackageName = process.argv[2] -if (!newPackageName) { - throw new Error("Usage: node scripts/new-package.js new-package-name") -} - -run().catch(e => { - console.error(e) - process.exit(1) -}) - -async function run() { - const files: [string, string][] = [ - ["index.d.ts", await getIndex()], - [`${newPackageName}-tests.ts`, ""], - ["tsconfig.json", `${JSON.stringify(getTSConfig(), undefined, 4)}\n`], - ["tslint.json", '{ "extends": "../tslint.json" }\n'], - ] - - try { - await pify1(mkdir, newPackageName) - await Promise.all(files.map(([name, text]) => write(name, text))) - } catch (e) { - if (e.code === "EEXIST") { - console.warn(`Module “${newPackageName}” already exists!`) - } else { - console.error(`Error creating module files: ${e}\nCleaning Up.`) - await Promise.all(files.map(([name]) => rm(name))) - await pify1(rmdir, newPackageName) - } - process.exit(1) - } -} - -async function getIndex() { - let version = "x.x" - let project = "https://github.com/baz/foo (Does not have to be to GitHub, but prefer linking to a source code repository rather than to a project website.)" - try { - const reg: Registry = JSON.parse(await loadString(`http://registry.npmjs.org/${newPackageName}`)) - - const { latest } = reg["dist-tags"] - const { homepage } = reg.versions[latest] - - version = latest.split(".").slice(0, 2).join(".") - if (homepage !== undefined) project = homepage - } catch (e) { - console.warn(`Warning: could not retrieve version/homepage information: ${e.message}`) - } - - return `// Type definitions for ${newPackageName} ${version} -// Project: ${project} -// Definitions by: My Self -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - -Fill the header in! -` -} - -function getTSConfig() { - return { - "compilerOptions": { - "module": "commonjs", - "lib": ["es6"], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - `${newPackageName}-tests.ts` - ] - } -} - -function write(name: string, content: string) { - return new Promise((resolve, reject) => { - writeFile(path.join(newPackageName, name), content, err => err ? reject(err): resolve()) - }) -} - -function rm(name: string) { - return pify1(unlink, path.join(newPackageName, name)).catch((e: NodeJS.ErrnoException) => { - if (e.code === "ENOENT") return - throw e - }) -} - -function pify1 void) => void, A>(fn: F, arg: A) { - return new Promise((resolve, reject) => { - fn(arg, err => err ? reject(err): resolve()) - }) -} - -function loadString(url: string): Promise { - return new Promise((resolve, reject) => { - get(url, (res) => { - if (res.statusCode !== 200) { - return reject(new Error(`HTTP Error ${res.statusCode}: ${STATUS_CODES[res.statusCode || 500]} for ${url}`)) - } - let rawData = "" - res.on("data", chunk => rawData += chunk) - res.on("end", () => resolve(rawData)) - }).on("error", e => reject(e)) - }) -}