UNPKG

@unito/integration-cli

Version:

Integration CLI

52 lines (51 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateIsIntegrationDirectory = validateIsIntegrationDirectory; exports.isIntegrationDirectory = isIntegrationDirectory; exports.copyBoilerplate = copyBoilerplate; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const fs_1 = tslib_1.__importDefault(require("fs")); const errors_1 = require("../errors"); const errors_2 = require("../errors"); const configuration_1 = require("./configuration"); function validateIsIntegrationDirectory() { if (!isIntegrationDirectory()) { throw new errors_2.NoIntegrationFoundError(); } } function isIntegrationDirectory() { return fs_1.default.existsSync((0, configuration_1.getConfigurationPath)()); } async function copyBoilerplate(integrationName, destinationPath) { const sourcePath = path_1.default.join(__dirname, `../../boilerplate`); const targetPath = `${destinationPath}/${integrationName.toLowerCase().replace(/ /g, '_')}`; if (fs_1.default.existsSync(targetPath)) { throw new errors_1.IntegrationAlreadyExistsError(); } await fs_1.default.promises.mkdir(targetPath, { recursive: true }); await copyFilesAndFolders(sourcePath, targetPath, ['node_modules', 'dist']); // The .npmrc file is created here because // it will be excluded during the CLI package's publishing process by npm. const npmrcContent = ['engine-strict=true', 'strict-ssl=true'].join('\n'); await fs_1.default.promises.writeFile(path_1.default.join(targetPath, '.npmrc'), npmrcContent); return targetPath; } async function copyFilesAndFolders(source, destination, excludedFolders) { const files = await fs_1.default.promises.readdir(source); for (const file of files) { if (excludedFolders.includes(file)) { continue; } const sourcePath = path_1.default.join(source, file); const targetPath = path_1.default.join(destination, file); const stats = await fs_1.default.promises.stat(sourcePath); if (stats.isFile()) { await fs_1.default.promises.copyFile(sourcePath, targetPath); } else if (stats.isDirectory()) { await fs_1.default.promises.mkdir(targetPath); await copyFilesAndFolders(sourcePath, targetPath, excludedFolders); } } }