Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
},
content: templateContent,
linkAfter: [],
})
const componentName = UIDLUtils.getComponentClassName(uidl)
const params = {
selector: UIDLUtils.createWebComponentFriendlyName(componentName),
templateUrl: `${UIDLUtils.getComponentFileName(uidl)}.${FileType.HTML}`,
}
const componentDecoratorAST = ASTBuilders.createComponentDecorator(params)
chunks.push({
type: ChunkType.AST,
name: componentDecoratorChunkName,
fileType: FileType.TS,
linkAfter: tsChunkAfter,
content: componentDecoratorAST,
})
/* We need to import EventEmitter and Output in Angular to temit events to the parent
So, to make sure if we need to import them we need to loop through all the methods and
check if any of them are referring to the function that is passed as prop*/
if (Object.keys(methodsObject).length > 0) {
const shouldImportEventEmitter = Object.keys(methodsObject).some((method) => {
const statements = methodsObject[method]
if (statements.length > 0) {
return statements.some((event) => event.type === 'propCall')
}
return false
})
if (shouldImportEventEmitter) {
// Routes will be present for only pages and root
if (routesAST) {
chunks.push({
name: moduleChunkName,
type: ChunkType.AST,
fileType: FileType.TS,
content: routesAST,
linkAfter: [importChunkName],
})
}
chunks.push({
name: decoratorChunkName,
type: ChunkType.AST,
fileType: FileType.TS,
content: [ngModuleAST, moduleDecoratorAST],
linkAfter: [importChunkName],
})
return structure
}
it('Should add Angular dependencies for root module', async () => {
const rootModule = createAngularModulePlugin({ moduleType: 'root' })
const rootUIDL = projectUIDL.root as ComponentUIDL
const structure: ComponentStructure = {
uidl: rootUIDL,
options: {},
chunks: [],
dependencies: {},
}
const { dependencies, chunks } = await rootModule(structure)
expect(chunks.length).toBe(2)
expect(Object.keys(dependencies).length).toBe(5)
expect(chunks[0].type).toBe(ChunkType.AST)
expect(chunks[0].fileType).toBe(FileType.TS)
expect(chunks[0].content).toBeDefined()
expect(chunks[1].type).toBe(ChunkType.AST)
expect(chunks[1].fileType).toBe(FileType.TS)
expect(chunks[1].content).toBeDefined()
})
const outputFolder = await generator.generateProject(uidlSample, template)
const assetsPath = generator.getAssetsPath()
expect(assetsPath).toBeDefined()
expect(outputFolder.name).toBe(template.name)
expect(outputFolder.files[0].name).toBe('package')
const srcFolder = outputFolder.subFolders[0]
const appFolder = srcFolder.subFolders[0]
const pagesFolder = appFolder.subFolders[0]
const componentsFolder = appFolder.subFolders[1]
expect(srcFolder.files[0].fileType).toBe(FileType.HTML)
expect(srcFolder.files[0].content).toBeDefined()
expect(appFolder.files[0].name).toBe('app.module')
expect(appFolder.files[0].fileType).toBe(FileType.TS)
expect(appFolder.files[0].content).toBeDefined()
expect(componentsFolder.name).toBe('components')
expect(componentsFolder.files[0].name).toBe('components.module')
expect(componentsFolder.files[0].content).toBeDefined()
expect(componentsFolder.subFolders.length).toBeGreaterThan(0)
expect(pagesFolder.name).toBe('pages')
expect(pagesFolder.subFolders.length).toBeGreaterThan(0)
})
const structure: ComponentStructure = {
uidl: rootUIDL,
options: {},
chunks: [],
dependencies: {},
}
const { dependencies, chunks } = await rootModule(structure)
expect(chunks.length).toBe(2)
expect(Object.keys(dependencies).length).toBe(5)
expect(chunks[0].type).toBe(ChunkType.AST)
expect(chunks[0].fileType).toBe(FileType.TS)
expect(chunks[0].content).toBeDefined()
expect(chunks[1].type).toBe(ChunkType.AST)
expect(chunks[1].fileType).toBe(FileType.TS)
expect(chunks[1].content).toBeDefined()
})
dependencies.EventEmitter = ANGULAR_CORE_DEPENDENCY
}
}
const exportAST = generateExportAST(
uidl,
propDefinitions,
stateDefinitions,
dataObject,
methodsObject
)
chunks.push({
type: ChunkType.AST,
name: exportClassChunk,
fileType: FileType.TS,
linkAfter: tsChunkAfter,
content: exportAST,
})
return structure
}
const createAngularProjectGenerator = () => {
const prettierTS = createPrettierTSPostProcessor({ fileType: FileType.TS })
const importStatementsPlugin = createImportPlugin({ fileType: FileType.TS })
const rootModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'root' })
const componentModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'component' })
const pagesModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'page' })
const angularComponentGenerator = createAngularComponentGenerator()
angularComponentGenerator.addMapping(AngularProjectMapping as Mapping)
const angularPageGenerator = createAngularComponentGenerator()
const angularRootModuleGenerator = createComponentGenerator()
angularRootModuleGenerator.addPlugin(rootModuleGeneratorAngular)
angularRootModuleGenerator.addPlugin(importStatementsPlugin)
angularRootModuleGenerator.addPostProcessor(prettierTS)
const createAngularProjectGenerator = () => {
const prettierTS = createPrettierTSPostProcessor({ fileType: FileType.TS })
const importStatementsPlugin = createImportPlugin({ fileType: FileType.TS })
const rootModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'root' })
const componentModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'component' })
const pagesModuleGeneratorAngular = createAngularModulePlugin({ moduleType: 'page' })
const angularComponentGenerator = createAngularComponentGenerator()
angularComponentGenerator.addMapping(AngularProjectMapping as Mapping)
const angularPageGenerator = createAngularComponentGenerator()
const angularRootModuleGenerator = createComponentGenerator()
angularRootModuleGenerator.addPlugin(rootModuleGeneratorAngular)
angularRootModuleGenerator.addPlugin(importStatementsPlugin)
angularRootModuleGenerator.addPostProcessor(prettierTS)
const angularComponentModuleGenerator = createComponentGenerator()
import {
createComponentGenerator,
GeneratorFactoryParams,
} from '@teleporthq/teleport-component-generator'
import angularComponentPlugin from '@teleporthq/teleport-plugin-angular-base-component'
import { createCSSPlugin } from '@teleporthq/teleport-plugin-css'
import { createImportPlugin } from '@teleporthq/teleport-plugin-import-statements'
import prettierTS from '@teleporthq/teleport-postprocessor-prettier-ts'
import prettierHTML from '@teleporthq/teleport-postprocessor-prettier-html'
import AngularMapping from './angular-mapping.json'
import { ComponentGenerator, FileType } from '@teleporthq/teleport-types'
const importStatementsPlugin = createImportPlugin({ fileType: FileType.TS })
const stylePlugin = createCSSPlugin({
inlineStyleAttributeKey: '[ngStyle]',
declareDependency: 'decorator',
})
const createAngularComponentGenerator = ({
mappings = [],
plugins = [],
postprocessors = [],
}: GeneratorFactoryParams = {}): ComponentGenerator => {
const generator = createComponentGenerator()
generator.addMapping(AngularMapping)
mappings.forEach((mapping) => generator.addMapping(mapping))
generator.addPlugin(angularComponentPlugin)