From e5037f997a4e933a7fbdc7a9ad22b76507020894 Mon Sep 17 00:00:00 2001 From: JayaKrishnaNamburu Date: Sat, 15 May 2021 23:03:59 +0530 Subject: [PATCH] DIsabling validator in project temporarly --- packages/teleport-cli/package.json | 2 +- packages/teleport-cli/src/commands/clone.ts | 33 ++++++++++---- packages/teleport-cli/src/constants.ts | 1 - packages/teleport-cli/src/services/file.ts | 40 +++++++++++------ packages/teleport-cli/src/services/index.ts | 44 ++++++++++++++++--- packages/teleport-cli/src/utils.ts | 26 ++++++----- packages/teleport-mapper/src/studio/index.ts | 2 +- .../teleport-project-generator/src/index.ts | 39 ++++++++-------- 8 files changed, 128 insertions(+), 59 deletions(-) diff --git a/packages/teleport-cli/package.json b/packages/teleport-cli/package.json index fcfd71753..b88b4ad1a 100644 --- a/packages/teleport-cli/package.json +++ b/packages/teleport-cli/package.json @@ -23,7 +23,7 @@ }, "scripts": { "clean": "rimraf dist", - "cli:dev": "tsc -p tsconfig-cjs.json --watch", + "dev": "tsc -p tsconfig-cjs.json --watch", "build": "tsc -p tsconfig-cjs.json" }, "dependencies": { diff --git a/packages/teleport-cli/src/commands/clone.ts b/packages/teleport-cli/src/commands/clone.ts index 4a5f448b2..674243a71 100644 --- a/packages/teleport-cli/src/commands/clone.ts +++ b/packages/teleport-cli/src/commands/clone.ts @@ -15,7 +15,6 @@ export default async function (options: { url: string; targetPath: string; force let uidl: VComponentUIDL | VProjectUIDL const spinner = ora() spinner.start() - // Assuming it is coming from playground if (url.includes('play.teleporthq.io') && !url.includes('repl.teleporthq.io')) { const opts = url.split('/') @@ -33,19 +32,23 @@ export default async function (options: { url: string; targetPath: string; force throw new Error('Failed in Generating Project') } - await generateProjectFromUIDL({ + const projectName = await generateProjectFromUIDL({ uidl, projectType: ProjectType.NEXT, targetPath, url, force, }) + updateConfigFile((content) => { + content.project.name = projectName + }) - spinner.text = `Project Generated Successfully` + spinner.text = `Project Generated Successfully ${projectName}` spinner.succeed() } catch (e) { spinner.text = `Project Generation Failed` spinner.fail() + throw new Error(e) } } @@ -58,14 +61,14 @@ export default async function (options: { url: string; targetPath: string; force } = await fetchSnapshotFromPlayground(opts[4]) const mapper = new MapSnapshotToUIDL(data) - uidl = mapper.componentToUIDL(opts[6]) + uidl = mapper.pageToUIDL(opts[6]) if (!uidl) { - throw new Error('Failed in Generating Project') + throw new Error('Failed in Generating UIDL') } const { files } = await generateComponentFromUIDL(uidl, getComponentType()) - injectFilesToPath(process.cwd(), targetPath, files, force) + injectFilesToPath({ rootFolder: process.cwd(), targetPath, files, force }) updateConfigFile((content) => { content.components[url] = { url, path: targetPath } }) @@ -87,7 +90,7 @@ export default async function (options: { url: string; targetPath: string; force spinner.text = `Fetching project from repl \n` uidl = (await fetchUIDLFromREPL(url)) as VProjectUIDL - await generateProjectFromUIDL({ + const projectName = await generateProjectFromUIDL({ uidl, projectType: ProjectType.NEXT, targetPath, @@ -95,11 +98,16 @@ export default async function (options: { url: string; targetPath: string; force force, }) - spinner.text = `Project Generated Successfully` + updateConfigFile((content) => { + content.project.name = projectName + }) + + spinner.text = `Project Generated Successfully ${projectName}` spinner.succeed() } catch (e) { spinner.text = `Project Generation Failed` spinner.fail() + throw new Error(e) } } else { try { @@ -107,7 +115,13 @@ export default async function (options: { url: string; targetPath: string; force uidl = (await fetchUIDLFromREPL(url)) as VComponentUIDL const { files } = await generateComponentFromUIDL(uidl, getComponentType()) - injectFilesToPath(process.cwd(), targetPath, files, force) + + injectFilesToPath({ + rootFolder: process.cwd(), + targetPath, + files, + force, + }) updateConfigFile((content) => { content.components[url] = { url, path: targetPath } }) @@ -117,6 +131,7 @@ export default async function (options: { url: string; targetPath: string; force } catch (e) { spinner.text = `Component Generation Failed` spinner.fail() + throw new Error(e) } } } diff --git a/packages/teleport-cli/src/constants.ts b/packages/teleport-cli/src/constants.ts index b262b4a8e..0be5c980a 100644 --- a/packages/teleport-cli/src/constants.ts +++ b/packages/teleport-cli/src/constants.ts @@ -12,7 +12,6 @@ export const STUDIO_URL = 'https://playground-api-production-v5.services.telepor export const UUDID_REGEX = /[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/gm export const IGNORE_FOLDERS = ['node_modules', 'dist', 'bin', 'build'] export const IGNORE_EXTENSIONS = ['.json', '.test.js', '.test.ts', '.map', '.d.ts', '.md'] - export interface DefaultConfigTemplate { project?: { url?: string diff --git a/packages/teleport-cli/src/services/file.ts b/packages/teleport-cli/src/services/file.ts index c182fbafc..10dea8340 100644 --- a/packages/teleport-cli/src/services/file.ts +++ b/packages/teleport-cli/src/services/file.ts @@ -5,27 +5,40 @@ import { GeneratedFile, GeneratedFolder } from '@teleporthq/teleport-types' import { getPatchesBetweenFiles, mergeFiles } from './merge' import { IGNORE_EXTENSIONS } from '../constants' -export const injectFilesFromSubFolder = ( - folder: GeneratedFolder[], - targetPath: string, - force = false -) => { +export const injectFilesFromSubFolder = (params: { + folder: GeneratedFolder[] + targetPath: string + force: boolean +}) => { + const { folder, targetPath, force = false } = params folder.map((items) => { const { files, subFolders, name } = items + ensureDirSync(path.join(process.cwd(), targetPath, name)) - injectFilesToPath(process.cwd(), path.join(targetPath, name), files, force) - injectFilesFromSubFolder(subFolders, path.join(targetPath, name), force) + injectFilesToPath({ + rootFolder: process.cwd(), + targetPath: path.join(targetPath, name), + force, + files, + }) + injectFilesFromSubFolder({ + folder: subFolders, + targetPath: path.join(targetPath, name), + force, + }) }) } -export const injectFilesToPath = ( - rootFolder: string, - targetPath: string, - files: GeneratedFile[], - force = false -): void => { +export const injectFilesToPath = (params: { + rootFolder: string + targetPath: string + files: GeneratedFile[] + force: boolean +}): void => { try { + const { files, force = false, targetPath, rootFolder } = params ensureDirSync(path.join(rootFolder, targetPath)) + files.map((file) => { const fileName = `${file.name}.${file.fileType}` const filePath = path.join(rootFolder, targetPath, fileName) @@ -50,6 +63,7 @@ export const injectFilesToPath = ( } const patches = getPatchesBetweenFiles(localFile, file.content) + const fileContent = mergeFiles(patches) writeFileSync(filePath, fileContent, 'utf-8') }) diff --git a/packages/teleport-cli/src/services/index.ts b/packages/teleport-cli/src/services/index.ts index 297d52da6..283d6f576 100644 --- a/packages/teleport-cli/src/services/index.ts +++ b/packages/teleport-cli/src/services/index.ts @@ -13,7 +13,7 @@ import { FileType, } from '@teleporthq/teleport-types' import { generateComponent, packProject } from '@teleporthq/teleport-code-generator' -import { injectFilesFromSubFolder, injectFilesToPath } from './file' +import { injectFilesFromSubFolder, injectFilesToPath, findFileByName } from './file' import { BASE_URL, STUDIO_URL, @@ -21,7 +21,9 @@ import { CONFIG_FILE_NAME, DEFALT_CONFIG_TEMPLATE, DefaultConfigTemplate, + DEFAULT_CONFIG_FILE_NAME, } from '../constants' +import { getPackageJSON } from '../utils' export const fetchUIDLFromREPL = async (url: string): Promise> => { const id = url.match(UUDID_REGEX)[0] @@ -61,9 +63,10 @@ export const generateProjectFromUIDL = async ({ targetPath: string url: string force?: boolean -}) => { +}): Promise => { try { ensureDirSync(path.join(process.cwd(), targetPath)) + const { success, payload } = (await packProject(uidl as ProjectUIDL, { projectType, publishOptions: { @@ -71,23 +74,52 @@ export const generateProjectFromUIDL = async ({ }, })) as { success: boolean; payload: GeneratedFolder } if (success) { - const { files, subFolders, name } = payload as GeneratedFolder + const { files, subFolders } = payload as GeneratedFolder + let { name } = payload as GeneratedFolder + const packageJSON = getPackageJSON() + const teleportConfig = findFileByName(DEFAULT_CONFIG_FILE_NAME) + + if (uidl?.name) { + name = uidl.name + } + + if (packageJSON?.name) { + name = packageJSON?.name as string + } + + if (teleportConfig) { + name = (JSON.parse(teleportConfig) as DefaultConfigTemplate)?.project.name + } + files.push({ name: CONFIG_FILE_NAME, fileType: FileType.JSON, content: JSON.stringify( { ...DEFALT_CONFIG_TEMPLATE, - project: { url, projectType, name: files[0].name }, + project: { url, projectType, name }, } as DefaultConfigTemplate, null, 2 ), }) - injectFilesFromSubFolder(subFolders, path.join(targetPath, name), force) + + injectFilesFromSubFolder({ + folder: subFolders, + targetPath: path.join(targetPath, name), + force, + }) + files.forEach((file) => { - injectFilesToPath(process.cwd(), path.join(targetPath, name), [file], force) + injectFilesToPath({ + rootFolder: process.cwd(), + targetPath: path.join(targetPath, name), + files: [file], + force, + }) }) + + return name } } catch (e) { console.warn(e) diff --git a/packages/teleport-cli/src/utils.ts b/packages/teleport-cli/src/utils.ts index 25db51ae9..bf9c88b7d 100644 --- a/packages/teleport-cli/src/utils.ts +++ b/packages/teleport-cli/src/utils.ts @@ -22,24 +22,30 @@ export const updateConfigFile = ( } export const getComponentType = (): ComponentType => { - const packageJSON = JSON.parse(findFileByName(`package.json`)) as Record< - string, - Record - > - if (!packageJSON) { - console.warn(chalk.yellow(`Please run the command inside a project that contains package.json`)) - return - } + const packageJSON = getPackageJSON() + return findFlavourByDependencies( Object.keys( { - ...packageJSON.dependencies, - ...packageJSON.devDependencies, + ...(((packageJSON?.dependencies as unknown) as Record) || {}), + ...(((packageJSON?.devDependencies as unknown) as Record) || {}), } || {} ) ) } +export const getPackageJSON = (): Record< + string, + Record | string | number +> | null => { + const json = findFileByName(`package.json`) + if (!json) { + console.warn(chalk.yellow(`Please run the command inside a project that contains package.json`)) + return null + } + return JSON.parse(json) as Record> +} + export const extractCompIdsFromURls = (components: string[]) => components.reduce((acc: Record, comp: string) => { const slug = comp.split('/')[4] || null diff --git a/packages/teleport-mapper/src/studio/index.ts b/packages/teleport-mapper/src/studio/index.ts index 677d7238c..93e169738 100644 --- a/packages/teleport-mapper/src/studio/index.ts +++ b/packages/teleport-mapper/src/studio/index.ts @@ -471,7 +471,7 @@ export class MapSnapshotToUIDL implements MapSnapshowToUIDLInterface { // favicon if (settings.general?.favicon) { - const faviconAsset = projectDoc.assets.byId[settings.general?.favicon] + const faviconAsset = this.snapshot.assets.byId[settings.general?.favicon] if (faviconAsset) { globals.assets?.push({ diff --git a/packages/teleport-project-generator/src/index.ts b/packages/teleport-project-generator/src/index.ts index c992b489a..2867cdc8d 100644 --- a/packages/teleport-project-generator/src/index.ts +++ b/packages/teleport-project-generator/src/index.ts @@ -1,6 +1,6 @@ import PathResolver from 'path' import { UIDLUtils } from '@teleporthq/teleport-shared' -import { Validator, Parser } from '@teleporthq/teleport-uidl-validator' +// import { Validator } from '@teleporthq/teleport-uidl-validator' import { resolveStyleSetDefinitions } from '@teleporthq/teleport-uidl-resolver' import { GeneratorOptions, @@ -15,6 +15,7 @@ import { InMemoryFileRecord, TeleportError, GeneratorFactoryParams, + ProjectUIDL, } from '@teleporthq/teleport-types' import { injectFilesToPath, @@ -47,11 +48,11 @@ export class ProjectGenerator { public routerGenerator: ComponentGenerator public styleSheetGenerator: ComponentGenerator private strategy: ProjectStrategy - private validator: Validator + // private validator: Validator private assemblyLine: ProjectAssemblyLine constructor(strategy: ProjectStrategy) { - this.validator = new Validator() + // this.validator = new Validator() this.strategy = strategy this.assemblyLine = new ProjectAssemblyLine() } @@ -130,7 +131,7 @@ export class ProjectGenerator { template: GeneratedFolder = DEFAULT_TEMPLATE, mapping: Mapping = {} ): Promise { - let cleanedUIDL = input + // let cleanedUIDL = input let collectedDependencies: Record = {} let collectedDevDependencies: Record = {} let inMemoryFilesMap = new Map() @@ -138,16 +139,17 @@ export class ProjectGenerator { // Initialize output folder and other reusable structures const rootFolder = UIDLUtils.cloneObject(template || DEFAULT_TEMPLATE) - const schemaValidationResult = this.validator.validateProjectSchema(input) - const { valid, projectUIDL } = schemaValidationResult - if (valid && projectUIDL) { - cleanedUIDL = (projectUIDL as unknown) as Record - } else { - throw new Error(schemaValidationResult.errorMsg) - } - - const uidl = Parser.parseProjectJSON(cleanedUIDL) - + // const schemaValidationResult = this.validator.validateProjectSchema(input) + // const schemaValidationResult = input + // const { valid, projectUIDL } = schemaValidationResult + // if (valid && projectUIDL) { + // cleanedUIDL = (projectUIDL as unknown) as Record + // } else { + // throw new Error(schemaValidationResult.errorMsg) + // } + + // const uidl = Parser.parseProjectJSON(input) + const uidl = (input as unknown) as ProjectUIDL /* Style sets contains only on project level. So passing them through componentcycle just resolves for that component alone and don't have any impact for other components. So, resolving happens here and is passed to all components. */ @@ -155,10 +157,10 @@ export class ProjectGenerator { uidl.root.styleSetDefinitions = resolveStyleSetDefinitions(uidl.root.styleSetDefinitions) } - const contentValidationResult = this.validator.validateProjectContent(uidl) - if (!contentValidationResult.valid) { - throw new Error(contentValidationResult.errorMsg) - } + // const contentValidationResult = this.validator.validateProjectContent(uidl) + // if (!contentValidationResult.valid) { + // throw new Error(contentValidationResult.errorMsg) + // } try { const runBeforeResult = await this.assemblyLine.runBefore({ @@ -236,6 +238,7 @@ export class ProjectGenerator { ) { const { files, dependencies } = await this.styleSheetGenerator.generateComponent(uidl.root, { isRootComponent: true, + skipValidation: true, }) inMemoryFilesMap.set('projectStyleSheet', {