Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
fix(docz-core): config watch for directory operations
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 7, 2018
1 parent f75881e commit 43fa7ab
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 65 deletions.
5 changes: 0 additions & 5 deletions examples/basic/src/test.mdx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@mdx-js/mdx": "^0.8.1",
"@mdx-js/mdxast": "^0.7.2",
"@sindresorhus/slugify": "^0.3.0",
"@types/shelljs": "^0.7.9",
"art-template": "^4.12.2",
"babel-loader": "^8.0.0-beta.1",
"babel-polyfill": "^7.0.0-beta.3",
Expand All @@ -44,11 +45,11 @@
"koa-static": "^4.0.2",
"load-cfg": "^0.0.1",
"lodash.get": "^4.4.2",
"mkdirp": "^0.5.1",
"prettier": "^1.12.0",
"react-hot-loader": "4.1.2",
"remark-parse": "^5.0.0",
"resolve": "^1.7.1",
"shelljs": "^0.8.1",
"thread-loader": "^1.1.5",
"ulid": "^2.3.0",
"unified": "^6.2.0",
Expand All @@ -66,7 +67,6 @@
"@types/del": "^3.0.1",
"@types/express": "^4.11.1",
"@types/html-webpack-plugin": "^2.30.3",
"@types/mkdirp": "^0.5.2",
"@types/node": "10.0.4",
"@types/prettier": "^1.12.1",
"@types/resolve": "^0.0.7",
Expand Down
51 changes: 31 additions & 20 deletions packages/docz-core/src/Entries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as glob from 'fast-glob'
import * as fs from 'fs'
import * as path from 'path'
import * as mkdir from 'mkdirp'
import { test, mkdir } from 'shelljs'
import { compile } from 'art-template'
import stringify from 'json-stringify-pretty-compact'

Expand All @@ -13,11 +13,7 @@ import { Entry } from './Entry'
import { Config } from './commands/args'

const mkd = (dir: string): void => {
try {
fs.lstatSync(dir)
} catch (err) {
mkdir.sync(dir)
}
!test('-d', dir) && mkdir('-p', dir)
}

const touch = (file: string, raw: string) => {
Expand All @@ -38,21 +34,12 @@ const html = compiled('index.tpl.html')
export type EntryMap = Record<string, Entry>

export class Entries {
public files: string[]
public config: Config
public entries: EntryMap

constructor(config: Config) {
const { files: pattern } = config

const ignoreGlob = '!node_modules'
const files: string[] = glob.sync(
Array.isArray(pattern) ? [...pattern, ignoreGlob] : [pattern, ignoreGlob]
)

this.files = files
this.config = config
this.entries = this.getEntries(files)
this.entries = this.getEntries(config)
}

public write(): void {
Expand All @@ -71,9 +58,26 @@ export class Entries {
public update(file: string): void {
const filepath = this.entryFilepath(file)

this.entries = {
...this.entries,
[filepath]: new Entry(file, this.config.src),
if (Entry.check(file)) {
this.entries = {
...this.entries,
[filepath]: new Entry(file, this.config.src),
}
}
}

public clean(dir: string): void {
if (test('-d', dir)) {
this.entries = this.getEntries(this.config)
return
}

const { paths } = this.config
const src = path.resolve(paths.root, this.config.src)

for (const file of Object.keys(this.entries)) {
const filepath = path.join(src, file)
if (!test('-f', filepath)) this.remove(filepath)
}
}

Expand All @@ -82,7 +86,14 @@ export class Entries {
return path.relative(srcPath, file)
}

private getEntries(files: string[]): EntryMap {
private getEntries(config: Config): EntryMap {
const { files: pattern } = config

const ignoreGlob = '!node_modules'
const files: string[] = glob.sync(
Array.isArray(pattern) ? [...pattern, ignoreGlob] : [pattern, ignoreGlob]
)

return files.filter(Entry.check).reduce((obj, file) => {
const entry = new Entry(file, this.config.src)
return { ...obj, [entry.filepath]: entry }
Expand Down
10 changes: 5 additions & 5 deletions packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ export class Entry {
return checkImport(file) && Boolean(getNameFromDoc(file))
}

public static parseName(file: string): string | null {
return getNameFromDoc(file)
public static slug(file: string): string | null {
const name = getNameFromDoc(file)
return name ? slugify(name) : null
}

public id: string
public slug: string
public filepath: string
public slug: string | null

constructor(file: string, src: string) {
const srcPath = path.resolve(paths.root, src)
const filepath = path.relative(srcPath, file)
const name = Entry.parseName(file)

this.id = ulid()
this.slug = slugify(name)
this.slug = Entry.slug(file)
this.filepath = filepath
}
}
8 changes: 5 additions & 3 deletions packages/docz-core/src/bundlers/webpack/devserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ export const devServerConfig = (
const nonExistentDir = path.resolve(__dirname, 'non-existent')

return {
content: [nonExistentDir],
compiler,
host,
dev: { logLevel: 'warn' },
port,
content: [nonExistentDir],
dev: {
logLevel: args.debug ? 'debug' : 'warn',
},
hot: {
logLevel: 'error',
reload: false,
},
logLevel: 'error',
port,
add: (app: any) => {
app.use(
convert(
Expand Down
55 changes: 31 additions & 24 deletions packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,55 @@ import chokidar from 'chokidar'
import del from 'del'

import * as paths from '../config/paths'
import { Entry } from '../Entry'
import { Entries } from '../Entries'
import { webpack } from '../bundlers'
import { Config } from './args'

process.env.BABEL_ENV = process.env.BABEL_ENV || 'development'
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const INITIAL_CONFIG = {
paths,
plugins: [],
mdPlugins: [],
hastPlugins: [],
const handleUpdate = (entries: Entries) => (file: string) => {
entries.update(file)
entries.rewrite()
}

const writeEntriesAndWatch = (config: Config) => {
const watcher = chokidar.watch(config.files, {
ignored: /(^|[\/\\])\../,
})

const entries = new Entries(config)
const handleRemove = (entries: Entries) => (file: string) => {
entries.remove(file)
entries.rewrite()
}

const onUnlink = (file: string) => {
entries.remove(file)
const handleRemoveDir = (entries: Entries) => (
event: string,
path: string,
details: any
) => {
if (details.event === 'moved' && details.type === 'directory') {
entries.clean(details.path)
entries.rewrite()
}
}

const onChange = (file: string) => {
const name = Entry.parseName(file)

if (name) {
entries.update(file)
entries.rewrite()
}
}
const writeEntriesAndWatch = (config: Config) => {
const entries = new Entries(config)
const watcher = chokidar.watch(config.files, {
ignored: /(^|[\/\\])\../,
})

watcher.on('unlink', onUnlink)
watcher.on('change', onChange)
watcher
.on('change', handleUpdate(entries))
.on('unlink', handleRemove(entries))
.on('raw', handleRemoveDir(entries))

entries.write()
}

const INITIAL_CONFIG = {
paths,
plugins: [],
mdPlugins: [],
hastPlugins: [],
}

export const dev = async (args: Config) => {
const config = load('docz', { ...args, ...INITIAL_CONFIG })
const bundler = webpack(config)
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"curly": false,
"interface-name": [true, "never-prefix"],
"ordered-imports": false,
"object-literal-sort-keys": false,
Expand Down
13 changes: 7 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1418,12 +1418,6 @@
version "3.0.3"
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"

"@types/mkdirp@^0.5.2":
version "0.5.2"
resolved "https://registry.npmjs.org/@types/mkdirp/-/mkdirp-0.5.2.tgz#503aacfe5cc2703d5484326b1b27efa67a339c1f"
dependencies:
"@types/node" "*"

"@types/node@*":
version "10.0.0"
resolved "https://registry.npmjs.org/@types/node/-/node-10.0.0.tgz#c40f8e07dce607d3ef25a626b93a6a7cdcf97881"
Expand Down Expand Up @@ -1481,6 +1475,13 @@
"@types/express-serve-static-core" "*"
"@types/mime" "*"

"@types/shelljs@^0.7.9":
version "0.7.9"
resolved "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.7.9.tgz#3abecb72d9cad9cd4b0e7cb86ed10a97d93ba602"
dependencies:
"@types/glob" "*"
"@types/node" "*"

"@types/tapable@*":
version "1.0.2"
resolved "https://registry.npmjs.org/@types/tapable/-/tapable-1.0.2.tgz#e13182e1b69871a422d7863e11a4a6f5b814a4bd"
Expand Down

0 comments on commit 43fa7ab

Please sign in to comment.