|
| 1 | +import { logger, readJson, Tree, updateJson } from '@nx/devkit'; |
| 2 | +import { CreateCommunityGeneratorSchema } from './schema'; |
| 3 | +import { exit } from 'node:process'; |
| 4 | +import { Community } from '../models/community'; |
| 5 | +import { isPublicAsset } from '../utils/isPublicAsset'; |
| 6 | + |
| 7 | +const COMMUNITIES_PATH = 'angular-hub/src/public/assets/data/community.json'; |
| 8 | + |
| 9 | +export async function createCommunityGenerator( |
| 10 | + tree: Tree, |
| 11 | + options: CreateCommunityGeneratorSchema, |
| 12 | +) { |
| 13 | + const { name, type, url, logo } = options; |
| 14 | + |
| 15 | + if (!name) { |
| 16 | + logger.error('[angular-hub] Name is missing'); |
| 17 | + return exit(1); |
| 18 | + } |
| 19 | + |
| 20 | + if (!type) { |
| 21 | + logger.error('[angular-hub] Type is missing'); |
| 22 | + return exit(1); |
| 23 | + } |
| 24 | + |
| 25 | + if (url && !isPublicAsset(url)) { |
| 26 | + logger.error( |
| 27 | + '[angular-hub] Url is not valid (should start with https or http). ', |
| 28 | + ); |
| 29 | + return exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + if (logo && !isPublicAsset(logo)) { |
| 33 | + logger.info( |
| 34 | + '[angular-hub] Make sure you upload the logo file at logos folder in the assets directory', |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + const existingCommunities: Community[] = readJson(tree, COMMUNITIES_PATH); |
| 39 | + |
| 40 | + if (isCommunityExisting(existingCommunities, name)) { |
| 41 | + logger.error(`[angular-hub] ${name} Community already exists`); |
| 42 | + return exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + updateJson(tree, COMMUNITIES_PATH, (communities: Community[]) => { |
| 46 | + communities.push({ |
| 47 | + name, |
| 48 | + type, |
| 49 | + url: options.url ?? '', |
| 50 | + location: options.location ?? '', |
| 51 | + logo: options.logo ?? '', |
| 52 | + twitter: options.twitter ?? '', |
| 53 | + linkedin: options.linkedin ?? '', |
| 54 | + callForPapers: options.callForPapers ?? '', |
| 55 | + events: [], |
| 56 | + }); |
| 57 | + |
| 58 | + return communities; |
| 59 | + }); |
| 60 | + |
| 61 | + logger.info( |
| 62 | + `[angular-hub] Community is added successfully. You can manually update the data if needed afterwards`, |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function isCommunityExisting(communities: Community[], name: string): boolean { |
| 67 | + return communities |
| 68 | + .map((community) => community.name.toLowerCase()) |
| 69 | + .includes(name.toLowerCase()); |
| 70 | +} |
| 71 | + |
| 72 | +export default createCommunityGenerator; |
0 commit comments