Skip to content

Commit 4538982

Browse files
committed
Merge branch 'v2'
2 parents ce974c6 + 61cc61d commit 4538982

File tree

79 files changed

+4238
-3183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+4238
-3183
lines changed

.github/workflows/publish.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ jobs:
2222
node-version: 14
2323
- run: npm ci
2424
- run: npm run build:tsc
25-
- run: npm run build:lib
26-
- run: npm test
2725
- run: npm run build:package
28-
- uses: JS-DevTools/npm-publish@v1
29-
with:
30-
token: ${{ secrets.NPM_TOKEN }}
31-
package: "./dist-package/package.json"
26+
- run: npm ci
27+
working-directory: ./tests
28+
- run: npm test
29+
working-directory: ./tests
30+
- run: node scripts/npm-publish.mjs
31+
env:
32+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
33+
NO_DRY_RUN: "1"

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ jobs:
2626
- run: npm ci
2727
- run: npm run build:tsc
2828
- run: npm run build:lib
29+
- run: npm ci
30+
working-directory: ./tests
2931
- run: npm test
32+
working-directory: ./tests

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules
22
/dist
3-
/dist-package
3+
/dist-package
4+
/tests/node_modules

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,27 @@ This package provides an alternative set of type definitions which replaces, and
1616

1717
This package also includes other improved, stricter type definitions.
1818

19-
## Usage
19+
## Installation
2020

21-
### Install
21+
You only need to install `better-typescript-lib`. Additional configuration is not needed; your TypeScript project automatically use `better-typescript-lib` definitions.
2222

23-
```sh
24-
npm i -D better-typescript-lib
25-
```
23+
Currently v2 is beta-released.
2624

27-
### Disable built-in library from tsconfig.json
28-
29-
```diff
30-
- "lib": ["es5", "dom"]
31-
+ "noLib": true
25+
```sh
26+
npm i -D better-typescript-lib@2.0.0-beta
3227
```
3328

34-
### Include better-typescript-lib
29+
### How it works
3530

36-
Include better-typescript-lib with triple-slash directives from the entry point of your code. Note that these directives must be placed at the very top of a `.ts` file.
37-
38-
```ts
39-
/// <reference path="./node_modules/better-typescript-lib/lib.es5.d.ts" />
40-
/// <reference path="./node_modules/better-typescript-lib/lib.dom.d.ts" />
41-
```
31+
Starting from TypeScript 4.5, the TypeScript compiler detects existence of `@typescript/xxx` packages (e.g. `@typescript/es2015`) and uses them instead of the built-in definitions. By installing `better-typescript-lib`, these package names are mapped to corresponding `@better-typescript-lib/xxx` packages.
4232

4333
## Supported TypeScript Versions
4434

4535
| better-typescript-lib | TypeScript |
4636
| --------------------- | --------------- |
47-
| 1.2.0 | TS 4.4 or later |
48-
| 1.1.0 | TS 4.2 or later |
49-
| 1.0.0 | TS 4.1 or later |
37+
| 2.0.0 | TS 4.5 or later |
38+
39+
For TS 4.4 and lower, see `v1` branch.
5040

5141
## Concepts
5242

TypeScript

Submodule TypeScript updated 2863 files

build/build.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { mkdir, readdir, rm, symlink, writeFile } from "fs/promises";
1+
import { mkdir, readdir, readFile, rm, writeFile } from "fs/promises";
22
import path from "path";
33
import ts from "typescript";
44
import { replacement } from "./replacement";
55

66
const projectDir = process.env.PROJECT || process.cwd();
7+
const betterLibDir = path.join(projectDir, "lib");
78
const distDir = path.join(projectDir, "generated");
89
const tsDir = path.join(projectDir, "TypeScript");
910

@@ -15,7 +16,6 @@ async function main() {
1516
await mkdir(distDir, {
1617
recursive: true,
1718
});
18-
await symlink(path.join(projectDir, "lib"), path.join(distDir, "better"));
1919

2020
// copy TypeScript lib files
2121
const tsLibDir = path.join(tsDir, "src", "lib");
@@ -32,10 +32,17 @@ async function main() {
3232
if (!file) {
3333
continue;
3434
}
35+
let result = "";
3536
const repl = replacement.get(libFile);
36-
let result = repl
37-
? `/// <reference path="./better/lib.${libFile}" />\n`
38-
: "";
37+
if (repl) {
38+
// copy better lib into the top of the file
39+
result += await readFile(
40+
path.join(betterLibDir, `lib.${libFile}`),
41+
"utf8"
42+
);
43+
result += "// --------------------\n";
44+
}
45+
3946
if (!repl) {
4047
for (const statement of file.statements) {
4148
result += statement.getFullText(file);
@@ -53,11 +60,6 @@ async function main() {
5360
}
5461
}
5562
result += file.text.slice(file.endOfFileToken.pos);
56-
// Replace <reference lib="" /> to <reference path="" />
57-
result = result.replace(
58-
/^\/\/\/\s*<reference\s+lib="(.+?)"\s*\/>/gm,
59-
(_, lib) => `/// <reference path="./lib.${lib}.d.ts" />`
60-
);
6163

6264
await writeFile(path.join(distDir, "lib." + libFile), result);
6365
console.log(libFile);

build/package.ts

Lines changed: 156 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,175 @@
1-
import { mkdir, readdir, readFile, rm, writeFile } from "fs/promises";
1+
import { mkdir, readdir, readFile, rm, symlink, writeFile } from "fs/promises";
22
import path from "path";
33

4+
const scope = "better-typescript-lib";
5+
46
const projectDir = process.env.PROJECT || process.cwd();
57
const libDir = path.join(projectDir, "generated");
6-
const betterDir = path.join(projectDir, "lib");
78
const templateDir = path.join(projectDir, "package-template");
89
const packageDir = path.join(projectDir, "dist-package");
10+
const testsDir = path.join(projectDir, "tests");
911

1012
async function main() {
1113
await rm(packageDir, {
1214
force: true,
1315
recursive: true,
1416
});
15-
await mkdir(path.join(packageDir, "better"), {
17+
await mkdir(packageDir, {
1618
recursive: true,
1719
});
1820

19-
// copy package template
20-
// copy lib files
21-
const files = (await readdir(libDir))
22-
.filter((libFile) => /\.d\.ts$/.test(libFile))
23-
.map((libFile) => path.join(libDir, libFile))
24-
.concat(
25-
(await readdir(templateDir)).map((file) => path.join(templateDir, file))
26-
)
27-
.concat([
28-
path.join(projectDir, "README.md"),
29-
path.join(projectDir, "LICENSE"),
30-
]);
31-
await Promise.all(
32-
files
33-
.map(async (libFile) => {
34-
await writeFile(
35-
path.join(packageDir, path.basename(libFile)),
36-
await readFile(libFile)
37-
);
38-
})
39-
.concat(
40-
(await readdir(betterDir)).map(async (libFile) => {
41-
await writeFile(
42-
path.join(packageDir, "better", libFile),
43-
await readFile(path.join(betterDir, libFile))
44-
);
45-
})
46-
)
21+
// Read version from root package.json
22+
const rootPackageJson = JSON.parse(
23+
await readFile(path.join(projectDir, "package.json"), "utf-8")
24+
);
25+
const rootPackageName: string = rootPackageJson.name;
26+
const version: string = rootPackageJson.version;
27+
28+
const packageTemplateFiles = await readdir(templateDir);
29+
30+
const libFiles = (await readdir(libDir)).filter((libFile) =>
31+
/\.d\.ts$/.test(libFile)
4732
);
33+
34+
const packageNames = new Set<string>();
35+
for (const libFile of libFiles) {
36+
console.log(`Processing ${libFile}`);
37+
const libFilePath = path.join(libDir, libFile);
38+
const { packageName, packagePath } =
39+
getPackageNameAndPathOfLib(libFilePath);
40+
packageNames.add(packageName);
41+
42+
const alreadyExisted = await mkdir(path.join(packageDir, packageName))
43+
.then(() => false)
44+
.catch((err) => {
45+
if (err.code !== "EEXIST") {
46+
throw err;
47+
}
48+
return true;
49+
});
50+
if (!alreadyExisted) {
51+
// install package template files
52+
await installPackageTemplateFiles(path.join(packageDir, packageName));
53+
// update package.json
54+
const packageJsonPath = path.join(
55+
packageDir,
56+
packageName,
57+
"package.json"
58+
);
59+
await writeToPackageJson(packageJsonPath, () => ({
60+
name: toScopedPackageName(packageName),
61+
version,
62+
}));
63+
}
64+
65+
// copy lib file
66+
const targetPath = path.join(packageDir, packageName, packagePath);
67+
const targetDir = path.dirname(targetPath);
68+
await mkdir(targetDir, { recursive: true });
69+
await writeFile(targetPath, await readFile(libFilePath));
70+
}
71+
// Prepare "main" package
72+
{
73+
const mainPackageDir = path.join(packageDir, "__main");
74+
await mkdir(mainPackageDir);
75+
76+
// install package template files
77+
await installPackageTemplateFiles(mainPackageDir);
78+
// copy root README to main package
79+
await writeFile(
80+
path.join(mainPackageDir, "README.md"),
81+
await readFile(path.join(projectDir, "README.md"))
82+
);
83+
// update package.json
84+
const packageJsonPath = path.join(mainPackageDir, "package.json");
85+
await writeToPackageJson(packageJsonPath, () => ({
86+
name: rootPackageName,
87+
version,
88+
types: "./dist/index.d.ts",
89+
dependencies: Object.fromEntries(
90+
[...packageNames].map((packageName) => [
91+
`@typescript/lib-${packageName}`,
92+
`npm:${toScopedPackageName(packageName)}@${version}`,
93+
])
94+
),
95+
}));
96+
// prepare symlink to dist
97+
await symlink(
98+
path.join(projectDir, "dist"),
99+
path.join(mainPackageDir, "dist")
100+
);
101+
}
102+
// update package.json in "tests" folder
103+
{
104+
const packageJsonPath = path.join(testsDir, "package.json");
105+
await writeToPackageJson(packageJsonPath, (original) => ({
106+
dependencies: {
107+
...original.dependencies,
108+
...Object.fromEntries(
109+
[...packageNames].map((packageName) => [
110+
`@typescript/lib-${packageName}`,
111+
`file:${path.relative(
112+
path.dirname(packageJsonPath),
113+
path.join(packageDir, packageName)
114+
)}`,
115+
])
116+
),
117+
},
118+
}));
119+
}
120+
121+
function installPackageTemplateFiles(dir: string) {
122+
return Promise.all(
123+
packageTemplateFiles.map(async (file) => {
124+
const filePath = path.join(templateDir, file);
125+
const targetPath = path.join(dir, file);
126+
return writeFile(targetPath, await readFile(filePath));
127+
})
128+
);
129+
}
130+
async function writeToPackageJson(
131+
packageJsonPath: string,
132+
updates: (original: any) => Record<string, unknown>
133+
) {
134+
const original = JSON.parse(await readFile(packageJsonPath, "utf-8"));
135+
return writeFile(
136+
packageJsonPath,
137+
JSON.stringify(
138+
{
139+
...original,
140+
...updates(original),
141+
},
142+
null,
143+
2
144+
) + "\n"
145+
);
146+
}
147+
function toScopedPackageName(packageName: string) {
148+
return `@${scope}/${packageName}`;
149+
}
150+
}
151+
152+
function getPackageNameAndPathOfLib(libFile: string) {
153+
const libFileName = path.basename(libFile, ".d.ts");
154+
const components = libFileName.split(".");
155+
156+
// lib.dom.d.ts -> @typescript/lib-dom
157+
// lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
158+
// lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
159+
160+
if (components[0] !== "lib") {
161+
throw new Error(`Invalid lib file: ${libFile}`);
162+
}
163+
164+
const packageName = components[1];
165+
const packagePath =
166+
components.length > 2
167+
? components.slice(2).join("-") + ".d.ts"
168+
: "index.d.ts";
169+
return {
170+
packageName,
171+
packagePath,
172+
};
48173
}
49174

50175
main().catch((err) => {

generated/better

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)