forked from handsontable/handsontable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-packages.mjs
More file actions
143 lines (123 loc) · 5.26 KB
/
link-packages.mjs
File metadata and controls
143 lines (123 loc) · 5.26 KB
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
/**
* Link the root-level monorepo packages to the framework directories of the examples monorepo.
*/
import fse from 'fs-extra';
import path from 'path';
import glob from 'glob';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { fileURLToPath } from 'url';
import {
displayConfirmationMessage,
displayErrorMessage,
displayWarningMessage
} from '../../scripts/utils/index.mjs';
import examplesPackageJson from '../package.json' with { type: 'json' };
import mainPackageJson from '../../package.json' with { type: 'json' };
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const exampleFrameworkSubdirs = examplesPackageJson.internal.framework_dirs;
const hotWorkspaces = mainPackageJson.workspaces;
const isPackageRequired = (packageName, packageLocation) => {
const frameworkName = packageName.split('/').pop() || null;
const isLegacyAngularExample = packageLocation.includes('/angular-9/') || packageLocation.includes('/angular-10/');
return (
// If the required package is handsontable
packageName === 'handsontable' ||
packageLocation.includes(frameworkName) ||
// If the required package is @handsontable/angular-wrapper
(frameworkName === 'angular' && packageName === '@handsontable/angular-wrapper' && !isLegacyAngularExample) ||
// If it's in the framework directory
packageLocation.split('/').pop().includes(frameworkName) ||
// If it's deeper in the framework directory
packageLocation.includes(`/${frameworkName}/`)
);
};
const packagesToLink = [];
const linkPackage = (sourceLocation, linkLocation, packageName, exampleDir = false) => {
const mainDependencyLocationPath = `${sourceLocation}/${packageName}`;
const destinationDependencyLocationPath = `${linkLocation}/${packageName}`;
if (isPackageRequired(packageName, linkLocation) && fse.pathExistsSync(path.resolve(mainDependencyLocationPath))) {
try {
const destinationPath = path.resolve(destinationDependencyLocationPath);
// Check if destination exists and remove it appropriately
if (fse.pathExistsSync(destinationPath)) {
const stats = fse.lstatSync(destinationPath);
if (stats.isSymbolicLink() || stats.isFile()) {
// Remove symlinks and files with unlinkSync
fse.unlinkSync(destinationPath);
} else if (stats.isDirectory()) {
// Remove directories with removeSync
fse.removeSync(destinationPath);
}
}
fse.ensureSymlinkSync(
path.resolve(mainDependencyLocationPath),
destinationPath,
'junction',
);
} catch (e) {
displayErrorMessage(e);
process.exit(1);
}
displayConfirmationMessage(`${exampleDir ? '\t' : ''}Symlink created for ${packageName} in ${
linkLocation.replace(path.resolve(__dirname, '..'), '').replace('/node_modules', '')}.`);
}
};
const argv = yargs(hideBin(process.argv))
.describe('examples-version', 'Version of the examples package to do the linking in.')
.alias('f', 'framework')
.array('f')
.describe('f', 'Target framework to the linking to take place in. Defaults to none.')
.argv;
if (!argv.f) {
displayWarningMessage('No frameworks passed as a `-f` argument, exiting.');
process.exit(0);
}
for (const hotPackageGlob of hotWorkspaces) {
const mainPackages = glob.sync(`../${hotPackageGlob}`);
for (const mainPackageUrl of mainPackages) {
const { default: packagePackageJson } = await import(`../${mainPackageUrl}/package.json`, { with: { type: 'json' } });
const packageName = packagePackageJson.name;
packagesToLink.push(packageName);
}
}
exampleFrameworkSubdirs.forEach((packagesLocation) => {
const subdirs = glob.sync(`./${packagesLocation}`);
subdirs.forEach((packageLocation) => {
const frameworkLocationName = packageLocation.split('/').pop();
if (
packageLocation.startsWith(`./${argv.examplesVersion}`) &&
((argv.framework && argv.framework.includes(frameworkLocationName)) ||
!argv.framework)
) {
// Currently linking the live dependencies only for the 'next' directory.
if (argv.examplesVersion.startsWith('next')) {
packagesToLink.forEach((packageName) => {
linkPackage(
path.resolve('./node_modules'),
path.resolve(packageLocation, './node_modules'),
packageName
);
});
}
// Additional linking to all the examples for Angular (required to load css files from `angular.json`)
if (/^angular(-(\d+|next|wrapper))?$/.test(frameworkLocationName)) {
const angularPackageJson = fse.readJSONSync(`${packageLocation}/package.json`);
const workspacesList = angularPackageJson?.workspaces.packages || angularPackageJson?.workspaces;
workspacesList.forEach((angularPackagesLocation) => {
const angularPackageDirs = glob.sync(`${packageLocation}/${angularPackagesLocation}`);
angularPackageDirs.forEach((angularPackageLocation) => {
packagesToLink.forEach((packageName) => {
linkPackage(
path.resolve(angularPackageLocation, '../node_modules'),
path.resolve(angularPackageLocation, './node_modules'),
packageName,
true
);
});
});
});
}
}
});
});