Skip to content

Commit

Permalink
feat(docz-core): read name from package.json to populate initial title
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 1, 2018
1 parent 10db2c6 commit 4f10a6d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"cpy": "^7.0.1",
"deepmerge": "^2.1.1",
"detect-port": "^1.2.3",
"docz-theme-default": "^0.3.4",
"express": "^4.16.3",
"fast-glob": "^2.2.2",
"file-loader": "^1.1.11",
Expand Down Expand Up @@ -66,6 +65,7 @@
"resolve": "^1.8.1",
"signale": "^1.2.1",
"strip-indent": "^2.0.0",
"titleize": "^1.0.1",
"to-vfile": "^5.0.0",
"uglifyjs-webpack-plugin": "^1.2.7",
"ulid": "^2.3.0",
Expand Down
15 changes: 14 additions & 1 deletion packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import * as fs from 'fs-extra'
import humanize from 'humanize-string'
import titleize from 'titleize'

import { Plugin } from '../Plugin'
import { BabelRC } from '../utils/babelrc'
import * as paths from '../config/paths'

const removeScope = (name: string) => name.replace(/^@.*\//, '')
const getInitialTitle = () => {
const pkg = fs.readJsonSync(paths.packageJson, { throws: false })
const name = pkg ? pkg.name : 'MyDoc'

return titleize(humanize(removeScope(name)))
}

export interface Argv {
/* io args */
Expand Down Expand Up @@ -59,7 +72,7 @@ export const args = (yargs: any) => {
})
yargs.positional('title', {
type: 'string',
default: 'MyDoc',
default: getInitialTitle(),
})
yargs.positional('description', {
type: 'string',
Expand Down
1 change: 1 addition & 0 deletions packages/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare module '@mdx-js/mdx'
declare module 'chokidar'
declare module '@sindresorhus/slugify'
declare module 'humanize-string'
declare module 'titleize'
declare module 'unified'
declare module 'strip-indent'
declare module 'unist-util-is'
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11037,6 +11037,10 @@ tiny-emitter@^2.0.0:
version "2.0.2"
resolved "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.0.2.tgz#82d27468aca5ade8e5fd1e6d22b57dd43ebdfb7c"

titleize@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/titleize/-/titleize-1.0.1.tgz#21bc24fcca658eadc6d3bd3c38f2bd173769b4c5"

tmp@^0.0.29:
version "0.0.29"
resolved "https://registry.npmjs.org/tmp/-/tmp-0.0.29.tgz#f25125ff0dd9da3ccb0c2dd371ee1288bb9128c0"
Expand Down

1 comment on commit 4f10a6d

@mAAdhaTTah
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit doesn't account for a package.json existing but not having a name property, e.g. in a monorepo. I get this stack trace:

$ docz dev
/path/to/project/node_modules/yargs/yargs.js:1144
      else throw err
           ^

TypeError: Cannot read property 'replace' of undefined
    at removeScope (/path/to/project/node_modules/docz-core/dist/index.js:1:21075)
    at getInitialTitle (/path/to/project/node_modules/docz-core/dist/index.js:1:21193)
    at Object.args [as builder] (/path/to/project/node_modules/docz-core/dist/index.js:1:21517)
    at Object.runCommand (/path/to/project/node_modules/yargs/lib/command.js:185:35)
    at Object.parseArgs [as _parseArgs] (/path/to/project/node_modules/yargs/yargs.js:1059:30)
    at Object.get [as argv] (/path/to/project/node_modules/yargs/yargs.js:1000:21)
    at Object.<anonymous> (/path/to/project/node_modules/docz/bin/index.js:13:74)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Please sign in to comment.