Skip to content

Commit

Permalink
feat(scripts): 添加all指令
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanaMaid committed Aug 31, 2018
1 parent 5ecad78 commit b3ea9cd
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 17 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ you can use `--output` to customize the output directory!
yoshino new Alert --output mycomponents
```

### all
output all components
```
yoshino all
```
you can use `--output` to customize the output directory!
```
yoshino all --output mycomponents
```

7 changes: 7 additions & 0 deletions bin/yoshino.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ program
require(path.resolve(__dirname, '../scripts/new.js'))(component, output);
})

program
.command('all')
.action(() => {
const output = path.resolve(process.cwd(), program.output || './components');
require(path.resolve(__dirname, '../scripts/all.js'))(output);
})

program.parse(process.argv);
28 changes: 28 additions & 0 deletions scripts/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fse = require('fs-extra');
const fs = require('fs');
const path = require('path');
const {backup} = require('../utils/hasBackup');
const {getAllComponents, newComponent} = require('../utils/component');
const showProcess = require('../utils/showProcess');
const consola = require('consola');

module.exports = (output) => {
const allComponents = getAllComponents();
const backupStyleDir = path.resolve(backup, `./components/styles`);
const outputStyleDir = path.resolve(output, './styles');

showProcess.start('starting output all components........');
// 创建styles 目录
// 组件公用style目录是否存在
if (!fs.existsSync(outputStyleDir)) {
fse.copySync(backupStyleDir, outputStyleDir);
}

let newCount = 0;
for (const component of allComponents) {
if (newComponent(component, output)) {
newCount++;
};
}
showProcess.end(`Ending output ${newCount} components`);
}
4 changes: 2 additions & 2 deletions scripts/new.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hasComponent = require('../utils/hasComponent');
const {hasComponent} = require('../utils/component');
const consola = require('consola');
const fs = require('fs');
const fse = require('fs-extra');
Expand Down Expand Up @@ -69,7 +69,7 @@ const newScript = (component, output) => {

// 组件是否命中依赖树
if (hasDependences(component)) {
consola.start(`component has other dependences! start parsing!`);
consola.start(`component ${component} has other dependences! start parsing!`);

// 解析依赖,生成对应的组件
parseDependense(component, output);
Expand Down
53 changes: 53 additions & 0 deletions utils/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const {backup} = require('./hasBackup');
const fs = require('fs');
const path = require('path');
const fse = require('fs-extra');
const consola = require('consola');

// 获取所有组件名
const getAllComponents = () => {
const files = fs.readdirSync(path.resolve(backup, './components'));
const notComponents = ['template', 'styles', 'utils', 'index.tsx', 'tsconfig.json'];
const components = new Set(files);
for (const item of notComponents) {
components.delete(item);
}
return Array.from(components);
}

// 是否有该组件
const hasComponent = (component) => {
return getAllComponents().indexOf(component) !== -1;
}


// 创建单个组件,不解析依赖树
const newComponent = (component, output) => {
const backupComponentDir = path.resolve(backup, `./components/${component}`);
const backupComponentStyleDir = path.resolve(backupComponentDir, `./style`);
const outputComponentDir = path.resolve(output, component);
const outputComponentStyleDir = path.resolve(outputComponentDir, './style');

if (fs.existsSync(outputComponentDir)) {
consola.error(`component ${component} has existed!`);
return false;
}

// 创建组件
fs.mkdirSync(outputComponentDir);
fse.copySync(backupComponentStyleDir, outputComponentStyleDir);

const template = `import './style/index.less';
import ${component} from 'yoshino/lib/${component}';
export default ${component}`;
fs.writeFileSync(path.resolve(outputComponentDir, './index.tsx'), template);

consola.success(`component ${component} has been successfully created!`);
return true;
}

module.exports = {
getAllComponents,
hasComponent,
newComponent,
}
15 changes: 0 additions & 15 deletions utils/hasComponent.js

This file was deleted.

0 comments on commit b3ea9cd

Please sign in to comment.