Skip to content

Commit

Permalink
Add verbose mode for easy troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
ianpogi5 committed May 14, 2020
1 parent 70b3912 commit a05e2f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ program
.option('--no-bundle', 'do not bundle the build output')
.option('-g, --git-origin <remote-url>', 'add git remote url as origin')
.option('-i, --info', 'print environment debug info')
.option('-v, --verbose', 'verbose output')
.action((name) => {
packageName = name;
})
Expand All @@ -39,8 +40,8 @@ program

const createPackage = async () => {
try {
const { bundle, gitOrigin } = program;
await run({ packageName, bundle, gitOrigin });
const { bundle, gitOrigin, verbose } = program;
await run({ packageName, bundle, gitOrigin, verbose });
log(chalk.green('\n\nYour package is ready!\n\n'));
log(chalk.blue(`\tcd ${packageName}`));
log(chalk.blue('\tcode .\n\n'));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kdcsoftware/create-nodejs",
"version": "0.3.0",
"version": "0.3.1",
"description": "NodeJs boilerplate",
"main": "lib/index.js",
"bin": "./cli.js",
Expand Down
44 changes: 28 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ const { log } = console;

const PKG_DIR = __dirname;

const run = async ({ packageName, bundle, gitOrigin }) => {
const run = async ({ packageName, bundle, gitOrigin, verbose }) => {
try {
log(chalk.green.bold('Creating your package...'));
log(chalk`Package Name: {blue ${packageName}}`);

if (verbose) {
log(chalk`Source: {blue ${PKG_DIR}}`);
log(chalk`Destination: {blue ${process.cwd()}/${packageName}}`);
}

if (fs.existsSync(packageName)) {
throw new Error('The directory exists.');
}
Expand All @@ -26,35 +31,42 @@ const run = async ({ packageName, bundle, gitOrigin }) => {
process.chdir(packageName);
const CUR_DIR = process.cwd();

log(chalk.green('Initializing git...'));
await cmd('git', ['init']);

log(chalk.green('Initializing npm...'));
await cmd('npm', ['init', '-y']);

log(chalk.green('Installing dev dependencies...'));
const devDeps = [...NPM_PACKAGES_DEV];
if (bundle) devDeps.push('parcel@next');
if (verbose) log(chalk.gray(devDeps));
await cmd('npm', ['i', '-D', ...devDeps]);

log(chalk.green('Installing prod dependencies...'));
if (verbose) log(chalk.gray(NPM_PACKAGES_PROD));
await cmd('npm', ['i', ...NPM_PACKAGES_PROD]);

let src;
let dest;
log(chalk.green('Copying configuration files...'));
Object.keys(CONFIG_FILES).forEach((k) => {
const c = CONFIG_FILES[k];
fse.copySync(
resolve(`${PKG_DIR}/..`, `templates/${k}`),
`${CUR_DIR}/${c}`
);
src = resolve(`${PKG_DIR}/..`, `templates/${k}`);
dest = `${CUR_DIR}/${c}`;
if (verbose) log(`Copying ${chalk.yellow(src)} to ${chalk.green(dest)}`);
fse.copySync(src, dest);
});

log(chalk.green('Copying source files...'));
fse.copySync(
resolve(`${PKG_DIR}/..`, 'templates/src/index.js'),
`${CUR_DIR}/src/index.js`
);
fse.copySync(
resolve(`${PKG_DIR}/..`, 'templates/tests/index.test.js'),
`${CUR_DIR}/tests/index.test.js`
);
src = resolve(`${PKG_DIR}/..`, 'templates/src/index.js');
dest = `${CUR_DIR}/src/index.js`;
if (verbose) log(`Copying ${chalk.yellow(src)} to ${chalk.green(dest)}`);
fse.copySync(src, dest);
src = resolve(`${PKG_DIR}/..`, 'templates/tests/index.test.js');
dest = `${CUR_DIR}/tests/index.test.js`;
if (verbose) log(`Copying ${chalk.yellow(src)} to ${chalk.green(dest)}`);
fse.copySync(src, dest);

log(chalk.green('Updating package.json...'));
const mods = [];
Expand All @@ -73,6 +85,7 @@ const run = async ({ packageName, bundle, gitOrigin }) => {
};

if (bundle) {
if (verbose) log(chalk.gray(`Including bundler configs`));
scripts.bundle = 'parcel build src/index.js';
scripts.build = 'npm run clean && npm run bundle';
mods.push({
Expand Down Expand Up @@ -119,14 +132,13 @@ const run = async ({ packageName, bundle, gitOrigin }) => {

pkg.mod(mods);

log(chalk.green('Initializing git...'));
await cmd('git', ['init']);
log(chalk.green('Adding files to git...'));
await cmd('git', ['add', '.']);
await cmd('git', ['commit', '-m', 'first commit']);

if (gitOrigin) {
if (verbose) log(chalk.gray(`Adding git remote`));
await cmd('git', ['remote', 'add', 'origin', gitOrigin]);
await cmd('git', ['push', '-u', 'origin', 'master']);
}
} catch (error) {
throw new Error(error);
Expand Down

0 comments on commit a05e2f0

Please sign in to comment.