Skip to content

Commit

Permalink
base index.d.ts compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpopelyshev committed Apr 10, 2021
1 parent 0b93aef commit 577459e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ common/temp/
common/autoinstallers/*/.npmrc
**/.rush/temp/
**/*.build.log
compile

# packages ignore
lib
2 changes: 1 addition & 1 deletion packages/base/api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "<projectFolder>/compile/index.d.ts",
"bundledPackages": [
"@pixi-spine/area-allocator"
"@pixi-spine/base"
],
"compiler": {
"tsconfigFilePath": "<projectFolder>/tsconfig.json"
Expand Down
1 change: 1 addition & 0 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"homepage": "https://github.com/pixijs/pixi-spine/#readme",
"devDependencies": {
"@microsoft/api-extractor": "7.13.4",
"rollup": "~2.33.1",
"@pixi-spine/eslint-config": "~1.0.0",
"eslint": "~7.13.0",
Expand Down
2 changes: 1 addition & 1 deletion rush.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
"rushVersion": "5.29.1",
"rushVersion": "5.43.0",
"pnpmVersion": "4.14.4",
"pnpmOptions": {
"pnpmStore": "global",
Expand Down
104 changes: 104 additions & 0 deletions scripts/injectGlobalMixins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { getPackages } from '@lerna/project';
import batchPackages from '@lerna/batch-packages';
import filterPackages from '@lerna/filter-packages';
import path from 'path';
import fs from 'fs';

/**
* simplified interface for a package.json
*/
interface SimplePackageJson
{
location: string
name: string
dependencies: Record<string, string>
}

/**
* Gets all the non-private packages package.json data
*/
async function getSortedPackages(): Promise<SimplePackageJson[]>
{
const packages = await getPackages(process.cwd());

const filtered = filterPackages(packages, undefined, undefined, false);

return batchPackages(filtered)
.reduce((arr: SimplePackageJson[], batch: SimplePackageJson[]) => arr.concat(batch), []);
}

/**
* Adds global reference to the start of a packages `index.d.ts` file
*/
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'));
}

/**
* 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 getSortedPackages();
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();

0 comments on commit 577459e

Please sign in to comment.