Skip to content

Commit

Permalink
refactor: Changes after pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaionut12345 committed Mar 29, 2019
1 parent 57c4223 commit 32832cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 deletions.
28 changes: 13 additions & 15 deletions src/project-generators/react-basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { createRouterIndexFile, buildFolderStructure } from './utils'
import {
createPackageJSONFile,
createHtmlIndexFile,
createPageFile,
createComponentFile,
joinComponentGeneratorOutputs,
createdPageOutputs,
createComponentOutputs,
joinGeneratorOutputs,
createManifestJSONFile,
} from '../../shared/utils/project-utils'

Expand All @@ -38,17 +38,15 @@ const initGenerator = (options: ProjectGeneratorOptions): ComponentGenerator =>
const createReactBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {}) => {
const reactGenerator = initGenerator(generatorOptions)

const addCustomMapping = (mappingOptions: ProjectGeneratorOptions = {}) => {
if (!mappingOptions.customMapping) {
return
}

reactGenerator.addMapping(mappingOptions.customMapping)
const addCustomMapping = (mapping: Mapping) => {
reactGenerator.addMapping(mapping)
}

const generateProject = async (uidl: ProjectUIDL, options: ProjectGeneratorOptions = {}) => {
// Step 0: Add any custom mappings found in the options
addCustomMapping(options)
if (options.customMapping) {
addCustomMapping(options.customMapping)
}

const { components = {}, root } = uidl
const { states = [] } = root.content
Expand Down Expand Up @@ -82,7 +80,7 @@ const createReactBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {
localDependenciesPrefix: LOCAL_DEPENDENCIES_PREFIX,
},
}
return createPageFile(pageParams)
return createdPageOutputs(pageParams)
})

// Step 2: The components generation process is started
Expand All @@ -93,18 +91,18 @@ const createReactBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {
componentUIDL,
componentOptions: { assetsPrefix: ASSETS_PREFIX },
}
return createComponentFile(componentParams)
return createComponentOutputs(componentParams)
})

// Step 3: The process of creating the pages and the components is awaited
const createdPageFiles = await Promise.all(pagePromises)
const createdComponentFiles = await Promise.all(componentPromises)

// Step 4: The generated page and component files are joined
const joinedPageFiles = joinComponentGeneratorOutputs(createdPageFiles)
const joinedPageFiles = joinGeneratorOutputs(createdPageFiles)
const pageFiles = joinedPageFiles.files

const joinedComponentFiles = joinComponentGeneratorOutputs(createdComponentFiles)
const joinedComponentFiles = joinGeneratorOutputs(createdComponentFiles)
const componentFiles = joinedComponentFiles.files

// Step 5: Global settings are transformed into the root html file and the manifest file for PWA support
Expand All @@ -116,7 +114,7 @@ const createReactBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {

// Step 6: Create the routing component (index.js)
const { routerFile, externalDependencies } = await createRouterIndexFile(root)
const htmlIndexFile = createHtmlIndexFile({ uidl, assetsPrefix: ASSETS_PREFIX })
const htmlIndexFile = createHtmlIndexFile(uidl, { assetsPrefix: ASSETS_PREFIX })

const srcFiles: GeneratedFile[] = [htmlIndexFile, routerFile]

Expand Down
26 changes: 12 additions & 14 deletions src/project-generators/react-next/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createPageFile,
createComponentFile,
joinComponentGeneratorOutputs,
createdPageOutputs,
createComponentOutputs,
joinGeneratorOutputs,
createManifestJSONFile,
createPackageJSONFile,
} from '../../shared/utils/project-utils'
Expand Down Expand Up @@ -37,17 +37,15 @@ const initGenerator = (options: ProjectGeneratorOptions): ComponentGenerator =>
const createReactNextGenerator = (generatorOptions: ProjectGeneratorOptions = {}) => {
const reactGenerator = initGenerator(generatorOptions)

const addCustomMapping = (mappingOptions: ProjectGeneratorOptions = {}) => {
if (!mappingOptions.customMapping) {
return
}

reactGenerator.addMapping(mappingOptions.customMapping)
const addCustomMapping = (mapping: Mapping) => {
reactGenerator.addMapping(mapping)
}

const generateProject = async (uidl: ProjectUIDL, options: ProjectGeneratorOptions = {}) => {
// Step 0: Add any custom mappings found in the options
addCustomMapping(options)
if (options.customMapping) {
addCustomMapping(options.customMapping)
}

const { components = {}, root } = uidl
const { states = [] } = root.content
Expand Down Expand Up @@ -88,7 +86,7 @@ const createReactNextGenerator = (generatorOptions: ProjectGeneratorOptions = {}
convertDefaultToIndex: true,
},
}
return createPageFile(pageParams)
return createdPageOutputs(pageParams)
})

// Step 3: The components generation process is started
Expand All @@ -99,18 +97,18 @@ const createReactNextGenerator = (generatorOptions: ProjectGeneratorOptions = {}
componentGenerator: reactGenerator,
componentOptions: { assetsPrefix: ASSETS_PREFIX },
}
return createComponentFile(componentParams)
return createComponentOutputs(componentParams)
})

// Step 3: The process of creating the pages and the components is awaited
const createdPageFiles = await Promise.all(pagePromises)
const createdComponentFiles = await Promise.all(componentPromises)

// Step 4: The generated page and component files are joined
const joinedPageFiles = joinComponentGeneratorOutputs(createdPageFiles)
const joinedPageFiles = joinGeneratorOutputs(createdPageFiles)
const pageFiles: GeneratedFile[] = documentComponentFile.concat(joinedPageFiles.files)

const joinedComponentFiles = joinComponentGeneratorOutputs(createdComponentFiles)
const joinedComponentFiles = joinGeneratorOutputs(createdComponentFiles)
const componentFiles: GeneratedFile[] = joinedComponentFiles.files

// Step 5: Global settings are transformed into the manifest file for PWA support
Expand Down
28 changes: 13 additions & 15 deletions src/project-generators/vue-basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import createVueGenerator from '../../component-generators/vue/vue-component'
import {
createPageFile,
createComponentFile,
joinComponentGeneratorOutputs,
createdPageOutputs,
createComponentOutputs,
joinGeneratorOutputs,
createManifestJSONFile,
createHtmlIndexFile,
createPackageJSONFile,
Expand Down Expand Up @@ -33,17 +33,15 @@ const initGenerator = (options: ProjectGeneratorOptions): ComponentGenerator =>
const createVueBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {}) => {
const vueGenerator = initGenerator(generatorOptions)

const addCustomMapping = (mappingOptions: ProjectGeneratorOptions = {}) => {
if (!mappingOptions.customMapping) {
return
}

vueGenerator.addMapping(mappingOptions.customMapping)
const addCustomMapping = (mapping: Mapping) => {
vueGenerator.addMapping(mapping)
}

const generateProject = async (uidl: ProjectUIDL, options: ProjectGeneratorOptions = {}) => {
// Step 0: Add any custom mappings found in the options
addCustomMapping(options)
if (options.customMapping) {
addCustomMapping(options.customMapping)
}

const { components = {}, root } = uidl
const { states = [] } = root.content
Expand Down Expand Up @@ -78,7 +76,7 @@ const createVueBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {})
localDependenciesPrefix: LOCAL_DEPENDENCIES_PREFIX,
},
}
return createPageFile(pageParams)
return createdPageOutputs(pageParams)
})

// Step 2: The components generation process is started
Expand All @@ -91,18 +89,18 @@ const createVueBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {})
componentOptions: { assetsPrefix: ASSETS_PREFIX },
}

return createComponentFile(componentParams)
return createComponentOutputs(componentParams)
})

// Step 3: The process of creating the pages and the components is awaited
const createdPageFiles = await Promise.all(pagePromises)
const createdComponentFiles = await Promise.all(componentPromises)

// Step 4: The generated page and component files are joined
const joinedPageFiles = joinComponentGeneratorOutputs(createdPageFiles)
const joinedPageFiles = joinGeneratorOutputs(createdPageFiles)
const pageFiles: GeneratedFile[] = [].concat(joinedPageFiles.files)

const joinedComponentFiles = joinComponentGeneratorOutputs(createdComponentFiles)
const joinedComponentFiles = joinGeneratorOutputs(createdComponentFiles)
const componentFiles = joinedComponentFiles.files

// Step 5: Global settings are transformed into the root html file and the manifest file for PWA support
Expand All @@ -112,7 +110,7 @@ const createVueBasicGenerator = (generatorOptions: ProjectGeneratorOptions = {})
publicFiles.push(manifestFile)
}

const htmlIndexFile = createHtmlIndexFile({ uidl, assetsPrefix: ASSETS_PREFIX })
const htmlIndexFile = createHtmlIndexFile(uidl, { assetsPrefix: ASSETS_PREFIX })
publicFiles.push(htmlIndexFile)

// Step 6: Create the routing component (router.js)
Expand Down
29 changes: 13 additions & 16 deletions src/project-generators/vue-nuxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
import { FILE_EXTENSIONS } from '../../shared/constants'

import {
createPageFile,
createComponentFile,
createdPageOutputs,
createComponentOutputs,
createManifestJSONFile,
createHtmlIndexFile,
createPackageJSONFile,
joinComponentGeneratorOutputs,
joinGeneratorOutputs,
} from '../../shared/utils/project-utils'

const initGenerator = (options: ProjectGeneratorOptions): ComponentGenerator => {
Expand All @@ -35,17 +35,15 @@ const initGenerator = (options: ProjectGeneratorOptions): ComponentGenerator =>
const createVueNuxtGenerator = (generatorOptions: ProjectGeneratorOptions = {}) => {
const vueGenerator = initGenerator(generatorOptions)

const addCustomMapping = (mappingOptions: ProjectGeneratorOptions = {}) => {
if (!mappingOptions.customMapping) {
return
}

vueGenerator.addMapping(mappingOptions.customMapping)
const addCustomMapping = (mapping: Mapping) => {
vueGenerator.addMapping(mapping)
}

const generateProject = async (uidl: ProjectUIDL, options: ProjectGeneratorOptions = {}) => {
// Step 0: Add any custom mappings found in the options
addCustomMapping(options)
if (options.customMapping) {
addCustomMapping(options.customMapping)
}

const { components = {}, root } = uidl
const { states = [] } = root.content
Expand Down Expand Up @@ -85,7 +83,7 @@ const createVueNuxtGenerator = (generatorOptions: ProjectGeneratorOptions = {})
},
}

return createPageFile(pageParams)
return createdPageOutputs(pageParams)
})

// Step 2: The components generation process is started
Expand All @@ -97,18 +95,18 @@ const createVueNuxtGenerator = (generatorOptions: ProjectGeneratorOptions = {})
componentGenerator: vueGenerator,
componentOptions: { assetsPrefix: ASSETS_PREFIX },
}
return createComponentFile(componentParams)
return createComponentOutputs(componentParams)
})

// Step 3: The process of creating the pages and the components is awaited
const createdPageFiles = await Promise.all(pagePromises)
const createdComponentFiles = await Promise.all(componentPromises)

// Step 4: The generated page and component files are joined
const joinedPageFiles = joinComponentGeneratorOutputs(createdPageFiles)
const joinedPageFiles = joinGeneratorOutputs(createdPageFiles)
const pageFiles = joinedPageFiles.files

const joinedComponentFiles = joinComponentGeneratorOutputs(createdComponentFiles)
const joinedComponentFiles = joinGeneratorOutputs(createdComponentFiles)
const componentFiles = joinedComponentFiles.files

// Step 5: Global settings are transformed into the manifest file for PWA support
Expand All @@ -118,8 +116,7 @@ const createVueNuxtGenerator = (generatorOptions: ProjectGeneratorOptions = {})
staticFiles.push(manifestFile)
}

const htmlIndexFile = createHtmlIndexFile({
uidl,
const htmlIndexFile = createHtmlIndexFile(uidl, {
assetsPrefix: ASSETS_PREFIX,
fileName: 'app',
appRootOverride: APP_ROOT_OVERRIDE,
Expand Down
18 changes: 10 additions & 8 deletions src/shared/utils/project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import { prefixPlaygroundAssetsURL, extractPageMetadata } from './uidl-utils'
import { slugify, sanitizeVariableName } from './string-utils'
import { FILE_EXTENSIONS } from '../constants'

interface HtmlIndexFileParams {
uidl: ProjectUIDL
assetsPrefix: string
interface HtmlIndexFileOptions {
assetsPrefix?: string
fileName?: string
appRootOverride?: string
}

export const createHtmlIndexFile = (params: HtmlIndexFileParams): GeneratedFile => {
const { uidl, assetsPrefix, fileName = 'index', appRootOverride } = params
export const createHtmlIndexFile = (
uidl: ProjectUIDL,
options: HtmlIndexFileOptions
): GeneratedFile => {
const { assetsPrefix = '', fileName = 'index', appRootOverride } = options
const { settings, meta, assets, manifest } = uidl.globals

const htmlNode = createHTMLNode('html')
Expand Down Expand Up @@ -177,7 +179,7 @@ export const createPackageJSONFile = (
return createFile('package', FILE_EXTENSIONS.JSON, JSON.stringify(content, null, 2))
}

export const createPageFile = async (
export const createdPageOutputs = async (
params: ComponentFactoryParams
): Promise<ComponentGeneratorOutput> => {
const {
Expand Down Expand Up @@ -226,7 +228,7 @@ export const createPageFile = async (
return { files, dependencies }
}

export const createComponentFile = async (
export const createComponentOutputs = async (
params: ComponentFactoryParams
): Promise<ComponentGeneratorOutput> => {
let dependencies: Record<string, string> = {}
Expand Down Expand Up @@ -256,7 +258,7 @@ export const createComponentFile = async (
return { files, dependencies }
}

export const joinComponentGeneratorOutputs = (
export const joinGeneratorOutputs = (
generatorOutputs: ComponentGeneratorOutput[]
): ComponentGeneratorOutput => {
return generatorOutputs.reduce(
Expand Down

0 comments on commit 32832cd

Please sign in to comment.