/** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import fs from 'fs'; import { execSync } from 'child_process'; import path from 'path'; import { prompt } from 'enquirer'; import colors from 'ansi-colors'; export function executeCommands(cwd: string, commands: string[]) { for (const command of commands) { console.log('Running:', command); execSync(command, { stdio: 'inherit', cwd, }); } } export async function createFiles(rootDir: string, files: Map, force: boolean = false) { for (const [relativeFilePath, value] of files) { const absoluteFilePath = path.join(rootDir, relativeFilePath); if (fs.existsSync(absoluteFilePath) && !force) { const { override } = await prompt<{ override: boolean }>({ type: 'confirm', name: 'override', message: `${absoluteFilePath} already exists. Should it override?`, initial: false }); if (!override) continue; } console.log(colors.gray(`Writing ${path.relative(process.cwd(), absoluteFilePath)}.`)); fs.mkdirSync(path.dirname(absoluteFilePath), { recursive: true }); fs.writeFileSync(absoluteFilePath, value, 'utf-8'); } } export function determineRootDir() { const givenPath = process.argv[2]; if (givenPath) return path.isAbsolute(givenPath) ? process.argv[2] : path.join(process.cwd(), process.argv[2]); return process.cwd(); } export function determinePackageManager(rootDir: string): 'yarn' | 'npm' { if (fs.existsSync(path.join(rootDir, 'yarn.lock'))) return 'yarn'; if (process.env.npm_config_user_agent) return process.env.npm_config_user_agent.includes('yarn') ? 'yarn' : 'npm'; return 'npm'; } export function executeTemplate(input: string, args: Record): string { for (const key in args) input = input.replace(`{{${key}}}`, args[key]); return input; }