forked from nsjcy/frontEnd/nsjcy

liuwh
2020-06-09 5a887acf1924950715086633d329db045223b5c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const hicode = require('../hicode');
module.exports = function (base, name, args) {
  if (!/^([A-Z][a-z0-9]*)+$/.test(name)) {
    console.log(chalk.red('Incorrect name convention...'));
    process.exit();
  }
 
  const file = path.join(base, name + '.jsx');
  if (fs.existsSync(file)) {
    console.log(chalk.red(`${file} existed...`));
    process.exit();
  }
 
  fs.createWriteStream(file).write(
    [
      hicode(args),
      `import React from 'react';`,
      `// import { Modal } from 'antd-mobile';`,
      `// import { Icon } from 'antd';`,
      ``,
      `// import Component from '../../shared/view/Component';`,
      `import Component from '../view/Component';`,
      ``,
      `import Fetch from '../fetch';`,
      ``,
      `export default class ${name} extends React.Component {`,
      `  constructor(props) {`,
      `    super(props);`,
      `    this.state = {`,
      `      data: null`,
      `    };`,
      `  }`,
      ``,
      `  componentDidMount() {`,
      `    document.title = 'Title';`,
      `    // const { id } = this.props.match.params;`,
      `  }`,
      ``,
      `  render() {`,
      `    return (`,
      `      <div className="app-page">`,
      `        ${name}`,
      `      </div>`,
      `    );`,
      `  }`,
      ``,
      `}`,
      ``
    ].join('\n')
  );
 
  console.log(chalk.green('Created: ' + file));
}