-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-package-env.js
More file actions
executable file
·118 lines (96 loc) · 2.99 KB
/
setup-package-env.js
File metadata and controls
executable file
·118 lines (96 loc) · 2.99 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
#!/usr/bin/env node
/**
* Setup Package Environment
* Creates a clean npm package installation for testing
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const GREEN = '\x1b[32m';
const YELLOW = '\x1b[33m';
const RESET = '\x1b[0m';
function log(message) {
console.log(`${GREEN}[setup]${RESET} ${message}`);
}
function info(message) {
console.log(`${YELLOW}[info]${RESET} ${message}`);
}
const rootDir = path.join(__dirname, '..');
const testEnvDir = path.join(rootDir, 'test-package-env');
const npmCacheDir = path.join(rootDir, '.npm');
const npmEnv = {
...process.env,
npm_config_cache: npmCacheDir
};
function withNpmEnv(options = {}) {
return {
...options,
env: {
...npmEnv,
...(options.env || {})
}
};
}
function setup() {
try {
fs.mkdirSync(npmCacheDir, { recursive: true });
// Clean up existing environment
if (fs.existsSync(testEnvDir)) {
log('Removing existing test environment...');
fs.rmSync(testEnvDir, { recursive: true, force: true });
}
// Pack the CLI
log('Packing CLI...');
execSync('npm pack', withNpmEnv({ cwd: rootDir, stdio: 'pipe' }));
const tarball = fs.readdirSync(rootDir).find(f => f.startsWith('zest-dev-') && f.endsWith('.tgz'));
if (!tarball) {
throw new Error('Package tarball not found');
}
info(`Package created: ${tarball}`);
// Create test environment
log('Creating test environment...');
fs.mkdirSync(testEnvDir, { recursive: true });
execSync('npm init -y', withNpmEnv({ cwd: testEnvDir, stdio: 'pipe' }));
// Install packaged CLI
log('Installing packaged CLI...');
const tarballPath = path.join(rootDir, tarball);
execSync(`npm install ${tarballPath}`, withNpmEnv({ cwd: testEnvDir, stdio: 'pipe' }));
// Verify CLI is executable
log('Verifying CLI...');
const versionOutput = execSync('npx zest-dev --version', withNpmEnv({
cwd: testEnvDir,
encoding: 'utf-8'
})).trim();
info(`CLI version: ${versionOutput}`);
// Create symlink for easy access
const cliPath = path.join(testEnvDir, 'node_modules', '.bin', 'zest-dev');
if (fs.existsSync(cliPath)) {
log('CLI is ready to use');
}
// Clean up tarball
fs.unlinkSync(tarballPath);
log('Package environment setup complete');
info(`Test environment: ${testEnvDir}`);
info(`Next: Set ZEST_DEV_CLI_PATH=${testEnvDir} and run integration tests`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
function cleanup() {
if (fs.existsSync(testEnvDir)) {
log('Cleaning up test environment...');
fs.rmSync(testEnvDir, { recursive: true, force: true });
info('Test environment cleaned up');
}
}
// CLI usage: node setup-package-env.js [setup|cleanup]
if (require.main === module) {
const command = process.argv[2] || 'setup';
if (command === 'cleanup') {
cleanup();
} else {
setup();
}
}
module.exports = { setup, cleanup };