2020-09-17 10:39:25 +03:00
|
|
|
import fs from "fs";
|
2020-09-21 08:14:06 +03:00
|
|
|
import { resolve as _resolve } from "path";
|
2020-09-17 10:39:25 +03:00
|
|
|
|
2020-09-21 08:14:06 +03:00
|
|
|
export const root: string = _resolve(__dirname, "../../../");
|
2020-09-17 10:39:25 +03:00
|
|
|
|
2020-09-21 08:14:06 +03:00
|
|
|
export function resolve(...args: Array<string>): string {
|
|
|
|
args.unshift(root);
|
|
|
|
return _resolve.apply(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathRootPackageJsonPath: string = resolve("package.json");
|
|
|
|
const pathPackages: string = resolve("packages");
|
2020-09-17 10:39:25 +03:00
|
|
|
|
|
|
|
export const dirs = Object.freeze({
|
|
|
|
rootPackageJsonPath: pathRootPackageJsonPath,
|
|
|
|
packages: pathPackages,
|
|
|
|
root,
|
|
|
|
});
|
|
|
|
|
2020-09-21 08:14:06 +03:00
|
|
|
|
2020-09-17 10:39:25 +03:00
|
|
|
type PackageInfo = {
|
|
|
|
dirname: string;
|
|
|
|
packageName: string;
|
|
|
|
packagePath: string;
|
|
|
|
packageJsonPath: string;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
2020-09-23 05:54:48 +03:00
|
|
|
export const dirnames: ReadonlyArray<string> = Object.freeze(fs.readdirSync(dirs.packages).filter((dirname) => {
|
|
|
|
return (dirname[0] !== ".");
|
|
|
|
}));
|
2020-09-17 10:39:25 +03:00
|
|
|
|
|
|
|
const packageLookup = dirnames.reduce((accum, dirname) => {
|
2020-09-21 08:14:06 +03:00
|
|
|
const packagePath = _resolve(dirs.packages, dirname);
|
|
|
|
const packageJsonPath = _resolve(packagePath, "package.json");
|
2020-09-17 10:39:25 +03:00
|
|
|
|
|
|
|
const info = JSON.parse(fs.readFileSync(packageJsonPath).toString());
|
|
|
|
const packageName = info.name;
|
|
|
|
const version = info.version;
|
|
|
|
|
|
|
|
accum[packageName] = accum[dirname] = {
|
|
|
|
dirname, packageName, packagePath, packageJsonPath, version
|
|
|
|
};
|
|
|
|
|
|
|
|
return accum;
|
|
|
|
}, <{ [ name: string ]: PackageInfo }>{ });
|
|
|
|
|
|
|
|
export const packages: ReadonlyArray<string> = Object.freeze(dirnames.map((dirname) => packageLookup[dirname].packageName));
|
|
|
|
|
|
|
|
function getPackageInfo(name: string): PackageInfo {
|
|
|
|
const value = packageLookup[name];
|
|
|
|
if (!value) { throw new Error(`unknown package: ${ name }`); }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPackagePath(name: string): string {
|
|
|
|
return getPackageInfo(name).packagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDirname(name: string): string {
|
|
|
|
return getPackageInfo(name).dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPackageJsonPath(name: string): string {
|
|
|
|
return getPackageInfo(name).packageJsonPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isEthers(name: string) {
|
|
|
|
return !!packageLookup[name];
|
|
|
|
}
|