From 23c73fff46c5f2339fd095dc6b39da3d9e6df23c Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Sun, 6 Nov 2016 10:09:23 -0800 Subject: [PATCH] Add script to generate new package --- README.md | 39 ++--------------------------- scripts/new-package.ts | 45 ++++++++++++++++++++++++++++++++++ scripts/rename-entry-points.ts | 4 +-- scripts/tsconfig.json | 8 ++---- 4 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 scripts/new-package.ts diff --git a/README.md b/README.md index 7da7d1530b..5c5ca5f4c6 100644 --- a/README.md +++ b/README.md @@ -109,43 +109,8 @@ Your package should have this structure: | foo-tests.ts | This contains sample code which tests the typings. This code does *not* run, but it is type-checked. | | tsconfig.json | This allows you to run `tsc` within the package. | -`index.d.ts` should start with a header looking like: - -```ts -// Type definitions for foo 1.2 -// Project: https://github.com/baz/foo -// Definitions by: My Self -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -``` - -The `Project` link does not have to be to GitHub, but prefer linking to a source code repository rather than to a project website. - -`tsconfig.json` should look like this: - -```json -{ - "compilerOptions": { - "module": "commonjs", - "target": "es6", - "noImplicitAny": true, - "strictNullChecks": false, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "foo-tests.ts" - ] -} -``` - -These should be identical accross projects except that `foo-tests` will be replaced with the name of your test file, -and you may also add the `"jsx"` compiler option if your library needs it. +Generate these by running `tsc -p scripts`, then `node scripts/new-package.js new-package-name`. +You may edit the `tsconfig.json` to add new files or to add the `"jsx"` compiler option. DefinitelyTyped members routinely monitor for new PRs, though keep in mind that the number of other PRs may slow things down. diff --git a/scripts/new-package.ts b/scripts/new-package.ts new file mode 100644 index 0000000000..69e914ffea --- /dev/null +++ b/scripts/new-package.ts @@ -0,0 +1,45 @@ +/// + +import { mkdirSync, writeFileSync } from "fs"; +import * as path from "path"; + +const newPackageName = process.argv[2]; +if (!newPackageName) { + throw new Error("Usage: node scripts/new-package.js new-package-name") +} + +mkdirSync(newPackageName); + +write("index.d.ts", `// Type definitions for ${newPackageName} 1.2 +// 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.) +// Definitions by: My Self +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +Fill the header in! +`); +const testsFile = `${newPackageName}-tests.ts`; +write(testsFile, ""); +const tsconfig = { + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + `${newPackageName}-tests.ts` + ] +}; +write("tsconfig.json", JSON.stringify(tsconfig, undefined, 4)); + +function write(name: string, content: string) { + writeFileSync(path.join(newPackageName, name), content); +} diff --git a/scripts/rename-entry-points.ts b/scripts/rename-entry-points.ts index e1ea0c51d3..14fce05ce3 100644 --- a/scripts/rename-entry-points.ts +++ b/scripts/rename-entry-points.ts @@ -25,7 +25,7 @@ function checkDir(home: string, count: number) { if (exists) { const old = JSON.parse(fs.readFileSync(target, 'utf-8')); - let entryPoint: string = undefined; + let entryPoint: string; let definitionFiles = files.filter(f => (f.indexOf('.d.ts') > 0)); if (definitionFiles.length === 1) { entryPoint = definitionFiles[0]; @@ -41,7 +41,7 @@ function checkDir(home: string, count: number) { return; } - let testFile = path.join(dir, path.basename(dir) + '-tests.ts'); + let testFile: string | undefined = path.join(dir, path.basename(dir) + '-tests.ts'); if (!fs.existsSync(testFile)) { let onlyTest = files.filter(f => f.toLowerCase().indexOf('-tests.ts') > 0)[0]; if (onlyTest) { diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 79b9eb6b27..f0f0258c77 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -3,16 +3,12 @@ "module": "commonjs", "target": "es6", "noImplicitAny": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" ], "types": [], - "noEmit": true, "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts" - ] + } } \ No newline at end of file