-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
schematic.ts
140 lines (126 loc) · 4.09 KB
/
schematic.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
import {
apply,
applyTemplates,
chain, externalSchematic,
mergeWith,
move,
Rule, SchematicContext,
Tree,
url
} from '@angular-devkit/schematics';
import {
addDepsToPackageJson,
addPackageWithInit,
addProjectToNxJsonInTree,
formatFiles,
names,
offsetFromRoot,
ProjectType,
toFileName, updateJsonInTree,
updateWorkspace
} from '@nrwl/workspace';
import { IonicAppSchema } from './schema';
import { AppType } from '../../utils/typings';
import { addDefaultBuilders, calculateStyle } from '../../utils/utils';
import core from '../core/core';
import { appsDir } from '@nrwl/workspace/src/utils/ast-utils';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
const projectType = ProjectType.Application;
function normalizeOptions(options: IonicAppSchema, host: Tree): IonicAppSchema {
const name = toFileName(options.name);
const projectDirectory = options.directory
? `${toFileName(options.directory)}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectRoot = `${appsDir(host)}/${projectDirectory}`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
const style = calculateStyle(options.style);
const appType = AppType.CapacitorApp;
const appTemplate = toFileName(options.appTemplate);
const capAppName = `${projectName}-cap`;
return {
...options,
projectName,
projectRoot,
projectDirectory,
parsedTags,
style,
appType,
appTemplate,
capAppName
} as IonicAppSchema;
}
function addFiles(options: IonicAppSchema): Rule {
return mergeWith(
apply(url(`./files/${options.appTemplate}`), [
applyTemplates({
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot)
}),
move(options.projectRoot),
formatFiles({ skipFormat: false })
])
);
}
function showInformation(options: IonicAppSchema): Rule {
return (host: Tree, context: SchematicContext) => {
context.logger.info(stripIndents`
You need to build '${options.projectName}' first.
nx build ${options.projectName}
After that you're able to add the native platforms to the Capacitor project
Android: nx run ${options.capAppName}:add --platform android
iOS: nx run ${options.capAppName}:add --platform ios
Now you're done and ready to run the project
Android: nx run ${options.capAppName}:open --platform android
iOS: nx run ${options.capAppName}:open --platform ios
The documentation for the nx capacitor plugin is here:
https://nxtend.dev/docs/capacitor/overview
Build something awesome!
`);
};
}
export default function(options: IonicAppSchema): Rule {
return (host: Tree) => {
const normalizedOptions = normalizeOptions(options, host);
return chain([
core(normalizedOptions),
updateWorkspace((workspace) => {
const targetCollection = workspace.projects.add({
name: normalizedOptions.projectName,
root: normalizedOptions.projectRoot,
sourceRoot: `${normalizedOptions.projectRoot}/src`,
projectType,
schematics: {
'@nxext/stencil:component': {
style: options.style,
storybook: false
}
}
}).targets;
addDefaultBuilders(
targetCollection,
projectType,
normalizedOptions
);
}),
addProjectToNxJsonInTree(normalizedOptions.projectName, {
tags: normalizedOptions.parsedTags
}),
addFiles(normalizedOptions),
addDepsToPackageJson({}, {"@nxtend/capacitor": "^1.0.0"}),
addPackageWithInit('@nxtend/capacitor'),
externalSchematic('@nxtend/capacitor', 'capacitor-project', {
name: normalizedOptions.capAppName,
project: normalizedOptions.projectName
}),
updateJsonInTree(`${appsDir(host)}/${normalizedOptions.capAppName}/capacitor.config.json`, json => {
json.webDir = `${json.webDir}/www`;
return json;
}),
showInformation(normalizedOptions)
]);
};
}