1. shift to cnpm mirror when push "cn" argument;

2. show the process when npm installing dev dependencies;
This commit is contained in:
pasturn 2017-04-07 16:08:20 +08:00
parent 1667f1f873
commit a833b5416e
7 changed files with 96 additions and 81 deletions

View File

@ -13,8 +13,9 @@ $ npm install omi-cli -g
### Usage
```
$ omi init // in current directory
$ omi init [project name] // in new directroy named project name
$ omi init // in current directory
$ omi init [project name] // in new directroy named project name
$ omi init [project name] [cn] // shift to cnpm mirror
```
Example:
```

View File

@ -1,30 +1,30 @@
#!/usr/bin/env node
'use strict'
'use strict';
const program = require('commander')
const join = require('path').join
const chalk = require('chalk')
const exists = require('fs-exists-sync');
const spawn = require('cross-spawn')
var program = require('commander');
var join = require('path').join;
var chalk = require('chalk');
var exists = require('fs-exists-sync');
var spawn = require('cross-spawn');
program
.version(require('../package').version, '-v, --version')
.usage('init [project-name]')
.on('--help', help)
.on('--help', help);
program.parse(process.argv)
program.parse(process.argv);
const args = process.argv.slice(3)
var args = process.argv.slice(3);
let cmdstr = program.args[0]
let cmdstr = program.args[0];
if (cmdstr) {
const binFile = executable(cmdstr)
const binFile = executable(cmdstr);
if (binFile) {
console.log()
console.log(chalk.bold.cyan("Omi-cli") + " is initializing... ")
console.log();
console.log(chalk.bold.cyan("Omi-cli") + " is initializing... ");
secede(spawn('node', [].concat([binFile], args), {stdio: 'inherit'}))
} else {
program.help();
@ -34,15 +34,17 @@ if (cmdstr) {
}
function help() {
console.log(' Commands:')
console.log()
console.log(' init [project-name] Initialize a new Omi application in the current folder ')
console.log()
console.log(' Commands:');
console.log();
console.log(` ${chalk.green('init [project-name]')} Initialize a new Omi application in the current folder `);
console.log();
console.log(` ${chalk.green('init [project-name] [cn]')} Shift the npm registry to cnpm mirror `);
console.log();
console.log(' All commands can be run with -h (or --help) for more information.')
}
function executable(cmdstr) {
const file = join(__dirname, 'omi-' + cmdstr)
const file = join(__dirname, 'omi-' + cmdstr);
if (exists(file)) {
return file
}

View File

@ -1,9 +1,9 @@
#!/usr/bin/env node
const program = require('commander')
var program = require('commander');
program
.arguments('[project-name]')
.parse(process.argv);
require('../lib/init')(program)
require('../lib/init')(program);

View File

@ -1,58 +1,71 @@
const { join, basename } = require('path')
const vfs = require('vinyl-fs')
const { renameSync } = require('fs')
const chalk = require('chalk')
const through = require('through2')
const emptyDir = require('empty-dir')
const { info, error, success } = require('./logger')
var join = require('path').join;
var basename = require('path').basename;
var vfs = require('vinyl-fs');
var renameSync = require('fs').renameSync;
var chalk = require('chalk');
var through = require('through2');
var emptyDir = require('empty-dir');
var info = require('./logger').info;
var error = require('./logger').error;
var success = require('./logger').success;
function init(program) {
const customPrj = program.args[0] || ''
const tpl = join(__dirname, '../template/app')
const dest = join(process.cwd(), customPrj)
const projectName = basename(dest)
var args = ['install'];
var customPrj = '';
if(program.args[0] && program.args[0] === "cn"){
args = ['install', "--registry", "https://registry.npm.taobao.org"];
} else if(program.args[1] && program.args[1] === "cn") {
customPrj = program.args[0];
args = ['install', "--registry", "https://registry.npm.taobao.org"];
} else {
customPrj = program.args[0] || '';
}
var tpl = join(__dirname, '../template/app');
var dest = join(process.cwd(), customPrj);
var projectName = basename(dest);
if (!customPrj && !emptyDir.sync(dest)) {
error('This directory existing files, please empty.');
process.exit(1);
}
console.log()
console.log(`Creating a new Omi app in ${dest}`)
console.log();
console.log(`Creating a new Omi app in ${dest}`);
/*
* Use vinyl-fs to init file
* https://github.com/dvajs/dva-cli/blob/master/src/init.js
*/
vfs.src(['**/*', '!mode_modules/**/*'], {cwd: tpl, cwdbase: true, dot: true})
.pipe(template(dest, tpl))
.pipe(vfs.dest(dest))
.on('end', function(){
try{
info('Rename', 'gitignore -> .gitignore')
renameSync(join(dest, 'gitignore'), join(dest, '.gitignore'));
if(customPrj) {
try{
process.chdir(customPrj);
}
catch (err) {
.pipe(template(dest, tpl))
.pipe(vfs.dest(dest))
.on('end', function(){
try{
info('Rename', 'gitignore -> .gitignore');
renameSync(join(dest, 'gitignore'), join(dest, '.gitignore'));
if(customPrj) {
try{
process.chdir(customPrj);
}
catch (err) {
console.log(error(err));
}
}
info('Run', 'npm install')
require('./install')(echoDone)
} catch(e){
}}
info('Run', 'npm will install dependencies');
console.log();
require('./install')(args, echoDone)
} catch(e){
console.log(error(e))
}
})
.resume()
}
})
.resume();
function echoDone(){
console.log()
console.log()
console.log()
success(`Congratulation! "${projectName}" has been created successful! `)
function echoDone(){
console.log();
console.log();
console.log();
success(`Congratulation! "${projectName}" has been created successful! `);
console.log(`
Using the scaffold with Gulp + Webpack + Babel + BrowserSync,
@ -60,8 +73,8 @@ Using the scaffold with Gulp + Webpack + Babel + BrowserSync,
In ${projectName}, you can run these commands:
> ${chalk.bold.white('npm run dev')} Starts the development server
> ${chalk.bold.white('npm run dist')} Publish your project`)
console.log()
> ${chalk.bold.white('npm run dist')} Publish your project`);
console.log();
console.log(`${chalk.bold.cyan('Omi!')} https://alloyteam.github.io/omi` )
}
@ -80,4 +93,4 @@ function template(dest, cwd) {
});
}
module.exports = init
module.exports = init;

View File

@ -1,11 +1,13 @@
const spawnNpm = require('cross-npm-spawn')
const ora = require('ora')
const spinner = ora("npm installing dependencies, please wait for a while...")
spinner.start()
var spawn = require('cross-spawn');
var ora = require('ora');
module.exports = function (done){
spawnNpm('install').then((result) => {
spinner.stop();
done()
})
}
module.exports = function (args, done){
var common = spawn('npm', args, { stdio: 'inherit' });
common.on('close', function () {
done();
process.exit(0);
});
common.on('error', function (reason) {
console.log('An error occured while executing the NPM command.', reason);
});
};

View File

@ -1,18 +1,15 @@
const chalk = require('chalk')
var chalk = require('chalk');
exports.info = function (type, message){
console.log()
console.log(`${chalk.bold.magenta(type)}: ${message}`)
}
};
exports.error = function (message){
console.log()
console.log(chalk.red(message))
console.log(chalk.red(message));
process.exit(1)
}
};
exports.success = function (message){
console.log()
console.log(chalk.green(message))
}
};

View File

@ -1,6 +1,6 @@
{
"name": "omi-cli",
"version": "0.2.5",
"version": "0.2.6",
"description": "CLI for scaffolding Omi.js projects.",
"main": "bin/omi",
"engines": {