-
-
Notifications
You must be signed in to change notification settings - Fork 372
feature: Installing dependencies with yarn #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
c4931dd
7c8ddd3
130dac3
42da57d
e0029ee
c6a19d1
0e0f569
d8b57b6
2c12da7
3541f93
1fc3a4c
05c4522
a6bcbec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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', | ||
|
|
@@ -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' }) | ||
|
|
||
| let pkg = JSON.parse(await fs.readFile(path.resolve(target, 'package.json'))); | ||
|
|
||
|
|
@@ -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', | ||
|
|
@@ -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' | ||
|
|
@@ -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') | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can slim this down by moving the |
||
| 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 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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)) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sneaky refactor 👣
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
initfunctionality like npm's?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@developit yes it does
There was a problem hiding this comment.
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
whichcheck up and use yarn here too. For now anyone running yarn would have npm installed anyway.