Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rediagram core module #189

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
60b4876
refactor(@rediagram/common): add class RediagramCore
kamiazya Nov 30, 2020
9fab073
refactor(rediagram): use RediagramCore
kamiazya Nov 30, 2020
a0fcbae
refactor(@rediagram/cli): use RediagramCore
kamiazya Nov 30, 2020
b4be978
chore: fix build script
kamiazya Nov 30, 2020
da517ee
fix(@rediagram/common): fix output dir
kamiazya Nov 30, 2020
e8a95b5
fix(@rediagram/cli): fix bug for this
kamiazya Nov 30, 2020
31a7076
feat(@rediagram/common): provides log methoeds
kamiazya Nov 30, 2020
9dd2fc8
feat(@rediagram/common): add error handling on runnning code #133
kamiazya Nov 30, 2020
9e4611f
feat(rediagram): add error handling on runnning code #133
kamiazya Nov 30, 2020
8364a50
chore(@rediagram/common): fix build script
kamiazya Dec 6, 2020
fdf8ba4
feat: add plugin system
kamiazya Jan 3, 2021
7e4051c
feat: load external plugins
kamiazya Jan 9, 2021
f93cd09
chore: fix plugin interface
kamiazya Jan 10, 2021
947a25c
chore: fix for core interface changes
kamiazya Jan 10, 2021
c3f1cec
feat(@rediagram/plugin-crf): add crf plugin package #201
kamiazya Jan 10, 2021
3daf9fa
chore: fix settings for @rediagram/plugin-crf
kamiazya Jan 10, 2021
eb5c7f0
feat(@rediagram/plugin-yaml): add @rediagram/plugin-yaml
kamiazya Jan 14, 2021
0a574d5
chore(@rediagram/plugin-crf): make private
kamiazya Jan 14, 2021
58b59b0
chore(@rediagram/gallery): add plugin
kamiazya Jan 14, 2021
df878b3
Merge branch 'master' into add-rediagram-core
kamiazya May 4, 2021
6adf7af
refactor: exclude preload plugins
kamiazya May 5, 2021
0f19e24
Merge branch 'master' into add-rediagram-core
kamiazya Jul 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion examples/gallery/.rediagramrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ version: beta1
output:
dir: img
includes:
- 'src/**/*.rediagram.tsx'
- 'src/**/*.rediagram.*'

# renderers:
# hoge: true
# exporters:
# hoge: true
# preprocessors:
# hoge: true
# postprocessors:
# hoge: true

plugins:
- name: '@rediagram/plugin-preset-default'
- name: '@rediagram/plugin-crf'
- name: '@rediagram/plugin-yaml'
Binary file modified examples/gallery/img/AppEngineAndCloudEndpoints.rediagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/gallery/img/ContentManagement.rediagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/gallery/img/GitToS3WebHooks.rediagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/gallery/img/HighPerformanceComputing.rediagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/gallery/img/MyInfra.rediagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/gallery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@rediagram/cli": "^0.1.4",
"@rediagram/firebase": "0.0.0",
"@rediagram/gcp": "^0.3.0",
"@rediagram/plugin-crf": "0.0.0",
"@rediagram/plugin-yaml": "0.0.0",
"@ts-graphviz/react": "^0.7.0",
"prop-types": "^15.7.2",
"react": "^16.13.1",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lerna": "lerna",
"test": "jest",
"prebuild": "lerna run --parallel clean",
"build": "yarn workspace @rediagram/common build && yarn workspace create-rediagram-project build && yarn workspace @rediagram/cdk build && yarn workspace @rediagram/cli build && yarn workspace rediagram build && yarn workspace @rediagram/aws build && yarn workspace @rediagram/gcp build",
"build": "yarn workspace @rediagram/common build && yarn workspace create-rediagram-project build && yarn workspace @rediagram/cdk build && yarn workspace rediagram build && yarn workspace @rediagram/cli build && yarn workspace @rediagram/aws build && yarn workspace @rediagram/gcp build && yarn workspace @rediagram/plugin-dot build && yarn workspace @rediagram/plugin-sharp build && yarn workspace @rediagram/plugin-sucrase build && yarn workspace @rediagram/plugin-preset-default build && yarn workspace @rediagram/plugin-crf build",
"prewatch": "yarn build",
"watch": "lerna run --parallel watch",
"rediagram": "yarn workspace @rediagram/cli start",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default {
file: './lib/rediagramc.js',
},
],
external: ['commander', 'fast-glob', 'path', 'fs', 'sucrase/dist/register', '@rediagram/common', 'chokidar'],
external: ['commander', 'fast-glob', 'path', 'fs', 'sucrase/dist/register', 'rediagram', 'chokidar'],
plugins: [typescript()],
};
20 changes: 5 additions & 15 deletions packages/cli/src/rediagramc.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
import cmd from 'commander';
import glob from 'fast-glob';
import chokidar from 'chokidar';
import { CONFIG } from '@rediagram/common';
import { registerAll } from 'sucrase/dist/register';
import path from 'path';
import { Rediagram } from 'rediagram';
import pkg from './pkg';

type Options = {
watch: boolean;
};

function runRediagram(src: string) {
// eslint-disable-next-line no-console
console.log(src);
const resolved = path.resolve(src);
// eslint-disable-next-line global-require, import/no-dynamic-require
require(resolved);
delete require.cache[resolved];
}
const rediagramProcess = Rediagram.process.bind(Rediagram);

cmd
.name('rediagramc')
.version(pkg.version)
.arguments('[pattarns...]')
.option('-w, --watch', 'Watch files for changes and rerun rediagram related to changed files.', false)
.action(async function rediagramc(this: Options, pattarns: string[]): Promise<void> {
registerAll();
const paths =
pattarns.length >= 1
? pattarns
: [...CONFIG.scope.getIncludesPattarns(), ...CONFIG.scope.getExcludesPattarns().map((p) => `!${p}`)];
: [...Rediagram.config.scope.includes, ...Rediagram.config.scope.excludes.map((p) => `!${p}`)];
if (this.watch) {
chokidar.watch(paths).on('add', runRediagram).on('change', runRediagram);
chokidar.watch(paths).on('add', rediagramProcess).on('change', rediagramProcess);
} else {
const sources = await glob(paths, {
dot: true,
extglob: true,
onlyFiles: true,
});
sources.forEach(runRediagram);
sources.forEach(rediagramProcess);
}
})
.parse(process.argv);
9 changes: 6 additions & 3 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"build": "ts-node scripts/build",
"watch": "tsc --watch"
},
"dependencies": {
"ajv": "^7.0.1",
"cosmiconfig": "^7.0.0",
"fs-extra": "^9.0.1",
"tslog": "^3.0.1"
},
"devDependencies": {
"@types/rimraf": "^3.0.0",
"rimraf": "^3.0.2",
Expand All @@ -35,8 +41,5 @@
"rollup-plugin-typescript2": "^0.28.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
},
"dependencies": {
"cosmiconfig": "^7.0.0"
}
}
14 changes: 13 additions & 1 deletion packages/common/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ import rimraf from 'rimraf';
async function build() {
await rollup({
input: path.resolve(__dirname, '../src/index.ts'),
external: ['path', 'cosmiconfig'],
external: [
'path',
'cosmiconfig',
'fs-extra',
'@ts-graphviz/react',
'@ts-graphviz/node',
'tslog',
'sharp',
'fs',
'jsdom',
'ajv',
'sucrase/dist/register',
],
plugins: [typescript()],
}).then((result) =>
result.write({
Expand Down
44 changes: 44 additions & 0 deletions packages/common/src/config/beta1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable @typescript-eslint/no-namespace */
import path from 'path';
import { RediagramConfig } from './types';

export namespace beta1 {
export const version = 'beta1';

interface Config {
version: typeof version;
includes?: string[];
excludes?: string[];
output?: {
dir?: string;
type?: string;
};
plugins?: {
name: string;
[option: string]: any;
}[];
dot?: {
timeout?: number;
};
}

export function load(filepath: string, data: Config): RediagramConfig {
return {
core: {
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/**/*'],
},
plugins: (data.plugins ?? []).map(({ name, ...options }) => ({ name, options })),
},
dot: {
timeout: data.dot?.timeout,
},
};
}
}
27 changes: 27 additions & 0 deletions packages/common/src/config/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RediagramConfig } from './types';

export function createDefaultConfig(): RediagramConfig {
return {
core: {
filepath: null,
scope: {
includes: ['**/*.rediagram.{jsx,tsx}'],
excludes: ['**/node_modules/**/*'],
},
output: {
format: 'png',
},
plugins: [
{
name: '@rediagram/plugin-dot',
},
{
name: '@rediagram/plugin-sharp',
},
{
name: '@rediagram/plugin-sucrase',
},
],
},
};
}
103 changes: 4 additions & 99 deletions packages/common/src/config/index.ts
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';
20 changes: 20 additions & 0 deletions packages/common/src/config/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface RediagramCoreOption {
filepath: string | null;
scope: Readonly<{
includes: ReadonlyArray<string>;
excludes: ReadonlyArray<string>;
}>;
output: Readonly<{
dir?: string;
format: string;
}>;
plugins: ReadonlyArray<{
name: string;
options?: any;
}>;
}

export interface RediagramConfig {
core: RediagramCoreOption;
[name: string]: any;
}
17 changes: 17 additions & 0 deletions packages/common/src/config/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { cosmiconfigSync } from 'cosmiconfig';
import { RediagramConfig } from './types';
import { beta1 } from './beta1';
import { createDefaultConfig } from './default';

const MODULE_NAME = 'rediagram';

export function loadConfig(): RediagramConfig {
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();
}
}
2 changes: 0 additions & 2 deletions packages/common/src/config/version.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './config';
export * from './rediagram';
Loading