-
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.
- Loading branch information
Showing
10 changed files
with
128 additions
and
93 deletions.
There are no files selected for viewing
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
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,87 +1,39 @@ | ||
import path from 'path'; | ||
import getFile from './get-file'; | ||
import logger from '~/utils/logger'; | ||
import { wrap } from '~/utils/errors'; | ||
import load from '../load'; | ||
import state from '../index'; | ||
import up from 'find-up'; | ||
import { IPathsOpts, IPaths } from '../types'; | ||
import { getSelfPaths, getRootPaths } from './retrieve'; | ||
|
||
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: IBasePaths[]; | ||
} | ||
|
||
/** | ||
* - if no `file` or `directory` are passed, it will **recurse up** from `cwd` to find both the `package.json` and `kpo.scripts` files. If the `package.json` is found closer to `cwd` or no `kpo.scripts` is found, then its `kpo.path` key will be used -if found- to locate the `kpo.scripts` file. | ||
* - if only `file` is passed, it will get the `kpo.scripts` on that location, and **recurse up** to find a `package.json`. | ||
* - if a `file` and a `directory` are passed, it will get the `kpo.scripts` on `file` location, and only use a `package.json` if it's found in `directory`. | ||
* - if only a `directory` is passed, it will try to find both the `kpo.script` and `package.json` files on `directory`; if no `kpo.scripts` is found exactly on directory, it will fall back to the `kpo.path` in `package.json` -if found. | ||
*/ | ||
export default async function paths(opts: IPathsOpts): Promise<IPaths> { | ||
// It will be strict if directory exists (it's passed on cli), | ||
// otherwise it will recurse up w/ strict = false. | ||
const base = await trunk(opts, Boolean(opts.directory)); | ||
await load(base); | ||
const self = await getSelfPaths(opts); | ||
await load(self); | ||
|
||
// 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, '../') }, | ||
false | ||
const rootDir = state.get('root'); | ||
const root = await getRootPaths( | ||
rootDir || path.join(self.directory, '../') | ||
).catch(async (err) => { | ||
return state.get('root') | ||
? wrap.rejects(err, { | ||
message: `root scope couldn't be retrieved: ${state.get('root')}` | ||
}) | ||
: null; | ||
// don't fail if root directory wasn't explicitly passed via options, | ||
// just set as null | ||
if (!rootDir) return null; | ||
|
||
return wrap.rejects(err, { | ||
message: `root scope couldn't be retrieved: ${state.get('root')}` | ||
}); | ||
}); | ||
|
||
return { | ||
...base, | ||
...self, | ||
// add also root bin path | ||
bin: [base.bin[0]] | ||
bin: [self.bin[0]] | ||
.concat(root ? root.bin[0] : []) | ||
.concat(base.bin.slice(1)) | ||
.concat(self.bin.slice(1)) | ||
.concat(root ? root.bin.slice(1) : []) | ||
.filter((x, i, arr) => x && arr.indexOf(x) === i), | ||
root, | ||
children: [] | ||
}; | ||
} | ||
|
||
export async function trunk( | ||
opts: IPathsOpts, | ||
strict: boolean | ||
): Promise<IBasePaths> { | ||
const { kpo, pkg } = await getFile(opts, strict); | ||
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 getBin(dir) | ||
}; | ||
} | ||
|
||
export async function getBin(dir: string): Promise<string[]> { | ||
const bin = await up('node_modules/.bin', { cwd: dir }); | ||
return bin ? [bin].concat(await getBin(path.join(dir, '../'))) : []; | ||
} |
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,7 @@ | ||
import path from 'path'; | ||
import up from 'find-up'; | ||
|
||
export default async function getBin(dir: string): Promise<string[]> { | ||
const bin = await up('node_modules/.bin', { cwd: dir }); | ||
return bin ? [bin].concat(await getBin(path.join(dir, '../'))) : []; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IPathsOpts, IBasePaths } from '../../types'; | ||
import getPaths from './paths'; | ||
|
||
/** | ||
* - `file` determines the path for the `kpo.scripts` file; if not passed, it will be resolved from `directory`. | ||
* - `directory` determines the path to look for the `package.json` and `kpo.scripts` files; if passed, files will be expected to be exactly in that directory, otherwise `directory` will be `cwd` and the search will **recurse up** until the root folder is reached. If a `package.json` is found closer to `directory` than any `kpo.scripts` containing a `kpo.path` key, that path for a `kpo.scripts` file will take precedence. | ||
*/ | ||
export async function getSelfPaths(opts: IPathsOpts): Promise<IBasePaths> { | ||
return getPaths(opts, Boolean(opts.directory)); | ||
} | ||
|
||
/** | ||
* Will always recurse up | ||
*/ | ||
export async function getRootPaths(directory: string): Promise<IBasePaths> { | ||
return getPaths({ directory }, false); | ||
} |
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,26 @@ | ||
import path from 'path'; | ||
import getFiles from './files'; | ||
import logger from '~/utils/logger'; | ||
import getBin from './bin'; | ||
import { IPathsOpts, IBasePaths } from '../../types'; | ||
|
||
export default async function getPaths( | ||
opts: IPathsOpts, | ||
strict: boolean | ||
): Promise<IBasePaths> { | ||
const { kpo, pkg } = await getFiles(opts, strict); | ||
|
||
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 dir = path.parse((pkg || kpo) as string).dir; | ||
return { | ||
kpo: kpo, | ||
pkg: pkg, | ||
directory: dir, | ||
bin: await getBin(dir) | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { IScripts, IOfType } from '~/types'; | ||
|
||
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: IBasePaths[]; | ||
} | ||
|
||
export interface ILoaded { | ||
kpo: IScripts | null; | ||
pkg: IOfType<any> | null; | ||
} | ||
|
||
export interface IScopeDefinition { | ||
name: string; | ||
directory: string; | ||
} |