This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
216 lines (189 loc) · 6.83 KB
/
index.mjs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import fs from 'fs-extra';
import path from 'path';
import { execSync } from 'child_process';
import replaceInFiles from 'replace-in-files';
import minimist from 'minimist';
/** Represents a single package. */
class Package {
constructor(parent, name, version, basePath) {
this.parent = parent;
this.name = name;
this.version = version;
this.basePath = basePath ?? path.resolve('node_modules', this.name);
this.info = null;
this.dependencies = null;
this.external = !this.name.startsWith('@pixi/') && !this.name.startsWith('pixi.js');
this.libPath = !this.external ? path.join(this.basePath, 'lib') : null;
this.targetName = Package.aliases[this.name] || '';
this.targetRelativeName = this.targetName.slice(this.targetName.indexOf('/') + 1);
this.targetPath = this.targetRelativeName.replace(/\//g, '-');
}
async load() {
this.info = await fs.readJson(path.join(this.basePath, 'package.json'));
this.dependencies = await Promise.all(
Object.keys(this.info.dependencies || {})
.map(name => Package.create(this, name, this.info.dependencies[name]))
);
}
/** Get the export inputs */
getExports() {
return { ['./' + this.targetRelativeName]: JSON.parse(
JSON.stringify(this.info.exports['.'])
.replace(/\/lib\//g, `/${this.targetPath}/`)
)
};
}
/** Copy the library the output destination */
async copy(dest) {
const targetPath = path.join(dest, this.targetPath);
await fs.copy(this.libPath, targetPath);
// Copy the global.d.ts file if we have it
const globalPath = path.join(this.basePath, 'global.d.ts');
if (await fs.pathExists(globalPath)) {
await fs.copy(globalPath, path.join(targetPath, 'global.d.ts'));
await replaceInFiles({
files: [path.join(targetPath, 'index.d.ts')],
from: '../global.d.ts',
to: './global.d.ts',
});
}
}
/** The cache of all packages found in dependency tree */
static packages = new Map();
/** Map of old packages names to new package names */
static aliases = null;
/**
* Create a new package data object.
* @param {Package|null} parent - Parent reference
* @param {string} packageName - Name of the package
* @param {string} version - Version range required in package.json
* @param {string} [basePath] - Base path directory auto resolved from node_modules if undefined
* @returns
*/
static async create(parent, packageName, version, basePath) {
if (!Package.aliases) {
throw new Error('Package aliases not loaded');
}
if (!this.packages.has(packageName)) {
const pkg = new Package(parent, packageName, version, basePath);
await pkg.load();
Package.packages.set(packageName, pkg);
}
return Package.packages.get(packageName);
}
}
// Optional override the default version
const { version, dryrun, tag } = minimist(process.argv.slice(2), {
string: ['version', 'tag'],
boolean: ['dryrun'],
default: {
version: null,
dryrun: false,
tag: 'latest',
}
});
// Create the output directory
console.log('Create output directory');
const outputPath = path.resolve('dist');
await fs.remove(outputPath);
await fs.copy(path.resolve('src'), outputPath);
Package.aliases = await fs.readJson(path.resolve(outputPath, 'aliases.json'));
// Create a collection of packages
console.log('Source all packages');
await Package.create(null, '.', '*', process.cwd());
// Remove the root package
Package.packages.delete('.');
// Get the packages
const packages = Array.from(Package.packages.values());
// Generate all of the external dependencies
const dependencies = packages
.filter(pkg => pkg.external && !pkg.parent.external)
.reduce((acc, pkg) => ({...acc, [pkg.name]: pkg.version}), {});
const libraries = packages.filter(pkg => !pkg.external);
// Update the dist package.json
console.log('Generate package.json');
const defaultPackage = packages.find(pkg => pkg.name === 'pixi.js');
const publishInfo = await fs.readJson(path.resolve(outputPath, 'package.json'));
Object.assign(
publishInfo, {
dependencies,
version: version ?? defaultPackage.info.version,
exports: libraries
.reduce((acc, pkg) => ({...acc, ...pkg.getExports() }),
publishInfo.exports
),
files: [...publishInfo.files, ...libraries.map(pkg => pkg.targetPath)].sort(),
}
);
publishInfo.exports['.'] = publishInfo.exports['./browser'];
// Sort export alphabetically
publishInfo.exports = Object.keys(publishInfo.exports).sort()
.reduce((acc, key) => ({ ...acc, [key]: publishInfo.exports[key] }), {});
// Create the public package.json
await fs.writeJSON(path.join(outputPath, 'package.json'), publishInfo, { spaces: 2 });
// Copy all the library folders
console.log('Copy all library files');
await Promise.all(libraries.map(pkg => pkg.copy(outputPath)));
const baseReplaceOptions = {
files: outputPath + '/*/**/*.{mjs,js,ts,map}',
optionsForFiles: {
"ignore": [
"package.json",
"aliases.json",
"README.md"
]
}
};
// Replace internal package name requests
console.log('Patch internal package names');
// Convert @pixi/filter-alpha to pixijs/filter/alpha
await replaceInFiles({
...baseReplaceOptions,
from: /@pixi\/filter-([\w-]+)/g,
to: `${publishInfo.name}/filter/$1`,
});
// Convert @pixi/math-extras to pixijs/math/extras
await replaceInFiles({
...baseReplaceOptions,
from: /@pixi\/(mesh|graphics|math)-(extras)/g,
to: `${publishInfo.name}/$1/$2`,
});
// Convert @pixi/mixin-cache-as-bitmap to pixijs/display/cache-as-bitmap
await replaceInFiles({
...baseReplaceOptions,
from: /@pixi\/mixin-([\w-]+)/g,
to: `${publishInfo.name}/display/$1`,
});
// Convert @pixi/canvas-sprite to pixijs/sprite/canvas
await replaceInFiles({
...baseReplaceOptions,
from: /@pixi\/(canvas)-([\w-]+)/g,
to: `${publishInfo.name}/$2/$1`,
});
// Convert all other packages to pixijs using the same name
await replaceInFiles({
...baseReplaceOptions,
from: /@pixi\//g,
to: `${publishInfo.name}/`,
});
// Change pixi.js-legacy to new package
await replaceInFiles({
...baseReplaceOptions,
from: /pixi.js-legacy/g,
to: Package.aliases['pixi.js-legacy'],
});
// Change pixi.js to new package name
await replaceInFiles({
...baseReplaceOptions,
from: /pixi.js/g,
to: Package.aliases['pixi.js'],
});
if (dryrun) {
console.log('Generating output');
execSync('npm pack', { cwd: outputPath });
}
else {
console.log('Publishing output');
execSync(`npm publish --tag ${tag}`, { cwd: outputPath });
}
console.log('Done');