You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.3 KiB

/* eslint-disable no-use-before-define */
/**
* pages模版快速生成脚本,执行命令 npm run tep `文件名`
*/
// eslint-disable-next-line import/no-commonjs
const fs = require('fs');
const dirName = process.argv[2];
if (!dirName) {
console.log('文件夹名称不能为空!');
console.log('示例:npm run tep test');
process.exit(0);
}
// 页面模版
const indexTep = `// package
import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux'
// relative components
// relative package
// styles
import styles from './index.module.less';
export default function ${titleCase(dirName)}() {
const state = useSelector(({ ${firstCase(dirName)} }) => ${firstCase(dirName)});
const dispatch = useDispatch();
useEffect(() => {
console.log({ state, dispatch })
}, []);
return (
<div className={styles.index}>
${dirName}
</div>
);
}
`;
// less文件模版
const lessTep = `.index { }`;
// model文件模版
const modelTep = `import { fetch } from './service';
export default {
namespace: '${firstCase(dirName)}',
state: {},
effects: {
*query({ payload }, { call, put }) {
fetch()
},
},
reducers: {
save(state, { payload }) {
return { ...state, ...payload };
},
},
};
`;
// service页面模版
const serviceTep = `import Request from '@/utils/request';
export async function fetch(params) {
return Request({
url: '路径',
method: 'POST',
data: { ...params },
});
}
`;
// config模板
const configTep = `export default {
navigationBarTitleText: '${dirName}'
}
`;
fs.mkdirSync(`./src/pages/${dirName}`); // mkdir $1
process.chdir(`./src/pages/${dirName}`); // cd $1
fs.writeFileSync('index.jsx', indexTep);
fs.writeFileSync('index.module.less', lessTep);
fs.writeFileSync('model.js', modelTep);
fs.writeFileSync('service.js', serviceTep);
// fs.writeFileSync('index.config.js', configTep);
console.log(`模版${dirName}已创建`);
function titleCase(str) {
const array = str.toLowerCase().split(' ');
for (let i = 0; i < array.length; i++) {
array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
}
const string = array.join(' ');
return string;
}
function firstCase(str) {
let string = str;
string = string.replace(string[0], string[0].toLowerCase());
return string;
}
process.exit(0);