@baaz/cli
Version:
This is the home of the DevKit and the Baaz Framework CLI code. You can find the Baaz Framework CLI specific README [here](/README.md).
94 lines (81 loc) • 2.58 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs';
import ncp from 'ncp';
import path from 'path';
import { promisify } from 'util';
import execa from 'execa';
import Listr from 'listr';
import { projectInstall } from 'pkg-install';
import { execSync } from 'child_process';
import { initTemplate } from './templates'
import rimraf from 'rimraf';
const rmrfAsync = promisify(rimraf);
const access = promisify(fs.access);
const copy = promisify(ncp);
async function updateRepo() {
return await execSync('git clone https://github.com/dominicg666/pwa.git', {
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: path.resolve(__dirname, './templates'), // path to where you want to save the file
})
}
async function copyTemplateFiles(options) {
return copy(options.templateDirectory, options.targetDirectory, {
clobber: false,
});
}
async function initGit(options) {
const result = await execa('git', ['init'], {
cwd: options.targetDirectory,
});
if (result.failed) {
return Promise.reject(new Error('Failed to initialize git'));
}
return;
}
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;
await rmrfAsync(templateDir);
await initTemplate();
await updateRepo()
try {
await access(templateDir, fs.constants.R_OK);
} catch (err) {
console.error('%s Invalid template name', chalk.red.bold('ERROR'));
process.exit(1);
}
const tasks = new Listr([
{
title: 'Copy project files',
task: () => copyTemplateFiles(options),
},
{
title: 'Initialize git',
task: () => initGit(options),
enabled: () => options.git,
},
{
title: 'Install dependencies',
task: () =>
projectInstall({
cwd: options.targetDirectory,
}),
skip: () =>
!options.runInstall
? 'Pass --install to automatically install dependencies'
: undefined,
},
]);
await tasks.run();
console.log('%s Project ready', chalk.green.bold('DONE'));
return true;
}