Skip to content

Commit

Permalink
feat(create-express-app): Register templates
Browse files Browse the repository at this point in the history
  • Loading branch information
getspooky committed Apr 24, 2020
1 parent 1546c99 commit 6a8bd6e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 33 deletions.
22 changes: 20 additions & 2 deletions packages/create-express-app/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

const chalk = require('chalk');
const inquirer = require('inquirer');
const {
Command
} = require('commander');
Expand All @@ -20,6 +21,7 @@ const {
checkingEnvironment,
initExpressApp,
installPackages,
registerTemplate,
happyCoding,
} = require('./createExpressApp');

Expand All @@ -43,16 +45,31 @@ console.log(
console.log();
console.log('Tools Version ' + chalk.green(version));

program
.command('new template')
.description('Register new template')
.action(function () {
registerTemplate();
});

program
.command('init <projectName>')
.alias('ci')
.description('init create-express-app project')
.option(
'-d, --directory <project-directory>',
'Setup the default folder structures to be used in the Project'
'Setup the default folder structures to be used in the Project',
)
.option(
'-u , --use <strategy>',
'Selecting a package manager'
)
.option(
'-rt , --registerTemplate <register-template>',
'Registering Template'
)
.option('-u', '--use <strategy>', 'Selecting a package manager')
.action(function (projectName, action) {
//
let directory = null;

if (typeof action.directory === 'undefined') {
Expand All @@ -73,6 +90,7 @@ program
console.log(chalk.red('Directory should end with slash'));
killProcess();
}

// Cheking NPM , Yarn and Node versions.
checkingEnvironment()
.then(() =>
Expand Down
100 changes: 69 additions & 31 deletions packages/create-express-app/createExpressApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
'use strict';

const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const inquirer = require('inquirer');
const ora = require('ora');
const {
exec
} = require('child_process');
const {
_EMOJIS
} = require('./interface');
const questions = require('./questions');
const templates = require('./template.json');
const { exec } = require('child_process');
const { _EMOJIS } = require('./interface');
const compareVersions = require('compare-versions');
const validateProjectName = require('validate-npm-package-name');

Expand All @@ -32,14 +31,10 @@ const versionCompatibility = {
var spinner = {
project: ora(_EMOJIS.CLONE + ' Creating Project...'),
installPackages: ora(_EMOJIS.PACKAGE + ' Installing Packages...'),
registerTemplate: ora(_EMOJIS.REGISTER + 'Registering Template...'),
};


const supportedTemplates = [
'cra-template-es5',
//'cra-template-typescript',
'cra-template-es6',
];
const supportedTemplates = Object.keys(templates);

/**
* @export
Expand All @@ -59,12 +54,12 @@ exports.checkNodeVersion = function (minimalNodeVersion) {
reject(
new Error(
'You are running Node ' +
nodeVersion +
'.\n' +
'Create Express App requires Node ' +
minimalNodeVersion +
' or higher. \n' +
'Please update your version of Node.'
nodeVersion +
'.\n' +
'Create Express App requires Node ' +
minimalNodeVersion +
' or higher. \n' +
'Please update your version of Node.'
)
);
}
Expand Down Expand Up @@ -131,7 +126,6 @@ exports.checkYarnVersion = function (minimalYarnVersion) {
});
};


/**
* @export
* @desc Requirements that should be checked before installing.
Expand All @@ -145,7 +139,7 @@ exports.checkingEnvironment = function () {
module.exports.checkYarnVersion(versionCompatibility.yarn),
module.exports.checkNodeVersion(versionCompatibility.node),
]);
}
};

/**
* @export
Expand Down Expand Up @@ -188,7 +182,7 @@ exports.checkIfRepositoryIsCloned = function () {
*/
exports.initGitRepository = function () {
return new Promise((resolve, reject) => {
spinner.checkingEnv.stop()
spinner.checkingEnv.stop();
exec('git init', (err, stdout) => {
if (err) {
reject(new TypeError(err));
Expand Down Expand Up @@ -222,6 +216,50 @@ exports.installPackages = function (directory, strategy) {
});
};

/**
* @exports
* @desc Register Local or hosted templates.
* @function
* @name registerTemplate
* @returns {Promise}
*/
exports.registerTemplate = function () {
console.log();
return inquirer
.prompt(questions)
.then(({ name, link, description, version }) => {
// add supported template.
const data = JSON.stringify(
Object.assign(
{
[name]: {
name,
version,
link,
description,
},
},
templates
)
);
// make sure that all given url start with file or https?
if (/^(https?)/.test(link) === false)
throw new TypeError('URL not supported (:');

fs.writeFile(path.join(__dirname, 'template.json'), data, (err) => {
if (err) throw err;
console.log();
console.log('Template registed successfully ' + chalk.green('✓'));
console.log(
'Learn more ' +
chalk.underline.blue(
'https://getspooky.github.io/create-express-app/'
)
);
});
});
};

/**
* @exports
* @desc Check create-express-app name.
Expand Down Expand Up @@ -265,9 +303,7 @@ exports.initExpressApp = function (appName, directory) {

if (fs.existsSync(directory)) {
console.log();
console.log(
chalk.red(appName + ' project already exists')
);
console.log(chalk.red(appName + ' project already exists'));
module.exports.killProcess();
}

Expand All @@ -288,12 +324,14 @@ exports.initExpressApp = function (appName, directory) {
exports.createExpressTemplate = function (directory) {
console.log();
return inquirer
.prompt([{
type: 'list',
name: 'template',
message: 'Please specify a template for the created project',
choices: supportedTemplates,
}, ])
.prompt([
{
type: 'list',
name: 'template',
message: 'Please specify a template for the created project',
choices: supportedTemplates,
},
])
.then((answers) => {
let getTemplate = require('./template.json');
return module.exports.getTemplateInstallPackage(
Expand Down Expand Up @@ -324,7 +362,7 @@ exports.getTemplateInstallPackage = function (template, dest, url) {
reject(new TypeError('The given template does not exists!'));
if (url.match(/^(https:\/\/github.com).+/) === null)
reject(
new TypeError(url + ' should match format : ^(https:\/\/github.com).+')
new TypeError(url + ' should match format : ^(https://github.com).+')
);
// clone specific template from repository
exec(`git clone -b ${template} ${url} ${dest}/`, (err, stdout) => {
Expand Down

0 comments on commit 6a8bd6e

Please sign in to comment.