Skip to content

Commit

Permalink
fix(docz-core): create deps algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 12, 2019
1 parent a93636c commit a4b8eb9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
61 changes: 10 additions & 51 deletions core/docz-core/src/bundler/machine/services/create-resources.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,18 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { finds } from 'load-cfg'
import { omit, getOr, fromPairs } from 'lodash/fp'
import latestVersion from 'latest-version'
import { omit, merge } from 'lodash/fp'
import findUp from 'find-up'
import sh from 'shelljs'

import * as paths from '../../../config/paths'
import { createDeps } from '../../../utils/create-deps'
import { ServerMachineCtx } from '../context'
import { outputFileFromTemplate } from '../../../utils/template'

const REQUIRED_DEPS = ['react', 'react-dom']
const REQUIRED_DEV_DEPS = ['gatsby', 'gatsby-mdx', 'gatsby-plugin-typescript']
const CORE_DEV_DEPS = ['docz', 'gatsby-theme-docz']
const LOCAL_DEV_DEPS = ['gatsby-plugin-compile-es6-packages', 'p-reduce']

const depsFromPairs = async (deps: any[], callback: (dep: string) => any) => {
return fromPairs(await Promise.all(deps.map(callback)))
}

const getDependencyVersion = async (dep: string) => {
const version = await latestVersion(dep)
return [dep, version]
}

const getCoreVersions = async (dep: string) => {
const depPath = path.join(paths.root, '../../core', dep)
const pkgJSONFilepath = path.join(depPath, 'package.json')
const pkgJSON = await fs.readJSON(pkgJSONFilepath, { throws: false })
const version = await latestVersion(dep)
return [dep, getOr(version, 'version', pkgJSON)]
}

const getDeps = async (deps: any[], ctx: ServerMachineCtx) => {
const list = ctx.isDoczRepo ? deps.concat(LOCAL_DEV_DEPS) : deps
return depsFromPairs(list, getDependencyVersion)
}

const getCoreDeps = async ({ isDoczRepo }: ServerMachineCtx) => {
const fn = isDoczRepo ? getCoreVersions : getDependencyVersion
return depsFromPairs(CORE_DEV_DEPS, fn)
}

const copyPkgJSON = () => {
const pkgJSON = path.join(paths.root, 'package.json')
sh.cp(pkgJSON, paths.docz)
const pkg = path.join(paths.root, 'package.json')
sh.cp(pkg, paths.docz)
}

export const copyDoczRc = async () => {
Expand All @@ -55,19 +23,10 @@ export const copyDoczRc = async () => {
const copyAndModifyPkgJson = async (ctx: ServerMachineCtx) => {
const filepath = path.join(paths.root, 'package.json')
const movePath = path.join(paths.docz, 'package.json')
const pkgJSON = await fs.readJSON(filepath, { throws: false })

const newPkgJSON = {
...pkgJSON,
dependencies: {
...pkgJSON.dependencies,
...(await getDeps(REQUIRED_DEPS, ctx)),
},
devDependencies: {
...pkgJSON.devDependencies,
...(await getDeps(REQUIRED_DEV_DEPS, ctx)),
...(await getCoreDeps(ctx)),
},
const pkg = await fs.readJSON(filepath, { throws: false })
const deps = await createDeps(ctx)
const newPkg = merge(pkg, {
...deps,
scripts: {
dev: 'gatsby develop',
build: 'gatsby build',
Expand All @@ -77,9 +36,9 @@ const copyAndModifyPkgJson = async (ctx: ServerMachineCtx) => {
private: true,
workspaces: ['../../../core/**', '../../../other-packages/**'],
}),
}
})

await fs.outputJSON(movePath, newPkgJSON, { spaces: 2 })
await fs.outputJSON(movePath, newPkg, { spaces: 2 })
}

const writeEslintRc = async ({ isDoczRepo }: ServerMachineCtx) => {
Expand Down
59 changes: 59 additions & 0 deletions core/docz-core/src/utils/create-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { get, getOr, fromPairs } from 'lodash/fp'
import latestVersion from 'latest-version'

import * as paths from '../config/paths'
import { ServerMachineCtx } from '../bundler/machine/context'

const CORE_DEV_DEPS = ['gatsby-theme-docz']
const LOCAL_DEV_DEPS = ['gatsby-plugin-compile-es6-packages', 'p-reduce']
const REQUIRED_DEV_DEPS = ['gatsby', 'gatsby-plugin-typescript']

const depsFromPairs = async (deps: any[], callback: (dep: string) => any) => {
return fromPairs(await Promise.all(deps.map(callback)))
}

const getCoreVersions = () => async (dep: string) => {
const depPath = path.join(paths.root, '../../core', dep)
const pkgJSONFilepath = path.join(depPath, 'package.json')
const pkg = await fs.readJSON(pkgJSONFilepath, { throws: false })
const version = await latestVersion(dep)
return [dep, getOr(version, 'version', pkg)]
}

const getDepVersion = (deps: any, devDeps: any, find?: string) => async (
dep: string
) => {
const current = get(find || dep, deps) || get(find || dep, devDeps)
const latest = await latestVersion(dep)
return [dep, current || latest]
}

const getDeps = async (depList: any[], ctx: ServerMachineCtx, pkg: any) => {
const list = ctx.isDoczRepo ? depList.concat(LOCAL_DEV_DEPS) : depList
const deps = pkg.dependencies
const devDeps = pkg.devDependencies
return depsFromPairs(list, getDepVersion(deps, devDeps))
}

const getCoreDeps = async ({ isDoczRepo }: ServerMachineCtx, pkg: any) => {
const fn = isDoczRepo ? getCoreVersions : getDepVersion
const deps = pkg.dependencies
const devDeps = pkg.devDependencies
return depsFromPairs(CORE_DEV_DEPS, fn(deps, devDeps, 'docz'))
}

export const createDeps = async (ctx: ServerMachineCtx) => {
const filepath = path.join(paths.root, 'package.json')
const pkg = await fs.readJSON(filepath, { throws: false })

return {
dependencies: pkg.dependencies,
devDependencies: {
...pkg.devDependencies,
...(await getDeps(REQUIRED_DEV_DEPS, ctx, pkg)),
...(await getCoreDeps(ctx, pkg)),
},
}
}

0 comments on commit a4b8eb9

Please sign in to comment.