-
Notifications
You must be signed in to change notification settings - Fork 26
/
version-copy-package.ts
85 lines (75 loc) · 2.96 KB
/
version-copy-package.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* eslint-disable no-console */
import fs from "fs/promises";
import process from "process";
import path from "path";
import { $ } from "zx";
async function copyFilesToDist(destDir: string) {
for (const file of ["./.gitignore", "./README.md", "./LICENSE.md"]) {
await fs.copyFile(file, path.join(destDir, file));
}
}
function getVersion() {
let version = "refs/tags/v0.0.0-smoke";
if (process.env.GITHUB_REF && process.env.GITHUB_REF.startsWith("refs/tags/v")) {
version = process.env.GITHUB_REF;
}
return version.split("/").slice(-1)[0].replace(/^v/, "");
}
async function patchVersion(packageJson: Record<string, unknown>) {
const version = getVersion();
console.log(`Patch version ${version} in package.json`);
packageJson.version = version;
}
async function createDenoJson(destDir: string, packageJson: Record<string, unknown>) {
const denoJson = {
imports: {
"@fireproof/core": "./index.js",
},
};
const pdeps = packageJson.dependencies as Record<string, string>;
for (const dep of Object.keys(pdeps)) {
denoJson.imports[dep] = `npm:${dep}@${pdeps[dep]}`;
}
const denoJsonFile = path.join(destDir, "deno.json");
await fs.writeFile(denoJsonFile, JSON.stringify(denoJson, null, 2));
}
async function transferVersionsFromPackageJson(srcDeps: Record<string, string>, destDeps: Record<string, string>) {
for (const dep of Object.keys(destDeps)) {
if (!srcDeps[dep]) {
console.error(`Dependency ${dep} not found in main package.json`);
} else {
destDeps[dep] = srcDeps[dep];
}
}
}
async function main() {
$.verbose = true;
const buildDest = process.argv[process.argv.length - 1];
if (!(buildDest.startsWith("dist/") || buildDest.startsWith("./dist/"))) {
console.error("Usage: tsx version-copy-package.ts dist/<path>/template-package.json");
process.exit(1);
}
const destDir = path.dirname(buildDest);
if (!(await fs.stat(destDir)).isDirectory) {
console.error(`Directory ${destDir} does not exist`);
process.exit(1);
}
await copyFilesToDist(destDir);
const mainPackageJson = JSON.parse(await fs.readFile("package.json", "utf8"));
const templateFile = path.basename(buildDest);
const destPackageJson = JSON.parse(await fs.readFile(templateFile, "utf-8"));
// copy version from package.json
const withCoreVersion = {
"@fireproof/core": `^${getVersion()}`,
...mainPackageJson.dependencies,
};
transferVersionsFromPackageJson(withCoreVersion, destPackageJson.dependencies);
transferVersionsFromPackageJson(withCoreVersion, destPackageJson.peerDependencies || {});
patchVersion(destPackageJson);
await createDenoJson(destDir, destPackageJson);
const destPackageJsonFile = path.join(destDir, "package.json");
await fs.writeFile(destPackageJsonFile, JSON.stringify(destPackageJson, null, 2));
console.log(`Copied ${templateFile} to ${destDir} with version ${destPackageJson.version}`);
await $`cd ${destDir} && pnpm pack`.pipe(process.stdout);
}
main().catch(console.error);