Skip to content

Commit

Permalink
feat(docz-core): add docz init command
Browse files Browse the repository at this point in the history
  • Loading branch information
rakannimer committed Sep 3, 2019
1 parent cb5e4ae commit 2cd87ee
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
11 changes: 9 additions & 2 deletions core/docz-core/src/bundler/machine/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ export const ensureFiles = () => {
themeDirs.forEach(dir => ensureFile(dir))
}

export const getIsFirstInstall = () => {
return !sh.test('-e', paths.docz)
}
export const getIsDoczRepo = () => {
return sh.test('-e', path.join(paths.root, '../../core'))
}

export const assignFirstInstall = assign((ctx: ServerMachineCtx) => {
const firstInstall = !sh.test('-e', paths.docz)
const firstInstall = getIsFirstInstall()
return assoc('firstInstall', firstInstall, ctx)
})

export const checkIsDoczRepo = assign((ctx: ServerMachineCtx) => {
const isDoczRepo = sh.test('-e', path.join(paths.root, '../../core'))
const isDoczRepo = getIsDoczRepo()
return assoc('isDoczRepo', isDoczRepo, ctx)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import { createDeps } from '../../../utils/create-deps'
import { ServerMachineCtx } from '../context'
import { outputFileFromTemplate } from '../../../utils/template'

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

export const copyDoczRc = async () => {
const filepath = await findUp(finds('docz'))
filepath && sh.cp(filepath, paths.docz)
Expand Down Expand Up @@ -94,7 +89,6 @@ const writeGatsbyBrowser = async () =>

export const createResources = async (ctx: ServerMachineCtx) => {
try {
copyPkgJSON()
await copyDoczRc()
await copyAndModifyPkgJson(ctx)
await writeEslintRc(ctx)
Expand Down
4 changes: 4 additions & 0 deletions core/docz-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import * as commands from './commands'

export const cli = () => {
return yargs
.command('init', 'initialize docz in your app', setArgs, async args => {
setEnv('development')
await commands.init(args)
})
.command('dev', 'initialize docz dev server', setArgs, async args => {
setEnv('development')
await commands.dev(args)
Expand Down
1 change: 1 addition & 0 deletions core/docz-core/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { dev } from './dev'
export { build } from './build'
export { serve } from './serve'
export { init } from './init'
36 changes: 36 additions & 0 deletions core/docz-core/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
process.setMaxListeners(Infinity)

import { Arguments } from 'yargs'
import { finds } from 'load-cfg'
import findUp from 'find-up'

import { parseConfig } from '../config/docz'
import { getIsFirstInstall, getIsDoczRepo } from '../bundler/machine/actions'
import {
ensureDirs,
createResources,
installDeps,
} from '../bundler/machine/services'

export const init = async (args: Arguments<any>) => {
const doczrcFilepath = await findUp(finds('docz'))
const config = await parseConfig(args)
const isFirstInstall = getIsFirstInstall()
const isDoczRepo = getIsDoczRepo()
await ensureDirs()
const serverMachineContext = {
args: config,
isDoczRepo,
firstInstall: isFirstInstall,
doczrcFilepath,
}
await createResources(serverMachineContext)
await installDeps(serverMachineContext)
console.log()
console.log(`✅ Docz is ready to go `)
console.log()
console.log(`💻 yarn docz dev`)
console.log(`⛏ yarn docz build`)
console.log(`👀 yarn docz serve`)
console.log()
}
8 changes: 5 additions & 3 deletions core/docz-core/src/utils/create-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export const createDeps = async (ctx: ServerMachineCtx) => {
const pkg = await fs.readJSON(filepath, { throws: false })
const dependencies = pkg.dependencies
return {
dependencies,
devDependencies: {
...pkg.devDependencies,
dependencies: {
...dependencies,
...(await getDeps(REQUIRED_DEV_DEPS, ctx, pkg)),
...(await getCoreDeps(ctx, pkg)),
},
devDependencies: {
...pkg.devDependencies,
},
}
}

0 comments on commit 2cd87ee

Please sign in to comment.