Skip to content

Commit d7ff118

Browse files
committed
feat: generate package.json for tests
1 parent 74b9303 commit d7ff118

File tree

4 files changed

+1077
-13
lines changed

4 files changed

+1077
-13
lines changed

.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

build/package.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const projectDir = process.env.PROJECT || process.cwd();
77
const libDir = path.join(projectDir, "generated");
88
const templateDir = path.join(projectDir, "package-template");
99
const packageDir = path.join(projectDir, "dist-package");
10+
const testsDir = path.join(projectDir, "tests");
1011

1112
async function main() {
1213
await rm(packageDir, {
@@ -30,14 +31,13 @@ async function main() {
3031
/\.d\.ts$/.test(libFile)
3132
);
3233

33-
const packageNames: string[] = [];
34+
const packageNames = new Set<string>();
3435
for (const libFile of libFiles) {
3536
console.log(`Processing ${libFile}`);
3637
const libFilePath = path.join(libDir, libFile);
3738
const { packageName, packagePath } =
3839
getPackageNameAndPathOfLib(libFilePath);
39-
const p = `@${scope}/${packageName}`;
40-
packageNames.push(p);
40+
packageNames.add(packageName);
4141

4242
const alreadyExisted = await mkdir(path.join(packageDir, packageName))
4343
.then(() => false)
@@ -56,10 +56,10 @@ async function main() {
5656
packageName,
5757
"package.json"
5858
);
59-
await writeToPackageJson(packageJsonPath, {
60-
name: p,
59+
await writeToPackageJson(packageJsonPath, () => ({
60+
name: toScopedPackageName(packageName),
6161
version,
62-
});
62+
}));
6363
}
6464

6565
// copy lib file
@@ -82,18 +82,41 @@ async function main() {
8282
);
8383
// update package.json
8484
const packageJsonPath = path.join(mainPackageDir, "package.json");
85-
await writeToPackageJson(packageJsonPath, {
85+
await writeToPackageJson(packageJsonPath, () => ({
8686
name: rootPackageName,
8787
version,
8888
types: "./dist/index.d.ts",
89-
dependencies: Object.fromEntries(packageNames.map((p) => [p, version])),
90-
});
89+
dependencies: Object.fromEntries(
90+
[...packageNames].map((packageName) => [
91+
`@typescript/${packageName}`,
92+
`npm:${toScopedPackageName(packageName)}@${version}`,
93+
])
94+
),
95+
}));
9196
// prepare symlink to dist
9297
await symlink(
9398
path.join(projectDir, "dist"),
9499
path.join(mainPackageDir, "dist")
95100
);
96101
}
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/${packageName}`,
111+
`file:${path.relative(
112+
path.dirname(packageJsonPath),
113+
path.join(packageDir, packageName)
114+
)}`,
115+
])
116+
),
117+
},
118+
}));
119+
}
97120

98121
function installPackageTemplateFiles(dir: string) {
99122
return Promise.all(
@@ -106,20 +129,24 @@ async function main() {
106129
}
107130
async function writeToPackageJson(
108131
packageJsonPath: string,
109-
obj: Record<string, unknown>
132+
updates: (original: any) => Record<string, unknown>
110133
) {
134+
const original = JSON.parse(await readFile(packageJsonPath, "utf-8"));
111135
return writeFile(
112136
packageJsonPath,
113137
JSON.stringify(
114138
{
115-
...JSON.parse(await readFile(packageJsonPath, "utf-8")),
116-
...obj,
139+
...original,
140+
...updates(original),
117141
},
118142
null,
119143
2
120144
) + "\n"
121145
);
122146
}
147+
function toScopedPackageName(packageName: string) {
148+
return `@${scope}/${packageName}`;
149+
}
123150
}
124151

125152
function getPackageNameAndPathOfLib(libFile: string) {

0 commit comments

Comments
 (0)