forked from event-catalog/eventcatalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventcatalog.ts
executable file
·164 lines (135 loc) · 4.12 KB
/
eventcatalog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Command } from 'commander';
import { execSync } from 'node:child_process';
import { join } from 'node:path';
import fs from 'fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import concurrently from 'concurrently';
import { generate } from './generate';
import logBuild from './analytics/log-build';
import { VERSION } from './constants';
import { watch } from './watcher';
import { catalogToAstro } from './catalog-to-astro-content-directory';
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const program = new Command().version(VERSION);
// The users dierctory
const dir = path.resolve(process.env.PROJECT_DIR || process.cwd());
// The tmp core directory
const core = path.resolve(process.env.CATALOG_DIR || join(dir, '.eventcatalog-core'));
// The project itself
const eventCatalogDir = path.resolve(join(currentDir, '../eventcatalog/'));
program.name('eventcatalog').description('Documentation tool for event-driven architectures');
const ensureDir = (dir: string) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
};
const copyCore = () => {
// make sure the core folder exists
ensureDir(core);
if (eventCatalogDir === core) {
// This is used for development purposes as it's not possible cp a dir to itself.
// Into development usually core is the root equals to eventCatalogDir.
return;
}
// Copy required eventcatlog files into users directory
fs.cpSync(eventCatalogDir, core, {
recursive: true,
filter: (src) => {
// if(src.includes('node_modules')) {
// return false;
// }
return true;
},
});
};
const clearCore = () => {
if (fs.existsSync(core)) fs.rmSync(core, { recursive: true });
};
program
.command('dev')
.description('Run development server of EventCatalog')
.option('-d, --debug', 'Output EventCatalog application information into your terminal')
.option('--force-recreate', 'Recreate the eventcatalog-core directory', false)
.action(async (options) => {
// // Copy EventCatalog core over
console.log('Setting up EventCatalog....');
if (options.debug) {
console.log('Debug mode enabled');
console.log('PROJECT_DIR', dir);
console.log('CATALOG_DIR', core);
}
if (options.forceRecreate) clearCore();
copyCore();
console.log('EventCatalog is starting at http://localhost:3000/docs');
await catalogToAstro(dir, core);
let watchUnsub;
try {
watchUnsub = await watch(dir, core);
const { result } = concurrently([
{
name: 'astro',
command: 'npx astro dev',
cwd: core,
env: {
PROJECT_DIR: dir,
CATALOG_DIR: core,
},
},
]);
await result;
} catch (err) {
console.error(err);
} finally {
await watchUnsub?.();
}
});
program
.command('build')
.description('Run build of EventCatalog')
.action(async (options) => {
console.log('Building EventCatalog...');
copyCore();
await logBuild(dir);
await catalogToAstro(dir, core);
execSync(`cross-env PROJECT_DIR='${dir}' CATALOG_DIR='${core}' npx astro build`, {
cwd: core,
stdio: 'inherit',
});
});
const previewCatalog = () => {
/**
* TODO: get the port and outDir from the eventcatalog.config.js.
*/
execSync(`cross-env PROJECT_DIR='${dir}' CATALOG_DIR='${core}' npx astro preview --root ${dir} --port 3000`, {
cwd: core,
stdio: 'inherit',
});
};
program
.command('preview')
.description('Serves the contents of your eventcatalog build directory')
.action((options) => {
console.log('Starting preview of your build...');
previewCatalog();
});
program
.command('start')
.description('Serves the contents of your eventcatalog build directory')
.action((options) => {
console.log('Starting preview of your build...');
previewCatalog();
});
program
.command('generate [siteDir]')
.description('Start the generator scripts.')
.action(async () => {
await generate(dir);
});
program
.parseAsync()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});