element/build/bin/new.js

136 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-07-27 14:15:02 +08:00
'use strict';
console.log();
process.on('exit', () => {
console.log();
});
if (!process.argv[2]) {
console.error('[组件名]必填 - Please enter new component name');
2016-07-27 14:15:02 +08:00
process.exit(1);
}
const path = require('path');
const fileSave = require('file-save');
const uppercamelcase = require('uppercamelcase');
const componentname = process.argv[2];
const chineseName = process.argv[3] || componentname;
const ComponentName = uppercamelcase(componentname);
2016-10-13 17:51:14 +08:00
const PackagePath = path.resolve(__dirname, '../../packages', componentname);
2016-07-27 14:15:02 +08:00
const Files = [
{
filename: 'index.js',
2016-11-09 13:49:12 +08:00
content: `import ${ComponentName} from './src/main';
2016-07-27 14:15:02 +08:00
/* istanbul ignore next */
2016-07-27 14:15:02 +08:00
${ComponentName}.install = function(Vue) {
Vue.component(${ComponentName}.name, ${ComponentName});
};
2016-11-09 13:49:12 +08:00
export default ${ComponentName};`
2016-07-27 14:15:02 +08:00
},
{
filename: 'cooking.conf.js',
content: `var cooking = require('cooking');
2017-01-23 14:06:01 +08:00
var gen = require('../../build/gen-single-config');
2016-07-27 14:15:02 +08:00
2017-01-23 14:06:01 +08:00
cooking.set(gen(__dirname, 'El${ComponentName}'));
2016-07-27 14:15:02 +08:00
2017-01-23 14:06:01 +08:00
module.exports = cooking.resolve();
`
2016-07-27 14:15:02 +08:00
},
{
filename: 'package.json',
content: `{
"name": "element-${componentname}",
2016-07-27 14:15:02 +08:00
"version": "0.0.0",
"description": "A ${componentname} component for Vue.js.",
"keywords": [
"element",
"vue",
"component"
],
"main": "./lib/index.js",
2016-11-06 12:26:14 +08:00
"repository": "https://github.com/ElemeFE/element/tree/master/packages/${componentname}",
2016-07-27 14:15:02 +08:00
"author": "elemefe",
"license": "MIT",
"dependencies": {}
}`
},
{
filename: 'src/main.vue',
content: `<template>
<div class="el-${componentname}"></div>
</template>
<script>
export default {
name: 'El${ComponentName}'
2016-07-27 14:15:02 +08:00
};
</script>`
},
{
2016-11-07 16:54:20 +08:00
filename: path.join('../../examples/docs/zh-CN', `${componentname}.md`),
content: `## ${chineseName}`
2016-11-09 13:49:12 +08:00
},
{
filename: path.join('../../examples/docs/en-US', `${componentname}.md`),
content: `## ${componentname}`
},
2016-11-09 13:49:12 +08:00
{
filename: path.join('../../test/unit/specs', `${componentname}.spec.js`),
content: `import { createTest, destroyVM } from '../util';
import ${ComponentName} from 'packages/${componentname}';
2016-11-09 13:49:12 +08:00
describe('${ComponentName}', () => {
2016-11-09 13:49:12 +08:00
let vm;
afterEach(() => {
destroyVM(vm);
});
it('create', () => {
vm = createTest(${ComponentName}, true);
2016-11-09 13:49:12 +08:00
expect(vm.$el).to.exist;
});
});
`
2016-07-27 14:15:02 +08:00
}
];
2016-07-27 14:15:02 +08:00
// 添加到 components.json
const componentsFile = require('../../components.json');
2016-07-27 14:15:02 +08:00
if (componentsFile[componentname]) {
console.error(`${componentname} 已存在.`);
process.exit(1);
}
2016-09-27 14:55:37 +08:00
componentsFile[componentname] = `./packages/${componentname}/index.js`;
fileSave(path.join(__dirname, '../../components.json'))
2016-07-27 14:15:02 +08:00
.write(JSON.stringify(componentsFile, null, ' '), 'utf8')
.end('\n');
2016-07-27 14:15:02 +08:00
// 创建 package
Files.forEach(file => {
fileSave(path.join(PackagePath, file.filename))
.write(file.content, 'utf8')
.end('\n');
});
2016-07-27 14:15:02 +08:00
// 添加到 nav.config.json
const navConfigFile = require('../../examples/nav.config.json');
2016-10-17 15:55:12 +08:00
Object.keys(navConfigFile).forEach(lang => {
let groups = navConfigFile[lang][2].groups;
groups[groups.length - 1].list.push({
path: `/${componentname}`,
title: lang === 'zh-CN' && componentname !== chineseName
? `${ComponentName} ${chineseName}`
: ComponentName
});
2016-07-27 14:15:02 +08:00
});
fileSave(path.join(__dirname, '../../examples/nav.config.json'))
2016-07-27 14:15:02 +08:00
.write(JSON.stringify(navConfigFile, null, ' '), 'utf8')
.end('\n');
2016-07-27 14:15:02 +08:00
console.log('DONE!');