forked from xcatliu/pagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagic.ts
161 lines (157 loc) · 5.9 KB
/
Pagic.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
// import type { logLevel } from '../mod.ts';
// import type { React } from '../deps.ts';
import { colors, fs, Confirm } from 'Pagic/deps.ts';
import { serve, PagicWatcherFactory, PagicConfiguration } from 'PagicUtils/mod.ts';
import type { PagicLogger, PagicConfig, CliffyCommandOptions } from 'PagicUtils/mod.ts';
// export { PagicConfig } from './utils/PagicConfigure.ts';
// import { default as PagicWatcherFactory } from './utils/PagicWatcherFactory.ts';
// import type { PagicConfig } from './utils/PagicConfiguration.ts';
// import { default as PagicConfiguration } from './utils/PagicConfiguration.ts';
// export type {
// GaProps,
// GitalkProps,
// PagePropsSidebar,
// PagicConfigSidebar,
// OnePagicConfigSidebar,
// PagePropsBlog,
// PagicLayout,
// PageProps,
// PagicConfigInterface,
// PagicEnvironment,
// PagicInitCommandConfig,
// PagicBuildCommandConfig,
// CliffyCommandOptions,
// } from './mod.ts';
// import type { CliffyCommandOptions } from '../mod.ts';
// export type { Pagic } from './mod.ts';
export default class Pagic extends PagicConfiguration {
// #region properties
public rebuilding = true;
// @ts-ignore
public config: PagicConfig = {};
// @ts-ignore
public logger: PagicLogger = {};
// @ts-ignore
private watchers: PagicWatcherFactory = {};
// private _config: PagicConfigure = {};
// #endregion
public constructor(config: Partial<PagicConfig> = {}, cmd: CliffyCommandOptions = {}) {
// pagic configure should be init'ed here and default name created
// configure process will utilize default name until it parses config
// then it will hand the name over to pagic here
// this.name = await config.logger;
// pull in PagicConfigure
super(cmd);
// this.console.log(config);
// this._config = config;
// this.config = config._config;
// console.log(env);
// this.runtimeConfig = options;
// const logger = await new PagicLogger(defaultEnv).init();
// this.name = new PagicLogger({
// logPath: defaultConfig.env?.PAGIC_LOG_PATH ?? defaultConfig.cmd?.pagic.logPath,
// logLevel: defaultConfig.env?.PAGIC_LOG_LEVEL ?? defaultConfig.cmd?.pagic.logLevel,
// logFormat: defaultConfig.env?.PAGIC_LOG_FORMAT ?? defaultConfig.cmd?.pagic.logFormat,
// consoleLevel: defaultConfig.env?.PAGIC_CONSOLE_LEVEL ?? defaultConfig.cmd?.pagic.consoleLevel,
// consoleColor: defaultConfig.env?.PAGIC_CONSOLE_COLOR ?? defaultConfig.cmd?.pagic.consoleColor,
// consoleFormat: defaultConfig.env?.PAGIC_CONSOLE_FORMAT ?? defaultConfig.cmd?.pagic.consoleFormat,
// });
// this.name.init();
}
public async start() {
this.logger = await this.config.l;
this.logger.success('hello from PAGIC');
}
public async build() {
await this.config.rebuild();
if (this.config.serve) {
this.serve();
}
if (this.config.watch) {
await this.watch();
}
}
public async init() {
// console.log(this.runtimeConfig);
if (this.config.runtimeConfig.init?.site) {
if (await fs.exists('pagic.config.ts')) {
this.logger.warn('pagic.config.ts already exists, exit');
return;
}
if (await fs.exists('pagic.config.tsx')) {
this.logger.warn('pagic.config.tsx already exists, exit');
return;
}
await Deno.writeTextFile('pagic.config.ts', 'export default {};\n');
} else if (this.config.runtimeConfig.init?.theme) {
if (
(await fs.exists('mod.ts')) &&
(this.runtimeConfig.init?.overwrite === undefined ? true : !this.runtimeConfig.init?.overwrite)
) {
const confirmed = await Confirm.prompt({
message: 'mod.ts already exists, do you want to override it?',
...(this.config.runtimeConfig.init?.overwrite === undefined
? {
hint: 'You can use --overwrite to make this COMPLETELY autopagic.',
}
: { hint: 'You can use pagic init [theme|init|plugin] for autopagic completion.' }),
});
if (!confirmed) {
return;
}
}
this.generateThemeMod();
} else if (this.config.runtimeConfig.init?.plugin) {
this.logger.error(`Plugin generation not implemented.`);
}
}
private async generateThemeMod() {
// this.config = {
// ...defaultConfig,
// exclude: [...defaultConfig.exclude!, 'mod.ts'],
// };
await this.config.initPaths();
await Deno.writeTextFile(
'./mod.ts',
`export default {\n files: [\n${[...this.config.staticPaths, ...this.config.layoutPaths]
.map((filePath) => ` '${filePath}'`)
.join(',\n')}\n ]\n};\n`,
);
}
private async watch() {
// watch pagic.config.ts/x, root project, and potential theme
this.watchers = new PagicWatcherFactory(this.config, this.config.pagicConfigPath);
await this.watchers.watch(async (status: string, path = '') => {
this.logger.success(
'watcher',
status === 'rebuild' ? 'Rebuilding...' : 'Reloading plugins change to: ',
colors.underline(path),
);
if (status === 'rebuild') {
await this.watchers.removeWatchers(); // this could go to rebuild function
this.rebuilding = true;
await this.config.rebuild();
this.rebuilding = false;
await this.watchers.init(this.config, this.config.pagicConfigPath); // this could go to rebuild function
} else {
// @ts-ignore if path not in array, push and reload
// if (this[status].indexOf(path) === -1) this[status].push(path);
await this.runPlugins().then(() => {
this.logger.success('watcher', 'Reloaded Plugins');
});
}
});
}
private async serve() {
serve({
serveDir: this.config.outDir,
root: this.config.root,
port: this.config.port,
});
this.logger.success(
'Serve',
colors.underline(this.config.outDir),
`on http://127.0.0.1:${this.config.port}${this.config.root}`,
);
}
}