Skip to content

Commit

Permalink
Merge pull request #29 from contentlayerdev/windows-filepaths
Browse files Browse the repository at this point in the history
fixed windows paths
  • Loading branch information
schickling committed Nov 9, 2021
2 parents 27fc312 + 9725c59 commit 589aa04
Show file tree
Hide file tree
Showing 58 changed files with 714 additions and 346 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": true,
"es6": true
},
"ignorePatterns": ["packages/_archive/*", "examples/*", "**/dist/*"],
"ignorePatterns": ["packages/_archive/*", "examples/*", "**/dist/*", "**/.nyc_output/*"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "simple-import-sort", "import"],
"extends": ["plugin:react-hooks/recommended", "plugin:@typescript-eslint/recommended", "prettier"],
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jobs:
strategy:
matrix:
node-version: [16.x]
# Note tests are currently not run in Windows because the path seperator on Windows invalidates the test results 🙈
os: [ubuntu-latest]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: schickling-actions/checkout-and-install@main
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages/_archive/*
**/dist/*
**/dist/*
**/.nyc_output/*
3 changes: 0 additions & 3 deletions examples/mdx/next.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions examples/mdx/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { withContentlayer } from 'next-contentlayer'

export default withContentlayer()({})
2 changes: 1 addition & 1 deletion examples/playground-azimuth-colocated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "next start"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"marked": "^3.0.4",
"next": "^12.0.2",
"react": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/playground-azimuth-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "next build"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"marked": "^3.0.4",
"next": "^12.0.2",
"react": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/playground-azimuth-sanity/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@contentlayer/source-sanity": "workspace:*",
"contentlayer": "workspace:*",
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"marked": "^3.0.4",
"next": "^12.0.2",
"react": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/playground-azimuth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "next start"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"marked": "^3.0.4",
"next": "^12.0.2",
"react": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/playground-contentful-starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "next start"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"next": "^12.0.2",
"react": "17.0.2",
"react-dom": "17.0.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/starter-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"gen:stackbit": "contentlayer-stackbit-yaml-generator"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"next": "^12.0.2",
"react": "17.0.2",
"react-dom": "17.0.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"gen:stackbit": "contentlayer-stackbit-yaml-generator"
},
"dependencies": {
"date-fns": "^2.24.0",
"date-fns": "^2.25.0",
"next": "^12.0.2",
"react": "17.0.2",
"react-dom": "17.0.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/cli",
"version": "0.0.28",
"version": "0.0.29",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
7 changes: 4 additions & 3 deletions packages/@contentlayer/cli/src/commands/BuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export class BuildCommand extends BaseCommand {
}

executeSafe = pipe(
T.suspend(() => core.getConfig({ configPath: this.configPath, cwd: process.cwd() })),
T.chain((source) => core.generateDotpkg({ source, verbose: this.verbose, cwd: process.cwd() })),
T.suspend(this.clearCacheIfNeeded),
T.chain(() => core.getConfig({ configPath: this.configPath })),
T.chain((source) => core.generateDotpkg({ source, verbose: this.verbose })),
T.tap(core.logGenerateInfo),
OT.withSpan('@contentlayer/cli/commands/BuildCommand:executeSafe', { attributes: { cwd: process.cwd() } }),
OT.withSpan('@contentlayer/cli/commands/BuildCommand:executeSafe'),
)
}
12 changes: 3 additions & 9 deletions packages/@contentlayer/cli/src/commands/DevCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ export class DevCommand extends BaseCommand {
}

executeSafe = pipe(
S.suspend(() =>
core.getConfigWatch({
configPath: this.configPath,
cwd: process.cwd(),
}),
),
S.fromEffect(T.suspend(this.clearCacheIfNeeded)),
S.chain(() => core.getConfigWatch({ configPath: this.configPath })),
S.tapSkipFirstRight(() => T.log(`Contentlayer config change detected. Updating type definitions and data...`)),
S.chainSwitchMapEitherRight((source) =>
core.generateDotpkgStream({ source, verbose: this.verbose, cwd: process.cwd() }),
),
S.chainSwitchMapEitherRight((source) => core.generateDotpkgStream({ source, verbose: this.verbose })),
S.tap(E.fold((error) => T.log(errorToString(error)), core.logGenerateInfo)),
S.runDrain,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PostInstallCommand extends BaseCommand {
T.gen(function* ($) {
// `process.env.INIT_CWD` is set by `yarn` or `npm` during installation
const cwd = process.env.INIT_CWD ?? process.cwd()
const artifactsDirPath = yield* $(ArtifactsDir.mkdir({ cwd }))
const artifactsDirPath = yield* $(ArtifactsDir.mkdir)

yield* $(T.forEachPar_(['data', 'types'], (moduleName) => makeModuleStub({ artifactsDirPath, moduleName })))

Expand Down
28 changes: 17 additions & 11 deletions packages/@contentlayer/cli/src/commands/_BaseCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { HasCwd } from '@contentlayer/core'
import * as core from '@contentlayer/core'
import { unknownToPosixFilePath } from '@contentlayer/utils'
import type { HasClock, OT } from '@contentlayer/utils/effect'
import { pipe, T } from '@contentlayer/utils/effect'
import { fs } from '@contentlayer/utils/node'
Expand All @@ -20,23 +22,27 @@ export abstract class BaseCommand extends Command {
description: 'More verbose logging and error stack traces',
})

abstract executeSafe: T.Effect<OT.HasTracer & HasClock, unknown, void>
abstract executeSafe: T.Effect<OT.HasTracer & HasClock & HasCwd, unknown, void>

execute = () =>
pipe(
pipe(clearCacheIfNeeded(this.clearCache), T.zipRight(this.executeSafe)),
this.executeSafe,
core.runMain({
tracingServiceName: 'contentlayer-cli',
verbose: this.verbose || process.env.CL_DEBUG !== undefined,
}),
)
}

const clearCacheIfNeeded = (shouldClearCache: boolean) =>
T.gen(function* ($) {
if (shouldClearCache) {
const artifactsDir = core.ArtifactsDir.getDirPath({ cwd: process.cwd() })
yield* $(fs.rm(artifactsDir, { recursive: true, force: true }))
yield* $(T.log('Cache cleared successfully'))
}
})
clearCacheIfNeeded = () => {
const { clearCache } = this

return T.gen(function* ($) {
if (clearCache) {
const cwd = unknownToPosixFilePath(process.cwd())
const artifactsDir = core.ArtifactsDir.getDirPath({ cwd })
yield* $(fs.rm(artifactsDir, { recursive: true, force: true }))
yield* $(T.log('Cache cleared successfully'))
}
})
}
}
2 changes: 1 addition & 1 deletion packages/@contentlayer/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/client",
"version": "0.0.28",
"version": "0.0.29",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/@contentlayer/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/core",
"version": "0.0.28",
"version": "0.0.29",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -29,7 +29,7 @@
"date-fns": "2.x",
"esbuild": "0.11.x || 0.12.x || 0.13.x",
"gray-matter": "^4.0.3",
"markdown-wasm": "^1.1.2",
"markdown-wasm": "^1.2.0",
"mdx-bundler": "^6.0.2",
"rehype-stringify": "^9.0.2",
"remark-parse": "^10.0.0",
Expand Down
44 changes: 25 additions & 19 deletions packages/@contentlayer/core/src/ArtifactsDir.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import type { PosixFilePath } from '@contentlayer/utils'
import { filePathJoin } from '@contentlayer/utils'
import type { OT } from '@contentlayer/utils/effect'
import { pipe, T } from '@contentlayer/utils/effect'
import type { GetContentlayerVersionError } from '@contentlayer/utils/node'
import { fs, getContentlayerVersion } from '@contentlayer/utils/node'

import type { HasCwd } from './cwd.js'
import { getCwd } from './cwd.js'
// import utilsPkg from '@contentlayer/utils/package.json'
import * as path from 'path'

export namespace ArtifactsDir {
export const getDirPath = ({ cwd }: { cwd: string }): string => path.join(cwd, 'node_modules', '.contentlayer')
export const getDirPath = ({ cwd }: { cwd: PosixFilePath }): PosixFilePath =>
filePathJoin(cwd, 'node_modules' as PosixFilePath, '.contentlayer' as PosixFilePath)

export const mkdir: T.Effect<OT.HasTracer & HasCwd, fs.MkdirError, PosixFilePath> = T.gen(function* ($) {
const cwd = yield* $(getCwd)
const dirPath = getDirPath({ cwd })
yield* $(fs.mkdirp(dirPath))

export const mkdir = ({ cwd }: { cwd: string }): T.Effect<OT.HasTracer, fs.MkdirError, string> =>
pipe(T.succeed(getDirPath({ cwd })), T.tap(fs.mkdirp))
return dirPath
})

export const getCacheDirPath = ({
cwd,
}: {
cwd: string
}): T.Effect<OT.HasTracer, GetContentlayerVersionError, string> =>
pipe(
getContentlayerVersion(),
T.map((contentlayerVersion) => path.join(getDirPath({ cwd }), '.cache', `v${contentlayerVersion}`)),
)
export const getCacheDirPath: T.Effect<OT.HasTracer & HasCwd, GetContentlayerVersionError, PosixFilePath> = pipe(
T.struct({
contentlayerVersion: getContentlayerVersion(),
cwd: getCwd,
}),
T.map(({ contentlayerVersion, cwd }) =>
filePathJoin(getDirPath({ cwd }), '.cache' as PosixFilePath, `v${contentlayerVersion}` as PosixFilePath),
),
)

export const mkdirCache = ({
cwd,
}: {
cwd: string
}): T.Effect<OT.HasTracer, fs.MkdirError | GetContentlayerVersionError, string> =>
pipe(getCacheDirPath({ cwd }), T.tap(fs.mkdirp))
export const mkdirCache: T.Effect<OT.HasTracer & HasCwd, fs.MkdirError | GetContentlayerVersionError, PosixFilePath> =
pipe(getCacheDirPath, T.tap(fs.mkdirp))
}
13 changes: 5 additions & 8 deletions packages/@contentlayer/core/src/DataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fs } from '@contentlayer/utils/node'
import * as path from 'path'

import { ArtifactsDir } from './ArtifactsDir.js'
import type { HasCwd } from './cwd.js'
import type { Document } from './data.js'

export namespace DataCache {
Expand Down Expand Up @@ -32,17 +33,15 @@ export namespace DataCache {

export const loadPreviousCacheFromDisk = ({
schemaHash,
cwd,
}: {
schemaHash: string
cwd: string
}): T.Effect<
OT.HasTracer,
OT.HasTracer & HasCwd,
fs.StatError | fs.ReadFileError | fs.JsonParseError | GetContentlayerVersionError,
Cache | undefined
> =>
T.gen(function* ($) {
const cacheDirPath = yield* $(ArtifactsDir.getCacheDirPath({ cwd }))
const cacheDirPath = yield* $(ArtifactsDir.getCacheDirPath)
const filePath = path.join(cacheDirPath, dataCacheFileName(schemaHash))

const cache = yield* $(
Expand All @@ -58,18 +57,16 @@ export namespace DataCache {
export const writeCacheToDisk = ({
cache,
schemaHash,
cwd,
}: {
cache: Cache
schemaHash: string
cwd: string
}): T.Effect<
OT.HasTracer,
OT.HasTracer & HasCwd,
never,
E.Either<fs.WriteFileError | fs.MkdirError | fs.JsonStringifyError | GetContentlayerVersionError, void>
> =>
pipe(
ArtifactsDir.mkdirCache({ cwd }),
ArtifactsDir.mkdirCache,
T.chain((cacheDirPath) => {
const filePath = path.join(cacheDirPath, dataCacheFileName(schemaHash))
return fs.writeFileJson({ filePath, content: cache })
Expand Down
20 changes: 20 additions & 0 deletions packages/@contentlayer/core/src/cwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { unknownToPosixFilePath } from '@contentlayer/utils'
import type { Has } from '@contentlayer/utils/effect'
import { T, tag } from '@contentlayer/utils/effect'

const CwdSymbol = Symbol()

export const makeCwd = T.gen(function* (_) {
const cwd = yield* _(T.succeedWith(() => unknownToPosixFilePath(process.cwd())))

return { serviceId: CwdSymbol, cwd } as const
})

export interface Cwd extends T.OutputOf<typeof makeCwd> {}
export const Cwd = tag<Cwd>()

export const provideCwd = T.provideServiceM(Cwd)(makeCwd)

export const getCwd = T.accessService(Cwd)((_) => _.cwd)

export type HasCwd = Has<Cwd>
Loading

0 comments on commit 589aa04

Please sign in to comment.