-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devtools): resolve config files from target directory upward to …
…the root path
- Loading branch information
Showing
9 changed files
with
128 additions
and
11 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
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,5 @@ | ||
import { StoragePresetOptions } from './preset'; | ||
|
||
export interface DevtoolsConfig { | ||
storagePresets?: StoragePresetOptions[]; | ||
} |
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,3 @@ | ||
export type * from './common'; | ||
export type * from './preset'; | ||
export type * from './config'; |
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,30 @@ | ||
import type { CookieSerializeOptions } from 'cookie-es'; | ||
|
||
export interface LocalStoragePresetItem { | ||
type: 'local-storage'; | ||
key: string; | ||
value: string | object | number; | ||
} | ||
|
||
export interface SessionStoragePresetItem { | ||
type: 'session-storage'; | ||
key: string; | ||
value: string | object | number; | ||
} | ||
|
||
export interface CookiePresetItem | ||
extends Omit<CookieSerializeOptions, 'encode'> { | ||
type: 'cookie'; | ||
key: string; | ||
value: string | object | number; | ||
} | ||
|
||
export type StoragePreset = | ||
| LocalStoragePresetItem | ||
| SessionStoragePresetItem | ||
| CookiePresetItem; | ||
|
||
export interface StoragePresetOptions { | ||
name: string; | ||
items: StoragePreset[]; | ||
} |
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,53 @@ | ||
import path from 'path'; | ||
import fs from '@modern-js/utils/fs-extra'; | ||
import { DevtoolsConfig } from '../types'; | ||
|
||
const CONFIG_FILENAME = 'modern.devtools.json'; | ||
|
||
async function findUp(filename: string, dir = process.cwd()) { | ||
const filePath = path.join(dir, filename); | ||
if (await fs.pathExists(filePath)) { | ||
return filePath; | ||
} | ||
const parent = path.dirname(dir); | ||
if (parent === dir) { | ||
return null; | ||
} | ||
return findUp(filename, parent); | ||
} | ||
|
||
export function resolveConfigFile(dir = process.cwd()) { | ||
return findUp(CONFIG_FILENAME, dir); | ||
} | ||
|
||
/** Resolve all config files from target directory upward to the root path. */ | ||
export async function resolveConfigFiles( | ||
dir = process.cwd(), | ||
): Promise<string[]> { | ||
const files = []; | ||
let currentDir = dir; | ||
while (true) { | ||
const configFile = await resolveConfigFile(currentDir); | ||
if (!configFile) { | ||
break; | ||
} | ||
files.push(configFile); | ||
currentDir = path.dirname(currentDir); | ||
} | ||
return files; | ||
} | ||
|
||
export async function loadConfigFile(dir = process.cwd()) { | ||
const filename = await resolveConfigFile(dir); | ||
if (!filename) { | ||
return null; | ||
} | ||
const raw = await fs.readJson(filename); | ||
return raw as DevtoolsConfig; | ||
} | ||
|
||
export async function loadConfigFiles(dir = process.cwd()) { | ||
const filenames = await resolveConfigFiles(dir); | ||
const configs = await Promise.all(filenames.map(loadConfigFile)); | ||
return configs.filter(Boolean) as DevtoolsConfig[]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.