- 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
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
import workspacesRun from 'workspaces-run';
import path from 'path';
import fs from 'fs';
/** simplified interface for a package.json */
interface SimplePackageJson
{
location: string
name: string
dependencies: Record<string, string>
}
/**
* Adds global reference to the start of a packages `index.d.ts` file
* @param basePath
* @param dataToWrite
*/
function writeToIndex(basePath: string, dataToWrite: string): void
{
const indexDtsPath = path.resolve(basePath, './index.d.ts');
const file = fs.readFileSync(indexDtsPath, { encoding: 'utf8' }).toString().split('\n');
file.unshift(dataToWrite);
fs.writeFileSync(indexDtsPath, file.join('\n'));
}
/**
* Collect the list of packages in the project
* @param result
*/
async function getPackages(result: SimplePackageJson[] = []): Promise<SimplePackageJson[]>
{
await workspacesRun({ cwd: process.cwd() }, async (pkg) =>
{
result.push({
location: pkg.dir,
name: pkg.name,
dependencies: pkg.config.dependencies,
});
});
return result;
}
/**
* This is a workaround for https://github.com/pixijs/pixi.js/issues/6993
*
* All this script does is inject a path reference into a packages `index.d.ts` if a `global.d.ts`
* exists.
*/
async function start(): Promise<void>
{
let pixiLocation: string;
let pixiLegacyLocation: string;
let pixiGlobalMixins = '';
let pixiLegacyGlobalMixins = '';
const packages = await getPackages();
const legacyPackages = Object.keys(packages.find((pkg) => pkg.name === 'pixi.js-legacy').dependencies);
const pixiPackages = Object.keys(packages.find((pkg) => pkg.name === 'pixi.js').dependencies);
packages.forEach((pkg) =>
{
const basePath = path.relative(process.cwd(), pkg.location);
if (pkg.name === 'pixi.js')
{
pixiLocation = pkg.location;
return;
}
if (pkg.name === 'pixi.js-legacy')
{
pixiLegacyLocation = pkg.location;
return;
}
const globalDtsPath = path.resolve(basePath, './global.d.ts');
if (fs.existsSync(globalDtsPath))
{
const pixiTypeData = `/// <reference types="${pkg.name}" />\n`;
const packageTypeData = `/// <reference path="./global.d.ts" />\n`;
if (pixiPackages.includes(pkg.name))
{
pixiGlobalMixins += pixiTypeData;
}
else if (legacyPackages.includes(pkg.name))
{
pixiLegacyGlobalMixins += pixiTypeData;
}
writeToIndex(basePath, packageTypeData);
}
});
// write the total types to the main packages
let basePath = path.relative(process.cwd(), pixiLocation);
writeToIndex(basePath, pixiGlobalMixins);
basePath = path.relative(process.cwd(), pixiLegacyLocation);
writeToIndex(basePath, pixiLegacyGlobalMixins);
}
start();