|
| 1 | +import path from 'path'; |
| 2 | +import { fileURLToPath } from 'url'; |
| 3 | +import shell from 'shelljs'; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = path.dirname(__filename); |
| 7 | + |
| 8 | +const ROOT_DIR = path.resolve(__dirname, '..'); |
| 9 | +const SPEC_FILENAME = 'openapi.yml'; |
| 10 | +const SPEC_THEME_FILENAME = 'swaggerTheme.css'; |
| 11 | + |
| 12 | +const userManagementEnabled = process.env.N8N_USER_MANAGEMENT_DISABLED !== 'true'; |
| 13 | +const publicApiEnabled = process.env.N8N_PUBLIC_API_DISABLED !== 'true'; |
| 14 | + |
| 15 | +shell.rm('-rf', path.resolve(ROOT_DIR, 'dist')); |
| 16 | + |
| 17 | +shell.exec('tsc'); |
| 18 | + |
| 19 | +if (userManagementEnabled) { |
| 20 | + copyUserManagementEmailTemplates(); |
| 21 | +} |
| 22 | + |
| 23 | +if (publicApiEnabled) { |
| 24 | + copySwaggerTheme(); |
| 25 | + bundleOpenApiSpecs(); |
| 26 | +} |
| 27 | + |
| 28 | +function copyUserManagementEmailTemplates(rootDir = ROOT_DIR) { |
| 29 | + const templates = { |
| 30 | + source: path.resolve(rootDir, 'src', 'UserManagement', 'email', 'templates'), |
| 31 | + destination: path.resolve(rootDir, 'dist', 'src', 'UserManagement', 'email'), |
| 32 | + }; |
| 33 | + |
| 34 | + shell.cp('-r', templates.source, templates.destination); |
| 35 | +} |
| 36 | + |
| 37 | +function copySwaggerTheme(rootDir = ROOT_DIR, themeFilename = SPEC_THEME_FILENAME) { |
| 38 | + const swaggerTheme = { |
| 39 | + source: path.resolve(rootDir, 'src', 'PublicApi', themeFilename), |
| 40 | + destination: path.resolve(rootDir, 'dist', 'src', 'PublicApi'), |
| 41 | + }; |
| 42 | + |
| 43 | + shell.cp('-r', swaggerTheme.source, swaggerTheme.destination); |
| 44 | +} |
| 45 | + |
| 46 | +function bundleOpenApiSpecs(rootDir = ROOT_DIR, specFileName = SPEC_FILENAME) { |
| 47 | + const publicApiDir = path.resolve(rootDir, 'src', 'PublicApi'); |
| 48 | + |
| 49 | + shell |
| 50 | + .find(publicApiDir) |
| 51 | + .reduce((acc, cur) => { |
| 52 | + return cur.endsWith(specFileName) ? [...acc, path.relative('.', cur)] : acc; |
| 53 | + }, []) |
| 54 | + .forEach((specPath) => { |
| 55 | + const distSpecPath = path.resolve(rootDir, 'dist', specPath); |
| 56 | + const command = `swagger-cli bundle ${specPath} --type yaml --outfile ${distSpecPath}`; |
| 57 | + shell.exec(command, { silent: true }); |
| 58 | + }); |
| 59 | +} |
0 commit comments