@quell/server
Version:
Quell is an open-source NPM package providing a light-weight caching layer implementation and cache invalidation for GraphQL responses on both the client- and server-side. Use Quell to prevent redundant client-side API requests and to minimize costly serv
291 lines (290 loc) • 10.9 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentDirName = exports.isGitRepository = exports.createDirectories = exports.updatePackageJson = exports.readPackageJson = exports.updateGitignore = exports.appendToFile = exports.createFile = exports.installPackages = exports.detectPackageManager = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Detects which package manager is being used in the project
*/
function detectPackageManager() {
if (fs.existsSync('yarn.lock'))
return 'yarn';
if (fs.existsSync('pnpm-lock.yaml'))
return 'pnpm';
return 'npm';
}
exports.detectPackageManager = detectPackageManager;
/**
* Installs packages using the detected package manager
*/
function installPackages(dependencies, devDependencies = [], logger) {
return __awaiter(this, void 0, void 0, function* () {
const packageManager = detectPackageManager();
try {
if (dependencies.length > 0) {
logger === null || logger === void 0 ? void 0 : logger.info(`Installing dependencies with ${packageManager}...`);
const depCommand = getInstallCommand(packageManager, dependencies, false);
logger === null || logger === void 0 ? void 0 : logger.code(` ${depCommand}`);
const { stdout, stderr } = yield execAsync(depCommand);
if (stderr && !stderr.includes('npm WARN')) {
throw new Error(stderr);
}
}
if (devDependencies.length > 0) {
logger === null || logger === void 0 ? void 0 : logger.info(`Installing dev dependencies with ${packageManager}...`);
const devCommand = getInstallCommand(packageManager, devDependencies, true);
logger === null || logger === void 0 ? void 0 : logger.code(` ${devCommand}`);
const { stdout, stderr } = yield execAsync(devCommand);
if (stderr && !stderr.includes('npm WARN')) {
throw new Error(stderr);
}
}
return {
success: true,
output: 'Packages installed successfully'
};
}
catch (error) {
return {
success: false,
output: '',
error: error instanceof Error ? error.message : String(error)
};
}
});
}
exports.installPackages = installPackages;
/**
* Gets the install command for the specified package manager
*/
function getInstallCommand(packageManager, packages, isDev) {
const packageList = packages.join(' ');
switch (packageManager) {
case 'yarn':
return isDev ? `yarn add -D ${packageList}` : `yarn add ${packageList}`;
case 'pnpm':
return isDev ? `pnpm add -D ${packageList}` : `pnpm add ${packageList}`;
default:
return isDev ? `npm install -D ${packageList}` : `npm install ${packageList}`;
}
}
/**
* Creates a file with content, optionally checking if it already exists
*/
function createFile(filePath, content, overwrite = false) {
return __awaiter(this, void 0, void 0, function* () {
const fullPath = path.resolve(filePath);
const exists = yield fs.pathExists(fullPath);
if (exists && !overwrite) {
return {
created: false,
existed: true,
path: fullPath
};
}
// Ensure directory exists
yield fs.ensureDir(path.dirname(fullPath));
// Write file
yield fs.writeFile(fullPath, content, 'utf8');
return {
created: true,
existed: exists,
path: fullPath
};
});
}
exports.createFile = createFile;
/**
* Appends content to an existing file or creates it if it doesn't exist
*/
function appendToFile(filePath, content, separator = '\n') {
return __awaiter(this, void 0, void 0, function* () {
const fullPath = path.resolve(filePath);
const exists = yield fs.pathExists(fullPath);
if (exists) {
const existingContent = yield fs.readFile(fullPath, 'utf8');
const newContent = existingContent + separator + content;
yield fs.writeFile(fullPath, newContent, 'utf8');
}
else {
yield fs.ensureDir(path.dirname(fullPath));
yield fs.writeFile(fullPath, content, 'utf8');
}
return {
created: true,
existed: exists,
path: fullPath
};
});
}
exports.appendToFile = appendToFile;
/**
* Updates .gitignore file with Quell-specific entries
*/
function updateGitignore(additions) {
return __awaiter(this, void 0, void 0, function* () {
const gitignorePath = '.gitignore';
const exists = yield fs.pathExists(gitignorePath);
if (exists) {
const existingContent = yield fs.readFile(gitignorePath, 'utf8');
// Check if Quell entries already exist
if (existingContent.includes('# Quell cache configuration')) {
return {
created: false,
existed: true,
path: path.resolve(gitignorePath)
};
}
// Append Quell entries
yield appendToFile(gitignorePath, additions);
}
else {
// Create new .gitignore
yield createFile(gitignorePath, additions.trim());
}
return {
created: true,
existed: exists,
path: path.resolve(gitignorePath)
};
});
}
exports.updateGitignore = updateGitignore;
/**
* Checks if a package.json exists and reads it
*/
function readPackageJson() {
return __awaiter(this, void 0, void 0, function* () {
const packagePath = 'package.json';
const exists = yield fs.pathExists(packagePath);
if (!exists) {
return null;
}
try {
const content = yield fs.readFile(packagePath, 'utf8');
return JSON.parse(content);
}
catch (error) {
return null;
}
});
}
exports.readPackageJson = readPackageJson;
/**
* Updates package.json with new scripts and dependencies
*/
function updatePackageJson(scripts, dependencies, devDependencies) {
return __awaiter(this, void 0, void 0, function* () {
const packagePath = 'package.json';
const exists = yield fs.pathExists(packagePath);
if (!exists) {
// Create a basic package.json
const basicPackage = {
name: path.basename(process.cwd()),
version: '1.0.0',
description: 'GraphQL server with Quell caching',
main: 'dist/server.js',
scripts: Object.assign({}, scripts),
dependencies: {},
devDependencies: {},
type: 'module'
};
yield fs.writeFile(packagePath, JSON.stringify(basicPackage, null, 2), 'utf8');
return {
created: true,
existed: false,
path: path.resolve(packagePath)
};
}
// Read existing package.json
const packageJson = yield readPackageJson();
if (!packageJson) {
throw new Error('Failed to read existing package.json');
}
// Update scripts (don't overwrite existing ones)
if (!packageJson.scripts) {
packageJson.scripts = {};
}
Object.entries(scripts).forEach(([key, value]) => {
if (!packageJson.scripts[key]) {
packageJson.scripts[key] = value;
}
});
// Initialize dependencies objects if they don't exist
if (!packageJson.dependencies) {
packageJson.dependencies = {};
}
if (!packageJson.devDependencies) {
packageJson.devDependencies = {};
}
yield fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), 'utf8');
return {
created: true,
existed: true,
path: path.resolve(packagePath)
};
});
}
exports.updatePackageJson = updatePackageJson;
/**
* Creates a directory structure
*/
function createDirectories(dirs) {
return __awaiter(this, void 0, void 0, function* () {
for (const dir of dirs) {
yield fs.ensureDir(dir);
}
});
}
exports.createDirectories = createDirectories;
/**
* Checks if we're in a Git repository
*/
function isGitRepository() {
return __awaiter(this, void 0, void 0, function* () {
return fs.pathExists('.git');
});
}
exports.isGitRepository = isGitRepository;
/**
* Gets the current working directory name
*/
function getCurrentDirName() {
return path.basename(process.cwd());
}
exports.getCurrentDirName = getCurrentDirName;