|
| 1 | +import*aschildProcessfrom'child_process'; |
| 2 | +import*asfsfrom'fs'; |
| 3 | +import*aspathfrom'path'; |
| 4 | +import*asrimraffrom'rimraf'; |
| 5 | +import*asmkdirpfrom'mkdirp'; |
| 6 | + |
| 7 | +consttemplatePackageName='Microsoft.DotNet.Web.Spa.ProjectTemplates'; |
| 8 | +consttemplatePackageArtifactsDir='../templates/package-builder/dist/artifacts'; |
| 9 | + |
| 10 | +exportfunctiongenerateProjectSync(targetDir: string,templateName: string){ |
| 11 | +installTemplatePackage(targetDir,templatePackageName,templateName); |
| 12 | +executeDotNetNewTemplateSync(targetDir,templateName); |
| 13 | +executeCommand('npm install',/* quiet */false,targetDir); |
| 14 | +} |
| 15 | + |
| 16 | +functioninstallTemplatePackage(targetDir: string,packageName: string,templateName: string){ |
| 17 | +// First figure out which file is the latest one for this package |
| 18 | +constpackagePaths=fs.readdirSync(templatePackageArtifactsDir) |
| 19 | +.filter(name=>name.startsWith(templatePackageName+'.')) |
| 20 | +.filter(name=>path.extname(name)==='.nupkg') |
| 21 | +.map(name=>path.join(templatePackageArtifactsDir,name)) |
| 22 | +.sort(); |
| 23 | +constlatestPackagePath=packagePaths[packagePaths.length-1]; |
| 24 | + |
| 25 | +if(!latestPackagePath){ |
| 26 | +thrownewError(`Could not find ${packageName}.*.nupkg in directory ${templatePackageArtifactsDir}`); |
| 27 | +} |
| 28 | + |
| 29 | +// Uninstall any older version so we can be sure the new one did install |
| 30 | +try{ |
| 31 | +console.log(`Uninstalling any prior version of ${packageName}...`); |
| 32 | +executeCommand(`dotnet new --uninstall ${packageName}`,/* quiet */true); |
| 33 | +}catch(ex){ |
| 34 | +// Either no prior version existed, or we failed to uninstall. We'll determine |
| 35 | +// which it was next. |
| 36 | +} |
| 37 | +try{ |
| 38 | +console.log(`Verifying that no prior version of ${packageName} is still installed...`); |
| 39 | +executeDotNetNewTemplateSync(targetDir,templateName,/* quiet */true); |
| 40 | +thrownewError(`Failed to uninstall template package ${packageName}. The template '${templateName}' was not removed as expected.`); |
| 41 | +}catch(ex){ |
| 42 | +// Looks like we successfully uninstalled it |
| 43 | +console.log(`Confirmed that no prior version of ${templatePackageName} remains installed.`); |
| 44 | +} |
| 45 | + |
| 46 | +// Now install the new version |
| 47 | +console.log(`Installing new templates package at ${latestPackagePath}...`); |
| 48 | +executeCommand(`dotnet new --install ${latestPackagePath}`,/* quiet */true); |
| 49 | +} |
| 50 | + |
| 51 | +functionexecuteDotNetNewTemplateSync(targetDir: string,templateName: string,quiet?: boolean){ |
| 52 | +rimraf.sync(targetDir); |
| 53 | +mkdirp.sync(targetDir); |
| 54 | +executeCommand(`dotnet new ${templateName}`,quiet,targetDir); |
| 55 | +} |
| 56 | + |
| 57 | +functionexecuteCommand(command: string,quiet?: boolean,cwd?: string){ |
| 58 | +childProcess.execSync(command,{ |
| 59 | + cwd, |
| 60 | +stdio: quiet ? null : 'inherit', |
| 61 | +encoding: 'utf8' |
| 62 | +}); |
| 63 | +} |
0 commit comments