/
index.ts
127 lines (124 loc) · 3.98 KB
/
index.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
import {
XplatHelpers,
prerun,
missingArgument,
getAppName,
getDefaultTemplateOptions,
getPrefix,
updateWorkspace,
updateNxProjects,
updatePackageScripts
} from '@nstudio/xplat';
import {
chain,
noop,
Tree,
SchematicContext,
SchematicsException,
branchAndMerge,
mergeWith,
apply,
url,
template,
move
} from '@angular-devkit/schematics';
import { Schema } from './schema';
import { XplatIonicHelpers } from '../../utils';
import { formatFiles } from '@nrwl/workspace';
export default function(options: Schema) {
if (!options.name) {
throw new SchematicsException(
missingArgument(
'name',
'Provide a name for your Ionic app.',
'nx g @nstudio/ionic:app name'
)
);
}
return chain([
prerun(options),
// adjust naming convention
XplatHelpers.applyAppNamingConvention(options, 'ionic'),
// create app files
(tree: Tree, context: SchematicContext) =>
addAppFiles(options, options.name)(tree, context),
// add root package dependencies
XplatIonicHelpers.updateRootDeps(options),
XplatHelpers.addPackageInstallTask(options),
// add start/clean scripts
(tree: Tree) => {
const scripts = {};
const platformApp = options.name.replace('-', '.');
const directory = options.directory ? `${options.directory}/` : '';
// ensure convenient clean script is added for workspace
scripts[
`clean`
] = `npx rimraf -- hooks node_modules package-lock.json && npm i`;
scripts[
`start.${platformApp}`
] = `cd apps/${directory}${options.name} && npm start`;
scripts[
`build.${platformApp}`
] = `cd apps/${directory}${options.name} && npm run build:ionic`;
scripts[
`prepare.${platformApp}`
] = `npm run clean && npm run clean.${platformApp} && npm run build.${platformApp}`;
scripts[
`prepare.${platformApp}.ios`
] = `npm run prepare.${platformApp} && cd apps/${directory}${options.name} && npm run cap.add.ios`;
scripts[
`prepare.${platformApp}.android`
] = `npm run prepare.${platformApp} && cd apps/${directory}${options.name} && npm run cap.add.android`;
scripts[
`open.${platformApp}.ios`
] = `cd apps/${directory}${options.name} && npm run cap.ios`;
scripts[
`open.${platformApp}.android`
] = `cd apps/${directory}${options.name} && npm run cap.android`;
scripts[
`sync.${platformApp}`
] = `cd apps/${directory}${options.name} && npm run cap.copy`;
scripts[
`clean.${platformApp}`
] = `cd apps/${directory}${options.name} && npx rimraf -- hooks node_modules platforms www plugins ios android package-lock.json && npm i && rimraf -- package-lock.json`;
return updatePackageScripts(tree, scripts);
},
(tree: Tree, context: SchematicContext) => {
const directory = options.directory ? `${options.directory}/` : '';
const projects = {};
projects[`${options.name}`] = {
root: `apps/${directory}${options.name}/`,
sourceRoot: `apps/${directory}${options.name}/src`,
projectType: 'application',
prefix: getPrefix()
};
return updateWorkspace({ projects })(tree, context);
},
(tree: Tree) => {
const projects = {};
projects[`${options.name}`] = {
tags: []
};
return updateNxProjects(tree, projects);
},
formatFiles({ skipFormat: options.skipFormat })
]);
}
function addAppFiles(options: Schema, appPath: string) {
const appname = getAppName(options, 'ionic');
const directory = options.directory ? `${options.directory}/` : '';
return branchAndMerge(
mergeWith(
apply(url(`./_files`), [
template({
...(options as any),
...getDefaultTemplateOptions(),
pathOffset: directory ? '../../../' : '../../',
appname,
xplatFolderName: XplatHelpers.getXplatFoldername('ionic')
}),
move(`apps/${directory}${appPath}`)
])
)
);
}