|
| 1 | +import { ExecutorContext, convertNxExecutor } from '@nrwl/devkit'; |
| 2 | +import * as childProcess from 'child_process'; |
| 3 | +import { TestBuilderSchema } from './schema'; |
| 4 | + |
| 5 | +export function runBuilder(options: TestBuilderSchema, context: ExecutorContext): Promise<{ success: boolean }> { |
| 6 | + return new Promise((resolve, reject) => { |
| 7 | + const projectConfig = context.workspace.projects[context.projectName]; |
| 8 | + // console.log('context.projectName:', context.projectName); |
| 9 | + const projectCwd = projectConfig.root; |
| 10 | + // console.log('projectCwd:', projectCwd); |
| 11 | + // console.log('context.targetName:', context.targetName); |
| 12 | + // console.log('context.configurationName:', context.configurationName); |
| 13 | + // console.log('context.target.options:', context.target.options); |
| 14 | + |
| 15 | + let targetConfigName = ''; |
| 16 | + if (context.configurationName && context.configurationName !== 'build') { |
| 17 | + targetConfigName = context.configurationName; |
| 18 | + } |
| 19 | + |
| 20 | + // determine if any trailing args that need to be added to run/build command |
| 21 | + const configTarget = targetConfigName ? `:${targetConfigName}` : ''; |
| 22 | + const projectTargetCmd = `${context.projectName}:${context.targetName}${configTarget}`; |
| 23 | + const projectTargetCmdIndex = process.argv.findIndex((c) => c === projectTargetCmd); |
| 24 | + // const additionalCliFlagArgs = []; |
| 25 | + // if (process.argv.length > projectTargetCmdIndex+1) { |
| 26 | + // additionalCliFlagArgs.push(...process.argv.slice(projectTargetCmdIndex+1, process.argv.length)); |
| 27 | + // // console.log('additionalCliFlagArgs:', additionalCliFlagArgs); |
| 28 | + // } |
| 29 | + |
| 30 | + const throwPlatformError = () => { |
| 31 | + throw new Error(`Configuration must exist for 'ios' or 'android' or options.platform should be set for this target.`); |
| 32 | + } |
| 33 | + |
| 34 | + const nsOptions = ['test']; |
| 35 | + |
| 36 | + const fileReplacements: Array<string> = []; |
| 37 | + let configOptions; |
| 38 | + if (context.target.configurations) { |
| 39 | + configOptions = context.target.configurations[targetConfigName]; |
| 40 | + // console.log('configOptions:', configOptions) |
| 41 | + |
| 42 | + if (configOptions) { |
| 43 | + if (['ios', 'android'].includes(targetConfigName)) { |
| 44 | + nsOptions.push(targetConfigName); |
| 45 | + } else if (options.platform) { |
| 46 | + nsOptions.push(options.platform); |
| 47 | + } else { |
| 48 | + throwPlatformError(); |
| 49 | + } |
| 50 | + if (configOptions.coverage) { |
| 51 | + nsOptions.push('--env.codeCoverage'); |
| 52 | + } |
| 53 | + if (configOptions.fileReplacements) { |
| 54 | + for (const r of configOptions.fileReplacements) { |
| 55 | + fileReplacements.push(`${r.replace.replace(projectCwd, './')}:${r.with.replace(projectCwd, './')}`); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const hasPlatform = nsOptions.filter(o => ['ios', 'android'].includes(o)).length > 0; |
| 62 | + if (!hasPlatform) { |
| 63 | + throwPlatformError(); |
| 64 | + } |
| 65 | + |
| 66 | + if (options.coverage && !nsOptions.includes('--env.codeCoverage')) { |
| 67 | + // allow target override for all configurations |
| 68 | + nsOptions.push('--env.codeCoverage'); |
| 69 | + } |
| 70 | + |
| 71 | + if (options.device) { |
| 72 | + nsOptions.push('--device'); |
| 73 | + nsOptions.push(options.device); |
| 74 | + } |
| 75 | + |
| 76 | + if (fileReplacements.length) { |
| 77 | + // console.log('fileReplacements:', fileReplacements); |
| 78 | + nsOptions.push('--env.replace'); |
| 79 | + nsOptions.push(fileReplacements.join(',')); |
| 80 | + } |
| 81 | + // always add --force (unless explicity set to false) for now since within Nx we use @nativescript/webpack at root only and the {N} cli shows a blocking error if not within the app |
| 82 | + if (options?.force !== false) { |
| 83 | + nsOptions.push('--force'); |
| 84 | + } |
| 85 | + |
| 86 | + // additional args after -- should be passed through |
| 87 | + const argSeparator = process.argv.findIndex((arg) => arg === '--'); |
| 88 | + let additionalArgs = []; |
| 89 | + if (argSeparator >= 0) { |
| 90 | + additionalArgs = process.argv.slice(argSeparator + 1); |
| 91 | + } |
| 92 | + |
| 93 | + console.log('---'); |
| 94 | + console.log(`Running NativeScript unit tests within ${projectCwd}`); |
| 95 | + console.log(' '); |
| 96 | + console.log([`ns`, ...nsOptions, ...additionalArgs].join(' ')); |
| 97 | + console.log('---'); |
| 98 | + // console.log('command:', [`ns`, ...nsOptions].join(' ')); |
| 99 | + const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', [...nsOptions, ...additionalArgs], { |
| 100 | + cwd: projectCwd, |
| 101 | + stdio: 'inherit', |
| 102 | + }); |
| 103 | + child.on('close', (code) => { |
| 104 | + console.log(`Done.`); |
| 105 | + resolve({ success: code === 0 }); |
| 106 | + }); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +export default convertNxExecutor(runBuilder); |
0 commit comments