Skip to content

Commit ffd46a1

Browse files
authored
refactor(service): proper models for JSON files (#31)
1 parent f459280 commit ffd46a1

File tree

21 files changed

+303
-147
lines changed

21 files changed

+303
-147
lines changed

packages/service/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/service/src/commands/build.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { Command, ICommandHandler, IRunOptions } from '../lib/command';
55
import { handleProcessError, runProcess } from '../utils/process';
66
import { logInfo, logInfoBold, Spinner } from '../utils/ui';
77
import { ensureDirectoryExists, packageRoot, runtimeRoot } from '../utils/path';
8-
import { Config } from '../models/Config';
98
import { sync } from 'rimraf';
9+
import { VuesionConfig } from '../models/VuesionConfig';
1010

1111
@Command({
1212
name: 'build',
@@ -105,8 +105,8 @@ const renderPage = async (renderer: BundleRenderer, route: string) => {
105105
return renderer.renderToString({
106106
url: route,
107107
cookies: {},
108-
acceptLanguage: Config.i18n.defaultLocale,
109-
htmlLang: Config.i18n.defaultLocale.substr(0, 2),
108+
acceptLanguage: VuesionConfig.i18n.defaultLocale,
109+
htmlLang: VuesionConfig.i18n.defaultLocale.substr(0, 2),
110110
appConfig: {},
111111
redirect: null,
112112
});
@@ -123,8 +123,8 @@ const renderPages = async (options: IRunOptions) => {
123123
const renderer: BundleRenderer = createBundleRenderer(runtimeRoot('dist/server/vue-ssr-bundle.json'), {
124124
template: fs.readFileSync(runtimeRoot('dist/index.html')).toString(),
125125
});
126-
const appShellRoute: string = pathOr<string>('/', ['spa', 'appShellRoute'], Config);
127-
const routes: string[] = pathOr<string[]>([], ['spa', 'additionalRoutes'], Config);
126+
const appShellRoute: string = pathOr<string>('/', ['spa', 'appShellRoute'], VuesionConfig);
127+
const routes: string[] = pathOr<string[]>([], ['spa', 'additionalRoutes'], VuesionConfig);
128128

129129
routes.unshift(appShellRoute);
130130

packages/service/src/commands/clean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Command, ICommandHandler, IRunOptions } from '../lib/command';
22
import { logError, Spinner } from '../utils/ui';
3-
import { Config } from '../models/Config';
43
import { sync } from 'rimraf';
4+
import { VuesionConfig } from '../models/VuesionConfig';
55

66
@Command({
77
name: 'clean',
@@ -15,7 +15,7 @@ export class Clean implements ICommandHandler {
1515
spinner.start(options.debug);
1616

1717
try {
18-
Config.clean.forEach((glob) => sync(glob));
18+
VuesionConfig.clean.forEach((glob) => sync(glob));
1919
spinner.message = 'Directories cleaned';
2020
spinner.stop();
2121
} catch (e) {

packages/service/src/commands/prettier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command, ICommandHandler } from '../lib/command';
2-
import { Config } from '../models/Config';
2+
import { VuesionConfig } from '../models/VuesionConfig';
33

44
@Command({
55
name: 'prettier',
@@ -29,7 +29,7 @@ const prettier = (args: string[], pattern: string) => {
2929
'--ignore-path',
3030
'.prettierignore',
3131
'--write',
32-
pattern ? pattern : `./**/*.{${Config.prettier.extensions}}`,
32+
pattern ? pattern : `./**/*.{${VuesionConfig.prettier.extensions}}`,
3333
];
3434

3535
process.argv = process.argv.concat(args);

packages/service/src/commands/test.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import * as fs from 'fs';
21
import { Command, ICommandHandler } from '../lib/command';
3-
import { getWebpackAliases } from '../models/Config';
4-
import { runtimeRoot } from '../utils/path';
52

63
@Command({
74
name: 'test',
@@ -16,20 +13,6 @@ export class Test implements ICommandHandler {
1613
process.env.NODE_ENV = 'test';
1714

1815
const jest = require('jest');
19-
const aliases = getWebpackAliases();
20-
const jestConfig = JSON.parse(fs.readFileSync(runtimeRoot('package.json')).toString()).jest;
21-
22-
if (aliases) {
23-
if (!jestConfig.moduleNameMapper) {
24-
jestConfig.moduleNameMapper = {};
25-
}
26-
27-
Object.keys(aliases).map((alias: string) => {
28-
jestConfig.moduleNameMapper[`^${alias}/(.*)$`] = `<rootDir>/${aliases[alias]}/$1`;
29-
});
30-
31-
args.push(`--config=${JSON.stringify(jestConfig)}`);
32-
}
3316

3417
jest.run(args);
3518
}

packages/service/src/generators/component/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
2-
import { Config } from '../../models/Config';
32
import { folderExists, runtimeRoot } from '../../utils/path';
3+
import { VuesionConfig } from '../../models/VuesionConfig';
44

55
export = {
66
description: 'Add a single file component',
@@ -14,7 +14,7 @@ export = {
1414
return 'name is required';
1515
}
1616

17-
return folderExists(runtimeRoot(path.join(Config.generators.outputDirectory, value)))
17+
return folderExists(runtimeRoot(path.join(VuesionConfig.generators.outputDirectory, value)))
1818
? `folder already exists (${value})`
1919
: true;
2020
},
@@ -30,19 +30,27 @@ export = {
3030
const filePath: string[] = data.name.split('/');
3131

3232
data.componentName = filePath.pop();
33-
data.basePath = path.join(process.cwd(), Config.generators.outputDirectory, filePath.join('/'));
33+
data.basePath = path.join(process.cwd(), VuesionConfig.generators.outputDirectory, filePath.join('/'));
3434

3535
const actions: any[] = [
3636
{
3737
type: 'add',
3838
path: '{{basePath}}/{{properCase componentName}}/{{properCase componentName}}.vue',
39-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'component/component.vue.hbs'),
39+
templateFile: path.join(
40+
process.cwd(),
41+
VuesionConfig.generators.blueprintDirectory,
42+
'component/component.vue.hbs',
43+
),
4044
abortOnFail: true,
4145
},
4246
{
4347
type: 'add',
4448
path: '{{basePath}}/{{properCase componentName}}/{{properCase componentName}}.spec.ts',
45-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'component/component.spec.ts.hbs'),
49+
templateFile: path.join(
50+
process.cwd(),
51+
VuesionConfig.generators.blueprintDirectory,
52+
'component/component.spec.ts.hbs',
53+
),
4654
abortOnFail: true,
4755
},
4856
];
@@ -53,7 +61,7 @@ export = {
5361
path: '{{basePath}}/{{properCase componentName}}/{{properCase componentName}}.stories.ts',
5462
templateFile: path.join(
5563
process.cwd(),
56-
Config.generators.blueprintDirectory,
64+
VuesionConfig.generators.blueprintDirectory,
5765
'component/component.stories.ts.hbs',
5866
),
5967
abortOnFail: true,

packages/service/src/generators/connected/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path';
2-
import { Config } from '../../models/Config';
2+
import { VuesionConfig } from '../../models/VuesionConfig';
33
import { folderExists, runtimeRoot } from '../../utils/path';
44

55
export = {
@@ -18,7 +18,7 @@ export = {
1818
return 'a connected component has to live in a module';
1919
}
2020

21-
return folderExists(runtimeRoot(path.join(Config.generators.outputDirectory, value)))
21+
return folderExists(runtimeRoot(path.join(VuesionConfig.generators.outputDirectory, value)))
2222
? `folder already exists (${value})`
2323
: true;
2424
},
@@ -29,20 +29,28 @@ export = {
2929

3030
data.componentName = filePath.pop();
3131
data.moduleName = filePath.pop();
32-
data.basePath = path.join(process.cwd(), Config.generators.outputDirectory, filePath.join('/'));
32+
data.basePath = path.join(process.cwd(), VuesionConfig.generators.outputDirectory, filePath.join('/'));
3333
data.wantVuex = true;
3434

3535
return [
3636
{
3737
type: 'add',
3838
path: '{{basePath}}/{{camelCase moduleName}}/{{properCase componentName}}/{{properCase componentName}}.vue',
39-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'connected/connected.vue.hbs'),
39+
templateFile: path.join(
40+
process.cwd(),
41+
VuesionConfig.generators.blueprintDirectory,
42+
'connected/connected.vue.hbs',
43+
),
4044
abortOnFail: true,
4145
},
4246
{
4347
type: 'add',
4448
path: '{{basePath}}/{{camelCase moduleName}}/{{properCase componentName}}/{{properCase componentName}}.spec.ts',
45-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'connected/connected.spec.ts.hbs'),
49+
templateFile: path.join(
50+
process.cwd(),
51+
VuesionConfig.generators.blueprintDirectory,
52+
'connected/connected.spec.ts.hbs',
53+
),
4654
abortOnFail: true,
4755
},
4856
];

packages/service/src/generators/module/index.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22
import { addModuleToRouter, addModuleToState } from '../ast';
3-
import { Config } from '../../models/Config';
3+
import { VuesionConfig } from '../../models/VuesionConfig';
44
import { folderExists, runtimeRoot } from '../../utils/path';
55

66
export = {
@@ -15,7 +15,7 @@ export = {
1515
return 'name is required';
1616
}
1717

18-
return folderExists(runtimeRoot(path.join(Config.generators.outputDirectory, value)))
18+
return folderExists(runtimeRoot(path.join(VuesionConfig.generators.outputDirectory, value)))
1919
? `folder already exists (${value})`
2020
: true;
2121
},
@@ -38,19 +38,27 @@ export = {
3838

3939
data.moduleName = filePath.pop();
4040
data.componentName = data.moduleName;
41-
data.basePath = path.join(process.cwd(), Config.generators.outputDirectory, filePath.join('/'));
41+
data.basePath = path.join(process.cwd(), VuesionConfig.generators.outputDirectory, filePath.join('/'));
4242

4343
let actions: any[] = [
4444
{
4545
type: 'add',
4646
path: '{{basePath}}/{{camelCase moduleName}}/{{properCase componentName}}/{{properCase componentName}}.vue',
47-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'connected/connected.vue.hbs'),
47+
templateFile: path.join(
48+
process.cwd(),
49+
VuesionConfig.generators.blueprintDirectory,
50+
'connected/connected.vue.hbs',
51+
),
4852
abortOnFail: true,
4953
},
5054
{
5155
type: 'add',
5256
path: '{{basePath}}/{{camelCase moduleName}}/{{properCase componentName}}/{{properCase componentName}}.spec.ts',
53-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'connected/connected.spec.ts.hbs'),
57+
templateFile: path.join(
58+
process.cwd(),
59+
VuesionConfig.generators.blueprintDirectory,
60+
'connected/connected.spec.ts.hbs',
61+
),
5462
abortOnFail: true,
5563
},
5664
];
@@ -59,66 +67,82 @@ export = {
5967
actions.push({
6068
type: 'add',
6169
path: '{{basePath}}/{{camelCase moduleName}}/routes.ts',
62-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/routes.ts.hbs'),
70+
templateFile: path.join(process.cwd(), VuesionConfig.generators.blueprintDirectory, 'module/routes.ts.hbs'),
6371
abortOnFail: true,
6472
});
6573

66-
addModuleToRouter(path.join(path.resolve(process.cwd()), Config.generators.routerFile), data.moduleName);
74+
addModuleToRouter(path.join(path.resolve(process.cwd()), VuesionConfig.generators.routerFile), data.moduleName);
6775
}
6876

6977
if (data.wantVuex) {
7078
actions = actions.concat([
7179
{
7280
type: 'add',
7381
path: '{{basePath}}/{{camelCase moduleName}}/actions.spec.ts',
74-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/actions.spec.ts.hbs'),
82+
templateFile: path.join(
83+
process.cwd(),
84+
VuesionConfig.generators.blueprintDirectory,
85+
'module/actions.spec.ts.hbs',
86+
),
7587
abortOnFail: true,
7688
},
7789
{
7890
type: 'add',
7991
path: '{{basePath}}/{{camelCase moduleName}}/actions.ts',
80-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/actions.ts.hbs'),
92+
templateFile: path.join(process.cwd(), VuesionConfig.generators.blueprintDirectory, 'module/actions.ts.hbs'),
8193
abortOnFail: true,
8294
},
8395
{
8496
type: 'add',
8597
path: '{{basePath}}/{{camelCase moduleName}}/getters.spec.ts',
86-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/getters.spec.ts.hbs'),
98+
templateFile: path.join(
99+
process.cwd(),
100+
VuesionConfig.generators.blueprintDirectory,
101+
'module/getters.spec.ts.hbs',
102+
),
87103
abortOnFail: true,
88104
},
89105
{
90106
type: 'add',
91107
path: '{{basePath}}/{{camelCase moduleName}}/getters.ts',
92-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/getters.ts.hbs'),
108+
templateFile: path.join(process.cwd(), VuesionConfig.generators.blueprintDirectory, 'module/getters.ts.hbs'),
93109
abortOnFail: true,
94110
},
95111
{
96112
type: 'add',
97113
path: '{{basePath}}/{{camelCase moduleName}}/module.ts',
98-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/module.ts.hbs'),
114+
templateFile: path.join(process.cwd(), VuesionConfig.generators.blueprintDirectory, 'module/module.ts.hbs'),
99115
abortOnFail: true,
100116
},
101117
{
102118
type: 'add',
103119
path: '{{basePath}}/{{camelCase moduleName}}/mutations.spec.ts',
104-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/mutations.spec.ts.hbs'),
120+
templateFile: path.join(
121+
process.cwd(),
122+
VuesionConfig.generators.blueprintDirectory,
123+
'module/mutations.spec.ts.hbs',
124+
),
105125
abortOnFail: true,
106126
},
107127
{
108128
type: 'add',
109129
path: '{{basePath}}/{{camelCase moduleName}}/mutations.ts',
110-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/mutations.ts.hbs'),
130+
templateFile: path.join(
131+
process.cwd(),
132+
VuesionConfig.generators.blueprintDirectory,
133+
'module/mutations.ts.hbs',
134+
),
111135
abortOnFail: true,
112136
},
113137
{
114138
type: 'add',
115139
path: '{{basePath}}/{{camelCase moduleName}}/state.ts',
116-
templateFile: path.join(process.cwd(), Config.generators.blueprintDirectory, 'module/state.ts.hbs'),
140+
templateFile: path.join(process.cwd(), VuesionConfig.generators.blueprintDirectory, 'module/state.ts.hbs'),
117141
abortOnFail: true,
118142
},
119143
]);
120144

121-
addModuleToState(path.join(path.resolve(process.cwd()), Config.generators.stateFile), data.moduleName);
145+
addModuleToState(path.join(path.resolve(process.cwd()), VuesionConfig.generators.stateFile), data.moduleName);
122146
}
123147

124148
return actions;

packages/service/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import * as commander from 'commander';
2-
import { Package } from './models/Package';
32
import { sync } from 'glob';
43
import { packageRoot } from './utils/path';
5-
import { updateTsConfig } from './utils/misc';
4+
import { TSConfig } from './models/TSConfig';
5+
import { VuesionPackage } from './models/VuesionPackage';
6+
import { ServicePackage } from './models/ServicePackage';
67

78
sync(`${packageRoot()}/dist/commands/*.js`).forEach((file: string) => require(file));
89

910
commander
1011
.name('vuesion')
11-
.version(Package.version, '-v, --version')
12+
.version(ServicePackage.version, '-v, --version')
1213
.option('-d, --debug', 'Show debugging output.', false)
13-
.description(Package.description);
14+
.description(ServicePackage.description);
1415

1516
/**
1617
* update tsconfig with latest aliases
1718
*/
18-
updateTsConfig();
19+
TSConfig.updateCompilerOptionsPaths();
20+
VuesionPackage.updateModuleNameMapper();
1921

2022
commander.parse(process.argv);

0 commit comments

Comments
 (0)