Skip to content

Commit e929109

Browse files
committed
fix: cherry-pick
1 parent f76107a commit e929109

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

scripts/build.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import "./clean";
33
import { copyPackageSet } from "./tools/copyPackageSet";
44
import { generateExportsField } from "./tools/dualPackageSupport";
55
import { shell } from "./tools/shell";
6+
import { cherryPick } from "./tools/cherry-pick";
67
import * as fs from "fs";
8+
import { posix as path } from "path";
79

8-
const { default: cherryPick } = require("cherry-pick");
910

1011
const main = async () => {
1112
await Promise.all([
@@ -14,8 +15,7 @@ const main = async () => {
1415
shell("yarn tsc -p tsconfig.esm.json"),
1516
]);
1617

17-
await cherryPick({ inputDir: "../src", cwd: "./lib", typesDir: "./$types", cjsDir: "./$cjs", esmDir: "./$esm" });
18-
18+
await cherryPick({ inputDir: "../src", cwd: path.join(__dirname, "../lib"), typesDir: "./$types", cjsDir: "./$cjs", esmDir: "./$esm" });
1919

2020
const outputList = fs.readdirSync("./lib");
2121

scripts/tools/cherry-pick.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import * as fs from "fs";
2+
import { posix as path } from "path";
3+
import { promisify } from "util";
4+
5+
const readPkgUp = require("read-pkg-up");
6+
import glob from "tiny-glob";
7+
8+
const mkDir = promisify(fs.mkdir);
9+
const rimraf = promisify(require("rimraf"));
10+
const stat = promisify(fs.stat);
11+
const writeFile = promisify(fs.writeFile);
12+
13+
export interface Option {
14+
name?: string;
15+
inputDir: string;
16+
cwd: string;
17+
cjsDir: string;
18+
esmDir: string;
19+
typesDir?: string;
20+
}
21+
22+
23+
interface ProxyPackage {
24+
name: string;
25+
private: true;
26+
main: string;
27+
module: string;
28+
types?: string;
29+
}
30+
31+
const isFile = (path: string) =>
32+
stat(path)
33+
.then(stats => stats.isFile())
34+
.catch(() => false);
35+
36+
const withDefaults = ({ cwd = ".", ...options }: Option, additionalDefaults: { cjsDir?: string; esmDir?: string } = {}): Option => ({
37+
cwd: path.resolve(process.cwd(), cwd),
38+
...additionalDefaults,
39+
...options,
40+
});
41+
42+
const noop = () => {};
43+
44+
const findFiles = async ({ cwd, inputDir }: { cwd: string; inputDir: string }) => {
45+
const filePaths = await glob(path.join(inputDir, "!(index).{js,jsx,ts,tsx}"), { cwd });
46+
return filePaths.filter(f => !f.endsWith(".d.ts")).map(filePath => path.basename(filePath).replace(/\.(js|ts)x?$/, ""));
47+
};
48+
49+
const pkgCache = new WeakMap();
50+
51+
const getPkgName = async (options: Option) => {
52+
if (options.name != null) {
53+
return options.name;
54+
}
55+
if (pkgCache.has(options)) {
56+
return pkgCache.get(options);
57+
}
58+
const result = await readPkgUp({ cwd: options.cwd });
59+
if (!result) {
60+
throw new Error("Could not determine package name. No `name` option was passed and no package.json was found relative to: " + options.cwd);
61+
}
62+
const pkgName = result.pkg.name;
63+
pkgCache.set(options, pkgName);
64+
return pkgName;
65+
};
66+
67+
const fileProxy = async (options: Option, file?: string) => {
68+
const { cwd, cjsDir, esmDir, typesDir } = options;
69+
const pkgName = await getPkgName(options);
70+
71+
const proxyPkg: ProxyPackage = {
72+
name: `${pkgName}/${file}`,
73+
private: true,
74+
main: path.join("..", cjsDir, `${file}.js`),
75+
module: path.join("..", esmDir, `${file}.js`),
76+
};
77+
78+
if (typeof typesDir === "string") {
79+
proxyPkg.types = path.join("..", typesDir, `${file}.d.ts`);
80+
} else if (await isFile(path.join(cwd, `${file}.d.ts`))) {
81+
proxyPkg.types = path.join("..", `${file}.d.ts`);
82+
// try the esm path in case types are located with each
83+
} else if (await isFile(path.join(cwd, esmDir, `${file}.d.ts`))) {
84+
proxyPkg.types = path.join("..", esmDir, `${file}.d.ts`);
85+
}
86+
87+
return JSON.stringify(proxyPkg, null, 2) + "\n";
88+
};
89+
90+
export const cherryPick = async (inputOptions: Option) => {
91+
const options = withDefaults(inputOptions, {
92+
cjsDir: "lib",
93+
esmDir: "es",
94+
});
95+
96+
console.log(options);
97+
98+
const files = await findFiles(options);
99+
100+
console.log({ files });
101+
102+
await Promise.all(
103+
files.map(async file => {
104+
const proxyDir = path.join(options.cwd, file);
105+
await mkDir(proxyDir).catch(noop);
106+
107+
console.log(`Generate: ${proxyDir}/package.json`);
108+
await writeFile(`${proxyDir}/package.json`, await fileProxy(options, file));
109+
}),
110+
);
111+
112+
return files;
113+
};
114+
115+
export const clean = async (inputOptions: Option) => {
116+
const options = withDefaults(inputOptions);
117+
const files = await findFiles(options);
118+
await Promise.all(files.map(async file => rimraf(path.join(options.cwd, file))));
119+
return files;
120+
};

0 commit comments

Comments
 (0)