/*! * Copyright (c) Microsoft. All rights reserved. * Licensed under the MIT license. See LICENSE file in the project. */ import { copyFile, exists } from 'fs' import path from 'path' import { fileURLToPath } from 'url' import * as log from '../../util/tasklogger.mjs' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) export function copyConfigFile(file: string, dot = false): Promise { const scriptPath = path.join(__dirname, `../../../config/init/${file}`) const pkgPath = path.join(process.cwd(), `${dot ? '.' : ''}${file}`) return fileExists(pkgPath).then((pkgFileExists) => { if (pkgFileExists) { log.error( `cannot init file ${file}; it already exists in the target directory`, ) return 1 } else { return copyFilePromise(scriptPath, pkgPath).then(() => 0) } }) } export const fileExists = (file: string): Promise => new Promise((resolve) => exists(file, (is) => resolve(is))) function copyFilePromise(source: string, target: string): Promise { return new Promise((resolve, reject) => { copyFile(source, target, (err) => { if (err) { reject(err) } else { resolve() } }) }) }