Skip to content

Commit 4ef3cf8

Browse files
authored
reorganising and splitting complex parts (#5)
* reorganising and splitting complex parts * Update write-index-htmls.ts
1 parent 0052466 commit 4ef3cf8

21 files changed

+240
-207
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
node_modules
3+
src/**/*.js
34
src/*.js
45
/tsconfig.tsbuildinfo

src/configuration.ts

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,6 @@
1-
import {
2-
readFileSync,
3-
existsSync,
4-
} from 'fs';
5-
6-
interface Sitemap {
7-
domain: string,
8-
build: boolean,
9-
}
10-
interface Routes {
11-
build: boolean,
12-
type: 'tsx'|'jsx',
13-
overridePathMappings: {
14-
[filesystemPath: string]: string,
15-
},
16-
useOutput: boolean,
17-
}
18-
interface HTMLMinify {
19-
collapseBooleanAttributes: boolean,
20-
conservativeCollapse: boolean,
21-
collapseWhitespace: boolean,
22-
removeAttributeQuotes: boolean,
23-
removeComments: boolean,
24-
}
25-
interface FileBuilder {
26-
buildIndex: boolean,
27-
minifyPages: boolean,
28-
appendPageChunk: boolean,
29-
preLoadCSS: boolean,
30-
preLoadJS: boolean,
31-
preloadStartDelay: number,
32-
}
33-
interface FileFinder {
34-
fileName: string,
35-
pagesRoot: string,
36-
distJSRoot: string,
37-
}
38-
interface Runtime {
39-
reloadWaitMs: number,
40-
}
41-
export interface Configuration {
42-
runtime: Runtime,
43-
sitemap: Sitemap,
44-
routes: Routes,
45-
htmlMinify: HTMLMinify,
46-
fileBuilder: FileBuilder,
47-
fileFinder: FileFinder,
48-
}
1+
import {Configuration} from './configuration/configuration.js';
2+
import cli from './configuration/cli.js';
3+
import file from './configuration/file.js';
494

505
export default (cwd: string, cliArguments: string[]): Configuration => {
516
const config: Configuration = {
@@ -86,46 +41,8 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
8641
preloadStartDelay: 5000,
8742
},
8843
};
89-
const configFile = cwd + '/.idrinth-react-file-based-routes.json';
90-
if (existsSync(configFile)) {
91-
const userconfig: Configuration = JSON.parse(readFileSync(configFile, 'utf-8'));
92-
if (typeof userconfig === 'object') {
93-
for (const prop of Object.keys(config) as [keyof Configuration]) {
94-
if (typeof userconfig[prop] === 'object' && typeof config[prop] === 'object') {
95-
for (const setting of Object.keys(config[prop]) as [keyof Configuration[typeof prop]]) {
96-
if (typeof config[prop][setting] === typeof userconfig[prop][setting]) {
97-
config[prop][setting] = userconfig[prop][setting];
98-
}
99-
}
100-
}
101-
}
102-
}
103-
}
104-
for (const param of cliArguments) {
105-
if (param.startsWith('--')) {
106-
const [setting, value,] = param.substring(2).split('=') as [string, undefined|string];
107-
const [group, detail,] = setting.split('.') as [keyof Configuration, keyof Runtime|keyof Sitemap|keyof Routes|keyof FileFinder|keyof FileBuilder|keyof HTMLMinify];
108-
if (group && typeof config[group] === 'object' && detail) {
109-
// @ts-ignore TS7053
110-
if (typeof config[group][detail] === 'boolean') {
111-
if (value === 'true') {
112-
// @ts-ignore TS7053
113-
config[group][detail] = true;
114-
} else if (value === 'false') {
115-
// @ts-ignore TS7053
116-
config[group][detail] = false;
117-
} else {
118-
// @ts-ignore TS7053
119-
config[group][detail] = !config[group][detail];
120-
}
121-
// @ts-ignore TS7053
122-
} else if (typeof config[group][detail] === 'string' && value) {
123-
// @ts-ignore TS7053
124-
config[group][detail] = value;
125-
}
126-
}
127-
}
128-
}
44+
file(cwd, config,)
45+
cli(cliArguments, config,);
12946
if (config.sitemap.build && !config.sitemap.domain) {
13047
throw new Error('sitemap.domain must be set if a sitemap should be build.');
13148
}

src/configuration/cli.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Configuration} from './configuration.js';
2+
import {Runtime} from './runtime.js';
3+
import {Sitemap} from './sitemap.js';
4+
import {Routes} from './routes.js';
5+
import {FileFinder} from './file-finder.js';
6+
import {FileBuilder} from './file-builder.js';
7+
import {HTMLMinify} from './html-minify.js';
8+
9+
export default (cliArguments: string[], config: Configuration) => {
10+
for (const param of cliArguments) {
11+
if (param.startsWith('--')) {
12+
const [setting, value,] = param.substring(2).split('=') as [string, undefined|string];
13+
const [group, detail,] = setting.split('.') as [keyof Configuration, keyof Runtime|keyof Sitemap|keyof Routes|keyof FileFinder|keyof FileBuilder|keyof HTMLMinify];
14+
if (group && typeof config[group] === 'object' && detail) {
15+
// @ts-ignore TS7053
16+
if (typeof config[group][detail] === 'boolean') {
17+
if (value === 'true') {
18+
// @ts-ignore TS7053
19+
config[group][detail] = true;
20+
} else if (value === 'false') {
21+
// @ts-ignore TS7053
22+
config[group][detail] = false;
23+
} else {
24+
// @ts-ignore TS7053
25+
config[group][detail] = !config[group][detail];
26+
}
27+
// @ts-ignore TS7053
28+
} else if (typeof config[group][detail] === 'string' && value) {
29+
// @ts-ignore TS7053
30+
config[group][detail] = value;
31+
}
32+
}
33+
}
34+
}
35+
}

src/configuration/configuration.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Runtime} from './runtime.js';
2+
import {Sitemap} from './sitemap.js';
3+
import {Routes} from './routes.js';
4+
import {HTMLMinify} from './html-minify.js';
5+
import {FileBuilder} from './file-builder.js';
6+
import {FileFinder} from './file-finder.js';
7+
8+
export interface Configuration {
9+
runtime: Runtime,
10+
sitemap: Sitemap,
11+
routes: Routes,
12+
htmlMinify: HTMLMinify,
13+
fileBuilder: FileBuilder,
14+
fileFinder: FileFinder,
15+
}

src/configuration/file-builder.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface FileBuilder {
2+
buildIndex: boolean,
3+
minifyPages: boolean,
4+
appendPageChunk: boolean,
5+
preLoadCSS: boolean,
6+
preLoadJS: boolean,
7+
preloadStartDelay: number,
8+
}

src/configuration/file-finder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface FileFinder {
2+
fileName: string,
3+
pagesRoot: string,
4+
distJSRoot: string,
5+
}

src/configuration/file.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {existsSync, readFileSync} from 'fs';
2+
import {Configuration} from './configuration.js';
3+
4+
export default (cwd: string, config: Configuration) => {
5+
const configFile = cwd + '/.idrinth-react-file-based-routes.json';
6+
if (existsSync(configFile)) {
7+
const userconfig: Configuration = JSON.parse(readFileSync(configFile, 'utf-8'));
8+
if (typeof userconfig === 'object') {
9+
for (const prop of Object.keys(config) as [keyof Configuration]) {
10+
if (typeof userconfig[prop] === 'object' && typeof config[prop] === 'object') {
11+
for (const setting of Object.keys(config[prop]) as [keyof Configuration[typeof prop]]) {
12+
if (typeof config[prop][setting] === typeof userconfig[prop][setting]) {
13+
config[prop][setting] = userconfig[prop][setting];
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

src/configuration/html-minify.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface HTMLMinify {
2+
collapseBooleanAttributes: boolean,
3+
conservativeCollapse: boolean,
4+
collapseWhitespace: boolean,
5+
removeAttributeQuotes: boolean,
6+
removeComments: boolean,
7+
}

src/configuration/routes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Routes {
2+
build: boolean,
3+
type: 'tsx' | 'jsx',
4+
overridePathMappings: {
5+
[filesystemPath: string]: string,
6+
},
7+
useOutput: boolean,
8+
}

src/configuration/runtime.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Runtime {
2+
reloadWaitMs: number,
3+
}

0 commit comments

Comments
 (0)