Skip to content

Commit

Permalink
wip undo
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Jun 12, 2020
1 parent 75de26c commit 7ce2f31
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
14 changes: 7 additions & 7 deletions src/cli/commands/__default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs-jetpack'
import { Command } from '../../lib/cli'
import * as Layout from '../../lib/layout'
import { rootLogger } from '../../lib/nexus-logger'
import { casesHandled, CWDProjectNameOrGenerate, generateProjectName } from '../../lib/utils'
import { CWDProjectNameOrGenerate, generateProjectName } from '../../lib/utils'
import { run as runCreateApp } from './create/app'
import { Dev } from './dev'

Expand Down Expand Up @@ -72,12 +72,12 @@ export class __Default implements Command {
console.log() // space after codeblock

break
case 'malformed_package_json':
// todo test this case
log.fatal(projectType.error.message)
break
default:
casesHandled(projectType)
// case 'malformed_package_json':
// // todo test this case
// log.fatal(projectType.error.message)
// break
// default:
// casesHandled(projectType)
}
}
}
90 changes: 51 additions & 39 deletions src/lib/layout/layout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { rightOrThrow } from '@nexus/logger/dist/utils'
import Chalk from 'chalk'
import { stripIndent } from 'common-tags'
import { Either, isLeft, left, right } from 'fp-ts/lib/Either'
Expand All @@ -7,11 +6,9 @@ import * as Path from 'path'
import * as ts from 'ts-morph'
import { PackageJson } from 'type-fest'
import type { ParsedCommandLine } from 'typescript'
import { findFile, isEmptyDir } from '../../lib/fs'
import { findFile, findFileRecurisvelyUpwardSync } from '../../lib/fs'
import { START_MODULE_NAME } from '../../runtime/start/start-module'
import { rewordError } from '../contextual-error'
import { rootLogger } from '../nexus-logger'
import * as PJ from '../package-json'
import * as PackageManager from '../package-manager'
import { createContextualError } from '../utils'
import { readOrScaffoldTsconfig } from './tsconfig'
Expand Down Expand Up @@ -197,10 +194,7 @@ export async function create(options?: Options): Promise<Either<Error, Layout>>

// TODO lodash merge defaults or something

const errScanResult = await scan({ cwd, entrypointPath: normalizedEntrypoint })
if (isLeft(errScanResult)) return errScanResult
const scanResult = errScanResult.right

const scanResult = await scan({ cwd, entrypointPath: normalizedEntrypoint })
const buildInfo = getBuildInfo(options?.buildOutputDir, scanResult, options?.asBundle)

log.trace('layout build info', { data: buildInfo })
Expand Down Expand Up @@ -261,24 +255,17 @@ export function createFromData(layoutData: Data): Layout {
* Analyze the user's project files/folders for how conventions are being used
* and where key modules exist.
*/
export async function scan(opts?: {
cwd?: string
entrypointPath?: string
}): Promise<Either<Error, ScanResult>> {
export async function scan(opts?: { cwd?: string; entrypointPath?: string }): Promise<ScanResult> {
log.trace('starting scan')
const projectRoot = opts?.cwd ?? process.cwd()
const packageManagerType = await PackageManager.detectProjectPackageManager({ projectRoot })
const maybeErrPackageJson = PJ.findRecurisvelyUpwardSync({ cwd: projectRoot })
const maybePackageJson = findPackageJson({ projectRoot })
const maybeAppModule = opts?.entrypointPath ?? findAppModule({ projectRoot })
const tsConfig = await readOrScaffoldTsconfig({
projectRoot,
})
const nexusModules = findNexusModules(tsConfig, maybeAppModule)

if (maybeErrPackageJson && isLeft(maybeErrPackageJson.contents)) {
return maybeErrPackageJson.contents
}

const result: ScanResult = {
app:
maybeAppModule === null
Expand All @@ -290,9 +277,7 @@ export async function scan(opts?: {
project: readProjectInfo(opts),
tsConfig,
packageManagerType,
packageJson: maybeErrPackageJson
? { ...maybeErrPackageJson, content: rightOrThrow(maybeErrPackageJson.contents) }
: maybeErrPackageJson,
packageJson: maybePackageJson,
}

log.trace('completed scan', { result })
Expand All @@ -303,7 +288,7 @@ export async function scan(opts?: {
process.exit(1)
}

return right(result)
return result
}

// todo allow user to configure these for their project
Expand Down Expand Up @@ -362,46 +347,46 @@ export async function scanProjectType(opts: {
cwd: string
}): Promise<
| { type: 'unknown' | 'new' }
| { type: 'malformed_package_json'; error: PJ.MalformedPackageJsonError }
| {
type: 'NEXUS_project' | 'node_project'
packageJson: {}
packageJsonLocation: { path: string; dir: string }
}
> {
const packageJson = PJ.findRecurisvelyUpwardSync(opts)
const packageJsonLocation = findPackageJsonRecursivelyUpward(opts)

if (packageJson === null) {
if (await isEmptyDir(opts.cwd)) {
if (packageJsonLocation === null) {
if (await isEmptyCWD()) {
return { type: 'new' }
}
return { type: 'unknown' }
}

if (isLeft(packageJson.contents)) {
const e = packageJson.contents.left
return {
type: 'malformed_package_json',
error: rewordError(`A package.json was found at ${e.context.path} but it was malformed`, e),
}
}

const pjc = rightOrThrow(packageJson.contents) // will never throw, check above
if (pjc.dependencies?.['nexus']) {
const packageJson = FS.read(packageJsonLocation.path, 'json')
if (packageJson?.dependencies?.['nexus']) {
return {
type: 'NEXUS_project',
packageJson: packageJson,
packageJsonLocation: packageJson,
packageJson: packageJsonLocation,
packageJsonLocation: packageJsonLocation,
}
}

return {
type: 'node_project',
packageJson: packageJson,
packageJsonLocation: packageJson,
packageJson: packageJsonLocation,
packageJsonLocation: packageJsonLocation,
}
}

/**
* Check if the CWD is empty of any files or folders.
* TODO we should make nice exceptions for known meaningless files, like .DS_Store
*/
async function isEmptyCWD(): Promise<boolean> {
const contents = await FS.listAsync()
return contents === undefined || contents.length === 0
}

const ENV_VAR_DATA_NAME = 'NEXUS_LAYOUT'

export function saveDataForChildProcess(layout: Layout): { NEXUS_LAYOUT: string } {
Expand Down Expand Up @@ -448,6 +433,33 @@ function readProjectInfo(opts?: { cwd?: string }): ScanResult['project'] {
}
}

/**
* Find the package.json file path. Looks recursively upward to disk root.
* Starts looking in CWD If no package.json found along search, returns null.
*/
function findPackageJsonRecursivelyUpward(opts: { cwd: string }) {
return findFileRecurisvelyUpwardSync('package.json', opts)
}

/**
*
*/
function findPackageJson(opts: { projectRoot: string }): ScanResult['packageJson'] {
const packageJsonPath = FS.path(opts.projectRoot, 'package.json')

try {
const content = FS.read(packageJsonPath, 'json')

return {
content,
path: packageJsonPath,
dir: Path.dirname(packageJsonPath),
}
} catch {
return null
}
}

function normalizeEntrypoint(entrypoint: string | undefined, cwd: string): Either<Error, string | undefined> {
if (!entrypoint) {
return right(undefined)
Expand Down

0 comments on commit 7ce2f31

Please sign in to comment.