-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(state): separates load into paths and load; gets root paths
- Loading branch information
Showing
8 changed files
with
102 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import getFile from './get-file'; | ||
import readFile from './read-file'; | ||
import { IOfType, IScripts } from '~/types'; | ||
import logger from '~/utils/logger'; | ||
import { IScripts, IOfType } from '~/types'; | ||
import { rejects } from 'errorish'; | ||
import { IBasePaths } from '~/state/paths'; | ||
import hash from 'object-hash'; | ||
import readFile from './read-file'; | ||
|
||
export interface ILoadOpts { | ||
file?: string; | ||
directory?: string; | ||
} | ||
|
||
export interface ILoad { | ||
export interface ILoaded { | ||
kpo: IScripts | null; | ||
pkg: IOfType<any> | null; | ||
directory: string; | ||
} | ||
|
||
export default async function load(opts: ILoadOpts): Promise<ILoad> { | ||
const { kpo, pkg } = await getFile(opts); | ||
|
||
let dir = path.parse(pkg || kpo || process.cwd()).dir; | ||
|
||
if (kpo) logger.debug('kpo configuration file found at: ' + kpo); | ||
if (pkg) logger.debug('package.json found at: ' + pkg); | ||
if (!kpo && !pkg) { | ||
throw Error(`No file or package.json was found in directory`); | ||
const cache: IOfType<ILoaded> = {}; | ||
export default async function load(paths: IBasePaths): Promise<ILoaded> { | ||
const key = hash(paths); | ||
if (!cache.hasOwnProperty(key)) { | ||
cache[key] = { | ||
kpo: paths.kpo ? await readFile(paths.kpo) : null, | ||
pkg: paths.pkg ? await fs.readJSON(paths.pkg).catch(rejects) : null | ||
}; | ||
} | ||
|
||
return { | ||
kpo: kpo ? await readFile(kpo) : null, | ||
pkg: pkg ? await fs.readJSON(pkg).catch(rejects) : null, | ||
directory: dir | ||
}; | ||
return cache[key]; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import path from 'path'; | ||
import getFile from './get-file'; | ||
import { IOfType } from '~/types'; | ||
import logger from '~/utils/logger'; | ||
import { wrap } from '~/utils/errors'; | ||
import load from '../load'; | ||
import state from '../index'; | ||
import up from 'find-up'; | ||
|
||
export interface IPathsOpts { | ||
file?: string; | ||
directory?: string; | ||
} | ||
|
||
export interface IBasePaths { | ||
kpo: string | null; | ||
pkg: string | null; | ||
bin: string[]; | ||
directory: string; | ||
} | ||
|
||
export interface IPaths extends IBasePaths { | ||
root: IBasePaths | null; | ||
children: IOfType<IBasePaths>; | ||
} | ||
|
||
export default async function paths(opts: IPathsOpts): Promise<IPaths> { | ||
const base = await trunk(opts); | ||
await load(base); | ||
|
||
// has to to called after load to wait for scope options to modify state | ||
const root = await trunk({ | ||
directory: state.get('root') || path.join(base.directory, '../') | ||
}).catch(async (err) => { | ||
return state.get('root') | ||
? wrap.rejects(err, { message: "@root couldn't be retrieved" }) | ||
: null; | ||
}); | ||
|
||
return { | ||
...base, | ||
// add also root bin path | ||
bin: base.bin | ||
.concat(root ? root.bin : []) | ||
.filter((x, i, arr) => arr.indexOf(x) === i), | ||
root, | ||
children: {} | ||
}; | ||
} | ||
|
||
export async function trunk(opts: IPathsOpts): Promise<IBasePaths> { | ||
const { kpo, pkg } = await getFile(opts); | ||
let dir = path.parse(pkg || kpo || process.cwd()).dir; | ||
|
||
if (kpo) logger.debug('kpo configuration file found at: ' + kpo); | ||
if (pkg) logger.debug('package.json found at: ' + pkg); | ||
if (!kpo && !pkg) { | ||
throw Error(`No file or package.json was found in directory`); | ||
} | ||
|
||
return { | ||
kpo: kpo, | ||
pkg: pkg, | ||
directory: dir, | ||
bin: [await up('node_modules/.bin', { cwd: dir })].filter( | ||
Boolean | ||
) as string[] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters