Created
May 15, 2019 17:15
-
-
Save Ivanjosevera/19437135bed6c944e49847e67dee08bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chalk from 'chalk'; | |
import fs from 'fs'; | |
import ncp from 'ncp'; | |
import path from 'path'; | |
import { promisify } from 'util'; | |
const access = promisify(fs.access); | |
const copy = promisify(ncp); | |
async function copyTemplateFiles(options) { | |
return copy(options.templateDirectory, options.targetDirectory, { | |
clobber: false, | |
}); | |
} | |
export async function createProject(options) { | |
options = { | |
...options, | |
targetDirectory: options.targetDirectory || process.cwd(), | |
}; | |
const currentFileUrl = import.meta.url; | |
const templateDir = path.resolve( | |
new URL(currentFileUrl).pathname, | |
'../../templates', | |
options.template.toLowerCase() | |
); | |
options.templateDirectory = templateDir; | |
try { | |
await access(templateDir, fs.constants.R_OK); | |
} catch (err) { | |
console.error('%s Invalid template name', chalk.red.bold('ERROR')); | |
process.exit(1); | |
} | |
console.log('Copy project files'); | |
await copyTemplateFiles(options); | |
console.log('%s Project ready', chalk.green.bold('DONE')); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment