Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dabit3 committed Jun 10, 2017
0 parents commit f3608de
Show file tree
Hide file tree
Showing 12,876 changed files with 2,436,366 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Empty file added .gitignore
Empty file.
Empty file added .npmignore
Empty file.
44 changes: 44 additions & 0 deletions README.md
@@ -0,0 +1,44 @@
# Create New Cli

### CLI Project Generator

With the rise in the popularity of projects like Create React App, Angular CLI, and Ionic CLI, as well as the productivity these clis bring to the table, there should be an easy way for developers to create their own CLI using boilerplates and also to introduce them to the world of CLI creation.

This project will allow developers to easily and quickly create CLIs that give them the most basic functionality in just a few seconds, and publish their projects to npm for users to install and start using right away.

## To create and publish your own CLI:

1. Install create-new-cli globally
```
npm i -g create-new-cli
```

2. Create a new cli following the commmand prompts by running the create-new-cli command
```
create-new-cli
```

### In the above step, we will create a name and a command for our cli. The name will be the folder created for our project, and the command will be what the application is registered as in the npm registry, which will be the name field in our newly created package.json.

### We will reference these two items as <YourProjectName> and <your-project-command> in the following steps.

3. CD into newly created project directory
```
cd <YourProjectName>
```

4. Publish to npm
```
npm publish
```

5. Install new cli globally on your own machine
```
npm i -g <your-project-command>
```

6. Create new application using your new CLI
```
<your-project-command> MyAppName
````

88 changes: 88 additions & 0 deletions bin/build.js
@@ -0,0 +1,88 @@
#!/usr/bin/env node

require('shelljs/global');
const paths = require('path');
const figlet = require('figlet');
const chalk = require('chalk');
const execSync = require('child_process').execSync;
const spawn = require('cross-spawn');
const Git = require("nodegit");
const fs = require('fs');

const commands = require('./rewritefiles');

function shouldUseYarn() {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}

const installPackages = () => {
console.log(chalk.white.bold('Installing Packages'));
return new Promise((resolve, reject) => {
let command;
let args = ['install'];

if (shouldUseYarn()) {
command = 'yarn';
} else {
command = 'npm';
}

const child = spawn(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`
});
return;
}
resolve();
})
})
}

const build = (appName, author, command, repo = false) => {
cp('-r', __dirname + '/../src/.', appName);
if (repo) {
Git.Clone(repo, `${appName}/src`)
.then(repository => {
doReWrites(appName, author, command)
})
} else {
mkdir(`${appName}/src`)
doReWrites(appName, author, command)
}
}

function doReWrites(appName, author, command, repo) {
commands.rewritePackageJson(`${appName}/package.json`, author, command)
.then(() => {
commands.rewriteBuild(`${appName}/bin/build.js`, appName)
.then(() => {
cd(appName);
installPackages()
.then(() => {
console.log('installed all packages..')
console.log('----------------------------------------------------------');
figlet(`${appName}`, function(err, data) {
if (err) {
console.log('error...', err)
return;
}
console.log(data)
});
console.log(chalk.green(`Congratulations, ${appName} has now been successfully generated...`))
})
.catch(error => {
console.log(chalk.red('An unexpected error occurred'))
console.log(chalk.red(error));
});
})
})
}

module.exports = build
102 changes: 102 additions & 0 deletions bin/cli.js
@@ -0,0 +1,102 @@
#!/usr/bin/env node

const program = require('commander');
const chalk = require('chalk');
const inquirer = require('inquirer');
const dashify = require('dashify');

const build = require('./build');
const pkg = require('../package.json');

let projectName;

program
.version(pkg.version)
.arguments('<project-directory>')
.action(name => {
projectName = name;
})

program.on('--help', function() {
help();
});

const isRepoPrompt = {
type: 'list',
name: 'isrepo',
message: 'Would you like your project to be fetched from a git repo, or would you like to place your project from an existing folder?',
choices: ['folder', 'repo'],
};

const commandPrompt = {
type: 'string',
name: 'command',
message: 'Please enter desired command to generate your CLI (kebab-case):',
};

const namePrompt = {
type: 'string',
name: 'name',
message: 'Name of your CLI? (CamelCase)',
};

const authorPrompt = {
type: 'string',
name: 'author',
message: 'CLI author name:',
};

console.log('');
console.log("Let's get started createing your new CLI.")
console.log('');

inquirer.prompt([
commandPrompt,
namePrompt,
isRepoPrompt,
authorPrompt,
]).then(answers => {
const command = dashify(answers.command);
const name = answers.name;
const isRepo = answers.isrepo === 'repo';
const author = answers.author;

if (isRepo) {
repoPrompt(name, author, command)
} else (
build(name, author, command)
)
})

function folderPrompt(name, author) {}

function repoPrompt(name, author, command) {
const gitPrompt = {
type: 'string',
name: 'repo',
message: 'Base repo for your project (git url, use https):',
};
inquirer.prompt([
gitPrompt
])
.then(answers => {
const repo = answers.repo
inquirer.prompt([
{
type: 'confirm',
name: 'confirmation',
message: `CLI name: ${name}. Repo: ${answers.repo}. Author: ${author}.`
}
])
.then(answers => {
if (answers.confirmation) {
build(name, author, command, repo)
} else {
console.log('Please try again.')
}
})
})
}

program.parse(process.argv);

41 changes: 41 additions & 0 deletions bin/rewritefiles.js
@@ -0,0 +1,41 @@
const fs = require('fs');

const commands = {};

const rewritePackageJson = (file, author, name) => {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
let result1 = data.replace(/"name": "",/g, `"name": "${name}",`);
const result2 = result1.replace(/"author": "",/g, `"author": "${author}",`);

fs.writeFile(file, result2, 'utf8', function (err) {
if (err) return console.log(err);
resolve();
});
});
})
}

const rewriteBuild = (file, name) => {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
let result = data.replace(/PROJECTNAME/g, `${name}`);

fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.log(err);
resolve();
});
});
})
}

commands.rewritePackageJson = rewritePackageJson;
commands.rewriteBuild = rewriteBuild;

module.exports = commands;
1 change: 1 addition & 0 deletions node_modules/.bin/shjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f3608de

Please sign in to comment.