Skip to content

Commit

Permalink
fixed options
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 25, 2018
1 parent e92e8b3 commit 97a8b29
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 163 deletions.
17 changes: 5 additions & 12 deletions packages/wepy-cli/src/bin/wepy-build.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import program from 'commander';
import compile from '../compile';

program
.option('-f, --file <file>', '待编译wpy文件')
.option('-s, --source <source>', '源码目录')
.option('-t, --target <target>', '生成代码目录')
.option('-o, --output <type>', '编译类型:web,weapp。默认为weapp')
.option('-p, --platform <type>', '编译平台:browser, wechat,qq。默认为browser')
.option('-w, --watch', '监听文件改动')
.option('--no-cache', '对于引用到的文件,即使无改动也会再次编译')
.parse(process.argv);
exports = module.exports = (program) => {

if (compile.init(program)) {
compile.build(program);
}

if (compile.init(program)) {
compile.build(program);
}
180 changes: 78 additions & 102 deletions packages/wepy-cli/src/bin/wepy-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,126 +5,102 @@ import chalk from 'chalk';
import tildify from 'tildify';
import inquirer from 'inquirer';
import home from 'user-home';
import program from 'commander';
import { sync as rm } from 'rimraf';
import download from '../cli/download';
import localPath from '../cli/local-path';
import checkVersion from '../cli/check-version';
import generate from '../cli/generate';
import * as logger from '../cli/logger';

program
.usage('<template-name> [project-name]')
.option('-c --clone', 'use git clone')
.option('--offline', 'use cached template')
.parse(process.argv);

program.on('--help', () => {
console.log();
console.log(' Example:');
console.log();
console.log(chalk.gray(' # create a new project with an official template'));
console.log(' $ wepy init standard my-project');
console.log();
console.log(chalk.gray(' # create a new project straight from a github template'));
console.log(' $ wepy init username/repo my-project');
console.log();
});

function help () {
program.parse(process.argv);
if (program.args.length < 1) return program.help();
}
help();
exports = module.exports = (template, rawName, program) => {
function gen (templatePath) {
generate(name, templatePath, to, err => {
if (err) logger.fatal(err);
console.log();
logger.success('Generated "%s".', name);
});
}

let template = program.args[0];
const hasSlash = template.indexOf('/') > -1;
const rawName = program.args[1];
const inPlace = !rawName || rawName === '.';
const name = inPlace ? path.relative('../', process.cwd()) : rawName;
const to = path.resolve(rawName || '.');
const clone = program.clone || false;
const offline = program.offline || false;
let tmp = path.join(home, '.wepy_templates', template.replace(/\//g, '-'));
function run () {
if (localPath.isLocalPath(template)) {
// use local/cache template
// Example:
// wepy init E:\workspace\wepy_templates\standard my-wepy-project
// wepy init standard my-wepy-project --offline
const templatePath = localPath.getTemplatePath(template);
if (fs.existsSync(templatePath)) {
gen(templatePath);
} else {
logger.fatal('Local template "%s" not found.', template);
}
} else {
checkVersion(() => {
downloadAndGenerate(template);
});
}
}

/**
* use offline cache
*/
if (offline) {
console.log(`> Use cached template at ${chalk.yellow(tildify(tmp))}`);
template = tmp;
}
function downloadAndGenerate (template) {
const spinner = ora('downloading template');
spinner.start();

if (fs.existsSync(to)) {
inquirer.prompt([{
type: 'confirm',
message: inPlace
? 'Generate project in current directory?'
: 'Target directory exists. Continue?',
name: 'ok'
}]).then(answers => {
if (answers.ok) {
run();
if (fs.existsSync(tmp)) {
rm(tmp);
}
}).catch();
} else {
run();
}

function gen (templatePath) {
generate(name, templatePath, to, err => {
if (err) logger.fatal(err);
console.log();
logger.success('Generated "%s".', name);
});
}

function run () {
if (localPath.isLocalPath(template)) {
// use local/cache template
// Example:
// wepy init E:\workspace\wepy_templates\standard my-wepy-project
// wepy init standard my-wepy-project --offline
const templatePath = localPath.getTemplatePath(template);
if (fs.existsSync(templatePath)) {
gen(templatePath);
if (!hasSlash) {
// use official template
download.downloadOfficialZip(template, tmp, { extract: true }).then(() => {
spinner.stop()
gen(tmp);
}).catch(e => {
if (e.statusCode === 404) {
logger.fatal(`Unrecongnized template: "${template}". Try "wepy list" to show all available templates `);
} else if (e) {
logger.fatal('Failed to download repo ' + template + ': ' + e.message.trim());
}
});
} else {
logger.fatal('Local template "%s" not found.', template);
}
} else {
checkVersion(() => {
downloadAndGenerate(template);
});
// use third party template
download.downloadRepo(template, tmp, { clone }, err => {
spinner.stop();
if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim());
gen(tmp);
});
}

}
}

function downloadAndGenerate (template) {
const spinner = ora('downloading template');
spinner.start();

if (fs.existsSync(tmp)) {
rm(tmp);
const hasSlash = template.indexOf('/') > -1;
const inPlace = !rawName || rawName === '.';
const name = inPlace ? path.relative('../', process.cwd()) : rawName;
const to = path.resolve(rawName || '.');
const clone = program.clone || false;
const offline = program.offline || false;
let tmp = path.join(home, '.wepy_templates', template.replace(/\//g, '-'));

/**
* use offline cache
*/
if (offline) {
console.log(`> Use cached template at ${chalk.yellow(tildify(tmp))}`);
template = tmp;
}

if (!hasSlash) {
// use official template
download.downloadOfficialZip(template, tmp, { extract: true }).then(() => {
spinner.stop()
gen(tmp);
}).catch(e => {
if (e.statusCode === 404) {
logger.fatal(`Unrecongnized template: "${template}". Try "wepy list" to show all available templates `);
} else if (e) {
logger.fatal('Failed to download repo ' + template + ': ' + e.message.trim());
}
});
if (fs.existsSync(to)) {
inquirer.prompt([{
type: 'confirm',
message: inPlace
? 'Generate project in current directory?'
: 'Target directory exists. Continue?',
name: 'ok'
}]).then(answers => {
if (answers.ok) {
run();
}
}).catch();
} else {
// use third party template
download.downloadRepo(template, tmp, { clone }, err => {
spinner.stop();
if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim());
gen(tmp);
});
run();
}

}
53 changes: 28 additions & 25 deletions packages/wepy-cli/src/bin/wepy-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
import chalk from 'chalk';
import request from 'request';

request({
url: 'https://raw.githubusercontent.com/wepyjs/wepy_templates/master/templates.json',
headers: {
'User-Agent': 'wepy-cli'
}
}, (err, res, body) => {
if (body.message) {
console.error(body.messge);
}
try {
body = JSON.parse(body);
} catch (e) {
console.error('Something wrong with your network');
}
if (Array.isArray(body)) {
console.log(' Available official templates:\n');
body.forEach(repo => {
console.log(
' ' + chalk.yellow('★') +
' ' + chalk.blue(repo.name) +
' - ' + repo.description);
});
console.log('\n');
}
});

exports = module.exports = (program) => {
request({
url: 'https://raw.githubusercontent.com/wepyjs/wepy_templates/master/templates.json',
headers: {
'User-Agent': 'wepy-cli'
}
}, (err, res, body) => {
if (body.message) {
console.error(body.messge);
}
try {
body = JSON.parse(body);
} catch (e) {
console.error('Something wrong with your network');
}
if (Array.isArray(body)) {
console.log(' Available official templates:\n');
body.forEach(repo => {
console.log(
' ' + chalk.yellow('★') +
' ' + chalk.blue(repo.name) +
' - ' + repo.description);
});
console.log('\n');
}
});
}
19 changes: 8 additions & 11 deletions packages/wepy-cli/src/bin/wepy-upgrade.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import program from 'commander';
import {exec} from 'child_process';
import util from '../util';

program
.option('--cli', 'upgrade wepy-cli')
.option('--wepy', 'upgrade wepy')
.parse(process.argv);

upgrade(program);


function upgradeCLI (cb) {
let cmd = 'npm install wepy-cli -g';
Expand Down Expand Up @@ -45,8 +40,10 @@ function upgradeWepy (cb) {
});
}

function upgrade (program) {
program.cli
? upgradeCLI()
: upgradeWepy();
}
exports = module.exports = (program) => {
if (program.cli) {
upgradeCLI();
} else {
upgradeWepy();
}
};
42 changes: 29 additions & 13 deletions packages/wepy-cli/src/bin/wepy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import chalk from 'chalk';
import program from 'commander';

program
Expand All @@ -8,29 +8,45 @@ program
program
.command('init <template-name> [project-name]')
.description('generate a new project from a template')
.action(() => {
require('./wepy-init');
.action(require('./wepy-init'))
.usage('<template-name> [project-name]')
.option('-c --clone', 'use git clone')
.option('--offline', 'use cached template')
.on('--help', () => {
console.log();
console.log(' Example:');
console.log();
console.log(chalk.gray(' # create a new project with an official template'));
console.log(' $ wepy init standard my-project');
console.log();
console.log(chalk.gray(' # create a new project straight from a github template'));
console.log(' $ wepy init username/repo my-project');
console.log();
});

program
.command('build')
.description('build your project')
.action(() => {
require('./wepy-build');
});
.action(require('./wepy-build'))

.option('-f, --file <file>', '待编译wpy文件')
.option('-s, --source <source>', '源码目录')
.option('-t, --target <target>', '生成代码目录')
.option('-o, --output <type>', '编译类型:web,weapp。默认为weapp')
.option('-p, --platform <type>', '编译平台:browser, wechat,qq。默认为browser')
.option('-w, --watch', '监听文件改动')
.option('--no-cache', '对于引用到的文件,即使无改动也会再次编译');

program
.command('list')
.description('list available official templates')
.action(() => {
require('./wepy-list');
});
.action(require('./wepy-list'));

program
.command('upgrade')
.description('upgrade to the latest version')
.action(() => {
require('./wepy-upgrade');
});
.action(require('./wepy-upgrade'))
.option('--cli', 'upgrade wepy-cli')
.option('--wepy', 'upgrade wepy');

program.parse(process.argv);
program.parse(process.argv);

0 comments on commit 97a8b29

Please sign in to comment.