Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"url-loader": "^0.5.8",
"webpack": "^2.3.3",
"webpack-dev-server": "^2.4.5",
"which": "^1.2.14",
"yargs": "^8.0.1"
}
}
29 changes: 27 additions & 2 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ora from 'ora';
import promisify from 'es6-promisify';
import spawn from 'cross-spawn-promise';
import path from 'path';
import which from 'which';

const TEMPLATES = {
default: 'examples/root',
Expand Down Expand Up @@ -132,9 +133,11 @@ export default asyncCommand({
'preact-router'
]);

await initializeVersionControl(target);

spinner.succeed('Done!\n');

return `
return trimLeft(`
To get started, cd into the new directory:
\u001b[32mcd ${path.relative(process.cwd(), target)}\u001b[39m

Expand All @@ -146,9 +149,31 @@ export default asyncCommand({

To start a production HTTP/2 server:
\u001b[32mnpm run serve\u001b[39m
`.trim().replace(/^\t+/gm, '') + '\n';
`) + '\n';
}
})

const trimLeft = (string) => string.trim().replace(/^\t+/gm, '');

const npm = (cwd, args) => spawn('npm', args, { cwd, stdio: 'ignore' });

// Initializes the folder using `git init` and a proper `.gitignore` file
// if `git` is present in the $PATH.
const initializeVersionControl = async function(target) {
if (which.sync('git')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which.sync throws an error if command is not found according to the docs. Is the exception swallowed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using the async version of which. Here's how:

import promisify from 'es6-promisify';

// ...

async function initializeVersionControl(target) {
  let git;
  try {
    git = await promisify(which)('git');
  }
  catch (err) {}
  if (!git) {
    // ... etc
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have to be async? It would throw in either case when using async/await.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to avoid blocking (nothing else is blocking in the CLI).

const gitignore = trimLeft(`
node_modules
build
`) + '\n';
const gitignorePath = path.resolve(target, '.gitignore');
await fs.writeFile(gitignorePath, gitignore);

const cwd = target;

await spawn('git', ['init'], { cwd });
await spawn('git', ['add', '-A'], { cwd });

const gitUser = 'Preact CLI<[email protected]>';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh - I am wondering if this will spam me with notifications 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of dear !! That sure will. Good luck with that !!

await spawn('git', ['commit', '--author', gitUser, '-m', 'initial commit from Preact CLI'], { cwd });
}
};