Skip to content

Commit

Permalink
DIsabling validator in project temporarly
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed May 15, 2021
1 parent ccd1edd commit e5037f9
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/teleport-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
33 changes: 24 additions & 9 deletions packages/teleport-cli/src/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')

Expand All @@ -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)
}
}

Expand All @@ -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 }
})
Expand All @@ -87,27 +90,38 @@ 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,
url,
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 {
spinner.text = `Fetching component from repl \n`

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 }
})
Expand All @@ -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)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/teleport-cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 27 additions & 13 deletions packages/teleport-cli/src/services/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -50,6 +63,7 @@ export const injectFilesToPath = (
}

const patches = getPatchesBetweenFiles(localFile, file.content)

const fileContent = mergeFiles(patches)
writeFileSync(filePath, fileContent, 'utf-8')
})
Expand Down
44 changes: 38 additions & 6 deletions packages/teleport-cli/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ 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,
UUDID_REGEX,
CONFIG_FILE_NAME,
DEFALT_CONFIG_TEMPLATE,
DefaultConfigTemplate,
DEFAULT_CONFIG_FILE_NAME,
} from '../constants'
import { getPackageJSON } from '../utils'

export const fetchUIDLFromREPL = async (url: string): Promise<Record<string, unknown>> => {
const id = url.match(UUDID_REGEX)[0]
Expand Down Expand Up @@ -61,33 +63,63 @@ export const generateProjectFromUIDL = async ({
targetPath: string
url: string
force?: boolean
}) => {
}): Promise<string> => {
try {
ensureDirSync(path.join(process.cwd(), targetPath))

const { success, payload } = (await packProject(uidl as ProjectUIDL, {
projectType,
publishOptions: {
outputPath: targetPath,
},
})) 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)
Expand Down
26 changes: 16 additions & 10 deletions packages/teleport-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ export const updateConfigFile = (
}

export const getComponentType = (): ComponentType => {
const packageJSON = JSON.parse(findFileByName(`package.json`)) as Record<
string,
Record<string, unknown>
>
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<string, string>) || {}),
...(((packageJSON?.devDependencies as unknown) as Record<string, string>) || {}),
} || {}
)
)
}

export const getPackageJSON = (): Record<
string,
Record<string, unknown> | 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<string, Record<string, unknown | string | number>>
}

export const extractCompIdsFromURls = (components: string[]) =>
components.reduce((acc: Record<string, string>, comp: string) => {
const slug = comp.split('/')[4] || null
Expand Down
2 changes: 1 addition & 1 deletion packages/teleport-mapper/src/studio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading

0 comments on commit e5037f9

Please sign in to comment.