Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
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"
}
}
35 changes: 28 additions & 7 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 @@ -83,7 +84,7 @@ export default asyncCommand({

spinner.text = 'Initializing project';

await npm(target, ['init', '-y']);
await spawn('npm', ['init', '-y'], { cwd: target, stdio: 'ignore' })
Copy link
Member

@developit developit May 25, 2017

Choose a reason for hiding this comment

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

lol this is what it used to be 😆

does Yarn not include an init functionality like npm's?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@developit yes it does

Copy link
Member

Choose a reason for hiding this comment

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

future thing to do might be to move the which check up and use yarn here too. For now anyone running yarn would have npm installed anyway.


let pkg = JSON.parse(await fs.readFile(path.resolve(target, 'package.json')));

Expand All @@ -103,8 +104,7 @@ export default asyncCommand({

spinner.text = 'Installing dev dependencies';

await npm(target, [
'install', '--save-dev',
await install(target, [
'preact-cli',
'if-env',
'eslint',
Expand All @@ -121,12 +121,11 @@ export default asyncCommand({
'less',
'less-loader'
] : [])
].filter(Boolean));
].filter(Boolean), 'dev');

spinner.text = 'Installing dependencies';

await npm(target, [
'install', '--save',
await install(target, [
'preact',
'preact-compat',
'preact-router'
Expand All @@ -150,5 +149,27 @@ export default asyncCommand({
}
})

const install = async (cwd, packages, env) => {
const isDev = env === 'dev' ? true : false
const isYarnAvailable = await isCommandAvailable('yarn')

const npm = (cwd, args) => spawn('npm', args, { cwd, stdio: 'ignore' });
if(isYarnAvailable) {
const args = ['add']
if(isDev) {
args.push('-D')
}

Copy link
Member

Choose a reason for hiding this comment

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

We can slim this down by moving the .filter(Boolean) into install, but I'll just slip that in post-merge :)

return await spawn('yarn', [...args, ...packages], { cwd, stdio: 'ignore' })
}

await spawn('npm', ['install', isDev ? '--save-dev' : '--save', ...packages], { cwd, stdio: 'ignore' })
}

const isCommandAvailable = async cmd => {
try {
await promisify(which)(cmd)
return true;
} catch(e){
return false
}
}
15 changes: 7 additions & 8 deletions src/lib/run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function showStats(stats) {
return stats;
}

export function writeJsonStats(stats) {
export async function writeJsonStats(stats) {
const outputPath = resolve(process.cwd(), 'stats.json')
const jsonStats = stats.toJson({
json: true,
Expand All @@ -66,13 +66,12 @@ export function writeJsonStats(stats) {
jsonStats.modules.forEach(normalizeModule)
jsonStats.chunks.forEach(c => c.modules.forEach(normalizeModule))

return fs.writeFile(outputPath, JSON.stringify(jsonStats))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sneaky refactor 👣

Copy link
Member

Choose a reason for hiding this comment

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

well-earned, I'd say ;)

.then(() => {
process.stdout.write('\nWebpack output stats generated.\n\n')
process.stdout.write('You can upload your stats.json to:\n')
process.stdout.write('- https://chrisbateman.github.io/webpack-visualizer/\n')
process.stdout.write('- https://webpack.github.io/analyse/\n')
})
await fs.writeFile(outputPath, JSON.stringify(jsonStats))

process.stdout.write('\nWebpack output stats generated.\n\n')
process.stdout.write('You can upload your stats.json to:\n')
process.stdout.write('- https://chrisbateman.github.io/webpack-visualizer/\n')
process.stdout.write('- https://webpack.github.io/analyse/\n')
}

const normalizeModule = m => {
Expand Down