|
| 1 | +import { |
| 2 | + apply, |
| 3 | + chain, |
| 4 | + Tree, |
| 5 | + Rule, |
| 6 | + url, |
| 7 | + move, |
| 8 | + template, |
| 9 | + mergeWith, |
| 10 | + branchAndMerge, |
| 11 | + SchematicContext, |
| 12 | + SchematicsException, |
| 13 | + schematic, |
| 14 | + noop |
| 15 | +} from "@angular-devkit/schematics"; |
| 16 | +import { |
| 17 | + stringUtils, |
| 18 | + prerun, |
| 19 | + getNpmScope, |
| 20 | + getPrefix, |
| 21 | + addRootDeps, |
| 22 | + updatePackageScripts, |
| 23 | + updateAngularProjects, |
| 24 | + updateNxProjects, |
| 25 | + applyAppNamingConvention, |
| 26 | + getGroupByName, |
| 27 | + getAppName, |
| 28 | + ITargetPlatforms, |
| 29 | + PlatformTypes, |
| 30 | + supportedPlatforms, |
| 31 | + unsupportedPlatformError, |
| 32 | + supportedHelpers, |
| 33 | + unsupportedHelperError, |
| 34 | + updateTsConfig |
| 35 | +} from "../utils"; |
| 36 | +import { Schema as HelperOptions } from "./schema"; |
| 37 | + |
| 38 | +let helpers: Array<string> = []; |
| 39 | +let platforms: Array<string> = []; |
| 40 | +export default function(options: HelperOptions) { |
| 41 | + if (!options.name) { |
| 42 | + throw new SchematicsException( |
| 43 | + `Missing name argument. Provide a comma delimited list of helpers to generate. Example: ng g xplat-helper imports` |
| 44 | + ); |
| 45 | + } |
| 46 | + if (!options.platforms) { |
| 47 | + throw new SchematicsException( |
| 48 | + `Missing platforms argument. Example: ng g xplat-helper imports --platforms=nativescript` |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + helpers = options.name.split(","); |
| 53 | + platforms = options.platforms.split(","); |
| 54 | + |
| 55 | + const helperChains = []; |
| 56 | + |
| 57 | + for (const platform of platforms) { |
| 58 | + if (supportedPlatforms.includes(platform)) { |
| 59 | + for (const helper of helpers) { |
| 60 | + if (supportedHelpers.includes(helper)) { |
| 61 | + const moveTo = getMoveTo(<PlatformTypes>platform, helper); |
| 62 | + helperChains.push((tree: Tree, context: SchematicContext) => { |
| 63 | + return addHelperFiles( |
| 64 | + options, |
| 65 | + <PlatformTypes>platform, |
| 66 | + helper, |
| 67 | + moveTo |
| 68 | + )(tree, context); |
| 69 | + }); |
| 70 | + // aside from adding files above, process any additional modifications |
| 71 | + helperChains.push((tree: Tree, context: SchematicContext) => { |
| 72 | + return processSupportingFiles( |
| 73 | + helperChains, |
| 74 | + options, |
| 75 | + <PlatformTypes>platform, |
| 76 | + helper |
| 77 | + ); |
| 78 | + }); |
| 79 | + } else { |
| 80 | + throw new SchematicsException(unsupportedHelperError(helper)); |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + throw new SchematicsException(unsupportedPlatformError(platform)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return chain([ |
| 89 | + prerun(options), |
| 90 | + // add helper chains |
| 91 | + ...helperChains |
| 92 | + // TODO: add refactor code to update per the helper where applicable |
| 93 | + ]); |
| 94 | +} |
| 95 | + |
| 96 | +function addHelperFiles( |
| 97 | + options: HelperOptions, |
| 98 | + platform: PlatformTypes, |
| 99 | + helper: string, |
| 100 | + moveTo: string |
| 101 | +): Rule { |
| 102 | + return branchAndMerge( |
| 103 | + mergeWith( |
| 104 | + apply(url(`./${platform}/${helper}/_files`), [ |
| 105 | + template({ |
| 106 | + ...(options as any), |
| 107 | + utils: stringUtils, |
| 108 | + npmScope: getNpmScope(), |
| 109 | + prefix: getPrefix(), |
| 110 | + dot: "." |
| 111 | + }), |
| 112 | + move(moveTo) |
| 113 | + ]) |
| 114 | + ) |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +function getMoveTo(platform: PlatformTypes, helper: string) { |
| 119 | + let moveTo = `xplat/${platform}/utils`; // default |
| 120 | + // TODO: define custom moveTo locations for various helpers |
| 121 | + // switch (helper) { |
| 122 | + // case "imports": |
| 123 | + // break; |
| 124 | + // } |
| 125 | + return moveTo; |
| 126 | +} |
| 127 | + |
| 128 | +function processSupportingFiles( |
| 129 | + helperChains: Array<any>, |
| 130 | + options: HelperOptions, |
| 131 | + platform: PlatformTypes, |
| 132 | + helper: string |
| 133 | +) { |
| 134 | + return (tree: Tree) => { |
| 135 | + switch (helper) { |
| 136 | + case "imports": |
| 137 | + switch (platform) { |
| 138 | + case "nativescript": |
| 139 | + let pathRef = `xplat/nativescript/utils/@nativescript/*`; |
| 140 | + // update root tsconfig |
| 141 | + helperChains.push(updateTsConfig(tree, (tsConfig: any) => { |
| 142 | + const updates: any = {}; |
| 143 | + updates[`@nativescript/*`] = [ |
| 144 | + pathRef |
| 145 | + ]; |
| 146 | + if (tsConfig) { |
| 147 | + if (!tsConfig.compilerOptions) { |
| 148 | + tsConfig.compilerOptions = {}; |
| 149 | + } |
| 150 | + tsConfig.compilerOptions.paths = { |
| 151 | + ...(tsConfig.compilerOptions.paths || {}), |
| 152 | + ...updates |
| 153 | + }; |
| 154 | + } |
| 155 | + })); |
| 156 | + |
| 157 | + // update all {N} app tsconfig's |
| 158 | + const appsDir = tree.getDir("apps"); |
| 159 | + const appFolders = appsDir.subdirs; |
| 160 | + pathRef = `../../${pathRef}`; |
| 161 | + |
| 162 | + // update {N} apps and configs |
| 163 | + for (const dir of appFolders) { |
| 164 | + // console.log(dir); |
| 165 | + if (dir.indexOf('nativescript-') === 0 || dir.indexOf('-nativescript') === 0) { |
| 166 | + const appDir = `${appsDir.path}/${dir}`; |
| 167 | + // console.log('appDir:', appDir); |
| 168 | + |
| 169 | + helperChains.push(updateTsConfig(tree, (tsConfig: any) => { |
| 170 | + const updates: any = {}; |
| 171 | + updates[`@nativescript/*`] = [ |
| 172 | + pathRef |
| 173 | + ]; |
| 174 | + if (tsConfig) { |
| 175 | + if (!tsConfig.compilerOptions) { |
| 176 | + tsConfig.compilerOptions = {}; |
| 177 | + } |
| 178 | + tsConfig.compilerOptions.paths = { |
| 179 | + ...(tsConfig.compilerOptions.paths || {}), |
| 180 | + ...updates |
| 181 | + }; |
| 182 | + } |
| 183 | + }, null, `${appDir}/`)); |
| 184 | + } |
| 185 | + } |
| 186 | + break; |
| 187 | + } |
| 188 | + break; |
| 189 | + } |
| 190 | + }; |
| 191 | +} |
0 commit comments