-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
18 changed files
with
301 additions
and
172 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
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,38 @@ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
import path from 'path'; | ||
import { Format } from '@ts-graphviz/node'; | ||
import { RediagramGlobalConfig } from './types'; | ||
|
||
export namespace beta1 { | ||
export const version = 'beta1'; | ||
|
||
interface Config { | ||
version: typeof version; | ||
includes?: string[]; | ||
excludes?: string[]; | ||
output?: { | ||
dir?: string; | ||
type?: Format; | ||
}; | ||
dot?: { | ||
timeout?: number; | ||
}; | ||
} | ||
|
||
export function load(filepath: string, data: Config): RediagramGlobalConfig { | ||
return { | ||
filepath, | ||
output: { | ||
dir: path.resolve(path.dirname(filepath), data.output?.dir ?? '.'), | ||
format: data.output?.type ?? 'png', | ||
}, | ||
scope: { | ||
includes: data.includes ?? ['**/*.rediagram.{jsx,tsx}'], | ||
excludes: data.excludes ?? ['**/node_modules/**/*'], | ||
}, | ||
dot: { | ||
timeout: data.dot?.timeout ?? 10_000, | ||
}, | ||
}; | ||
} | ||
} |
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 { RediagramGlobalConfig } from './types'; | ||
|
||
export function createDefaultConfig(): RediagramGlobalConfig { | ||
return { | ||
filepath: null, | ||
scope: { | ||
includes: ['**/*.rediagram.{jsx,tsx}'], | ||
excludes: ['**/node_modules/**/*'], | ||
}, | ||
output: { | ||
format: 'png', | ||
}, | ||
dot: { | ||
timeout: 10_000, | ||
}, | ||
}; | ||
} |
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,99 +1,4 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/* eslint-disable import/no-dynamic-require */ | ||
/* eslint-disable global-require */ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
/* eslint-disable no-useless-constructor */ | ||
/* eslint-disable max-classes-per-file */ | ||
import path from 'path'; | ||
import { cosmiconfigSync } from 'cosmiconfig'; | ||
import { Beta1 } from './version'; | ||
|
||
export class OutputConfig { | ||
constructor(private readonly data?: { dir?: string }) {} | ||
|
||
public getDir(): string { | ||
return this.data?.dir ?? process.cwd(); | ||
} | ||
} | ||
|
||
export class ScopeConfig { | ||
constructor(private readonly data?: { includes?: string[]; excludes?: string[] }) {} | ||
|
||
public getIncludesPattarns(): string[] { | ||
return this.data?.includes ?? ['**/*.rediagram.{jsx,tsx}']; | ||
} | ||
|
||
public getExcludesPattarns(): string[] { | ||
return this.data?.excludes ?? ['**/node_modules/**/*']; | ||
} | ||
} | ||
|
||
export class DotConfig { | ||
constructor(private readonly data?: { timeout?: number }) {} | ||
|
||
public getTimeout(): number { | ||
return this.data?.timeout ?? 10_000; | ||
} | ||
} | ||
|
||
export type RediagramGlobalConfig = { | ||
readonly filepath: string | null; | ||
readonly output: OutputConfig; | ||
readonly scope: ScopeConfig; | ||
readonly dot: DotConfig; | ||
}; | ||
|
||
const MODULE_NAME = 'rediagram'; | ||
|
||
function createDefaultConfig(): RediagramGlobalConfig { | ||
return { | ||
filepath: null, | ||
scope: new ScopeConfig(), | ||
output: new OutputConfig(), | ||
dot: new DotConfig(), | ||
}; | ||
} | ||
|
||
namespace beta1 { | ||
type Config = { | ||
version: Beta1; | ||
includes?: string[]; | ||
excludes?: string[]; | ||
output?: { | ||
dir?: string; | ||
}; | ||
dot?: { | ||
timeout?: number; | ||
}; | ||
}; | ||
|
||
export function load(filepath: string, data: Config): RediagramGlobalConfig { | ||
return { | ||
filepath, | ||
output: new OutputConfig({ | ||
dir: path.resolve(path.dirname(filepath), data.output?.dir ?? '.'), | ||
}), | ||
scope: new ScopeConfig({ | ||
includes: data.includes, | ||
excludes: data.excludes, | ||
}), | ||
dot: new DotConfig({ | ||
timeout: data.dot?.timeout, | ||
}), | ||
}; | ||
} | ||
} | ||
|
||
function loadConfig(): RediagramGlobalConfig { | ||
const { search } = cosmiconfigSync(MODULE_NAME); | ||
const result = search(); | ||
if (result !== null) { | ||
if (result.config.version === 'beta1') { | ||
return beta1.load(result.filepath, result.config); | ||
} | ||
} | ||
return createDefaultConfig(); | ||
} | ||
|
||
export const CONFIG = loadConfig(); | ||
export * from './beta1'; | ||
export * from './default'; | ||
export * from './types'; | ||
export * from './utils'; |
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,16 @@ | ||
import { Format } from '@ts-graphviz/node'; | ||
|
||
export type RediagramGlobalConfig = { | ||
filepath: string | null; | ||
scope: Readonly<{ | ||
includes: ReadonlyArray<string>; | ||
excludes: ReadonlyArray<string>; | ||
}>; | ||
output: Readonly<{ | ||
dir?: string; | ||
format: Format; | ||
}>; | ||
dot: Readonly<{ | ||
timeout: number; | ||
}>; | ||
}; |
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 { cosmiconfigSync } from 'cosmiconfig'; | ||
import { RediagramGlobalConfig } from './types'; | ||
import { beta1 } from './beta1'; | ||
import { createDefaultConfig } from './default'; | ||
|
||
const MODULE_NAME = 'rediagram'; | ||
|
||
export function loadConfigfile(): RediagramGlobalConfig { | ||
const { search } = cosmiconfigSync(MODULE_NAME); | ||
const result = search(); | ||
switch (result?.config.version) { | ||
case beta1.version: | ||
return beta1.load(result.filepath, result.config); | ||
default: | ||
return createDefaultConfig(); | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './config'; | ||
export * from './rediagram'; |
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,75 @@ | ||
import path from 'path'; | ||
import { ensureDir } from 'fs-extra'; | ||
import { Logger } from 'tslog'; | ||
import { ReactElement } from 'react'; | ||
import { renderToDot } from '@ts-graphviz/react'; | ||
import { exportToFile } from '@ts-graphviz/node'; | ||
import { RediagramGlobalConfig, loadConfigfile } from '../config'; | ||
import { RenderOption } from './types'; | ||
|
||
export class RediagramCore { | ||
public static readonly MODULE_NAME = 'rediagram'; | ||
|
||
private static instance?: RediagramCore; | ||
|
||
public logger = new Logger({ | ||
type: 'pretty', | ||
name: 'rediagram', | ||
displayDateTime: false, | ||
displayFilePath: 'hidden', | ||
displayFunctionName: false, | ||
}); | ||
|
||
public static create(): RediagramCore { | ||
if (!this.instance) { | ||
const config = loadConfigfile(); | ||
this.instance = new RediagramCore(config); | ||
} | ||
return this.instance; | ||
} | ||
|
||
public readonly config: Readonly<RediagramGlobalConfig>; | ||
|
||
constructor({ ...config }: RediagramGlobalConfig) { | ||
this.config = Object.freeze(config); | ||
|
||
this.logger.info( | ||
'Config file is', | ||
config.filepath ? `"./${path.relative(process.cwd(), config.filepath)}".` : 'not exist.', | ||
); | ||
} | ||
|
||
public async render(element: ReactElement, options: RenderOption): Promise<void> { | ||
const dot = renderToDot(element); | ||
const dir = options.dir ?? this.config.output.dir; | ||
const format = options.format ?? this.config.output.format; | ||
const output = path.format({ | ||
dir, | ||
name: options.name, | ||
ext: `.${format}`, | ||
}); | ||
if (dir !== undefined) { | ||
await ensureDir(dir); | ||
} | ||
this.logger.info('Output', path.relative(process.cwd(), output)); | ||
await exportToFile(dot, { | ||
format, | ||
output, | ||
childProcessOptions: { | ||
timeout: this.config.dot.timeout, | ||
}, | ||
}); | ||
} | ||
|
||
public async run(src: string): Promise<void> { | ||
try { | ||
this.logger.info('Runing', src); | ||
const resolved = path.resolve(src); | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
require(resolved); | ||
delete require.cache[resolved]; | ||
} catch (err) { | ||
this.logger.error(err); | ||
} | ||
} | ||
} |
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,2 @@ | ||
export * from './types'; | ||
export * from './core'; |
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,16 @@ | ||
import { Format } from '@ts-graphviz/node'; | ||
|
||
export type RenderOption = { | ||
/** | ||
* Output destination directory. | ||
*/ | ||
dir?: string; | ||
/** | ||
* Output file name. | ||
*/ | ||
name: string; | ||
/** | ||
* Output file format. | ||
*/ | ||
format?: Format; | ||
}; |
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 @@ | ||
import { RediagramCore } from '@rediagram/common'; | ||
|
||
export const Rediagram = RediagramCore.create(); |
Oops, something went wrong.