@ufdevsllc/auth-me
Version:
Comprehensive licensing, security monitoring, and data mirroring package with hardcoded vendor-controlled database connection
422 lines (364 loc) ⢠15 kB
JavaScript
/**
* Installation check script for @ufdevsllc/auth-me package
* Verifies package installation and provides setup guidance
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
/**
* Installation check results
*/
class InstallationChecker {
constructor() {
this.results = {
packageInstalled: false,
dependenciesInstalled: false,
nodeVersionCompatible: false,
filesPresent: false,
moduleLoadable: false,
configurationValid: false,
errors: [],
warnings: [],
info: []
};
}
/**
* Run all installation checks
* @returns {Object} Check results
*/
async runAllChecks() {
console.log('š @ufdevsllc/auth-me Installation Checker\n');
try {
this.checkNodeVersion();
this.checkPackageInstallation();
this.checkDependencies();
this.checkRequiredFiles();
this.checkModuleLoading();
await this.checkConfiguration();
this.generateSummary();
} catch (error) {
this.results.errors.push(`Installation check failed: ${error.message}`);
}
return this.results;
}
/**
* Check Node.js version compatibility
*/
checkNodeVersion() {
console.log('š Checking Node.js version...');
const currentVersion = process.version;
const requiredVersion = '14.0.0';
const current = this.parseVersion(currentVersion);
const required = this.parseVersion(requiredVersion);
if (this.compareVersions(current, required) >= 0) {
this.results.nodeVersionCompatible = true;
this.results.info.push(`Node.js version ${currentVersion} is compatible`);
console.log(`ā
Node.js ${currentVersion} (>= ${requiredVersion} required)`);
} else {
this.results.nodeVersionCompatible = false;
this.results.errors.push(`Node.js version ${currentVersion} is not compatible. Version ${requiredVersion} or higher is required.`);
console.log(`ā Node.js ${currentVersion} (>= ${requiredVersion} required)`);
}
}
/**
* Check if package is properly installed
*/
checkPackageInstallation() {
console.log('\nš¦ Checking package installation...');
try {
// Check if package.json exists
const packageJsonPath = path.join(__dirname, '..', 'package.json');
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
this.results.packageInstalled = true;
this.results.info.push(`Package ${packageJson.name}@${packageJson.version} found`);
console.log(`ā
Package ${packageJson.name}@${packageJson.version}`);
} else {
this.results.packageInstalled = false;
this.results.errors.push('package.json not found');
console.log('ā package.json not found');
}
} catch (error) {
this.results.packageInstalled = false;
this.results.errors.push(`Error reading package.json: ${error.message}`);
console.log(`ā Error reading package.json: ${error.message}`);
}
}
/**
* Check if dependencies are installed
*/
checkDependencies() {
console.log('\nš Checking dependencies...');
const requiredDependencies = [
'mongoose',
'jsonwebtoken',
'crypto'
];
let allDependenciesInstalled = true;
for (const dep of requiredDependencies) {
try {
require.resolve(dep);
console.log(`ā
${dep}`);
this.results.info.push(`Dependency ${dep} is available`);
} catch (error) {
allDependenciesInstalled = false;
this.results.errors.push(`Missing dependency: ${dep}`);
console.log(`ā ${dep} - not found`);
}
}
this.results.dependenciesInstalled = allDependenciesInstalled;
// Check for node_modules
const nodeModulesPath = path.join(__dirname, '..', 'node_modules');
if (!fs.existsSync(nodeModulesPath)) {
this.results.warnings.push('node_modules directory not found - run npm install');
console.log('ā ļø node_modules directory not found');
}
}
/**
* Check if required files are present
*/
checkRequiredFiles() {
console.log('\nš Checking required files...');
const requiredFiles = [
'index.js',
'index.d.ts',
'src/index.js',
'src/core/SecureGuard.js',
'src/core/LicenseValidator.js',
'src/core/TamperDetector.js',
'src/core/DataMirrorService.js',
'src/core/UsageTracker.js',
'src/core/DeploymentMonitor.js',
'src/core/EncryptionManager.js',
'src/core/TokenValidator.js',
'src/core/ConfigManager.js'
];
let allFilesPresent = true;
for (const file of requiredFiles) {
const filePath = path.join(__dirname, '..', file);
if (fs.existsSync(filePath)) {
console.log(`ā
${file}`);
} else {
allFilesPresent = false;
this.results.errors.push(`Missing required file: ${file}`);
console.log(`ā ${file}`);
}
}
this.results.filesPresent = allFilesPresent;
// Check for build info
const buildInfoPath = path.join(__dirname, '..', 'build-info.json');
if (fs.existsSync(buildInfoPath)) {
try {
const buildInfo = JSON.parse(fs.readFileSync(buildInfoPath, 'utf8'));
this.results.info.push(`Build info: ${buildInfo.buildMode} mode, built ${buildInfo.buildTime}`);
console.log(`ā¹ļø Build: ${buildInfo.buildMode} mode (${buildInfo.buildTime})`);
} catch (error) {
this.results.warnings.push('Invalid build-info.json file');
}
} else {
this.results.warnings.push('build-info.json not found - package may not be built');
}
}
/**
* Check if main module can be loaded
*/
checkModuleLoading() {
console.log('\nš§ Checking module loading...');
try {
// Try to require the main module
const mainModulePath = path.join(__dirname, '..', 'index.js');
const SecureGuardModule = require(mainModulePath);
// Check for expected exports
const expectedExports = [
'SecureGuard',
'init',
'isInitialized',
'getUsageStats',
'getLicenseInfo',
'getDeploymentFingerprint'
];
let allExportsPresent = true;
for (const exportName of expectedExports) {
if (typeof SecureGuardModule[exportName] !== 'function' && !SecureGuardModule[exportName]) {
allExportsPresent = false;
this.results.errors.push(`Missing export: ${exportName}`);
console.log(`ā Export: ${exportName}`);
} else {
console.log(`ā
Export: ${exportName}`);
}
}
if (allExportsPresent) {
this.results.moduleLoadable = true;
this.results.info.push('All required exports are available');
} else {
this.results.moduleLoadable = false;
}
} catch (error) {
this.results.moduleLoadable = false;
this.results.errors.push(`Module loading failed: ${error.message}`);
console.log(`ā Module loading failed: ${error.message}`);
}
}
/**
* Check configuration setup
*/
async checkConfiguration() {
console.log('\nāļø Checking configuration...');
// Check for example configuration
const exampleConfigPath = path.join(__dirname, '..', 'example-config.json');
const userConfigPath = path.join(process.cwd(), 'secure-guard-config.json');
if (fs.existsSync(userConfigPath)) {
try {
const config = JSON.parse(fs.readFileSync(userConfigPath, 'utf8'));
// Basic configuration validation
if (config.licenseKey && config.vendorEndpoint && config.schemas) {
this.results.configurationValid = true;
this.results.info.push('User configuration file found and appears valid');
console.log('ā
User configuration file found');
} else {
this.results.configurationValid = false;
this.results.warnings.push('User configuration file found but may be incomplete');
console.log('ā ļø User configuration file found but may be incomplete');
}
} catch (error) {
this.results.configurationValid = false;
this.results.errors.push(`Invalid user configuration: ${error.message}`);
console.log(`ā Invalid user configuration: ${error.message}`);
}
} else {
this.results.configurationValid = false;
this.results.warnings.push('No user configuration file found');
console.log('ā ļø No user configuration file found');
// Provide setup guidance
this.results.info.push('Create secure-guard-config.json in your project root');
}
// Check if example config exists
if (!fs.existsSync(exampleConfigPath)) {
this.createExampleConfig(exampleConfigPath);
}
}
/**
* Create example configuration file
*/
createExampleConfig(configPath) {
const exampleConfig = {
"licenseKey": "your-license-key-here",
"vendorEndpoint": "https://your-vendor-endpoint.com/api",
"schemas": [
{
"name": "User",
"schema": "// Your Mongoose schema here"
}
],
"options": {
"enableEnvironmentBinding": true,
"enableTamperDetection": true,
"enableUsageTracking": true,
"crashOnViolation": true,
"verboseLogging": false
}
};
try {
fs.writeFileSync(configPath, JSON.stringify(exampleConfig, null, 2));
this.results.info.push(`Example configuration created at ${configPath}`);
console.log(`ā¹ļø Example configuration created at ${configPath}`);
} catch (error) {
this.results.warnings.push(`Could not create example configuration: ${error.message}`);
}
}
/**
* Generate installation summary
*/
generateSummary() {
console.log('\nš Installation Summary');
console.log('========================');
const checks = [
{ name: 'Node.js Version', passed: this.results.nodeVersionCompatible },
{ name: 'Package Installation', passed: this.results.packageInstalled },
{ name: 'Dependencies', passed: this.results.dependenciesInstalled },
{ name: 'Required Files', passed: this.results.filesPresent },
{ name: 'Module Loading', passed: this.results.moduleLoadable },
{ name: 'Configuration', passed: this.results.configurationValid }
];
let passedChecks = 0;
for (const check of checks) {
const status = check.passed ? 'ā
' : 'ā';
console.log(`${status} ${check.name}`);
if (check.passed) passedChecks++;
}
console.log(`\n${passedChecks}/${checks.length} checks passed`);
if (this.results.errors.length > 0) {
console.log('\nšØ Errors to fix:');
this.results.errors.forEach(error => console.log(` ⢠${error}`));
}
if (this.results.warnings.length > 0) {
console.log('\nā ļø Warnings:');
this.results.warnings.forEach(warning => console.log(` ⢠${warning}`));
}
// Installation guidance
if (passedChecks < checks.length) {
console.log('\nš§ Next steps:');
if (!this.results.nodeVersionCompatible) {
console.log(' 1. Update Node.js to version 14.0.0 or higher');
}
if (!this.results.dependenciesInstalled) {
console.log(' 2. Run: npm install');
}
if (!this.results.configurationValid) {
console.log(' 3. Create and configure secure-guard-config.json');
console.log(' See example-config.json for reference');
}
console.log(' 4. Run this check again: npm run install:check');
} else {
console.log('\nš Installation complete! Your package is ready to use.');
console.log('\nš Next steps:');
console.log(' 1. Configure your license key and vendor endpoint');
console.log(' 2. Register your Mongoose schemas');
console.log(' 3. Initialize SecureGuard in your application');
console.log('\nš Documentation: https://github.com/ufdevs/auth-me#readme');
}
}
/**
* Parse version string into comparable format
*/
parseVersion(version) {
return version.replace('v', '').split('.').map(Number);
}
/**
* Compare two version arrays
*/
compareVersions(a, b) {
for (let i = 0; i < Math.max(a.length, b.length); i++) {
const aVal = a[i] || 0;
const bVal = b[i] || 0;
if (aVal > bVal) return 1;
if (aVal < bVal) return -1;
}
return 0;
}
}
/**
* Main function
*/
async function main() {
const checker = new InstallationChecker();
const results = await checker.runAllChecks();
// Exit with appropriate code
const hasErrors = results.errors.length > 0;
const allCriticalChecksPassed = results.nodeVersionCompatible &&
results.packageInstalled &&
results.dependenciesInstalled &&
results.filesPresent &&
results.moduleLoadable;
process.exit(hasErrors || !allCriticalChecksPassed ? 1 : 0);
}
// Export for use as module
module.exports = InstallationChecker;
// Run if called directly
if (require.main === module) {
main().catch(error => {
console.error('Installation check failed:', error);
process.exit(1);
});
}