|
| 1 | +import * as childProcess from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as rimraf from 'rimraf'; |
| 5 | +import * as mkdirp from 'mkdirp'; |
| 6 | + |
| 7 | +const templatePackageName = 'Microsoft.DotNet.Web.Spa.ProjectTemplates'; |
| 8 | +const templatePackageArtifactsDir = '../templates/package-builder/dist/artifacts'; |
| 9 | + |
| 10 | +export function generateProjectSync(targetDir: string, templateName: string) { |
| 11 | + installTemplatePackage(targetDir, templatePackageName, templateName); |
| 12 | + executeDotNetNewTemplateSync(targetDir, templateName); |
| 13 | + executeCommand('npm install', /* quiet */ false, targetDir); |
| 14 | +} |
| 15 | + |
| 16 | +function installTemplatePackage(targetDir: string, packageName: string, templateName: string) { |
| 17 | + // First figure out which file is the latest one for this package |
| 18 | + const packagePaths = 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 | + const latestPackagePath = packagePaths[packagePaths.length - 1]; |
| 24 | + |
| 25 | + if (!latestPackagePath) { |
| 26 | + throw new Error(`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 | + throw new Error(`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 | +function executeDotNetNewTemplateSync(targetDir: string, templateName: string, quiet?: boolean) { |
| 52 | + rimraf.sync(targetDir); |
| 53 | + mkdirp.sync(targetDir); |
| 54 | + executeCommand(`dotnet new ${templateName}`, quiet, targetDir); |
| 55 | +} |
| 56 | + |
| 57 | +function executeCommand(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