-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlink-packages.js
More file actions
executable file
·202 lines (169 loc) · 6.27 KB
/
link-packages.js
File metadata and controls
executable file
·202 lines (169 loc) · 6.27 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
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
/**
* Script to link all packages in the packages folder using npm link
* and then link dependencies between them
*/
const PACKAGES_DIR = path.join(__dirname, 'packages');
const PLUGIN_FLEX_DIR = path.join(PACKAGES_DIR, 'plugin-flex');
// Color codes for better output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m'
};
function log(message, color = colors.reset) {
console.log(`${color}${message}${colors.reset}`);
}
function execCommand(command, cwd = process.cwd()) {
try {
log(`${colors.cyan}Executing: ${command}${colors.reset}`, colors.cyan);
const result = execSync(command, {
cwd,
stdio: 'inherit',
encoding: 'utf8'
});
return result;
} catch (error) {
log(`${colors.red}Error executing: ${command}${colors.reset}`, colors.red);
log(`${colors.red}${error.message}${colors.reset}`, colors.red);
throw error;
}
}
function getPackageInfo(packagePath) {
const packageJsonPath = path.join(packagePath, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
return null;
}
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
return {
name: packageJson.name,
dependencies: {
...packageJson.dependencies,
...packageJson.devDependencies
}
};
} catch (error) {
log(`${colors.red}Error reading package.json for ${packagePath}: ${error.message}${colors.reset}`, colors.red);
return null;
}
}
function getAllPackages() {
const packages = [];
const packageDirs = fs.readdirSync(PACKAGES_DIR, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
for (const dir of packageDirs) {
const packagePath = path.join(PACKAGES_DIR, dir);
const packageInfo = getPackageInfo(packagePath);
if (packageInfo) {
packages.push({
...packageInfo,
path: packagePath,
directory: dir
});
}
}
return packages;
}
function createGlobalLinks(packages) {
log(`${colors.bright}${colors.yellow}Step 1: Creating global npm links for all packages...${colors.reset}`, colors.yellow);
for (const pkg of packages) {
log(`${colors.green}Creating global link for ${pkg.name}...${colors.reset}`, colors.green);
try {
execCommand('npm link', pkg.path);
log(`${colors.green}✓ Successfully linked ${pkg.name}${colors.reset}`, colors.green);
} catch (error) {
log(`${colors.red}✗ Failed to link ${pkg.name}${colors.reset}`, colors.red);
// Continue with other packages
}
}
}
function linkDependencies(packages) {
log(`${colors.bright}${colors.yellow}Step 2: Linking package dependencies...${colors.reset}`, colors.yellow);
// Create a map of package names for quick lookup
const packageNameMap = new Map();
packages.forEach(pkg => packageNameMap.set(pkg.name, pkg));
for (const pkg of packages) {
log(`${colors.blue}Processing dependencies for ${pkg.name}...${colors.reset}`, colors.blue);
const dependencies = pkg.dependencies || {};
const localDependencies = [];
// Find dependencies that are also local packages
for (const [depName, depVersion] of Object.entries(dependencies)) {
if (packageNameMap.has(depName)) {
localDependencies.push(depName);
}
}
if (localDependencies.length > 0) {
log(`${colors.magenta} Found local dependencies: ${localDependencies.join(', ')}${colors.reset}`, colors.magenta);
for (const depName of localDependencies) {
try {
log(`${colors.cyan} Linking ${depName} to ${pkg.name}...${colors.reset}`, colors.cyan);
execCommand(`npm link "${depName}"`, pkg.path);
log(`${colors.green} ✓ Successfully linked ${depName} to ${pkg.name}${colors.reset}`, colors.green);
} catch (error) {
log(`${colors.red} ✗ Failed to link ${depName} to ${pkg.name}${colors.reset}`, colors.red);
// Continue with other dependencies
}
}
} else {
log(`${colors.yellow} No local dependencies found${colors.reset}`, colors.yellow);
}
}
}
function runPluginFlexLink() {
log(`${colors.bright}${colors.yellow}Step 3: Running npm run link in plugin-flex package...${colors.reset}`, colors.yellow);
if (!fs.existsSync(PLUGIN_FLEX_DIR)) {
log(`${colors.red}Error: plugin-flex directory not found at ${PLUGIN_FLEX_DIR}${colors.reset}`, colors.red);
return;
}
try {
log(`${colors.green}Running npm run link in plugin-flex...${colors.reset}`, colors.green);
execCommand('npm run link', PLUGIN_FLEX_DIR);
log(`${colors.green}✓ Successfully ran npm run link in plugin-flex${colors.reset}`, colors.green);
} catch (error) {
log(`${colors.red}✗ Failed to run npm run link in plugin-flex${colors.reset}`, colors.red);
throw error;
}
}
function main() {
try {
log(`${colors.bright}${colors.blue}Starting package linking process...${colors.reset}`, colors.blue);
log(`${colors.bright}${colors.blue}Working directory: ${__dirname}${colors.reset}`, colors.blue);
log(`${colors.bright}${colors.blue}Packages directory: ${PACKAGES_DIR}${colors.reset}`, colors.blue);
// Get all packages
const packages = getAllPackages();
log(`${colors.bright}${colors.green}Found ${packages.length} packages:${colors.reset}`, colors.green);
packages.forEach(pkg => {
log(` - ${pkg.name} (${pkg.directory})`, colors.green);
});
// Step 1: Create global links
createGlobalLinks(packages);
// Step 2: Link dependencies
linkDependencies(packages);
// Step 3: Run npm run link in plugin-flex
runPluginFlexLink();
log(`${colors.bright}${colors.green}🎉 Package linking completed successfully!${colors.reset}`, colors.green);
} catch (error) {
log(`${colors.bright}${colors.red}❌ Package linking failed: ${error.message}${colors.reset}`, colors.red);
process.exit(1);
}
}
// Run the script
if (require.main === module) {
main();
}
module.exports = {
getAllPackages,
createGlobalLinks,
linkDependencies,
runPluginFlexLink
};