firebase-tools
Version:
Command-Line Interface for Firebase
92 lines (91 loc) • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAndResolveDirectoryKit = validateAndResolveDirectoryKit;
exports.buildAndInstallDirectoryKit = buildAndInstallDirectoryKit;
exports.createDirectoryAdapter = createDirectoryAdapter;
const path = require("path");
const fs = require("fs-extra");
const error_1 = require("../../../error");
const spawn_1 = require("../../../init/spawn");
const utils_1 = require("../../../utils");
const projectConfig_1 = require("../../projectConfig");
const types_1 = require("./types");
async function validateAndResolveDirectoryKit(projectDir, rawDir) {
const absDirectoryPath = (0, utils_1.resolveWithin)(projectDir, rawDir, `Directory '${rawDir}' is outside of project directory. Function kit directory must be inside the project directory.`);
if (!(await fs.pathExists(absDirectoryPath))) {
throw new error_1.FirebaseError(`Directory '${rawDir}' does not exist.`);
}
const stat = await fs.stat(absDirectoryPath);
if (!stat.isDirectory()) {
throw new error_1.FirebaseError(`Directory '${rawDir}' is not a directory.`);
}
const packageJsonPath = path.join(absDirectoryPath, "package.json");
if (!(await fs.pathExists(packageJsonPath))) {
throw new error_1.FirebaseError(`Directory '${rawDir}' must contain a package.json file to be installed as a function kit.`);
}
let packageJson = {};
try {
packageJson = (await fs.readJson(packageJsonPath)) || {};
}
catch (err) {
throw new error_1.FirebaseError(`Failed to parse package.json in '${rawDir}': ${(0, error_1.getErrMsg)(err)}`);
}
const hasBuildScript = Boolean(packageJson?.scripts?.build);
let relSourcePath = path.relative(projectDir, absDirectoryPath);
if (!relSourcePath) {
relSourcePath = ".";
}
relSourcePath = relSourcePath.split(path.sep).join("/");
return {
absDirectoryPath,
relSourcePath,
hasBuildScript,
};
}
async function buildAndInstallDirectoryKit(absSourcePath, hasBuildScript) {
(0, utils_1.logLabeledBullet)("functions", "Running npm install...");
try {
await (0, spawn_1.wrapSpawn)("npm", ["install"], absSourcePath);
}
catch (err) {
throw new error_1.FirebaseError(`NPM install failed: ${(0, error_1.getErrMsg)(err)}`);
}
if (hasBuildScript) {
(0, utils_1.logLabeledBullet)("functions", "Building TypeScript source...");
try {
await (0, spawn_1.wrapSpawn)("npm", ["run", "build"], absSourcePath);
}
catch (err) {
throw new error_1.FirebaseError(`TypeScript build failed: ${(0, error_1.getErrMsg)(err)}`);
}
}
}
function createDirectoryAdapter(config, directory) {
const absDir = path.isAbsolute(directory)
? directory
: path.resolve(config.projectDir, directory);
let validated;
return {
defaultKitId: (0, types_1.sanitizeToKitId)(path.basename(absDir)) || "kit",
findExistingKit(existingFunctions) {
return existingFunctions.find((c) => (0, projectConfig_1.isKitConfig)(c) &&
!c.sourcePackage &&
path.resolve(config.projectDir, c.source) === absDir);
},
async validateAndConfirm() {
validated = await validateAndResolveDirectoryKit(config.projectDir, directory);
},
async prepareSource() {
if (!validated) {
validated = await validateAndResolveDirectoryKit(config.projectDir, directory);
}
await buildAndInstallDirectoryKit(validated.absDirectoryPath, validated.hasBuildScript);
return {
sourcePath: validated.relSourcePath,
...(validated.hasBuildScript
? { predeploy: ['npm --prefix "$RESOURCE_DIR" run build'] }
: {}),
};
},
};
}